Computer Science Department
Programming Methods Laboratory

Compilation     summer 01
Ecole Polytechnique Federale de Lausanne
Programming Exercise 3

In this programming exercise we will work on name analysis for the expression langauge (Language, concrete and abstract syntax are all the same as in the last exercises). A typical program in this expression language looks like that:

   x = 5 * 3;
   y = x + 2;
The interpreter will read this and print 17, which is the value for the last variable.
The full concrete syntax for this language is:

   Program    ::= { Definition }
   Definition ::= { IDENT "=" Expression ";" }
   Expression ::= Term { ("+"|"-") Term }
   Term       ::= Factor { ("*"|"/") Factor }
   Factor     ::= IDENT | INTLIT | "(" Expression ")"

The abstract syntax is

   Program    = PROGRAM { Definition }
   Definition = DEF String Expression
   Expression = IDENT String
              | INTLIT int
              | BINOP Expression Expression char

Your job is to complete the analyze-visitor in Analyzer.java and then to complete the interpreter visitor for names.

  • Get the file exercice3.tar (Alternatively, if you have done the previous exercise, you can continue on that code).
  • Unpack it with the command tar xof exercice3.tar (This will create a new directory exercice3).
  • Go into the directory exercice3.
  • Run make to compile the given sources
  • Check the given analyzer with make analyzerTest < examples/example1.expr.
  • Now complete the source of the analyzer in source/expression/Analyzer.java by writing the methods caseDef(..) and caseIdent(..).
    • The tree data structure is given in source/expression/Tree.java
    • Use Report.error(...) to give error messages.
  • Recompile your analyzer with make and check it with make analyzerTest < examples/example1.expr
  • Create your own examples, to see whether undefined/doubly defined identifiers are reported as errors correctly.
  • Now complete the interpreter in source/expression/Interpreter.java by writing the methods caseDef(..) and caseIdent(..).
    • The class Symbol has a field val, which you can use to store the value for the expression.
  • Recompile your interpreter with make and check it with make interpreterTest < examples/example1.expr
If you have extra time, you can turn your interpreter into a real compiler from the expression language to Java.
  • Modify the pretty-printer to emit valid Java-Code (a class Main) by modifying source/expression/Printer.java
  • Use make printerTest < examples/example1.expr > Main.java to print the result into the file Main.java
  • Compile with javac Main.java and run it with java Main.
  • Does it work if you use Java keywords as identifiers?


Last modified: Thu Jun 14 18:39:31 MEST 2001