[antlr-interest] ANTLR tree structure

Varun Bhargava varun.bhargava at carsales.com.au
Wed Jul 30 16:55:00 PDT 2008


Hi All,

 

I'm writing a utility to substitute parameter values in the WHERE clause
of a SQL like language. I have a sample query below and here is what I
need to do. I need to allow the user to select parameter values from a
UI (PARAM1 and PARAM2) and then just before running the query,
substitute the parameter placeholders with the values that the user has
selected.

 

SELECT a, b FROM MyExpr WHERE a = @PARAM1 AND b = @PARAM2 OR c = 34

 

I have written the grammar and the parser that recognises the above
statements and creates an AST. The where clause is rooted at the WHERE
node. There is another root for the comparisons

 

So the tree for the WHERE clause would look like 

 

WHERE

|

A ----------AND------b-------OR-----------c

|                             |                             |

= --- @PARAM1         = --- @PARAM2         = 34     

 

 

 

If @PARAM1 or @PARAM2 is NULL then the comparison clause needs to be
removed from the where clause. The problem then arises that I might have
hanging instances of AND or OR without a left or right hand side. And if
brackets are used, it complicated things further.

 

So after substituting parameter values and deleting unwanted nodes, when
I recreate the queryString form the tree it might look like (assuming
@PARAM1 is null

 

SELECT a, b FROM MyExpr WHERE AND b = 'substituted param2' OR c = 34

 

The only way I can think is to manually then walk the tree to find such
occurances and fix them. Is there an automatic/elegant way to resolve
this? Any help would be appreciated.

 

Thanks

Varun

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080731/3fc7278c/attachment.html 


More information about the antlr-interest mailing list