[antlr-interest] Problem with grammar

Hiten R antlr3 at gmail.com
Sun Mar 20 16:19:24 PDT 2011


Nicely put. Thanks for helping me understand.

Hiten

On Sat, Mar 19, 2011 at 9:50 AM, Kevin J. Cummings <
cummings at kjchome.homeip.net> wrote:

> On 03/19/2011 08:18 AM, Wojciech Tomasz Cichon wrote:
> > i’m struggling with this grammar for a few days now, and nothing really
> working,
> > so i started from beginning and i have :
> > grammar myGrammar;
> >
> > options {
> >   language = Java;
> >   k=1;
> > }
> >
> > rule: term ;
> >
> > factor  :  '-'? NUMBER
> >        ;
> >
> > term  : factor '*'  term
> >       | factor '/'  term
> >       | factor '%'  term
> >       | factor;
>
> This is the classic case where the following is probably the proper left
> factored LL(1) for you:
>
> term : factor ( ( '*' | '/' | '%' ) factor )*
>     ;
>
> Now you have a single "factor" that will always match at the beginning
> of your term rule, and not have to chose which of the 4 choices to
> associate a '-' with without changing to k=2 or k=3, or turning on
> backtracking.
>
> > NUMBER :  '0'..'9'+
> >     ;
> > WS  :   ( ' '
> >         | '\t'
> >         | '\r'
> >         | '\n'
> >         ) {$channel=HIDDEN;}
> >     ;
> >
> >
> > and i got errors:
> > warning(200): /ANTLR_TEST/myGrammar.g:13:7: Decision can match input such
> as "'-'" using multiple alternatives: 1, 2, 3, 4
> > As a result, alternative(s) 2,3,4 were disabled for that input
> > |---> term  : factor '*'  term
>
> It can't tell which of your 4 term alternatives to choose based on a '-'
> look-ahead character....
>
> > warning(200): /ANTLR_TEST/myGrammar.g:13:7: Decision can match input such
> as "NUMBER" using multiple alternatives: 1, 2, 3, 4
> > As a result, alternative(s) 2,3,4 were disabled for that input
> > |---> term  : factor '*'  term
>
> The same here, which of the 4 term alternatives does it choose when the
> look-ahead character is a NUMBER?
>
> > error(201): /ANTLR_TEST/myGrammar.g:13:7: The following alternatives can
> never be matched: 2,3,4
> > |---> term  : factor '*'  term
>
> Because it "chose" the first alternative for the 2 cases above, it will
> never try a division, modulus, or simple factor.
>
> > can anyone tell me what i’m doing wrong
>
> left factor your grammar....
>
> > regards
>
> --
> Kevin J. Cummings
> kjchome at verizon.net
> cummings at kjchome.homeip.net
> cummings at kjc386.framingham.ma.us
> Registered Linux User #1232 (http://counter.li.org)
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list