For this assignment you will write Python interpreter for a tiny programminglanguage, lets call it TL (Tiny Language). The language contains just three types ofstatements:let variableName = expressionif expression goto labelprint expression1, expression2, input variableNameEach statement may contain a preceding label. A label is an alphanumeric stringending with a colon (:).A number of simplifying assumptions have been made about the syntax of thelanguage. White space (blanks) are important and must be used to separate each tokenincluding around the arithmetic operators. There can be only one statement per line. The expressions are limited to constant numbers, constant strings, variablenames, and binary expressions involving just one of the following operators: +,-, *, /, <, >, <=, >=, ==, or !=, with their conventional meanings.Note again that the operators must be surrounded by spaces, which makes foreasier parsing. The only types are strings and floating point numbers (Float in Haskell) andstrings are only used in print statements. The result of Boolean operations is 0 iffalse and 1 if true. Furthermore any numeric expression can be used in an ifstatementand as with the C language, 0 is false and everything else is true. Blank lines are ignored.let variableName = expression computes the value of expression then binds that valueto the name variableName.if expression goto label computes the value of expression, if the value is 0 executioncontinues with the next statement. If the value is non-zero then execution continueswith the statement labeled label. If no such statement exists, the programterminates with the message: Illegal goto label at line x. where x is the actual linenumber of the illegal goto statement.print expression1, expression2, evaluates each expression, then prints their values,all on one line, separated by spaces, terminating the line with a newline character.input variableName attempts to read a number from the standard input. If successfulthat value is bound to the name variableName. If the read fails the program shouldterminate with the message: Illegal or missing input.If at any point an attempt is made to evaluate an expression that references avariable variableName for which there is no binding, the program terminates withthe message: Undefined variable variableName at line x. where x is the actual linenumber of the failed expression.Your program tli.py (tiny language interpreter) will take one command lineargument, the name of the source file. It will compile the program into an internalform and then execute the compiled program. If the input program contains anysyntax errors, tli should exit, printing a message of the form Syntax error on line x.for the first syntax error detected, without attempting to execute the program.Although your program is allowed to continue and report additional syntax errorsor provide additional error information, that is not required and will not affect yourscore.tli.py should make just one pass over the program source building an internalrepresentation that is a list of statements and a symbol table that maps labels intoline numbers. The complete internal representation must be built before attemptingto execute the first statement of the program. You may use this same symbol table tostore variable bindings during execution of the program.You are free to implement the program in anyway you see fit, with the singleconstraint that it must build an internal representation that is clearly derived fromthe starter code which provides the beginnings of a class for statements (Stmt) anda class for expressions (Expr). You are allowed to modify these as you see fit. Beforeprogram execution begins (after compilation) your program must have created a listof Stmt values that represent the statements of the TL program, and whereappropriate, those Stmt objects must contain Expr values.Here is a tiny language program that prints out a sequence of numbers.input startinput endlet x = startrepeat: print xlet x = x + 1if x < end goto repeatprint thats all, xAssuming the above program is stored in prog1.txt, when executed the commandtli.py prog1.txt and the user entering 1 and 5, tli.py should produce the output1.02.03.04.0thats all 5.0You should turn in one file, tli.py.
CSE112
[Solved] CSE112 Assignment 3-Python interpreter
$25
File Name: CSE112_Assignment_3-Python_interpreter.zip
File Size: 357.96 KB
Only logged in customers who have purchased this product may leave a review.
Reviews
There are no reviews yet.