Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go target generates tables for parser and lexer using non-unique names #3452

Open
kaby76 opened this issue Jan 2, 2022 · 0 comments
Open

Comments

@kaby76
Copy link
Contributor

kaby76 commented 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)

@kaby76 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant