Skipping a level in the parse tree

23 views
Skip to first unread message

Niels Basjes

unread,
Nov 25, 2016, 5:20:08 AM11/25/16
to antlr-di...@googlegroups.com
Hi,

I have a situation where I get something like this in my parser (over simplified example to make the question clear!!):

rule1: something ( a | b | c | d | e | f | g ) 
     | somethingElse ( a | b | c | d | e | f | g ) 
     ;

rule2: moreStuff ( a | b | c | d | e | f | g ) 
     | evenMoreStuff ( a | b | c | d | e | f | g ) 
     ;

As you can see the rule block a | b | c | d | e | f | g ) appears several times and I would like to make my rules more readable.
So I try this to improve the maintainability and readability of my code:

letters: ( a | b | c | d | e | f | g );
 
rule1: something letters 
     | somethingElse letters
     ;

rule2: moreStuff letters 
     | evenMoreStuff letters
     ;

Yet when I do this I get (as expected) an additional "Letters" level in the parse tree. 
But because I'm actually "walking around" the tree this extra level makes things like "go up 1 level" and "go down 1 level" more tricky.

Now my question is: Can I specify the rule "Letters" as being something for which NO ParseTree node should be created?

At this point the only "Solution" I see is to use something like the C-preprocessor and use macros to write a 'readable' form and generate the other one from that.

What is the best way to 'solve' this in Antlr4? 

Niels Basjes

Mike Lischke

unread,
Nov 25, 2016, 5:38:51 AM11/25/16
to antlr-di...@googlegroups.com


Letters: 
( A | B | C | D | E | F | G );
 
Rule1: Something Letters 
     | SomethingElse Letters
     ;

Rule2: MoreStuff Letters 
     | EvenMoreStuff Letters
     ;

Yet when I do this I get (as expected) an additional "Letters" level in the parse tree. 
But because I'm actually "walking around" the tree this extra level makes things like "go up 1 level" and "go down 1 level" more tricky.

Now my question is: Can I specify the rule "Letters" as being something for which NO ParseTree node should be created?

At this point the only "Solution" I see is to use something like the C-preprocessor and use macros to write a 'readable' form and generate the other one from that.

Wow, you are really taking multiple extra rounds only to prevent one extra round :-) What is the problem of having to walk this extra level? There is no way to avoid this in the parse tree if you define your grammar that way. You are explicitly telling ANTLR to generate exactly that structure. You cannot specify the opposite at the same time. At the end of the day you access your A B C etc. nodes now via the visitLetters() functions instead of visitRule1(). That's really a trivial change.


Niels Basjes

unread,
Nov 25, 2016, 5:56:42 AM11/25/16
to antlr-discussion
Hi,

Yes, my current code works. Yet I want to see if I can make it better/cleaner.
The rules of my parser ( productName in this one: https://github.com/nielsbasjes/yauaa/blob/master/analyzer/src/main/antlr4/nl/basjes/parse/useragent/parser/UserAgent.g4#L189 ) require me to have code like this 

Hence my question.

Niels Basjes

Op vrijdag 25 november 2016 11:38:51 UTC+1 schreef Mike Lischke:
Reply all
Reply to author
Forward
0 new messages