[antlr-interest] Tree grammars & optional AST nodes

Nicolas Rouquette nicolas.rouquette at jpl.nasa.gov
Fri Oct 20 23:24:47 PDT 2006


Terence Parr <parrt at ...> writes:

> On Oct 19, 2006, at 5:09 PM, Nicolas Rouquette wrote:
> 
> >  <at> init { boolean flag=false; }
> >   : e1=conditionalExpression (op=assignmentOperator e2=expression  
> > {flag=true;})?
> >     -> {flag} ? ^($op $e1 $e2)
> >     -> $e1
> >   ;
> 
> or use {$op!=null}? without the flag mumbojumbo.

Thanks for this suggestion; however, there's one small glitch...
If I write:

expression
 : e1=conditionalExpression (op=assignmentOperator e2=expression)?
   -> {$op!=null} ? ^($op $e1 $e2)
   -> $e1
 ;

3.0b4 produces this kind of code:

if ($op!=null) {
  // the code for ^($op $e1 $e2)
} else {
  // the code for $e1
}

... and a telltale error message:

missing attribute access on rule scope: op

If, instead of {$op!=null} I try: {$expression.op!=null}
the code produce has a similar problem:

if ($expression.op!=null) ...

and the message is equally informative:

unknown attribute for rule expression: op

After some digging, I found that these messages
from from ERROR_X and ERROR_XY respectively in action.g

This gives a lot of clues w.r.t. troubleshooting this problem
since it's clear that none of the other lexer rules found
the action stuff palatable enough to eat it before the
voracious ERROR_X/Y rules did.

Which rules ought have matched?
Looks to me that we'd expect:

"$op" => LABEL_REF
"$expression.op" => TOKEN_SCOPE_ATTR

At this point, I don't know enough about the internal states 
of the lexer & parser to tell why the rules didn't match.

It's possible that my analysis is wrong,
that the problem is elsewhere entirely.
I tried some variations, including checking against
a non-optional part of the rule, e.g.:

expression 
  : e1=... (... e2=...)?
    -> {$e1!=null} ...

The results are the than if I check the optional part of the rule.

-- Nicolas.
 
> Ter
> 
> 






More information about the antlr-interest mailing list