TC-2 Code to Write

Warning

For Apprenti only :

Record and Array are optional, Hence you do not need to implement the following nodes : array-ty, array-exp, field, field-init, field-var, nil-exp, subscript-var, record-exp, record-ty

What is to be done:

src/ast

Complete the abstract syntax tree module: no FIXME: should be left. Several files are missing in full. See src/ast/README for additional information on the missing classes.

You will now have to create your ast, the goal will be to implement the different classes present in src/ast, you will find FIXME on the one you will have to implement.

src/parse/tiger-driver.hxx

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

As mentioned above the tiger-driver 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 class you created and the method declared in the TigerDriver class. The TigerParser class has an attribut of type TigerDriver call td_. From this attribut you can access all TigerDriver methods.

/* example */
tp.td_.make_OpExp(/* insert the correct arguments */);
tp.td_.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: