You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a problem with the Go target (commonly referred to as "Golang" in order to appease web search engines) with grammars such as grammars-v4/csharp/ (the C# language), or grammars-v4/cpp, where there are two or more parser grammars, or two or more lexer grammars within one directory, i.e., for preprocessing.
When there are two grammars, e.g., CSharpParser.g4 and CSharpPreprocessor.g4, in one directory, one can easily generate the .cs or .java files using the Antlr tool and compile with a driver program. (This is exactly what trgen does so you do not have to remember how to call the Antlr tool .jar, nor waste time relearning how to write a driver program for your target language.) The parser code compiles because the generated code is enclosed in a class, which has a unique name for each grammar name.
For the Go target, the tables parserATN, literalNames, symbolicNames, ruleNames for each parser are in a global scope. When generated for two parsers, and compiled, the Go compiler fails with the error that you are trying to define these tables more than once.
To work around this problem, I can call the Antlr tool with -package on the command line so the globals don't conflict. But, each package must be placed in a different directory. Go restricts sources with a package name to have one package name in one directory, and that the package name equal the name of the enclosing directory.
In addition, the workaround creates a problem when trying to share a lexer grammar across two or more parsers because you cannot "import '../lexerfoobar'" (i.e., you cannot use ".." in an import declaration), whereas for all other targets, it's possible to share.
It would make my life far easier if the Antlr tool would just generate a unique name for the table (e.g., append the name of the grammar) and offer getters. All the other targets (CSharp, Java, Dart, JavaScript, Python3) have classes supported by the language, so it's unique only to the Go target.
The text was updated successfully, but these errors were encountered:
kaby76
changed the title
Go target generates non-unique tables for parser and lexer
Go target generates tables for parser and lexer using non-unique names
Jan 2, 2022
This is a problem with the Go target (commonly referred to as "Golang" in order to appease web search engines) with grammars such as grammars-v4/csharp/ (the C# language), or grammars-v4/cpp, where there are two or more parser grammars, or two or more lexer grammars within one directory, i.e., for preprocessing.
When there are two grammars, e.g., CSharpParser.g4 and CSharpPreprocessor.g4, in one directory, one can easily generate the .cs or .java files using the Antlr tool and compile with a driver program. (This is exactly what trgen does so you do not have to remember how to call the Antlr tool .jar, nor waste time relearning how to write a driver program for your target language.) The parser code compiles because the generated code is enclosed in a class, which has a unique name for each grammar name.
For the Go target, the tables parserATN, literalNames, symbolicNames, ruleNames for each parser are in a global scope. When generated for two parsers, and compiled, the Go compiler fails with the error that you are trying to define these tables more than once.
To work around this problem, I can call the Antlr tool with
-package
on the command line so the globals don't conflict. But, each package must be placed in a different directory. Go restricts sources with a package name to have one package name in one directory, and that the package name equal the name of the enclosing directory.In addition, the workaround creates a problem when trying to share a lexer grammar across two or more parsers because you cannot "import '../lexerfoobar'" (i.e., you cannot use ".." in an import declaration), whereas for all other targets, it's possible to share.
It would make my life far easier if the Antlr tool would just generate a unique name for the table (e.g., append the name of the grammar) and offer getters. All the other targets (CSharp, Java, Dart, JavaScript, Python3) have classes supported by the language, so it's unique only to the Go target.
For additional details, see antlr/grammars-v4#2452 (comment)
The text was updated successfully, but these errors were encountered: