grammars-v4 icon indicating copy to clipboard operation
grammars-v4 copied to clipboard

Java8 and C# grammar: Tree structure of a variable that is initialized with a literal during declaration

Open PawanKartikS opened this issue 1 year ago • 2 comments
trafficstars

Hi,

I have a question regarding the tree that's generated for the following Java8 code:

class C {
    public void m() {
        int i = 0;
    }
}

The tree for the initialisation in the declaration looks like: Screenshot 2024-03-04 at 3 09 16 PM

Why is the initialization so convoluted? Why are inclusive* and shift* nodes involved here when the RHS is a simple literal? Do I have to go through all those nodes to access the literal? I see that this happens for other languages like C# as well.

PawanKartikS avatar Mar 04 '24 15:03 PawanKartikS

The parse tree describes the rules in the parse. It is not an abstract syntax tree. The parse tree for the literal is caused by implementing operator precedence using a chain of parser rules.

In an "optimized Antlr grammar", the expression syntax will be implemented using one rule with "alt-ordering". (See the java grammar expression syntax.) This alternative implementation eliminates the long chains of parse tree nodes, and makes for a faster parse. However, it takes a lot of work to develop an alt-ordered rule to implement precedence. Most language specs, like that for C# and C, define operator precedence using a precedence chain of rules.

Yes you will need to "go through" all nodes to get the literal value if you use visitors or listeners. But, working with visitors and listeners are like programming in assembly language. There are better ways to work with trees, e.g., XPath.

kaby76 avatar Mar 04 '24 23:03 kaby76

That explains it! Thank you.

PawanKartikS avatar Mar 05 '24 17:03 PawanKartikS