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

[various grammars] Malformed EOF-terminated start rules. #4317

Open
kaby76 opened this issue Nov 9, 2024 · 0 comments
Open

[various grammars] Malformed EOF-terminated start rules. #4317

kaby76 opened this issue Nov 9, 2024 · 0 comments

Comments

@kaby76
Copy link
Contributor

kaby76 commented Nov 9, 2024

The start rule for java/java/ was incorrect because it had an EOF on only one of the alts of the start rule. This can cause two issues: parse errors may not displayed; parse trees may be truncated.

For example, take this Arithmetic.g4 grammar with EOF on only one alt. Then, running the parser on the input "1 2", we don't get any parse errors, and we get a truncated tree.

11/09-04:43:03 ~/temp2/Generated-CSharp
$ cat Arithmetic.g4  | head -11
// Template generated code from trgen 0.23.9

// $antlr-format alignTrailingComments true, columnLimit 150, minEmptyLines 1, maxEmptyLinesToKeep 1, reflowComments false, useTab false
// $antlr-format allowShortRulesOnASingleLine false, allowShortBlocksOnASingleLine true, alignSemicolons hanging, alignColons hanging
grammar Arithmetic;

file_
    : expression (SEMI expression)*
    | EOF
    ;
11/09-04:43:05 ~/temp2/Generated-CSharp
$ ./bin/Debug/net8.0/Test.exe -tree -input "1 2; 3"
(file_ (expression (atom (scientific 1))))
CSharp 0 string0 success 0.0215697
Total Time: 0.0690269
11/09-04:43:27 ~/temp2/Generated-CSharp
$

It's unclear of the intent is. But, it can lead to a lot of problems.

This is changed in #4310 for java/java. It is probably the right thing because a "compilationUnit" should read to the end of the file and report all errors.

Other grammars have the same issue.

As far as I can tell, an EOF in only one alt is equivalent to the empty string.

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