Decision can match input such as "MULOP LETTER" using multiple alternatives: 1, 2

67 views Asked by At

I'm getting this error [22:52:55] warning(200): ProjLang.g:53:30: Decision can match input such as "MULOP LETTER" using multiple alternatives: 1, 2

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

It seems like there could be some ambiguity in my grammar from my googling. I don't know how I can remove the ambiguity.

fragment LETTER 
    :   ('a'..'z') | ('A'..'Z');
fragment DIGIT  
    :   ('0'..'9');
ADDOP   :   ('+'|'-'|'|')
    ;
MULOP   :   ('*'|'/'|'&')
    ;

RELOP   :   ('<'|'=')
    ;
LPAR    :   '(';
RPAR    :   ')';
BOOL    :    'true'|'false';
LCURL   :   '{';
RCURL   :   '}';

// parser rules: non terminal states should be lowercase

input   :    expr EOF
    ;

expr    :   'if' expr 'then' expr 'else' expr
    |   'let'( 'val' id RELOP expr 'in' expr 'end'|'fun' id LPAR id RPAR RELOP expr 'in' expr 'end')    
    |   'while' expr 'do' expr
    |   LCURL expr (';'expr)* RCURL
    |   '!'expr
    |   id ':=' expr
    |   relexpr
    ;

relexpr 
    :    arithexpr (RELOP arithexpr)?
    ;

arithexpr 
    :    term (MULOP term)*
    ;

term    :    factor (MULOP factor)*
    ;

factor  :    num
    |    BOOL
    |    id (LPAR expr RPAR)?
    |    LPAR expr RPAR
    ;

id  :   LETTER (LETTER | DIGIT)*;
num :   DIGIT+;

I expect to write a grammar without error message so I can generate a lexer and a parser for it.

1

There are 1 answers

0
Mike Lischke On

These rules do essentially the same:

arithexpr 
    :    term (MULOP term)*
    ;

term    :    factor (MULOP factor)*
    ;

If you combine them you will get:

arithexpr: factor (MULOP factor)* (MULOP factor (MULOP factor)*)*

which contains an ambiquity (which of the two MULOP tokens should be matched after the initial factor?). But from the rewrite it's easy to see what to do:

arithexpr: factor (MULOP factor)*;

which replaces the original term and arithexpr rules.