grammars-v4
grammars-v4 copied to clipboard
Java8 and C# grammar: Tree structure of a variable that is initialized with a literal during declaration
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:
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.
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.
That explains it! Thank you.