GammaVega
GammaVega

Reputation: 779

Not recognizing token

I recently moved my SQL parser code from paraboiled to ANTLR and it was very smooth migration, however i am getting this error message specifically when my SQL contains AND or OR conditional operator. I am sharing sample grammar, would appreciate any help.

If i try to parse this sample sql "SELECT Name,Age,Salary FROM Employee WHERE Age=12 AND Dept=15"

I get line 1:50 mismatched input 'AND' expecting {, OPAND, OPOR}

However, if i replace with below rule then it works, i am trying to implement case insensitive parsing

binaryConditionalExp: binaryExp | binaryConditionalExp CONDOPERATOR=('AND' | 'OR') binaryConditionalExp | binaryparenExp;

/**
 * Define a grammar called Hello
 */
grammar SQLParser;


@header
{
    package somu.parsers;   
}




prog  : sqlsyntax;         // match keyword hello followed by an identifier


sqlsyntax : (selectclause fromclause whereclause) | (selectclause fromclause ) ;
selectclause : 'SELECT' columnclause;

columnclause : columnName (',' columnName)*;

columnName : columnLiteral;
columnLiteral : ID | sqlsyntax;

fromclause : 'FROM' tableclause;
tableclause : (tableclausealiaswithas | tableclauseplainalias | tableclausenoalias);  
tableclausenoalias  : ID | ;
tableclausealiaswithas : ID 'as' ID;
tableclauseplainalias : ID ID;

whereclause : 'WHERE' binarystmt;

binarystmt : binaryConditionalExp;
binaryExp: columnName OPERATOR columnName; 
binaryparenExp: '(' binaryConditionalExp ')';
binaryConditionalExp:  binaryExp | 
                       binaryConditionalExp CONDOPERATOR=(OPAND | OPOR) binaryConditionalExp | 
                       binaryparenExp;



ID : [a-zA-Z0-9]+ ;             // match identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines 
OPERATOR: [=><]+ ;
OPAND : A N D ;
OPOR : O R;

fragment DIGIT : [0-9];
fragment A : [aA];
fragment B : [bB];
fragment C : [cC];
fragment D : [dD];
fragment E : [eE];
fragment F : [fF];
fragment G : [gG];
fragment H : [hH];
fragment I : [iI];
fragment J : [jJ];
fragment K : [kK];
fragment L : [lL];
fragment M : [mM];
fragment N : [nN];
fragment O : [oO];
fragment P : [pP];
fragment Q : [qQ];
fragment R : [rR];
fragment S : [sS];
fragment T : [tT];
fragment U : [uU];
fragment V : [vV];
fragment W : [wW];
fragment X : [xX];
fragment Y : [yY];
fragment Z : [zZ];

Upvotes: 1

Views: 914

Answers (1)

Marc Q.
Marc Q.

Reputation: 551

Due to the ordering of your rules, the lexer considers AND as an identifier, not a keyword. If you change your lexer rule section to the following, the string "AND" gets properly tokenized as OPAND.

// match RESERVED WORDS first
OPAND : A N D ;
OPOR : O R;

// match IDENTIFIERS etc.
ID : [a-zA-Z0-9]+ ;
WS : [ \t\r\n]+ -> skip ; 
OPERATOR: [=><]+ ;

Upvotes: 2

Related Questions