ANTLR - No viable alternative at input

718 views Asked by At

I am getting the error: no viable alternative at input 'mult' when I attempt to parse the input: div(mean(mult(field_a, field_b)), sum(field_b)).

Here's my grammar:

grammar Analytics;

start: expr | stat;

expr
    : UNARY_EXPR '(' (stat | expr | NUMBER ) ')'                                        #unaryExpr
    | BINARY_EXPR '(' (stat | expr | constant) ',' (stat | expr | constant) ')'         #binaryExpr
    | MULTIPLE_EXPR '(' (stat | expr | constant) ',' (stat | expr | constant)+ ')'      #multipleExpr
    ;       

stat
    : UNARY_STAT  '(' (operation | field) ')'                                           #unaryStat
    | BINARY_STAT '(' (operation | field) ',' (operation | field) ')'                   #binaryStat
    ;

operation
    : UNARY_OPERATION '(' operation ')'                                                 #unaryOperation
    | BINARY_OPERATION '(' operation ',' operation ')'                                  #binaryOperation
    | MULTIPLE_OPERATION '(' operation ',' operation+ ')'                               #multipleOperation
    | field                                                                             #fieldOperation
    ;

constant: NUMBER;

field: IDENTIFIER;

UNARY_EXPR: 'neg' | 'const_num' | 'rev' | 'miss' | 'const_date' | 'const_str';
BINARY_EXPR: 'div' | 'pow' | 'log'; 
MULTIPLE_EXPR: 'add' | 'mult' | 'date_math' | 'concat'; 

UNARY_STAT: 'count' | 'missing' | 'min' | 'max' | 'stddev' | 'sum' | 'sumofsquares' | 'mean' | 'unique' | 'median' |
    'const_num' | 'neg' | 'abs';
BINARY_STAT: 'add' | 'mult' | 'div' | 'pow' | 'log';

UNARY_OPERATION: 'neg' | 'rev' | 'const_date' | 'const_str';
BINARY_OPERATION: 'div' | 'pow' | 'log' | 'miss'; 
MULTIPLE_OPERATION: 'add' | 'mult' | 'date_math' | 'concat'; 

OPEN_BRACKET: '(';
CLOSE_BRACKET: ')';
COMMA: ',';

NUMBER: ('0'..'9')+ ('.' ('0'..'9')+)? ;
IDENTIFIER : [a-zA-Z][a-zA-Z0-9_]*;

WS : (' ' | '\t')+ -> skip;

What am I doing wrong here that is causing it to not match the mult? Is there a suggested approach I could use to debug this error?

Also, how could I check which alternatives are matched by each rule? For example, the toStringTree() output i get is: (start (expr div ( (stat mean ( mult ( field) , field ) ) , sum ( field ) ))). How could I tell that the first expr rule is matched with BINARY_EXPR '(' (stat | expr | constant) ',' (stat | expr | constant) ')' rather than UNARY_EXPR '(' (stat | expr | NUMBER ) ')' ?

1

There are 1 answers

0
Bart Kiers On BEST ANSWER

Whenever one or more characters can be matched by more than 1 lexer rule, the rule defined first will "win".

Since the input mult can be matched by these rules:

MULTIPLE_EXPR: 'add' | 'mult' | 'date_math' | 'concat'; 
BINARY_STAT: 'add' | 'mult' | 'div' | 'pow' | 'log';
MULTIPLE_OPERATION: 'add' | 'mult' | 'date_math' | 'concat'; 

the first rule, MULTIPLE_EXPR, will always be chosen for the input mult.

You will have to do something like this instead:

multiple_expr      : ADD | MULT | ... ; 
binary_stat        : ADD | MULT | ... ;
multiple_operation : ADD | MULT | ... ; 

ADD  : 'add';
MULT : 'mult';
...