ANTLR3 - Decision can match input using multiple alternatives

121 views
Skip to first unread message

J. Jin

unread,
Jun 22, 2017, 4:12:24 PM6/22/17
to antlr-di...@googlegroups.com
When running ANTLR3 on the following code, I get the message - warning(200): MYGRAMMAR.g:40:36: Decision can match input such as "QMARK" using multiple alternatives: 3, 4
As a result, alternative(s) 4 were disabled for that input.

The warning message is pointing me to postfixExpr. Is there a way to fix this?




grammar MYGRAMMAR;
options {language = C;}

 tokens {
  BANG        = '!';
  COLON       = ':';
  FALSE_LITERAL = 'false';
  GREATER     = '>';
  LSHIFT      = '<<';
  MINUS       = '-';
  MINUS_MINUS = '--';
  PLUS        = '+';
  PLUS_PLUS   = '++';
  QMARK       = '?';
  QMARK_COLON = '?:';
  TILDE       = '~';
  TRUE_LITERAL = 'true';
}


condExpr
                    :  shiftExpr  (QMARK  condExpr COLON  condExpr)? ;

shiftExpr
                    :  addExpr  ( shiftOp   addExpr)* ;

addExpr
                    :  qmarkColonExpr  ( addOp  qmarkColonExpr)* ;

qmarkColonExpr
                    :  prefixExpr  ( QMARK_COLON  prefixExpr )? ;

prefixExpr
                    :  ( prefixOrUnaryMinus |  postfixExpr) ;

prefixOrUnaryMinus
                    :  prefixOp  prefixExpr ;

postfixExpr
                    :  primaryExpr ( postfixOp | BANG | QMARK )*;

primaryExpr
                    :   literal ;



shiftOp
                    :  ( LSHIFT  | rShift);

addOp
                    :  (PLUS  | MINUS);

prefixOp
                    :  ( BANG  | MINUS  | TILDE  | PLUS_PLUS  | MINUS_MINUS );

postfixOp
                    :  (PLUS_PLUS  | MINUS_MINUS);

rShift
                    :  (GREATER GREATER)=> a=GREATER b=GREATER {assertNoSpace($a,$b)}? ;

literal
                    :  ( TRUE_LITERAL  | FALSE_LITERAL );

assertNoSpace       [pANTLR3_COMMON_TOKEN t1, pANTLR3_COMMON_TOKEN t2]
                    : { $t1->line == $t2->line && $t1->getCharPositionInLine($t1) + 1 == $t2->getCharPositionInLine($t2) }? ;

John B. Brodie

unread,
Jun 22, 2017, 4:40:46 PM6/22/17
to antlr-di...@googlegroups.com, J. Jin

Greetings!

instead of:

prefixExpr
                    :  ( prefixOrUnaryMinus |  postfixExpr) ;

prefixOrUnaryMinus
                    :  prefixOp  prefixExpr ;


use:

prefixExpr : prefixOp* postfixExpr ;



untested.

hope this helps...

   -jbb


On 06/22/2017 04:12 PM, J. Jin wrote:
When running ANTLR3 on the following simplified code, I get the message - warning(200): MYGRAMMAR.g:40:36: Decision can match input such as "QMARK" using multiple alternatives: 3, 4
--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

J. Jin

unread,
Jun 23, 2017, 10:25:31 AM6/23/17
to antlr-discussion
Thanks for the reply!

I made the changes but the warning message was still there - warning(200): MYGRAMMAR.g:40:63: Decision can match input such as "QMARK MINUS {BANG, MINUS..MINUS_MINUS, PLUS_PLUS, TILDE} {FALSE_LITERAL, TRUE_LITERAL}" using multiple alternatives: 3, 4

As a result, alternative(s) 4 were disabled for that input


Best,

Jaimie
Reply all
Reply to author
Forward
0 new messages