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

Jan van Mansum janvanmansum at gmail.com
Mon Sep 1 08:05:56 PDT 2008


Fixed the problem with

row_value_constructor_element
	:	value_expression
		-> ^({new CommonToken(COLNAME

$insert_statement::columnNames.get($row_value_constructor_list::i++))
                          }  value_expression)

Also, I had put CommonTree objects in columnNames by mistake. I have
now made sure that columnNames
contains Strings. This works, only ANTLRWorks doesn't show the token
labels in its diagram. However, if I print
the result with Tree.toStringTree() they do show up.


2008/9/1 Jan van Mansum <janvanmansum at gmail.com>
>
> 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



--
Jan van Mansum


More information about the antlr-interest mailing list