[antlr-interest] Problem with grammar

Jim Idle jimi at temporal-wave.com
Sat Mar 19 09:41:50 PDT 2011


You need to construct the correct LL tree form of the rules. Always copy
from a pre-written grammar in the examples or read the book and you will not
struggle so much:

factor
  : addexp (('+'|'-')* addexp
  ;

addexp
   : mulexp (('*'|'/') mulexp)*
   ;

mulexp
   : '-' mulexp
   | atom
   ;

atom
   : NUMBER
   | '(' factor ')'
   ;

Jim


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Wojciech Tomasz Cichon
> Sent: Saturday, March 19, 2011 5:19 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Problem with grammar
>
> 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;
>
>
>
> 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
>
> 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
>
> error(201): /ANTLR_TEST/myGrammar.g:13:7: The following alternatives
> can never be matched: 2,3,4
> |---> term  : factor '*'  term
>
> can anyone tell me what i’m doing wrong
> regards
>
> 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