简体   繁体   中英

ANTLR mismatched input 'NUM' expecting {… , 'NUM'}

I have a problem with ANTLR4 when trying to parse a file. It worked in the first place, but after adding the following to my grammar i get an error (see below):

TYPE_NUM: 'NUM';

mismatched input 'NUM' expecting {'LOAD', '\n', 'HEAR', 'NUM', 'STRING', 'COORD', 'BOOL', 'VOID', 'LIST'}

The 'NUM' keyword in my language is in the following rule:

typePrefix: type=('NUM' | 'BOOL' | 'STRING' | 'COORD' | 'LIST');

I also have the same problems with all the rest of my typePrefixes, but I'm guessing it's the same solution for all of them.

I have tried to replace all the options of the typePrefix rule with TYPE_NUM, TYPE_BOOL and so forth, but that did not seem to work.

EDIT: By request in the comments I've posted the part of my grammar, where I use 'NUM':

prog
    :   roboDcl loads roboBodyDcl;
loads
    :   recursion=loads 'LOAD' '(' load_id=StringLit ')' '\n' 
    |   //lambda
    ;
memberDcl
    :   dcl=fieldDcl
    |   met_dcl=methodDcl
    |   '\n'
    ;
roboDcl
    :   id=Identifier':''\n'
    ;
roboBodyDcl
    :   recursion=roboBodyDcl dcl=memberDcl
    |   dcl=memberDcl   
    ;
fieldDcl
    :   t=typePrefix dcl_list=variableDclList '\n';
typePrefix
    :   type=('NUM' | 'BOOL' | 'STRING' | 'COORD' | 'LIST');
variableDclList
    :   single=variableDcl
    |   list=variableDclList ',' single=variableDcl
    ;
variableDcl
    :   var_init=variableInitializer
    |   id=Identifier '=' list_init=listInitializer
    ;
variableInitializer 
    :   expr=assignmentExpression
    ;
TYPE_NUM: 'NUM';
TYPE_STRING: 'STRING';
TYPE_COORD: 'COORD';
TYPE_BOOL: 'BOOL';
TYPE_VOID: 'VOID';
TYPE_LIST: 'LIST';

And as said before I have tried to replace the typePrefix rule with the following:

typePrefix
    :    type=(TYPE_NUM | TYPE_BOOL | TYPE_STRING | TYPE_COORD | TYPE_LIST);

I hope this will suffice and thanks in advance!

I figured out the problem and will try to provide an explanation in-case someone ends up in the same situation.

As stated "The Definitive Antlr 4 Reference" ANTLR resolves ambiguities in the lexer rules by choosing the rule defined first. And states "That means your ID rule should be defined after all of your keyword rules..."

In my case I had defined my grammar in the following manner:

fragment NameStartChar
    :       'A'..'Z' | 'a'..'z';
fragment NameChar
    :       NameStartChar
    |       Num
    |       '_'
    ;
fragment Num
    :       '0'..'9';
Identifier
    :   NameStartChar NameChar*;
TYPE_NUM: 'NUM';

This ended up matching all of my keywords 'NUM', 'BOOL' and so forth.

The solution was therefore to define the Identifier rule as the last of the lexer rules. An example can be seen below:

fragment NameStartChar
    :       'A'..'Z' | 'a'..'z';
fragment NameChar
    :       NameStartChar
    |       Num
    |       '_'
    ;
fragment Num
    :       '0'..'9';
TYPE_NUM: 'NUM';
//All other keyword definitions ('BOOL', 'STRING' and so alike)
Identifier
    :   NameStartChar NameChar*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM