[antlr-interest] Re: Problem with x: (A)(B)? ;
Terence Parr
parrt at cs.usfca.edu
Fri Feb 27 12:34:12 PST 2004
On Feb 25, 2004, at 10:26 AM, dotlessbraille wrote:
> I really appreciate your trying to help but request you to please
> look at the generated Java. I understand that ANTLR will put in a
> test for an EOF if I don't put one in. However, this test is at the
> highest level of alternatives.
>
> The problem I am encountering is that ANTLR interprets the (B)? as
> requiring either a B or an EOF directly after the A. In other
> words, it won't handle the case where there are two A's. It works
> fine if I use x:A(B)* which isn't what I want, however.
>
> startRule :
> (x:A(B)?
> |b:B
> |eof:EOF
> );
Let me take a look at this. Hmm...i don't see how that will match two
A's. It matches A, AB, B, EOF. You're use of x: confusing me. Is
this a rule or a label as you have here? Assuming it's a label. Ok,
let me rephrase your question to see if I understand:
start : (A (B)* | B)? ;
works but
start : (A (B)? | B)? ;
does not. This is as designed. In the loop case, I avoid a lookahead
test for the exit branch each time by simply saying "while lookahead
consistent with stuff inside; B in this case". For the (B)? optional
case, i test the lookahead for what follows the (B)? subrule which is
EOF.
You cannot escape that EOF follows your start symbol, I'm afraid. Can
u explain why you don't want EOF to match? Anything non-B should fail
to enter the (B)? subrule, right?
Ter
--
Professor Comp. Sci., University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com
Cofounder, http://www.knowspam.net enjoy email again!
Cofounder, http://www.peerscope.com pure link sharing
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