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:

digraph structs {
  splines=line;
  node [shape=plaintext]
  93884347923344 [label=<
    <table cellborder='0' cellspacing='0'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='0' cellspacing='0' cellpadding='0'>
            <tr>
              <td port='nodename' colspan='1'>ChunkList</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347875392 [label=<
    <table cellborder='0' cellspacing='0'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='0' cellspacing='0' cellpadding='0'>
            <tr>
              <td port='nodename' colspan='1'>FunctionChunk</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347923344:nodename:s -> 93884347875392:nodename:n
  93884347710336 [label=<
    <table border='0' cellborder='0' cellspacing='0' cellpadding='0' color='red1'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='nodename'>FunctionDec</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='name'>name:&nbsp;_main</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='formals'>formals</td>
              <td port='result'>result</td>
              <td port='body'>body</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347875392:nodename:s -> 93884347710336:nodename:n
  93884347789088 [label=<
    <table cellborder='0' cellspacing='0'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='0' cellspacing='0' cellpadding='0'>
            <tr>
              <td port='nodename' colspan='1'>VarChunk</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347710336:formals:s -> 93884347789088:nodename:n
  93884347923920 [label=<
    <table border='0' cellborder='0' cellspacing='0' cellpadding='0' color='blue2'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='nodename'>SeqExp</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td cellpadding='0'>
                <table border='0' cellborder='0' cellspacing='0' cellpadding='0'>
                  <tr>
                    <td port='exps' colspan='2'>exps</td>
                  </tr>
                  <tr>
                    <td port='exps0'>0</td>
                    <td port='exps1'>1</td>
                  </tr>
                </table>
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347710336:body:s -> 93884347923920:nodename:n
  93884347776640 [label=<
    <table border='0' cellborder='0' cellspacing='0' cellpadding='0' color='blue2'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='nodename'>LetExp</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='chunks'>chunks</td>
              <td port='body'>body</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347923920:exps0:s -> 93884347776640:nodename:n
  93884347798912 [label=<
    <table cellborder='0' cellspacing='0'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='0' cellspacing='0' cellpadding='0'>
            <tr>
              <td port='nodename' colspan='1'>ChunkList</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347776640:chunks:s -> 93884347798912:nodename:n
  93884347788992 [label=<
    <table cellborder='0' cellspacing='0'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='0' cellspacing='0' cellpadding='0'>
            <tr>
              <td port='nodename' colspan='1'>VarChunk</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347798912:nodename:s -> 93884347788992:nodename:n
  93884347709296 [label=<
    <table border='0' cellborder='0' cellspacing='0' cellpadding='0' color='red1'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='nodename'>VarDec</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='name'>name:&nbsp;life</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='type_name'>type_name</td>
              <td port='init'>init</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347788992:nodename:s -> 93884347709296:nodename:n
  93884347791328 [label=<
    <table border='0' cellborder='0' cellspacing='0' cellpadding='0' color='blue2'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='nodename'>IntExp</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='value'>value:&nbsp;42</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347709296:init:s -> 93884347791328:nodename:n
  93884347716944 [label=<
    <table border='0' cellborder='0' cellspacing='0' cellpadding='0' color='blue2'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='nodename'>SeqExp</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td cellpadding='0'>
                <table border='0' cellborder='0' cellspacing='0' cellpadding='0'>
                  <tr>
                    <td port='exps' colspan='2'>exps</td>
                  </tr>
                  <tr>
                    <td port='exps0'>0</td>
                    <td port='exps1'>1</td>
                  </tr>
                </table>
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347776640:body:s -> 93884347716944:nodename:n
  93884347708064 [label=<
    <table border='0' cellborder='0' cellspacing='0' cellpadding='0' color='blue2'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='nodename'>CallExp</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='name'>name:&nbsp;print_int</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td cellpadding='0'>
                <table border='0' cellborder='0' cellspacing='0' cellpadding='0'>
                  <tr>
                    <td port='args' colspan='1'>args</td>
                  </tr>
                </table>
              </td>
              <td port='def'>def</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347716944:exps0:s -> 93884347708064:nodename:n
  93884347796848 [label=<
    <table border='0' cellborder='0' cellspacing='0' cellpadding='0' color='orange1'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='nodename'>SimpleVar</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='name'>name:&nbsp;life</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='def'>def</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347708064:args:s -> 93884347796848:nodename:n
  93884347718912 [label=<
    <table border='0' cellborder='0' cellspacing='0' cellpadding='0' color='blue2'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='nodename'>CallExp</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='name'>name:&nbsp;print</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td cellpadding='0'>
                <table border='0' cellborder='0' cellspacing='0' cellpadding='0'>
                  <tr>
                    <td port='args' colspan='1'>args</td>
                  </tr>
                </table>
              </td>
              <td port='def'>def</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347716944:exps1:s -> 93884347718912:nodename:n
  93884347717504 [label=<
    <table border='0' cellborder='0' cellspacing='0' cellpadding='0' color='blue2'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='nodename'>StringExp</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='value'>value:&nbsp;"\\n"</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347718912:args:s -> 93884347717504:nodename:n
  93884347776832 [label=<
    <table border='0' cellborder='0' cellspacing='0' cellpadding='0' color='blue2'>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td port='nodename'>SeqExp</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td cellpadding='0'>
          <table border='0' cellborder='1' cellspacing='0' cellpadding='2'>
            <tr>
              <td cellpadding='0'>
                <table border='0' cellborder='0' cellspacing='0' cellpadding='0'>
                  <tr>
                    <td port='exps' colspan='1'>exps</td>
                  </tr>
                </table>
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  >]
  93884347923920:exps1:s -> 93884347776832:nodename:n
  
}

print-42.png