Token with different interpretations (i.e. keyword and identifier)

十年热恋 提交于 2019-12-02 08:07:09

问题


I am writing a grammar with a lot of case-insensitive keywords in ANTLR4. I collected some example files for the format, that I try to test parse and some use the same tokens which exist as keywords as identifiers in other places. For example there is a CORE keyword, which in other places is used as a ID for a structure from user input. Here some parts of my grammar:

fragment A : [aA]; // match either an 'a' or 'A'
fragment B : [bB];
fragment C : [cC];
[...]
CORE:        C O R E ;
[...]
IDSTRING:    [a-zA-Z_] [a-zA-Z0-9_]*;
id:          IDSTRING ;

The error thrown then is line 7982:8 mismatched input 'core' expecting IDSTRING, as the user input is intended as IDSTRING, but always eaten by the keyword rule. In the input it exists both as keyword and as id like this:

MACRO oa12f01
    CLASS CORE ; #here it is a KEYWORD
[...]
SITE core ; #here it is a ID

Is there a way I can let users use some keywords as identifiers by changing my grammar somehow like "casting" the token to IDSTRING for conjunctive rules like this or is this a false hope in not hand written parsers?


回答1:


You can simply list the keywords that are allowed as identifiers as alternatives in the id rule:

id: IDSTRING | CORE | ... ;


来源:https://stackoverflow.com/questions/50801266/token-with-different-interpretations-i-e-keyword-and-identifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!