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.
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address
--
You received this message because you are subscribed to the Google Groups
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/il-antlr-interest?hl=en.