[antlr-interest] newbiew question on parsar grammar

John MccLain jmcclain at tcshealthcare.com
Fri Dec 10 08:44:04 PST 2004



Howdy,
Sebastian Kaliszewski replied to an earlier  post of mine (See original post
below) stating that the parser was waiting
for more input. He was correct - If I entered another NUM, the parser
processed it and continued to wait again. He also mentioned that my
startrule should have a check for EOF. I.E.:
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.

BUT, what is the rule for EOF, and will this cause the parser to stop busy
waiting for input? That is what I want to do. Right now I am using standard
input (through the console in eclipse), but ultimately, I will be passing an
expression or function
string into the parser for it to build a parse tree. How would I gracefully
exit the parser at the end of the input string ( or for now, standard in)


Here is my original post :


I am not understanding this at all. I am building a grammar
to parse powerbuilder functions. 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'.
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'. If I use 'simpleexp | simpleexp
((LE|GE|NE|EQ|LT|GT) simpleexp )?', all is well, but I don't understand why
I should have to do this???

John McClain
Senior Software Engineer
TCS Healthcare
jmcclain at tcshealthcare.com
(530)886-1700x235
"Before you criticize someone, walk a mile in their shoes.
That way, you'll be a mile from them, and you'll have their shoes."



 
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