OCaml Code: I need help with writing the push and pop statements in order to create an interpreter for OCaml. I need help with writing the print statement as well. The println command pops a string off the top of the stack and writes it, followed by a newline, to the output file that is specified as the second argument to the interpreter function. In the case that the top element on the stack is not a string, it should be returned to the stack and an :error: pushed. If the stack is empty, an :error: shall be pushed. Below is the unfinished code. Make sure to use the test cases as well and show the screenshots of the code passing the test cases. Attached is info. for push and pop.    interpreter.ml type stackValue = BOOL of bool | INT of int | ERROR | STRING of string | NAME of string | UNIT type command = ADD | SUB | MUL | DIV | PUSH of stackValue | POP of stackValue | REM | NEG | TOSTRING | SWAP | PRINTLN | QUIT let interpreter (input, output) =   let ic = open_in input   in   let oc = open_out output   in   let rec loop_read acc =     try       let l = String.trim(input_line ic) in loop_read (l::acc)     with       | End_of_file -> List.rev acc   in     let strList = loop_read [] in let str2sv s =   match s with   | ":true:" -> BOOL true   | ":false:" -> BOOL false   | ":error:" -> ERROR   | ":unit:" -> UNIT   | _->     let rec try_parse_int str =       try         INT (int_of_string str)       with       | Failure _ -> try_parse_name str     and try_parse_name str=         NAME str     in     try_parse_int s   in letstr2com s =   match s with     | "Add" -> ADD     | "Sub" -> SUB     | "Mul" -> MUL     | "Div" -> DIV     | "Swap" -> SWAP     | "Neg" -> NEG     | "Rem" -> REM     | "ToString" -> TOSTRING     | "Println" -> PRINTLN     | "Quit" -> QUIT     | _ -> PUSH (str2sv s) in let com2str c =   match c with     | "Add" -> ADD     | "Sub" -> SUB     | "Mul" -> MUL     | "Div" -> DIV     | "Swap" -> SWAP     | "Neg" -> NEG     | "Rem" -> REM     | "ToString" -> TOSTRING     | "Println" -> PRINTLN     | "Quit" -> QUIT     | _ -> PUSH (str2sv c) in let sv2str sv =   match sv with     | BOOL b -> string_of_bool b     | INT i -> string_of_int i     | ERROR -> ":error:"     | STRING s -> s     | NAME n -> n in let comList = List.map str2com strList   in   let processor cl stack =   match (cl, stack) with   | (ADD::restOfCommands, INT(a)::INT(b)::restOfStack) -> processor restOfCommands ( INT(a+b)::restOfStack)   | (ADD::restOfCommands, s) -> processor restOfCommands (ERROR::stack)   | (SUB::restOfCommands, INT(a)::INT(b)::restOfStack) -> processor restOfCommands ( INT(a-b)::restOfStack)   | (SUB::restOfCommands, s) -> processor restOfCommands (ERROR::stack)   | (MUL::restOfCommands, INT(a)::INT(b)::restOfStack) -> processor restOfCommands ( INT(a*b)::restOfStack)   | (MUL::restOfCommands, s) -> processor restOfCommands (ERROR::stack)   | (DIV::restOfCommands, INT(a)::INT(b)::restOfStack) -> processor restOfCommands ( INT(a/b)::restOfStack)   | (DIV::restOfCommands, s) -> processor restOfCommands (ERROR::stack)   | (REM::restOfCommands, INT(a)::INT(b)::restOfStack) -> processor restOfCommands ( INT(a%b)::restOfStack)   | (REM::restOfCommands, s) -> processor restOfCommands (ERROR::stack)   | (SWAP::restOfCommands, x::y::restOfStack) -> processor restOfCommands y::x::restOfStack   | (SWAP::restOfCommands, s) -> processor restOfCommands (ERROR::stack)   | (NEG::restOfCommands, INT(a)::restOfStack) -> processor restOfCommands ( INT(-a)::restOfStack)   | (NEG::restOfCommands, s) -> processor restOfCommands (ERROR::stack)   | (TOSTRING::restOfCommands, x::restOfStack) -> processor restOfCommands ( (sv2str x)::restOfStack)   | (TOSTRING::restOfCommands, s) -> processor restOfCommands (ERROR::stack)   in processor comList []       Test cases:    input1.txt: push 1 toString println quit   output1.txt: 1

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

