[antlr-interest] Extra right parenthesis in "An Introduction toANTLR" not failing - why?

Jeff Steward jeffsteward at hotmail.com
Tue Jun 28 10:16:41 PDT 2005


Brian,

Thanks so much!  That's the piece of the puzzle I was missing!  Everything 
is much more clear now.  :-)

Gratefully yours,

Jeff

>From: Bryan Ewbank <ewbank at gmail.com>
>Reply-To: Bryan Ewbank <ewbank at gmail.com>
>To: ANTLR Interest <antlr-interest at antlr.org>
>Subject: Re: [antlr-interest] Extra right parenthesis in "An Introduction 
>toANTLR" not failing - why?
>Date: Tue, 28 Jun 2005 13:00:45 -0400
>
>Hi Jeff,
>
>What you are seeing is exactly correct, once you realize what's going on...
>
>For the input "3+3)", only the "3+3" is matched by the grammar, then
>ANTLR's generated parser terminates because the ")" cannot be
>recognized in the context of the grammar.
>
>The grammar defined for this parser does not require that it consume
>the entire token stream, so it does not - it simply quits when it sees
>something it cannot recognize.
>
>In the same way, if I have the string "abcabcdef", the regular
>expression "(abc)*" will not fail - it will only match the first six
>characters.
>
>To force the parser to consume the entire token stream, you must add a
>top-level production something like this:
>
>     program : expr EOF! ;
>
>Using this production as your entry point will force the parser to
>report an error as you expect, because by adding the "EOF" at the end
>of the you have told ANTLR "match an expr, AND NOTHING ELSE".
>
>Said another way, ANTLR's parser will expect EOF but see ")", which is
>clearly a parse error.
>
>Hope this helps,
>- Bryan Ewbank
>
>Jeff said:
> > It appears the generated ExprParser will give an incorrect parse of
> > the following (that is, it seems to me that the following should fail):
> >
> > 3+3)
> >
> > In addition, something such as 3+3)+2 will not only parse without error,
> > it will evaluate as 3+3 - behavior that would be highly undesirable for
> > my user-defined input.
> >
> > Therefore, what I would really like to do is check for exact matching of
> > parenthesis.  Is there a simple way to do this with ANTLR grammar?




More information about the antlr-interest mailing list