1
votes

I have a parser rule which looks like below

nested_query: ~(LPARAN | RPARAN)+? LPARAN nested_query RPARAN ~(LPARAN | RPARAN)+?
    | nested_query nested_query_op LPARAN nested_query RPARAN
    | ~(LPARAN | RPARAN)+?
    ;
nested_query_op: binary_in | binary_not_in ;
binary_in: 'in'; 
binary_not_in: 'not' 'in';
LPARAN: '(';
RPARAN: ')';

This correctly matches the string list(srcVm) of flows where typeTag ="TAG_SRC_IP_VM" until timestamp

enter image description here

But when I try to parse a string having more than one matching brackets it does not get properly parsed for example list(srcVm) of flows where (typeTag ="TAG_SRC_IP_VM") until timestamp

enter image description here

Can someone let me know how can I modify the above rule to match a string with more than one matching braces under nested_query rule like below

                      nested_query:1
                            |
    ---------------------------------------------------------         
    list ( nested_query:3 ) of flows where ( nested_query:4) until timestamp                                                   
                  |                                |
                srcVM                    (typeTag ="TAG_SRC_IP_VM")
2
Can you specify more info please. What are the definitions of LPARAN/RPARAN/nested_query_op/... ?Niels Basjes
Updated the question. I missed those rules. Thanks for pointing this out.tuk

2 Answers

1
votes

This ought to do the trick:

nested_query
 : ( LPARAN nested_query RPARAN | ~( LPARAN | RPARAN ) )+
 ;

list(srcVm) of flows where typeTag ="TAG_SRC_IP_VM" until timestamp

enter image description here

list(srcVm) of flows where (typeTag ="TAG_SRC_IP_VM") until timestamp

enter image description here

0
votes

Well there simply isn't any rule that would allow input with two sets of parentheses following each other without in|not in just in front of the second opening brace`:

  • The only first alternative in nested_query only allows one occurrence of parentheses (although they might be nested) - outside of the top-level parentheses the must be no parentheses.
  • The second alternative allows top-level parentheses (from first nested_query) followed by non-parentheses, then in or not in and then second top/level parentheses.
  • The third alternative doesn't allow parentheses at all.

To match multiple parentheses on one level, the first alternative of nested_query should be something like

~(LPARAN | RPARAN)* (LPARAN nested_query RPARAN ~(LPARAN | RPARAN)*)+ ~(LPARAN | RPARAN)*

But then the second alternative will be in conflict with this, because everything that can match it can also match this modified first alternative.