问题描述:

I am getting following error on parsing but not sure why it's happening.

line 1:24 mismatched input '1' expecting NUM

line 1:24 mismatched input '1' expecting NUM

select a from abc limit 1 ;

--

grammar SQLCmd;

parse : sql

;

sql : ('select' ((columns (',' columns))|count) 'from')

tables

('where' condition ((and|or) condition))* (limit)? ';'

;

limit : 'limit' NUM

;

num : NUM

;

count : 'count(*)'

;

columns : VAL

;

tables : VAL

;

condition : ( left '=' right )+

;

and : 'and'

;

or : 'or'

;

left : VAL

;

right : VAL

;

VAL : [*a-z0-9A-Z~?]+

;

NUM : [0-9]+

;

WS : [ \t\n\r]+ -> skip

;

网友答案:

It looks like you have a VAL instead of a NUM.

The "1" is both a VAL and a NUM but since VAL comes first, there will never be NUM tokens since every NUM will be a VAL.

Try putting the NUM rule before the VAL rule.

You could have found out this by yourself by looking at the token types from the lexer. This will tell you the actual type of the token that is present.

@TheAntlrGuy: Maybe one could add the actual token type to the error message?

相关阅读:
Top