Mismatched input error in simple antlr4 grammar

后端 未结 1 843
渐次进展
渐次进展 2021-01-03 10:31

I\'m trying to parse a simple subset of SQL using antlr4.

My grammar looks like this:

grammar Query;
query : select;
select : \'select\' colname (\',         


        
相关标签:
1条回答
  • 2021-01-03 10:38

    You cannot have two lexer rules that match the same (at least, not in the same mode/state...):

    ...
    COLNAME: [a-z]+ ;
    TABLENAME : [a-z]+;
    ...
    

    Do this instead:

    grammar Query;
    query     : select;
    select    : 'select' colname (',' colname)* 'from' tablename;
    colname   : ID;
    tablename : ID;
    ID        : [a-z]+;
    WS        : [ \t\n\r]+ -> skip;
    
    0 讨论(0)
提交回复
热议问题