Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANTLR4: Tree construction

Tags:

antlr4

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.

like image 636
Har Avatar asked Feb 21 '13 17:02

Har


1 Answers

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.

like image 93
Sam Harwell Avatar answered Sep 28 '22 11:09

Sam Harwell