Symbol table for statically-typed language

Skip to end of metadata
Go to start of metadata

Your goal is to build a symbol table for a simple object-oriented programming language, which we can call L. As a practical matter, all you need to do is add actions to the grammar shown below to create symbol objects and add them to a symbol table that is a cross between patterns 18 and 19 in Language implementation patterns. To keep things simple, you don't need to handle inheritance, but there are classes; even classes nested in functions. You also don't need to do field lookup to keep the assignment short.

Here's a sample input program.

There are only two built-in types: int and float. We can't even reference the classes we define (since we are going to access fields, there is no point). functions within a single class must be uniquely named and all class names must be unique. Do not worry about forward references. In other words, do everything in one pass. You do not need to build tree. Let's focus on just getting the symbol table correct.

The grammar below reads this in and detects syntax errors, but nothing else. Add actions as appropriate to build up a scope tree. At each class, field, function, argument, and variable definition, you need to create a symbol object. The symbols for classes and functions play the role of scopes as well and each contain a list of symbols in that scope. Each scope symbol has a parent or enclosing scope. The enclosing scope of the function is always a class, but the enclosing scope of the class could be a class or a function. For each variable reference (see rules stat and expr), look up the symbol and announce which scope it lives in.

The output of the program is a series of symbol definition and lookup reports, prefixed with the line and character position in the line. For example, for the above input, I would expect something like:

1:6: def class C
2:2: def field x of type int
3:2: def field z of type int
5:6: def fun f
5:14: def arg x of type float
5:23: def arg y of type float
6:10: def class D
...
9:8: ref D.x of type int
9:12: ref arg f.y of type float
...
17:4 ref fun C.f
18:4 unknown symbol h
19:4 ref local g.q of type int

Grammar

You should put the main() directly in the @members section of your grammar L.g.

Submission

You will create a jar file called sym.jar containing source, grammar, and *.class files and place in your build directory:

https://www/svn/userid/cs652/sym/build/sym.jar

I will run your code by executing the following:

$java -cp "sym.jar:antlr-3.3.jar:$CLASSPATH" LParser < test.l

You can use the svn account for development of the software too if you would like, but I will only be looking at your jar file in the build directory.

For more information, see svn in CS601. Naturally you will have to substitute cs652 for cs601.

Labels: