Terence Parr <parrt <at> cs.usfca.edu> writes:
>
> Hi all. Yep, I put ^ and ! suffix operators into ANTLR in about '93.
> Makes building ASTs really easy for things like expressions. Note
> that turning on AST construction with no operators builds a linked
> list (flat tree) of entire input; very convenient.
>
> Here's an example. This builds proper ASTs for additive expressions:
>
> add : atom ('+'^ atom)* ;
>
How does ^ work in the above context? Does it root the tree at the
'+' with all child atoms? The following is a pictorial explanation of
my automatic reduction rule as well as my non-excludable and raise
children operator.
Given the grammar:
Atom
: -<number> /* note: this is a non-excludable */
;
Additive
: ^Additive '+' Atom
: Atom
;
And the sequence of tokens: 1 + 2 + 3, the untransformed parse tree
appears as:
Additive
/ | \
Additive + Atom
/ | \ |
Additive + Atom 3
| |
Atom 2
|
1
The automatic reductions raise up the numbers in place of the Atom
as follows, and the '+' tokens are dropped as they are excludable:
Additive
/ \
Additive 3
/ \
Additive 2
|
1
And finally the ^ raises the children of the two lower Additives up
into the root Additive:
Additive
| | |
1 2 3
The actual process does not involve distinct phase, this is all simply
done as the parse tree is being built. Terrence, does the ^ operator
in ANTLR work in a similar fashion?
--
Best Regards,
Peter Goodman.
http://ioreader.com
_______________________________________________
PEG mailing list
[email protected]
https://lists.csail.mit.edu/mailman/listinfo/peg