Go target generates tables for parser and lexer using non-unique names
- Dominant language
- Java
- Stars
- 19k
- Forks
- 3.5k
- PR merge metrics
- No merged PRs in 30d
Description
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)](https://github.com/antlr/grammars-v4/tree/master/csharp), or [grammars-v4/cpp](https://github.com/antlr/grammars-v4/tree/master/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](https://github.com/kaby76/Domemtech.Trash/tree/main/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 https://github.com/antlr/grammars-v4/issues/2452#issuecomment-1003720415
Contributor guide
Research direction
Start by reproducing Go-target generation with two parser or lexer grammars in one directory, then trace the ANTLR tool's Go code-generation path. Verify the generated output with the Go compiler and confirm that multiple grammars can coexist while sharing a lexer without separate packages.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100