|
In this programming exercise we will work on a pretty printer
for an interpreter of an expression langauge (The language, concrete
and abstract syntax are all the same as in the last exercise). 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 pretty-printer visitor in
Printer.java. If you have extra time you can also
work on the interpreter visitor, but this will only work
with identifiers after name analysis.
- Get the file exercice2.tar
(Alternatively, if you have done the previous exercise, you can
continue on that code).
- Unpack it with the command
tar xof exercice2.tar
(This will create a new directory exercice2
)
- Go into the directory
exercice2
- Run
make
to compile the given sources
- Check the given pretty-printer with
make printerTest < examples/example1.expr
(This
will just print some empty lines)
- Now complete the source of the pretty-printer in
source/expression/Printer.java
by writing
the methods caseDef(..)
, caseIdent(..)
,
and caseBinOp(..)
.
- The tree data structure is given in
source/expression/Tree.java
- You can look at the example in
caseProgram(..)
- If you use your own parser solution, be aware that
strange results or NullPointerExceptions may happen
because of errors in the tree generation part of the parser.
- Recompile your pretty printer with
make
and check it with
make printerTest < examples/example2.expr
- Now you can continue on the interpreter-visitor in
source/expression/Interpreter.java
.
Here you can fix caseBinOp(..)
to return the proper result. You can
again look at the other cases for examples.
- Check the interpreter with
make interpreterTest < examples/example1.expr
- We cannot yet do the
caseIdent(..)
, because we have
not yet done name analysis, so the second example will not yet work.