[antlr-interest] Expression grammar issues (typecasts, method calls)

Nigel Sheridan-Smith nbsherid at secsme.org.au
Sun Jul 24 20:51:50 PDT 2005



> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of John Barnette
> Sent: Monday, 25 July 2005 10:42 AM
> To: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Expression grammar issues (typecasts,method
> calls)
> 
> On 7/24/05, Nigel Sheridan-Smith <nbsherid at secsme.org.au> wrote:
> > > There are two commented-out lines in the grammar, one in
> > > unaryExpression and one in primaryExpression. Grep down for "typecast"
> > > to find the first, and "func" to find the second. When either or both
> > > of these lines are uncommented, parsing fails with an "unexpected
> > > token: null" exception.
> 
> (snip)
> 
> > It looks to be your actions, although I can't say for sure without
> further
> > testing.
> >
> > Firstly, you probably don't need to use '#' in a normal parser, as this
> sort
> > of thing is only needed for tree parsers, so you can remove these.
> Secondly,
> > I'm not sure if you can do a setText() at this point. Try taking out the
> > '#', and if that doesn't work, remove the actions temporarily to see if
> that
> > makes any difference.
> 
> Thanks, Nigel. I've removed the actions, which seems to have at least
> made the typecast statement work. However, I'm still getting an
> unexpected token when I uncomment the line that should match function
> calls. Here's a minimal testcase in Java that will reproduce the
> error:
> 
> HippoParser parser = new HippoParser(new HippoLexer(new
> StringReader("foo")));
> parser.expression();
> 
> I've placed a slightly-modified grammar (with the misbehaving line
> uncommented) at: http://hippo.sourceforge.net/hippo.g. The issue is in
> primaryExpression:
> 
> primaryExpression
> 	:	IDENTIFIER
> 	    (PAREN_LEFT^ argList PAREN_RIGHT!)?
> 	|	literal
> 	|	PAREN_LEFT^ expression PAREN_RIGHT!
> 	;
> 
> Any further advice, abuse, or general nattering is much appreciated. :)
> 


It might be a problem that is common with StringReader, in that you need to
force the end-of-file on your top-level rule.

Add the following line:

expressionToParse: primaryExpression EOF!


Then, in your code:

HippoParser parser = new HippoParser(new HippoLexer(new
	StringReader("foo")));
parser.expressionToParse();



Cheers,

Nigel

--
Nigel Sheridan-Smith
PhD research student

Faculty of Engineering
University of Technology, Sydney
Phone: 02 9514 7946
Fax: 02 9514 2435




More information about the antlr-interest mailing list