4

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?

whisperstream
  • 1,897
  • 3
  • 20
  • 25
  • If you want to write a clone of an existing dsl, then why do you need to put restrictions on the _rule_ names (not the keywords in your dsl)? Rule names are only important in your post processing steps, not for any client code. – Onur Aug 23 '14 at 12:14
  • Maybe also post your grammar. At least the lines that cause the error (i.e. something around line 138 for instance). – Onur Aug 23 '14 at 12:15

1 Answers1

11

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!

Onur
  • 5,017
  • 5
  • 38
  • 54