OCaml Code: I need help with writing the push and pop statements in order to create an interpreter for OCaml. I need help with writing the print statement as well. The println command pops a string off the top of the stack and writes it, followed by a newline, to the output file that is specified as the second argument to the interpreter function. In the case that the top element on the stack is not a string, it should be returned to the stack and an :error: pushed. If the stack is empty, an :error: shall be pushed.

Below is the unfinished code. Make sure to use the test cases as well and show the screenshots of the code passing the test cases. Attached is info. for push and pop. 

 

interpreter.ml

type stackValue = BOOL of bool | INT of int | ERROR | STRING of string | NAME of string | UNIT

type command = ADD | SUB | MUL | DIV | PUSH of stackValue | POP of stackValue | REM | NEG | TOSTRING | SWAP | PRINTLN | QUIT

let interpreter (input, output) =

  let ic = open_in input
  in

  let oc = open_out output
  in

  let rec loop_read acc =
    try
      let l = String.trim(input_line ic) in loop_read (l::acc)
    with
      | End_of_file -> List.rev acc
  in
 
  let strList = loop_read []
in

let str2sv s =
  match s with
  | ":true:" -> BOOL true
  | ":false:" -> BOOL false
  | ":error:" -> ERROR
  | ":unit:" -> UNIT
  | _->
    let rec try_parse_int str =
      try
        INT (int_of_string str)
      with
      | Failure _ -> try_parse_name str
    and try_parse_name str=  
      NAME str
    in
    try_parse_int s

  in

letstr2com s =
  match s with
    | "Add" -> ADD
    | "Sub" -> SUB
    | "Mul" -> MUL
    | "Div" -> DIV
    | "Swap" -> SWAP
    | "Neg" -> NEG
    | "Rem" -> REM
    | "ToString" -> TOSTRING
    | "Println" -> PRINTLN
    | "Quit" -> QUIT
    | _ -> PUSH (str2sv s)

in

let com2str c =
  match c with
    | "Add" -> ADD
    | "Sub" -> SUB
    | "Mul" -> MUL
    | "Div" -> DIV
    | "Swap" -> SWAP
    | "Neg" -> NEG
    | "Rem" -> REM
    | "ToString" -> TOSTRING
    | "Println" -> PRINTLN
    | "Quit" -> QUIT
    | _ -> PUSH (str2sv c)
in

let sv2str sv =
  match sv with
    | BOOL b -> string_of_bool b
    | INT i -> string_of_int i
    | ERROR -> ":error:"
    | STRING s -> s
    | NAME n -> n
in

let comList = List.map str2com strList
 
in
 
let processor cl stack =
  match (cl, stack) with
  | (ADD::restOfCommands, INT(a)::INT(b)::restOfStack) -> processor restOfCommands ( INT(a+b)::restOfStack)
  | (ADD::restOfCommands, s) -> processor restOfCommands (ERROR::stack)

  | (SUB::restOfCommands, INT(a)::INT(b)::restOfStack) -> processor restOfCommands ( INT(a-b)::restOfStack)
  | (SUB::restOfCommands, s) -> processor restOfCommands (ERROR::stack)

  | (MUL::restOfCommands, INT(a)::INT(b)::restOfStack) -> processor restOfCommands ( INT(a*b)::restOfStack)
  | (MUL::restOfCommands, s) -> processor restOfCommands (ERROR::stack)

  | (DIV::restOfCommands, INT(a)::INT(b)::restOfStack) -> processor restOfCommands ( INT(a/b)::restOfStack)
  | (DIV::restOfCommands, s) -> processor restOfCommands (ERROR::stack)

  | (REM::restOfCommands, INT(a)::INT(b)::restOfStack) -> processor restOfCommands ( INT(a%b)::restOfStack)
  | (REM::restOfCommands, s) -> processor restOfCommands (ERROR::stack)

  | (SWAP::restOfCommands, x::y::restOfStack) -> processor restOfCommands y::x::restOfStack
  | (SWAP::restOfCommands, s) -> processor restOfCommands (ERROR::stack)

  | (NEG::restOfCommands, INT(a)::restOfStack) -> processor restOfCommands ( INT(-a)::restOfStack)
  | (NEG::restOfCommands, s) -> processor restOfCommands (ERROR::stack)

  | (TOSTRING::restOfCommands, x::restOfStack) -> processor restOfCommands ( (sv2str x)::restOfStack)
  | (TOSTRING::restOfCommands, s) -> processor restOfCommands (ERROR::stack)


 
