Project Part 3 - Recursive-Descent Parser

This part is highly recommended to be solved and handed in groups of two. We give you one week more as students had last year, because there is a lot of work to do.

Goal

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.)

Deadline

You have to hand in your answers no later than Friday November 8th Oct 2002 at 10:15 am.

Contents

The answers handed in must include:

The program

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).

Error messages

We require the program to continue parsing after some errors:

Symbol table

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.

Type checking

Examples

A first example with syntax tree output.

Fibonacci

Two programs computing Fibonacci numbers:

  1. Recursive: fibrec.c-
  2. With arrays: fibarr.c-

Documentation

Hint