You are going to implement in Java a recursive-descent parser recognising valid C- programs, checking whether the program is type correct, and outputting the syntax tree and symbol table for the program. If the program is invalid, an error message is to be printed.
You are highly recommended to do the exercise in groups of two. (If there is an odd number of students, you should ask us. We accept one, but only one group of three.)
You have to hand in your answers no later than Friday November 8th Oct 2002 at 10:15 am.
The answers handed in must include:
The program is to read source code for a C- program from standard in, and print the abstract syntax tree and symbol table to standard out after the program having finished. If errors are found in the source code, the program should report the error to standard err. We give some examples of what the output may look like (see below).
We require the program to continue parsing after some errors:
if ( a <= b <= c ) { foobar ( a + b ) ; }the program shall report an error in the condition, and continue parsing to analyse the statement block.
int foobar ; void foobar ( int b ) { ... } ;in the same block, the program should report that foobar is already defined, but continue. Each multiple declaration should cause but one error message, so if you call foobar both as a function and as a variable after this multiple declaration, the program should not report another error.
In the next handin, you will need more attributes for functions and local variables, on top of those required here. It may be wise to take this into account even at this point.
To handle local variables, you may use a stack of symbol tables. When you come to a compound statement, you add a new table on top of the stack, and when you reach the end, you pop the stack. Each node in the syntax tree may keep a reference to the symbol table stack which is valid in this particular node. Thus the symbol tables are available from the syntax tree, even if the parser routines throws them away. To print the symbol table, you may travers the syntax tree and print the top-most table in each node ending a scope.
A first example with syntax tree output.
Two programs computing Fibonacci numbers: