[antlr-interest] How do I generate an AST node with a given text?

Jan van Mansum janvanmansum at gmail.com
Mon Sep 1 02:38:49 PDT 2008


Hello group:

I am writing an SQL parser and I am trying to get an AST like the
example below

SQL:

CREATE TABLE myTable (col01 VARCHAR(10), col02 INT);

INSERT INTO myTable (col01, col02)
VALUES ("Some string", 12)
       ("Some other string", 13);

Expected AST:

(TABLEDEF myTable
  (COLUMN col01
     (TYPE varchar 10))
  (COLUMN col02
     (TYPE int)))

(TABLEDATA myTable
  (ROW (col01 "Some string")
       (col02 12))
  (ROW (col01 "Some other string")
       (col02 13)))

Actual AST:

(TABLEDEF myTable
  (COLUMN col01
     (TYPE varchar 10))
  (COLUMN col02
     (TYPE int)))

(TABLEDATA myTable
  (ROW)
  (ROW (col01 ("Some string" "Some other string"))
       (col02 (12 13)))

I collect the column names in a scoped ArrayList<String> when parsing
the first line of the INSERT statement. When I get to the rule that
generates the column values I want to generate a node that contains the
column name and attach the value under it. Like this:

row_value_constructor_element
	:	value_expression
-> ^({$row_value_constructor::columnNames.get($row_value_constructor_list::i++)}
value_expression)
	;	

The problem seems to be that the nodes col01 and col02 are constructed only
once and that each time the parser gets to above line it attaches the new
value under the existing node.

How can I make sure that a new node is inserted?

Thanks for any help,

regards,


-- 
Jan van Mansum


More information about the antlr-interest mailing list