如何处理我的ANTLR语法与目标语言之间的冲突函数名
我有一个语法规则,其中包含了叫做 eval 和 round 的函数名,这两个名字在 Python 中已经是现成的函数了。当我尝试用以下命令生成监听器时:
antlr4 -listener -lib /src/grammar -Dlanguage=Python3 -o /gen -no-visitor /src/grammar/Grammar.g4
我遇到了以下错误:
错误(134):Grammar.g4:138:0:符号 round 与目标语言或运行时生成的代码冲突;错误(134):Grammar.g4:174:0:符号 eval 与目标语言或运行时生成的代码冲突;错误(134):Grammar.g4:62:3:符号 eval 与目标语言或运行时生成的代码冲突;错误(134):Grammar.g4:134:3:符号 round 与目标语言或运行时生成的代码冲突。
我不能简单地把 eval 和 round 改成其他名字,因为我是在写一个不同 DSL 的克隆。请问有没有办法创建一个命名空间,或者用其他方法解决这个问题,而不改变我的语法规则?
1 个回答
11
一个可能解决你问题的方法是,在那些有问题的规则前面加上类似 r_
的前缀。
举个例子:
现在的样子:
eval: 'eval' anotherRule ';' ;
anotherRule : '1';
改成:
r_eval: 'eval' anotherRule ';' ;// change the rule name since eval is a reserved identifier in Python
anotherRule : '1'; // you don't have to change this rule, since "anotherRule" is most likely not reserved.
注意,'eval'(用户在dsl中输入的关键词)是 没有 改变的!