-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Java 20 bad Parser #4331
Comments
Please post the entire input (or inputs) as text, or attach the input in a .txt file (or files). It's best to not post pictures. Thanks. |
Input is only 898 lines, takes ~90s to parse, result is success. Yes, this is terrible performance. But, unfortunately expected. This grammar is a direct implementation of the Java Language Spec 20 grammar in Chapter 19. It is very ambiguous. Here is the ambiguity uncovered for the input. The tools used are part of the Trash Toolkit.
Output:
The good news, if you say anything good about this, is that the ratio of the number of ambiguities to the number of fallbacks is more or less one-to-one. This means that most of the problem is with ambiguity and not DFA transition conflicts. DFA transition conflicts are generally harder to fix.
Decision state 315 is the problem. Decision 315, the first state after entry, has a choice among a half dozen alts. (I can't tell which alts because |
So this is a problem of grammar? |
Yes. It's a grammar problem, not an "Antlr problem". Many of the grammars in this repo can be slow because of ambiguity. This usually happens because someone derives the grammar from another grammar, then tries to use that with Antlr. Much of the time, the grammar requires a symbol table to disambiguate. For better or worse, Antlr will accept an ambiguous grammar, and generate a parser for it. But, just as one can write atrocious code in Java, C#, JavaScript, etc., one can do with Antlr. People then say "Antlr is terrible," but it's usually the grammar that is the problem. The solution is to eliminate ambiguity in the grammar. |
Is there a solution for treatment? |
Yes, the grammar should be fixed. I am working my way through the grammars and cleaning up ambiguity and fallbacks. I can address this grammar after postgresql, mysql/Oracle, then java/java20, likely a couple of weeks from now. |
oh tanks sir🥰 |
@kaby76 I have a question in my mind, is it possible to make a code formatter with lexer and parser? |
Yes, it can. I ported Codebuff to C# but there is the original Java version. See https://scholar.google.com/scholar?hl=en&as_sdt=0%2C22&q=codebuff&btnG= Also check out https://github.com/antlr/codebuff Basically you need to provide a sampling of files that are formatted by hand, the grammar, and some input you want to format, and it outputs the formatted input file. |
@kaby76 I saw that this source is very old and strangely, I couldn't understand it. Is there a simpler source that can provide lexer and parser to easily format my own Java codes? |
There is nothing newer written in Java. I ported it to C# but that was several years ago. The papers on Codebuff are a good intro to the code. |
Can you port a normal version for me because there are a lot of Java files and most of them are for Swing, I tried to port it on Android but it didn't work, if you can help me to port it on Android, your experience is much higher😅🌹 |
As you can see in the photo, the Java 20 parser cannot analyze well
my code
The text was updated successfully, but these errors were encountered: