Compiler HWK#4 Due date: 2:20pm, May 31, 2007 Write a simple interpreter for a simple PASCAL-like expression language. A sample program is as follows: PROGRAM program_name %% newline is required here VAR %% beginning of variable declarations, required even no var's %% VAR must be in a line by itself x, y, z : INTEGER; %% var names cannot be the same with %% program_name BEGIN READ(y); x := (y + 3 * 4) - 5; WRITE(y); WRITESP(); WRITE(x); WRITELN(); END The definition of the language is as follows: 1. Case-sensitive with reserved words. Reserved words are upper-cased. Program_name is a special variable name. Program has no subroutine and control statements. Program is enclosed by BEGIN ... END 2. Assignment is ":=", no space is allowed between ":" and "=". the statement terminator is ";" each line has at most one statement 3. Variable must be declared before its usage. Variable/program names follow C style. Can have only the type of INTEGER (4-byte signed) 4. Can have only three different statements: a. assignment of expressions, where expressions can have variables, integer-constant, (, ), +, -, *, /, ^ exponention and minus "-". An integer-constant is in the form of "digits", where digits = [0-9]+ b. input statement READ(single variable) c. output statements WRITE(single variable) --- output a variable using "%d" format WRITESP() --- output a single space WRITELN() --- write a new line There is no space before and in between "()" for WRITESP() and WRITELN(). 5. Anything after %% are comments until to the end of the line. Allow white space of tab and space. Can have empty lines. Cannot have empty statements, ie, a line with only a ";" is illegal. 6. Must detect the following error and reports its line number: a. invalid ID names b. undeclared variables c. illegal syntax d. a variable declared twice e. constant overflow 7. Must run the program and produce outputs accordingly if there is no error. Hints: (1) Assume <= 1024 variables can be declared. Each variable name has at most 32 characters. First allocate 1024*4 bytes and keep track of the address of each variable. (2) The semantics of a variable name are different on the LHS and RHS of an assignment. LHS: retrieve the storage address of a variable and store a value from the RHS to it RHS: retrieve the value of the variable Note: You need to have LEX and YACC programs. Your program code is in the file "program.p". LEX takes input from program.p. Your keyboard input becomes the standard input of your program. TA will post sample test data and more specs.