具有不同解释的令牌(即关键字和标识符) [英] Token with different interpretations (i.e. keyword and identifier)
问题描述
我正在 ANTLR4 中编写包含许多不区分大小写关键字的语法.我收集了一些格式的示例文件,我尝试测试解析,有些使用相同的标记,这些标记作为关键字存在于其他地方作为标识符.例如,有一个 CORE
关键字,它在其他地方用作来自用户输入的结构的 ID
.这是我语法的一些部分:
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 ;
然后抛出的错误是 line 7982:8 mismatched input 'core' expecting IDSTRING
,因为用户输入的目的是 IDSTRING
,但总是被关键字规则吃掉.在输入中,它既作为关键字又作为 id 存在,如下所示:
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
有没有一种方法可以让用户通过改变我的语法来使用一些关键字作为标识符,比如将标记投射"到 IDSTRING
以获得这样的连接规则,或者这是一个错误的希望写解析器?
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?
推荐答案
您可以简单地列出允许作为标识符的关键字作为 id
规则中的替代项:
You can simply list the keywords that are allowed as identifiers as alternatives in the id
rule:
id: IDSTRING | CORE | ... ;
这篇关于具有不同解释的令牌(即关键字和标识符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!