Compiler HWK#4 Due date: 2:20pm, May 19, 2005 Write a simple compiler-interpreter for a simple PASCAL-like expression language. A sample program is as follows: PROGRAM program_name VAR %% beginning of variable declarations, required even no var's x, y, z: INTEGER; BEGIN READ(y); x := (y + 3 * 4) - 5; WRITE(y); WRITESP(); WRITE(x); WRITELN(); END The definition of your language is as follows: 1. This language is case-sensitive with resevered words. 2. assignment is ":=", the statement terminator is ";" each line has at most one statement 3. variable must be declared before its usage. variable/program names uses the one in HWK1. 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, (, ), +, -, *, / and "uminus". b. input statement READ(single variable) c. output statements WRITE(single variable) --- output a variable WRITESP() --- output a single space WRITELN() --- write a new line There is no space before and in between "()". 5. Anything after %% are comments until to the end of the line. Watch out for white spaces as in HWK 1. 6. Must detect the following error and reports its line number: a. invalid ID names b. undeclared variables c. illegal syntax d. duplicatedly declared variables 7. Must run the program if there is no error. Hints: (1) Assume <= 256 variables can be declared. Each variable name has at most 32 characters. First allocate 256*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: (1) To submit your homework, send an email to comp05@csie.ntu.edu.tw subject: HWK4: your student number and name eg: "HWK4 - B91922xxx ¤ý¤p©ú" attachments: one file: B91922xxx.tar.gz files must contain in the current dir after uncompression and untar (2) You need to have LAX and YACC programs. This homework MUST execute on Linux or FreeBSD workstations in our department. Submitting files include a lex file, a Makefile, and optional C codes. The Makefile MUST supports "make clean" and "make all" for cleaning, and building the homework. An example of the Makefile can be obtained from the course web. Do not use the samples without proper changes. The name of your executable after "make all" must be "hw4" and takes input from standard input and writes to standard output. Note that 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.