Compiler代写:CS327 Interpreter

代写Interpreter,根据语法规则,实现对输入文件的解析。

Requirement

For this assignment, you will construct the third and final module for our Scheme-like language.

The evaluator receives the expression tree structure that the parser constructed to represent the input expression, and recursively evaluates the expression and sub-expressions.

Required Functionality

The basic semantics of our language are defined by reference to the semantics of Scheme. The supported functionality is restricted to the special forms and predefined functions defined below. Since our focus is on how an interpreter works, rather than providing a robust programming environment, the supported forms and functions are fairly limited. If you wish to implement more functionality than the minimum described here, you can earn extra credit on this assignment - greater credit will be given to implementations of special forms, e.g. (let), and predefined functions that differ substantially from what is already implemented, e.g. (cons), in contrast to trivial extensions, e.g. (multiply).

Predefined Functions

For all predefined functions, arguments is a sequence of zero or more valid expressions. These expressions are evaluated before they are passed to the function.

  • ( plus arguments )
    • plus operates like the + procedure in Scheme.
    • If there are no arguments, this function returns 0.
    • If any argument is not an integer atom or a real atom, plus fails.
    • If all arguments are integer atoms, plus sums the arguments with integer addition and returns an integer atom.
    • If all arguments are integer atoms or real atoms, and at least one argument is a real atom, plus sums the arguments with real addition and returns a real atom.
  • ( lessthan arguments )
    • lessthan operates similar to the < procedure in Scheme.
    • There must be exactly two arguments. Both must be integer atoms or real atoms.
    • lessthan returns a Boolean atom with value #t if the first argument is smaller than the second argument, and #f otherwise.
  • ( isnull arguments )
    • isnull operates like the null? procedure in Scheme.
    • There must be exactly one argument.
    • isnull returns a Boolean atom with value #t if the argument is an empty list, and #f otherwise.
  • ( car arguments )
    • car operates like the car function in Scheme.
    • There must be exactly one argument, which must be a list.
    • car returns the car of the argument.
  • ( cdr arguments )
    • cdr operates like the cdr function in Scheme.
    • There must be exactly one argument, which must be a list.
    • cdr returns the cdr of the argument. If the cdr of the argument null, an empty list object is returned.

Special Forms

  • ( define variable expression )
    • variable must be a symbol atom, which is not evaluated.
    • expression can be any valid expression.
    • expression is evaluated and its value is bound to variable in the global scope of the symbol table.
      This expression has no value.
  • datum
    • datum is any valid expression in the grammar for our language.
    • datum is not evaluated.
    • The value of this expression is the unevaluated datum.
  • ( if test consequent alternate )
    • Evaluate test.
    • If test evaluates to #t, evaluate consequent and return its value.
    • Otherwise, evaluate alternate and return its value.

Input and Output Requirements

Your interpreter must read from standard input and write to standard output. Programs will be tested by redirecting standard input and standard output from/to text files. For example, if you submit a Python program, whose main file is Interpreter.py, we will test it using a command like:

python Interpreter.py <test_input.txt> test_output.txt

Program Requirements

Your program will consist of four separate units: the scanner, the parser, the interpreter and the test driver. It should be possible to set a flag in your program to either display or suppress the output from the scanner and parser modules. Your test driver program should be a loop, as follows:

Loop:
Read a line from standard input.
If the input line is empty: exit program.
[if display flag set to true] Print the input line to standard output.
Pass the input line to the scanner and receive the list of tokens.
[if display flag set to true] Print the tokens to standard output, in order, one token per line. 
If the scanner didn't encounter an error:
         Pass the token list to the parser module and receive the abstract syntax tree.
      [if display flag set to true] Print the abstract syntax tree to standard output.
         If the parser didn't encounter an error:
            Pass the tree to the evaluator and receive the resulting value or expression.
            Print the result to standard output.

Error Handling Requirements

If any of the modules encounters an error while processing an input line, it should abort in a graceful manner, displaying a meaningful error message and leaving the module ready for the next expression.

The interpreter main function should be constructed such that it recognizes a scanner, parser or evaluator error, reports the error, then continues to process the next line of input. The format and content of reported errors is up to you, as long as the interpreter identifies whether the error was from the scanner, the parser or the evaluator and provides information about exactly what kind of error occurred.

Submission Requirements

Your submission should include the source code for your scanner, parser and interpreter modules and test driver, along with a text file containing instructions for building and running your program. Every source code file should have your name and the assignment number in a comment at the top of the file. The instruction file must at least identify the compiler that you used for testing your program.

If you are not using C++/Java/Python, include a README file in your submission with detailed instructions to tell the grader where to find a compiler or interpreter, and how to build and run your program.