question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parsing Java with JavaScript parser fails while CSharp/Java parser succeeds

See original GitHub issue

I have been parsing some Java code using grammars-v4/java/java/*.g4, and I noticed that for the following input, the JavaScript parser fails but the CSharp and Java parsers succeed.

public class HelloWorld { 
   public static void main(String[] args) { 
      System.out.println("Hello, World" + "hi There");
   }
}

Note, in all parsers I generate for the grammar, I used the default listeners.

Debugging the JavaScript parser side-by-side with the CSharp parser, I’ve determined that the JS parser fails to parse the expression “Hello, World” + “hi There” at the ‘+’ operator.

Looking over the generated parsers JavaParser.cs and JavaParser.js, I have noticed that the likely culprit is the generated code:

CSharp:

if ( ParseListeners!=null )
    TriggerExitRuleEvent();

JavaScript:

if(this._parseListeners!==null) {
    this.triggerExitRuleEvent();
}

These codes are not the same, and they execute differently.

In CSharp, ParseListeners in the snippet is a get property call in the Parser class that returns an empty list if the backing field is null. In this case, it returns an empty list, which is for sure not equal to null, and this.triggerExitRuleEvent() is called.

In JavaScript, this._parseListeners is a reference to the backing field itself, not the getter getParseListeners(). In JavaScript, triggerExitRuleEvent() is never called.

Also, just scanning through the generated parsers, I’ve also noticed constants ~0x3f used in CSharp vs ~0x1f used in JavaScript. While it’s likely not important in this example, it is rather irritating to see magic number differences between the runtimes.

–Ken

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:24 (24 by maintainers)

github_iconTop GitHub Comments

1reaction
ericvergnaudcommented, Mar 18, 2021

@kaby76 Thanks for the tough work of narrowing down the scenario.

0reactions
kaby76commented, Mar 18, 2021

I always loved doing this stuff. The only good way to learn code. This seems like a nasty bug though. Hoping to see 4.9.3 in a few months. No hurries, but I’d expect others might be a b*tching.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ANTLR parsing works when using TestRig, but fails when ...
Update: I'm now using the Char stream directly from file, but the error still remains. private static void parseFile(String fileName) ...
Read more >
Parsing in Java: all the tools and libraries you can use
We present and compare all possible alternatives you can use to parse languages in Java. From libraries to parser generators, we present all...
Read more >
Parsing Expressions - Crafting Interpreters
When parsing, ambiguity means the parser may misunderstand the user's code. As we parse, we aren't just determining if the string is valid...
Read more >
A Guide to Parsing: Algorithms and Terminology
We have already introduced a few parsing terms, while listing the major tools and libraries used for parsing in Java, C#, Python and...
Read more >
Create Your Own Expression Parser | by Don Cross
Design a mathematical expression parser. JavaScript demo code ... The code starts by parsing the user's input text into a parse tree.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found