antlrantlr4antlrworksantlr4cs

How to make antlr find invalid input throw exception


I have the following grammar:

grammar Expr;


expr : '-' expr                                       # unaryOpExpr
     | expr ('*'|'/'|'%') expr                        # mulDivModuloExpr
     | expr ('+'|'-') expr                            # addSubExpr
     | '(' expr ')'                                   # nestedExpr
     | IDENTIFIER '(' fnArgs? ')'                     # functionExpr
     | IDENTIFIER                                     # identifierExpr
     | DOUBLE                                         # doubleExpr
     | LONG                                           # longExpr
     | STRING                                         # string
     ;


fnArgs : expr (',' expr)*                             # functionArgs
       ;

IDENTIFIER : [_$a-zA-Z][_$a-zA-Z0-9]* | '"' (ESC | ~ ["\\])* '"';
LONG : [0-9]+;
DOUBLE : [0-9]+ '.' [0-9]*;
WS : [ \t\r\n]+ -> skip ;

STRING: '"' (~["\\\r\n] | ESC)* '"';

fragment ESC : '\\' (['"\\/bfnrt] | UNICODE) ;
fragment UNICODE : 'u' HEX HEX HEX HEX ;
fragment HEX : [0-9a-fA-F] ;

MINUS : '-' ;
MUL : '*' ;
DIV : '/' ;
MODULO : '%' ;
PLUS : '+' ;

// math function
MAX: 'MAX';

when I enter following text,It should be effective

-1.1

bug when i enter following text:

-1.1ffff

I think it should report an error, bug antlr didn't do it, antlr captures the previous "-1.1", discard "ffff", but i want to change this behavior, didn't discard invalid token, but throw exception,report detection invalid token.

So what should i do, Thanks for your advice


Solution

  • Are you using expr as your main rule? if so make another rule, call it something like parse or program and simply write it like this:

    parse: expr EOF;
    

    This will make antlr not ignore trailing tokens that don't make sense, and actually throw an error.