[antlr-interest] Get Values after parse
Harald M. Müller
harald_m_mueller at gmx.de
Sun Dec 30 11:24:38 PST 2007
Usually, you would just add actions to the grammar which store the relevant
values somewhere else - typically in the parser object itself:
grammar SomeSQL;
@members {
public List<String> columnNames = new ArrayList<String>();
public String whereClause;
// instead of public, write accessors in real-life code ...
}
sql : selectClause fromClause whereClause?
;
selectClause
: 'select' columnList
;
columnList
: (ID '.')? '*' { columnNames.Add(...???...); }
| c1=column { columnNames.Add($c1.text); }
( ','
c2=column { columnNames.Add($c2.text); }
)*
;
etc.
In your main code, after running
SomeSQLParser p = new SomeSQLParser(...);
p.sql();
you can then get all the information from your SomeSQLParser object:
for (String columnName : p.columnNames) { ... }
...o.whereClause...
Instead of using the parser class, you can also pass some "storage/context"
object as a parameter to the parser's constructor and have the actions
record their findings in that context (in compiler talk, this would be a
"symbol table").
Regards
Harald
_____
From: antlr-interest-bounces at antlr.org [mailt
o:antlr-interest-bounces at antlr.org] On Behalf Of Cory Isaacson
Sent: Sunday, December 30, 2007 2:56 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Get Values after parse
I am new to ANTLR, and have a simple question. I need to parse a simple SQL
SELECT statement, and can see how to create the grammar for the lexer and
parser. I need to then get specific values from the SELECT statement, such
as table name(s), column name(s), the WHERE clause, etc.
To actually get the values, do I need to output an AST tree? If so, what is
the simplest way to access specific node/values?
If you can suggest examples to look at and/or the best API to use that would
be great.
Thanks in advance for any assistance.
Cory
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071230/84901e02/attachment-0001.html
More information about the antlr-interest
mailing list