[antlr-interest] Tree grammars & optional AST nodes

Terence Parr parrt at cs.usfca.edu
Thu Oct 19 17:52:15 PDT 2006


On Oct 19, 2006, at 5:09 PM, Nicolas Rouquette wrote:

>>
>> Hard to beat tree operators for expression stuff.
>
> An ANTLR afficionado colleague suggested
> adding a simple semantic predicate
> to guide the tree construction:
>
> @init { boolean flag=false; }
>   : e1=conditionalExpression (op=assignmentOperator e2=expression  
> {flag=true;})?
>     -> {flag} ? ^($op $e1 $e2)
>     -> $e1
>   ;
>
> I tried this w/ the ANTLRWorks debugger on a grammar
> with several nested expression-like rules. Works very nicely!

Hooray!

or use {$op!=null}? without the flag mumbojumbo.

> I've used the same options as the java grammar
> (although my language isn't java at all).
>
> Adding the semantic predicate obfuscates a bit the grammar
> but the price is really worth the result IMHO.

:)

Ter


More information about the antlr-interest mailing list