jack_carver
jack_carver

Reputation: 1526

Antlr AST construction

I am trying build an AST for the below grammer using ANTLR

condition_in
    :   column_identifier ('NOT')? 'IN' (sql_element_list | LPAREN select_stmt RPAREN)
    ;

for the above how do i build a rooted tree at NOT IN or IN depending upon the input ? or is there any better way ?

Also for python like dicts, how do i construct an ast, tree with MAP as root and a child MAP_PAIR for each key:value should be great i guess

map :   '{' collection_element':'collection_element (',' collection_element':'collection_element)* '}'

I tried several alternatives with label and tree rewrites but antrlworks always complains

Any help would be appreciated

Upvotes: 1

Views: 266

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170158

Try something like this:

grammar T;

options {
  output=AST;
}

tokens {
  NOT_IN;
  MAP_PAIR;
  MAP;
}

condition_in
 : column_identifier ( 'NOT' 'IN' in_end -> ^(NOT_IN column_identifier in_end)
                     | 'IN' in_end       -> ^('IN' column_identifier in_end)
                     )
 ;

in_end
 : sql_element_list 
 | LPAREN select_stmt RPAREN -> select_stmt
 ;

map
 : '{' (entry (',' entry)*)? '}' -> ^(MAP entry*)

entry
 : k=collection_element ':' v=collection_element -> ^(MAP_PAIR $k $v)
 ;

// ...

Upvotes: 1

Related Questions