Compiler HWK#5 Due date: 2:20pm, June 16, 2005 Write a simple compiler to generate C-- code for a simple PASCAL-like expression language with block structures. A sample program is as follows: PROGRAM program_name BEGIN VAR %% beginning of variable declarations, required even no var's x, y, z: INTEGER; ENDVAR READ(y); x := (y + 3 * 4) - 5; BEGIN VAR w, x, z: INTEGER; ENDVAR x := 2; WRITE(x); WRITESP(); WRITE(y); WRITELN(); END 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) 3.1 BEGIN and END starts and ends a block. 3.2 VAR ... ENDVAR is a declaration of local variables which can be empty or completely missing. 3.3 VAR ... ENDVAR must be before any statements and can have only one appearance. 4. can have only three different types of 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. If there is no error, genertae a C-- target code. 8. The length of a variable name is 1024. There can have lots of variables. I expect you to use either hash table or balanced-binary search tree for symbol tables. 9. Your keyboard input becomes the standard input of your program. Note: (1) To submit your homework, use the software submission system designed by TA. Read TA's web site for details. (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 "hw5". To compile a program, type hw5 filename.p The object code is "filename.c". We will then execute your C-- code.