[antlr-interest] Newbie needs guidance on parsing SQL
Michiel Vermandel
Michiel_Vermandel at axi.be
Thu Sep 21 00:56:46 PDT 2006
Hi,
I have written a Forms, Sql and Plsql Parser.
I solved the problem in the following way:
(Lets see to an Insert statement)
insert into my_table (...)
values(...)
;
In my sql.g I have the following:
insert_command:
"insert" "into" selected_table
column_list
( ( "values" OPEN_PAREN! plsql_exp_list CLOSE_PAREN! )
| select_statement )
{ ## = #([INSERT_STATEMENT, "insert_statement"], ##); }
;
with selected_table:
selected_table:
( table_spec | subquery ) ( table_alias )?
;
table_spec:
( schema_name DOT )? table_reference ( AT_SIGN link_name )?
;
table_reference:
object_name
{ ## = #([TABLE_REF, "ref_table"], ##); }
;
object_name:
( identifier ( DOT (object_dot_name) )?
)
{ #object_name = #([OBJECT_NAME, "<#OBJECT_NAME#>"], #object_name); };
In this way I get in my AST something like
INSERT_STATEMENT
TABLE_REF
OBJECT_NAME
"my_table"
I walk the AST tree (own code) and whenever I get a TABLE_REF node I
expect it to have a child node OBJECT_NAME which must have a child node on
its own which contains the table name.
For every TABLE_REF I create a custom made SqlTable object and add it to a
list of references.
Then in a second face I try to resolve all objects in my references list
by looking them up in a repository...
Terence Parr <parrt at cs.usfca.edu>
Sent by: antlr-interest-bounces at antlr.org
20/09/2006 20:16
To
Antlr List <antlr-interest at antlr.org>
cc
Subject
Re: [antlr-interest] Newbie needs guidance on parsing SQL
On Sep 20, 2006, at 9:53 AM, WALTERS, CRAIG P [AG/1000] wrote:
>
> I am doing my 1st antlr project (version 2.7.6). I downloaded
> DmlSQL2/SQL2
> and converted them from C++ to Java. What I need to do is scan a
> string
> containing (Oracle) SQL DML and extract all of the names of the
> tables that
> are the targets of modifications (INSERT, UPDATE, DELETE,
> TRUNCATE). I
> can't figure out how to get the table names. Can someone give me some
> suggestions?
Hi. Write the grammar for the subset and then put in an action that
references the IDENTIFIER matched for table name. Use the text for
that ID as you wish.
Ter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060921/369958e9/attachment-0001.html
More information about the antlr-interest
mailing list