[antlr-interest] Bug in Antlr - confusion on ANTLR grammar syntax???

John MccLain jmcclain at tcshealthcare.com
Thu Dec 9 09:56:49 PST 2004



Ok - how do you tell the parser to exit gracefully when it encounters EOF or
any other character???

-----Original Message-----
From: Sebastian Kaliszewski [mailto:sk at z.pl]
Sent: Thursday, December 09, 2004 9:24 AM
To: antlr-interest at yahoogroups.com
Subject: Re: [antlr-interest] Bug in Antlr - confusion on ANTLR grammar
syntax???



John MccLain wrote:
> I am not understanding this at all. I am building a grammar
> to parse powerbuilder. Here is all of the parser grammar - I reduced it
down
> to localize
> the confusion
>
> expression: simpleexp ((LE|GE|NE|EQ|LT|GT) simpleexp )?
> ;
>
> simpleexp:term
> ;
>
> term: factor
> ;
>
> factor: NUM
> ;
> and the lexer grammar (k = 4) for NUM is:
>
> NUM: ('0'..'9')+
> {System.out.println("2" + getText());}
> ;
>
> What I dont understand is the ANTLR grammar syntax (I think).
> Given an input of "1" (without the quotes), The rule 'simpleexp
> ((LE|GE|NE|EQ|LT|GT) simpleexp )?'
> causes the parser to process then hang inside the parser, and not exit.
> However, when I change the rule to
> just 'simpleexp', the parser does not hang, and I exit gracefully out of
> main. The parser start rule is 'expression'.

I suspect id dos not hang it simply just waits for more input probably.
Does entering one more symbol (or sending the eof) still yeild no
result? (I think it will return then).

> My question is - why does 'simpleexp ((LE|GE|NE|EQ|LT|GT) simpleexp )?'
not
> work? the parenthetic stmt is OPTIONAL - I wouldn't think it should cause
a
> different behavior than just 'simpleexp'.

Of course it should. Parser must check if input does not fit the
(LE|GE|NE|EQ|LT|GT) part before deciding whether to return or to
continue processing.


> If I use 'simpleexp | simpleexp
> ((LE|GE|NE|EQ|LT|GT) simpleexp )?', all is well,

Probalby you have just 1 token lookahead (if you didn't set the
lookahead in your parser the default is 1). Then the rule is ambigous
and Antlr just chooses the first one which terminates immediately.

> but I don't understand why
> I should have to do this???

Your start rule should have have EOF checking in it.
something like:

expression: simpleexp ((LE|GE|NE|EQ|LT|GT) simpleexp )? EOF


EOF checking is not implicit since many languages allow arbitrary
garbage afterthe program.

rgds
--
Sebastian Kaliszewski



Yahoo! Groups Links









 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 





More information about the antlr-interest mailing list