1

I am trying to parse a SQL statement that allows for both a BETWEEN expr1 AND expr2 and also expr1 AND expr2. An example would be:

SELECT * FROM tbl WHERE
    col1 BETWEEN 1 AND 5
    AND col3 = 10;

What would be a good way to disambiguate this, as my grammar is currently like the following:

grammar DBParser;
statement:expr EOF;

expr
    : '(' expr ')'
    | expr '=' expr
    | expr 'BETWEEN' expr 'AND' expr
    | expr 'AND' expr
    | ATOM
    ;

ATOM: [a-zA-Z0-9]+;

WHITESPACE: [ \t\r\n] -> skip;

And with the input (col1 BETWEEN 1 AND 5) AND (col3 = 10);:

enter image description here

samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
  • You cut the image from the Intellij plugin to remove the "[a]mbiguities" column. There is no ambiguity. You can also test it [here](http://lab.antlr.org/), or [use this tool](https://github.com/kaby76/Domemtech.Trash/tree/main/src/trperf). All show the same result: the number of ambiguities is 0. It's 0 because Antlr disambiguates `expr` alts by the order of the alts. Also, ';' in the input is not recognized by the grammar. It should be removed. – kaby76 Aug 21 '22 at 09:54
  • You should adjust the example input to something like `col1 BETWEEN 1 AND 5 AND 10` in order to make the question clearer. – kaby76 Aug 21 '22 at 10:31
  • The usual way to fix this is to restrict one of the alts, e.g., `grammar DB; statement: e EOF; e : '(' e ')' | e '=' e | e 'BETWEEN' fe 'AND' fe | e 'AND' e | ATOM ; fe : '(' e ')' | fe '=' fe | ATOM ; ATOM: [a-zA-Z0-9]+; WHITESPACE: [ \t\r\n] -> skip;` – kaby76 Aug 21 '22 at 10:41
  • @kaby76 what does `fe` mean in the above? I don't see that defined. – samuelbrody1249 Aug 21 '22 at 22:44
  • 1
    `fe : '(' e ')' | fe '=' fe | ATOM ;`. – kaby76 Aug 21 '22 at 22:46
  • 1
    @kaby76 got it, thank you. Want to post an answer and I can accept it? I think your comments combined explain it all. – samuelbrody1249 Aug 21 '22 at 23:14

0 Answers0