ANTLR4: Tree construction
Asked Answered
P

1

7

I am extending the baseClass Listener and am attempting to read in some values, however there doesnt seem to be any hierrarchy in the order.

A cut down version of my grammar is as follows:

start: config_options+
config_options: (KEY) EQUALS^ (PATH | ALPHANUM) (' '|'\r'|'\n')* ;

KEY: 'key' ;
EQUALS: '=' ;
ALPHANUM: [0-9a-zA-Z]+ ;

However the parse tree of this implementation is flat at the config_options level (Terminal level) i.e.the rule start has many children of config_options but EQUALS is not the root of subtrees of config_options, all of the TOKENS have the rule config_options as root node. How can I make one of the terminals a root node instead?

In this particular rule I dont want any of the spaces to be captured, I know that there is the -> skip directed for the lexer however there are some cases where I do want the space. i.e. in String '"'(ALPHANUM|' ')'"'

(Note: the ^ does not seem to work)

an example for input is:

key=abcdefg

key=90weata

key=acbefg9

All I want to do is extract the key and value pairs. I would expect that the '=' would be the root and the two children would be the key and the value.

Principate answered 21/2, 2013 at 17:48 Comment(0)
B
7

When you generate your grammar, you should be getting a syntax error over the use of the ^ operator, which was removed in ANTLR 4. ANTLR 4 generates parse trees, the roots of which are implicitly defined by the rules in your grammar. In other words, for the grammar you gave above the parse tree nodes will be start and config_options.

The generated config_options rule will return an instance of Config_optionsContext, which contains the following methods:

  • KEY() returns a TerminalNode for the KEY token.
  • EQUALS() (same for the EQUALS token)
  • PATH() (same for the PATH token)
  • ALPHANUM() (same for the ALPHANUM token)

You can call getSymbol() on a TerminalNode to get the Token instance.

Brandie answered 21/2, 2013 at 18:28 Comment(3)
How would you be able to define a relationship between terminals? i.e get the children of the equals sign. Or is this not a valid idea in ANTLR4? Is there a way to create an AST instead of a parse tree?Principate
@user1932405, create an extra rule, value for example: config_options: KEY EQUALS value ; value : (PATH | ALPHANUM) (' '|'\r'|'\n')* ;Graptolite
Oh okay, Thanks for that, so Ill have to create AST based on parse rules and not tokens.Principate

© 2022 - 2024 — McMap. All rights reserved.