0

I'm using antlr for parse sql condition queries like:

[where] Field1 = "Param1" and Field2 = "Param2"

for this case I have an antlr condition:

andExpr
    :   inExpr
        (
            AND^
            inExpr
        )*
    ;

inExpr      
    :    (eqcompareExpr)
        (
            IN^
            args
        )?  
    ;

eqcompareExpr
    :   (compareExpr)
        (
            (EQUAL^|NOTEQ^|IS^|LIKE^)
            (compareExpr)
        )?
    ;

compareExpr
    :   addExpr
        (
            (LESS^|MORE^|LESSEQ^|MOREEQ^)
            addExpr 
        )?
    ;

and tokens like:

LIKE    :   "like"
    ;
AND :   "and"
    ;
OR  :   "or"

How do I change the antlr code for add combined conditions with 'contains' like:

[where] Field1 = "Param1" and Contains (Field2, "Param2")

Also the queries can be like this:

[where] Contains (Field2, "Param2")

or

[where] Field1 = "Param1" or Contains (Field2, "Param2")
6
  • Why can't you add them the same way as the existing ones? Feb 9, 2018 at 20:23
  • @Jiri Tousek I don't know how to do that, I try add contains into "in" expression, but tree was built wrong. I need an example of how to build dependencies, because here the embedded contains operator Feb 10, 2018 at 3:05
  • You're mixing ANTLR v3 syntax (the ^ operators), and in your lexer rules you're using double quotes (OR : "or";), which suggests ANTLR v2. Which version are you using, and why not go for the most recent version, v4?
    – Bart Kiers
    Feb 11, 2018 at 21:00
  • I am using the antlr.net-2.7.6. It works much time, now I just need to add the "contains" method into working solution, that's why I do not go for the most recent version Feb 12, 2018 at 4:03
  • If the only reason for using that old version is because "It works much time" (?), I highly recommend you upgrade to v4. I doubt there are many people around familiar with v2. Good luck!
    – Bart Kiers
    Feb 12, 2018 at 9:14

1 Answer 1

1

Maybe the following rules can satisfy your needs

andExpr
    :   inOrContainsExpr
        (
            AND
            inOrContainsExpr
        )*
    ;

inOrContainsExpr:   inExpr | containsExpr;

inExpr
    :   (eqcompareExpr) ( IN^ args ) ?
    ;

containsExpr
    :   CONTAINS^ args
    ;

and so on. Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.