Computer Science Department
Programming Methods Laboratory

Compilation     summer 01
Ecole Polytechnique Federale de Lausanne
Programming Exercise

In this programming exercise we will work on the parser for an interpreter of an expression langauge. 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 top-down parser in Parser.java (it does not yet understand the full concrete syntax) and to complete the code which generates the tree.

  • Get the file exercice.tar
  • Unpack it with the command tar xof exercice.tar (This will create a new directory exercice
  • Go into the directory exercice
  • Run make to compile the given sources
  • Check the given parser with make parserTest < examples/example1.expr
  • Now change the source of the parser in source/expression/Parser.java to also accept identifiers and expressions in parenthesis as specified in the concrete syntax (function factor())
  • Check your parser with make parserTest < examples/example2.expr
  • Now change the source of the parser to also build trees in the functions term() and factor().
    • The tree data structure is given in source/expression/Tree.java
    • You need to insert the corresponding new calls in source/expression/Parser.java.
    • Each node has a pos-field containing the source position corresponding to the tree node. The pos-field from the parser contains the position of the token last read and can be used to set this field
    • The parser also has a chars-field, which contains the last token as a String.
  • Check whether your parser still works.
  • After completing the pretty-printer in the next programming exercise, you can see more easily, whether you generated the right tree.