[antlr-interest] error in tree grammar syntax...

pady prabha_pady at comcast.net
Mon Dec 29 13:51:03 PST 2008


I was under the assumption that the paranthesis is what makes the parser 
choose different paths...so to differentiate input like

if ( bool )
  var = val;  // assignment

if ( bool ) {
  var = val; // assignment
  if ( bool2 ) // statement
   var2 = val2; // assignment
}

I have unit tested some cases like above and it seems to be working. About 
the "optional statements", this is where my understanding on rewrite rules 
is poor...i dont understand how to differentiate the 2 optional statements 
using more structure in the ast...ie the syntax
to use is what i am not clear on.


Thanks

-- pady


----- Original Message ----- 
From: "Gavin Lambert" <antlr at mirality.co.nz>
To: "Pady Srinivasan" <padysrini at hotmail.com>; <antlr-interest at antlr.org>
Sent: Monday, December 29, 2008 4:41 PM
Subject: Re: [antlr-interest] error in tree grammar syntax...


> At 10:27 30/12/2008, Pady Srinivasan wrote:
> >When I try something like this in my tree grammar, I get an
> error:
> >
> >statement
> > : assignment
> > | ^('if' boolExpression assignment? statement* ^('else'
> >   (statement)? (statement)? )? )
> > ;
> >
> >
> >error(100): GwfWalker.g:32:85: syntax error: antlr:
> >GwfWalker.g:32:85: expecting RPAREN, found '?'
> >error(100): GwfWalker.g:32:87: syntax error: antlr:
> >GwfWalker.g:32:87: expecting SEMI, found ')'
> >error(106): GwfWalker.g:26:4: reference to undefined rule:
> >statement
>
> Did you try removing the parentheses around "statement"?
>
> Also, you're going to have trouble with a construct like
> that.  Having two optional statements in series -- when only one
> statement is present in the input tree, which is it?  The first or
> the second?  You need to either put more structure in your AST or
> change the nesting so that it's obvious which one is "more
> optional" than the other, when only one of the two is present.
>
> You'll have similar trouble with "assignment? statement*", since
> an assignment is a statement.  This is a redundancy, and those
> almost always lead to ambiguity and weird behaviour.
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: 
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address 



More information about the antlr-interest mailing list