如何处理我的antlr语法和目标语言之间冲突的函数名称

whi*_*eam 4 python grammar parsing name-conflict antlr4

我有一个包含eval和round函数名的语法,这些函数在python中已经是函数,当我尝试使用以下函数生成监听器:

antlr4 -listener -lib/src/grammar -Dlanguage = Python3 -o/gen -no-visitor /src/grammar/Grammar.g4

我得到以下内容:

error(134):Grammar.g4:138:0:符号与目标语言或运行时错误中生成的代码冲突(134):Grammar.g4:174:0:符号eval与目标语言中生成的代码或运行时错误冲突( 134):Grammar.g4:62:3:符号eval与目标语言或运行时错误中生成的代码冲突(134):Grammar.g4:134:3:符号与目标语言或运行时生成的代码发生冲突

我不能简单地将eval/round更改为其他名称,因为我正在编写不同dls的克隆.是否可以创建命名空间或以其他方式解决此问题而不更改我的语法语言语法?

Onu*_*nur 9

可能解决您的问题的方法是在违规规则前加上类似的东西r_.

例:

当前:

 eval:  'eval' anotherRule ';' ;
 anotherRule  : '1';
Run Code Online (Sandbox Code Playgroud)

更改:

 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.
Run Code Online (Sandbox Code Playgroud)

请注意,'eval'(用户在dsl中输入的关键字)不会更改!