TC-1/2-Parser Optional GraphViz Dumper
The DumperDot class is optional: it is an optional visitor for TC-1/2. This
visitor is accessible with the option --ast-dump. It displays a graph
in dot format.
The --ast-dump command dumps the AST so that it can be exported to
dot style.
This option will give you a better visual on your AST, to ease your debugging.
If you wish to use it, you will need to fill the various FIXME in
src/ast/dumper-dot.cc (The src/ast Directory). The instructions will always be
in the same order. Take inspiration from the examples given in full.
This option will be of great help in the following steps, especially TC3 and TC4.
void DumperDot::operator()(const FunctionDec& e)
{
// Start html node, set the parent_id and return previous parent_id.
unsigned long old_parent_id = node_html_header(e, "FunctionDec");
// Optional data field with name.
node_html_field("name", e.name_get());
// List of children keys.
node_html_ports({"formals", "result", "body"});
// End html node.
footer_and_link(old_parent_id);
// Recursive call to every children using key.
dump("formals", e.formals_get());
dump("result", e.result_get());
dump("body", e.body_get());
// Reset parent_id to previous parent_id.
parent_id = old_parent_id;
}
Some ASTs have attributes which contains a list of nodes. Due to that, two other member functions exist: node_html_port_list and dump_list.
Here is an example for SeqExp which contains a list of Exp.
void DumperDot::operator()(const SeqExp& e)
{
// Start html node, set the parent_id and return previous parent_id.
unsigned long old_parent_id = node_html_header(e, "SeqExp");
// Optional data field with name is not present.
// Initialize children keys part.
node_html_ports();
// Create a multi port, one for each exp of the list.
node_html_port_list("exps", e.exps_get());
// End html node.
footer_and_link(old_parent_id);
// Recursive call to every element of the list using helper dump_list.
dump_list("exps", e.exps_get());
// Reset parent_id to previous parent_id.
parent_id = old_parent_id;
}
Here is a rather simple example of the steps to use your visitor.
let
var life := 42
in
print_int(life);
print("\n")
end
$ tc -X --ast-dump print-42.tig > print-42.dot
$ echo $?
0
You can now generate the image using dot.
$ dot -Tpng print-42.dot -o print-42.png
![digraph structs {
splines=line;
node [shape=plaintext]
902030976 [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>
>]
902192768 [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>
>]
902030976:nodename:s -> 902192768:nodename:n
902026624 [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: _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>
>]
902192768:nodename:s -> 902026624:nodename:n
902037168 [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>
>]
902026624:formals:s -> 902037168:nodename:n
902027744 [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>
>]
902026624:body:s -> 902027744:nodename:n
902114048 [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>
>]
902027744:exps0:s -> 902114048:nodename:n
902199856 [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>
>]
902114048:chunks:s -> 902199856:nodename:n
902037072 [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>
>]
902199856:nodename:s -> 902037072:nodename:n
902025552 [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: 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>
>]
902037072:nodename:s -> 902025552:nodename:n
902039936 [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: 42</td>
</tr>
</table>
</td>
</tr>
</table>
>]
902025552:init:s -> 902039936:nodename:n
902035248 [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>
>]
902114048:body:s -> 902035248:nodename:n
902200608 [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: 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>
>]
902035248:exps0:s -> 902200608:nodename:n
902114176 [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: 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>
>]
902200608:args:s -> 902114176:nodename:n
902038144 [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: 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>
>]
902035248:exps1:s -> 902038144:nodename:n
902026512 [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: "\\n"</td>
</tr>
</table>
</td>
</tr>
</table>
>]
902038144:args:s -> 902026512:nodename:n
902031600 [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>
>]
902027744:exps1:s -> 902031600:nodename:n
}](../../../_images/graphviz-f066b5015ef67500991e57943b6ee84f1056a441.png)
print-42.png