Parse-Tree

Top  Previous  Next

Examples > Java > Parse-Tree

 

The Java parser is a relatively extensive project and a lot of work can be expected, to make a complete transformation program from it. The wizards of the TextTransformer can help greatly here.

As an example, at first the code for the generation of a parse tree shall be inserted in the project by the tree wizard. With the function table wizard then functions can be created by which the parse tree can be evaluated.

It is frequently recommendable to create a transformation program at first that simply copies the source files. With such a program the parser and the parse tree can be tested well: the program works correctly if the transformed files are identical with the source files. At the copy program then simple modifications can be made, the results of which can also be verified easily.

 

Who don't like to reproduce the following steps in detail can load the ready result made by the tree wizard also directly:

 

...\TextTransformer\Examples\Java\JavaTree.ttp

 

With the tree wizard code, which is required for the creation of a parse tree, can be inserted in all productions. Since it doesn't suffice for a copying program to insert simple nodes for the recognized tokens, but a node always must be inserted for the ignored text before the token too, at first a function is defined on the element page to do this.

 

Name: IgLit  // Ignorierter Text und Literal

Parameter: node& xnNode, const str& xs

Text:

{{

node n("IgLit");

xnNode.addChildLast(n);

n.add("IGNORED", xState.str(-1));

n.add(xs, xState.str());

}}

 

A sub-node  with the label "Iglit" is added to the tree node xnNode and this sub-node gets the nodes for the ignored text and the text recognized by the token.

 

Now call the Tree wizard from the menu Help, and choose the name n for the nodes:

 

TreeWizard_Name

 

Below of the name field you can already see, how the node parameter in the parameter field and the node declaration in the text of the production will look like.

 

TreeWizard_ActionType_en

 

Then choose the complete option on the next page of the wizard:

 

TreeWizard_For_en

 

On the next page of the wizard an action can be defined, which will be inserted after each occurrence of a literal token in the project. The function for this action was just defined above. Here you have to write, how to call it.

 

TreeWizard_LitAction_en

 

IgLit is a function, which has to be defined afterwards.

On the next page activate the check box: Pass parameters to all calls.

Then you can go to the last page and click on the Finish button.

 

 

TreeWizard_Finish_en

 

After the results are shown you can close the Tree wizard by the Cancel button.

 

 

To be absolutely correct, you can insert a semantic action at the end of CompilationUnit, by which the text is inserted into the tree, which follows on the last recognized token.

 

{{

node ni("IgLit");

n.addChildLast(ni);

ni.add("IGNORED", xState.str(-2));

ni.add("REST", "");

}}

 

 

You can examine a generated tree after execution of the program with the start rule CompilationUnit, as demonstrated for the XML example in the variable inspector: 

 

TreWizard_Tree_en

 

Remark: At the end of the start rule CompilationUnit explicitely the symbol EOF is set. The wizard has inserted an action for this symbol too:

 

EOF {{IgLit(n, "Literal");}}

 

In this last action also the ignored text will be inserted in the tree, which follows on the last symbol of the Java grammar:

 

the comment: "// JavaParser"

 



This page belongs to the TextTransformer Documentation

Home  Content  German