[antlr-interest] Tree building limitation in tree grammars?
John B. Brodie
jbb at acm.org
Mon Nov 28 08:56:38 PST 2011
Greetings!
On 11/28/2011 08:08 AM, franck102 wrote:
> My parser grammar can generate a number of subtrees with similar structure,
> and just a different root type. In the tree grammar I am trying to just pass
> such a subtree along without any changes, but that is turning out to be much
> trickier than I expected, am I missing something?
>
> Given this AST: ^( AND true false ) I want the tree grammar to generate the
> same AST subtree.
>
> The following attempts are not working:
>
> ^( ( AND | OR ) booleanExpression booleanExpression ) // no rewrite =>
> flattens the tree,
i am surprised. flattening has not been my experience, altho i have not
used an alternative for the root node....
>
> // I get a nil root and 3 children
>
> ^( ( root=AND | root=OR ) booleanExpression booleanExpression )
> -> ( ( AND | OR ) booleanExpression booleanExpression ) //
> syntax error
>
> ^( ( root=AND | root=OR ) booleanExpression booleanExpression )
> -> ( AND? OR? booleanExpression booleanExpression ) //
> syntax error
>
> ^( root=(AND | OR ) booleanExpression booleanExpression )
> -> ( $root booleanExpression booleanExpression ) // runtime
> error, $root is empty
>
> Am I going to have to split those rules??
>
> Any suggestion appreciated!
>
>
3 untested suggestions (i happen to prefer the third, but i am weird;-):
^( (root=AND|root=OR) e1=booleanExpression e2=booleanExpression )
-> ^($root $e1 $e2)
or
^( binaryOperator booleanExpression booleanExpression ) // no rewrite
or
^( op=binaryOperator e1=booleanExpression e2=booleanExpression )
-> ^($op $e1 $e2)
where
binaryOperator = AND | OR /* | others go here... */ ;
More information about the antlr-interest
mailing list