|
Home |
Download |
News |
Wiki |
About ANTLR |
Feedback |
Support |
Bugs
|
|
|
Latest version is 2.7.7. Download now! » |
|
|
Draft specification for Antlr Tree Generation
Control of automatic tree generation in the ParserBy default the Parser will automatically generate AST trees. This generation can be disabled globally by setting buildAST=false. When buildAST=false ALL code related to AST tree building is removed and the only ways to build your own tree are :-
(Note: in this mode you should not need to link or load any AST code unless you reference it yourself from action, etc) With buildAST = true (that is the default) you can selectively disable
tree generation by using the ! syntax. This can be used on either a
rule or token basis. Example of a rule based use of ! to disable tree
generation
In this case no tree generation code is generated for this rule. If
you want to create a tree by hand for this rule you need to return it
as shown below
So I suggest this be relaxed a little to say that No tree generation
code is output except that labels in the rule are initialized with the
appropiate minimal tree. For example
results in #t3 containing tree resulting from the rule table_name and
would set up a tree for label i consisting of a single root node containing the INT token This allows the user to control what tree code is added to their code if the tree generation is turned off for a rule. If there are not labels then no code. To suppress a single token use ! after the token. It will not be
added to the tree, eg.
Note as far as the rule statement is concerned
Is the same as
But in the second case no rule in the parser can get a tree from the
addition rule. And
is redundant but legal. You would probably actually use something like
Note: It makes no sense in this system to allow ! to be applied to
alternative of rules that is :-
is now illegal In all other cases (that is when buildAST is true and ! is not used) the return tree is always generated and assigned to the global AST_return to be picked up by the parent rule. This AST_return can be modified/overwritten using the syntax discussed below. Syntax for manual modification of trees in the ParserNote this is for modification of trees that have been automatically created. If you set buildAST=false or use ! on a rule, you are on your own as no tree code is generated for you. Tree nodes are created using
Trees are created using
where
Elements of the current rule can be addressed using the following
When these occur on the rhs of = they are replaced by clones of their respective nodes or trees. This prevents deadly loops. As an optimisation
could be done without cloning ##. When these occur on the lhs of = they refer to that location in the
tree. This allows subtree replacements. eg,
|
|||||||||||||||||||||||||||||||||||||||||||||||||