[antlr-interest] Partial parsing
Anton Bychkov
bychkov.anton at gmail.com
Tue Mar 30 09:51:09 PDT 2010
Hi.
I'm trying to implement partial SQL parsing in my application with the
help of ANTLR.
It is hard to write proper grammar for some SQL expressions, so I want
to leave them as a plain text.
Consider the following SQL statement:
SELECT name1, func(first expression), (second expression) as 'foo' FROM bar
I want this to be parsed in a list of field/function names with
expressions as text.
In this example the list would be:
'name1'
'func', 'first expression'
'second expression'
Writing grammar rules for list of fields names was easy, but I have no
idea how to write grammar rule for unparsed expressions.
I'm new to ANTLR, so off the top of my head, I have the following
algorithm for the rule:
- accept any characters except left and right braces
- for '(' increase brace count
- for ')' decrease brace count
- if got ')' with brace count equal to zero then stop parsing this entity
So, I tried something like this to parse 'func(first expression)'
entity (using C target):
fragment functionCall
@init {
int nb = 0;
}
: NAME LEFT_BRACE
(
LEFT_BRACE { nb ++; }
| { nb > 0 }?=> RIGHT_BRACE { nb --; }
| ~(LEFT_BRACE | RIGHT_BRACE)
)*
RIGHT_BRACE;
And got antlrworks crashing and glitching, compilation errors in
generated code (error C2065: 'nb' : undeclared identifier).
That makes me think I'm going in wrong direction...
Please, help me write this goddamned rule or tell how to do it in a right way.
Thank you.
More information about the antlr-interest
mailing list