how do I handle conflicting function names between my antlr grammar and target language

3.3k Views Asked by At

I have a grammar that contains function names called eval and round, these are already functions in python and when I try to generate the listener using:

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

I get the following:

error(134): Grammar.g4:138:0: symbol round conflicts with generated code in target language or runtime error(134): Grammar.g4:174:0: symbol eval conflicts with generated code in target language or runtime error(134): Grammar.g4:62:3: symbol eval conflicts with generated code in target language or runtime error(134): Grammar.g4:134:3: symbol round conflicts with generated code in target language or runtime

I can't simply change eval/round to a different name, because I'm writing a clone of a different dls. Is it possible to create a namespace or work around this issue another way without changing my grammar language syntax?

1

There are 1 best solutions below

0
On BEST ANSWER

Something that might solve your problems would be to prefix the offending rules with something like r_.

Example:

Current:

 eval:  'eval' anotherRule ';' ;
 anotherRule  : '1';

Changed:

 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.

Note that 'eval' (the keyword your user enters in the dsl) is not changed!