TC-2 Code to Write

Warning

Take time to read all ast classes in the ast directory to understand them well as you will build nodes in the parser and print them in the Pretty Printer.

What is to be done:

src/parse/tiger-factory.hxx

Implement all methods to create AST nodes on the driver with make_ methods.

As mentioned above the tiger-factory functions will be used in Bison’s actions to create the different nodes of the AST.

src/parse/tasks.cc

Complete the parse procedure to set the_program (the root of the AST) as the result of the parsing.

src/parse/parsetiger.yy
Build the AST

To build your AST you will have to use Bison’s actions.

Here is an example of actions you can do for arithmetic operations:

/* Arithmetics.  */
exp: /*this is the production rule */
  exp "+"  exp { $$ = $1 + $3; }
| exp "="  exp { $$ = $1 == $3; }
...

Statements between brackets are called actions. They allow to link the reduction of a rule to code. What we want to do is recursively build an AST. For that, we make use of action features.

We use the $1 action feature to retrieve the parsing production of the the first symbol (here, the left-hand side exp). $3 correspond to the third symbol (the right-hand side exp); @$ retrieves the location of the rule and $$ means that the assigned expression will be the result of the production rule.

The previous code example can also be written in this way:

/* Arithmetics.  */
exp[res]:
  exp[left] "+" exp[right] { $result = $left + $right; }
| exp[left] "=" exp[right] { $result = $left == $right; }
...

The two version have the same behavior, but in this version we replaced the action features by named references. Here, $$ is replaced by $result, $1 is replaced by $left and $3 is replaced by $right.

To produce the AST you will have to use the same process but using this time the functions declared in the tiger-factory files.

/* example */
make_OpExp(/* insert the correct arguments */);
make_IfExp(/* insert the correct arguments */);

Warning

To have a full understanding of Bison’s actions read the Bison documentation about actions and action features.

Desugar Boolean operators and unary minus in concrete syntax

In the original version of the exercise, the | and & operators and the unary minus operator are desugared in abstract syntax (i.e., using explicit instantiations of AST nodes). Using TWEAST, you can desugar using Tiger’s concrete syntax instead. This second solution is advised.

More information available in the syntactic sugar section on the CCMP slides.

Support object-related syntax (Optional)

Supporting object constructs, an improvement suggested for TC-1 (see TC-1 Improvements), is highly recommended.

Error recovery (Optional)

There should be at least three uses of the error token. Read the Bison documentation about it. Use %destructor to reclaim the memory bound to semantic values thrown away during error recovery.

src/ast/default-visitor.hxx

Complete the GenDefaultVisitor class template. It is the basis for following visitors in the Tiger compiler.

For more information on the visitor design pattern here is a link that may help you.

src/ast/object-visitor.hxx (Optional)

Likewise, complete GenObjectVisitor. This class template is used to instantiate visitors factoring common code (default traversals of object-related nodes) and serves as a base class of ast::PrettyPrinter (and later bind::Binder).

src/ast/pretty-printer.*

The PrettyPrinter class must be written entirely. It must use the misc::indent features to support indentation.

src/ast/dumper-dot.* (Optional)

The DumperDot class is used to create a graphviz dumper of your ast.

The dumper dot, even if it is not mandatory, is very practical for the debug it allows to visualize its AST very simply. This part is optional an will not be tested (see TC-2 Optional GraphViz Dumper).

An example of what the dumper dot produces: