无法识别令牌



我最近将SQL解析器代码从paraboled移到了ANTLR,迁移非常顺利,但我收到了这个错误消息,特别是当我的SQL包含and或or条件运算符时。我正在分享语法示例,如果有任何帮助,我将不胜感激。

如果我尝试解析此示例sql "SELECT Name,Age,Salary FROM Employee WHERE Age=12 AND Dept=15"

我得到1:50行不匹配的输入"AND",应为{,OPAND,OPOR}

然而,如果我用下面的规则替换,那么它就可以工作了,我正在尝试实现不区分大小写的解析

binaryConditionalExp:binaryExp|binaryConditionalExp CONDORATOR=('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 : [ trn]+ -> 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];

由于规则的顺序,lexer将AND视为标识符,而不是关键字。如果将lexer规则部分更改为以下内容,则字符串"AND"将被正确地标记为OPAND。

// match RESERVED WORDS first
OPAND : A N D ;
OPOR : O R;
// match IDENTIFIERS etc.
ID : [a-zA-Z0-9]+ ;
WS : [ trn]+ -> skip ; 
OPERATOR: [=><]+ ;

最新更新