in processor comList []
 
 
 
Test cases: 
 

input1.txt:

push 1
toString
println
quit

 

output1.txt:

1

 

 



4 Part 1: Basic Computation
Suggested Completion Date: June 20 2023, 11:59pm
Your interpreter should be able to handle the following commands:
4.1 push
4.1.1 Pushing Integers to the Stack
push num
where num is an integer, possibly with a '-' suggesting a negative value. Here ¹-0' should be regarded
as '0'. Entering this expression will simply push num onto the stack. For example,
4.1.2 Pushing Strings to the Stack
input
stack
0
push 5
push-0 5
push string
where string is a string literal consisting of a sequence of characters enclosed in double quotation
marks, as in "this is a string". Executing this command would push the string onto the stack:
input
push "deadpool"
push "batman"
push "this is a string"
Spaces are preserved in the string, i.e. any preceding or trailing whitespace must be kept inside
the string that is pushed to the stack:
1. example
stack
this a string
batman
deadpool
input
push " deadp ool "
push "this is a string _deadp_ool_
"1
stack
this_is_a_string..
You can assume that the string value would always be legal and not contain quotations or escape
sequences within the string itself, i.e. neither double quotes nor backslashes will appear inside a
string.
4.2 Pushing Names to the Stack
push name
where name consists of a sequence of letters, digits, and underscores, starting with a letter or
underscore.
Transcribed Image Text:4 Part 1: Basic Computation Suggested Completion Date: June 20 2023, 11:59pm Your interpreter should be able to handle the following commands: 4.1 push 4.1.1 Pushing Integers to the Stack push num where num is an integer, possibly with a '-' suggesting a negative value. Here ¹-0' should be regarded as '0'. Entering this expression will simply push num onto the stack. For example, 4.1.2 Pushing Strings to the Stack input stack 0 push 5 push-0 5 push string where string is a string literal consisting of a sequence of characters enclosed in double quotation marks, as in "this is a string". Executing this command would push the string onto the stack: input push "deadpool" push "batman" push "this is a string" Spaces are preserved in the string, i.e. any preceding or trailing whitespace must be kept inside the string that is pushed to the stack: 1. example stack this a string batman deadpool input push " deadp ool " push "this is a string _deadp_ool_ "1 stack this_is_a_string.. You can assume that the string value would always be legal and not contain quotations or escape sequences within the string itself, i.e. neither double quotes nor backslashes will appear inside a string. 4.2 Pushing Names to the Stack push name where name consists of a sequence of letters, digits, and underscores, starting with a letter or underscore.
4.5 pop
The command pop removes the top value from the stack. If the stack is empty, an error literal
(: error:) will be pushed onto the stack. For example,
input
push 5
pop
pop
→
stack
5
stack
input
:error:
Transcribed Image Text:4.5 pop The command pop removes the top value from the stack. If the stack is empty, an error literal (: error:) will be pushed onto the stack. For example, input push 5 pop pop → stack 5 stack input :error:
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 12 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

I ran the code and used the test cases and this is the error I got when I compiled my code. Attached is an image of the error. Don't reject the question. This is NOT a graded work. Please fix the code and show the screenshots of the code passing the test cases.

File "interpreter.ml", line 1:
Error: The implementation interpreter.ml
does not match the interface interpreter.cmi:
Values do not match:
val interpreter : instruction list -> unit
is not included in
val interpreter : string * string -> unit
File "interpreter.ml", line 23, characters 4-15: Actual declaration
Makefile:3: recipe for target 'all' failed
make: *** [all] Error 2
Transcribed Image Text:File "interpreter.ml", line 1: Error: The implementation interpreter.ml does not match the interface interpreter.cmi: Values do not match: val interpreter : instruction list -> unit is not included in val interpreter : string * string -> unit File "interpreter.ml", line 23, characters 4-15: Actual declaration Makefile:3: recipe for target 'all' failed make: *** [all] Error 2
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education