Question
Modify the compiler from Exercise 2.4 to write the lists of integers to a file. An F# list inss of integers may be output to
Modify the compiler from Exercise 2.4 to write the lists of integers to a file. An F# list inss of integers may be output to the file called fname using this function (found in Intcomp1.fs):
Then modify the stack machine interpreter in Machine.java to read the sequence of integers from a text file, and execute it as a stack machine program. The name of the text file may be given as a command-line parameter to the Java program. Reading numbers from the text file may be done using the StringTokenizer class or StreamTokenizer class; see e.g. Java Precisely [4, Example 145]. It is essential that the compiler (in F#) and the interpreter (in Java) agree on the intermediate language: what integer represents what instruction.
Exercise 2.4:
Write a bytecode assembler (in F#) that translates a list of bytecode instructions for the simple stack machine in Intcomp1.fs into a list of integers. The integers should be the corresponding bytecodes for the interpreter in Machine.java. Thus you should write a function assemble : sinstr list -> int list. Use this function together with scomp from Intcomp1.fs to make a compiler from the original expressions language expr to a list of bytecodes int list.
You may test the output of your compiler by typing in the numbers as an int array in the Machine.java interpreter. (Or you may solve Exercise 2.5 below to avoid this manual work).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started