简体   繁体   中英

ANTLR Parser matching token not identifier

I am writing a parser for SQL statements. I defined tokens for the keywords like:

tokens
{
CREATE = 'create';
TABLE = 'table';
YEAR = 'year';
NAME = 'name';
...
}

and Identifier as

IDENTIFIER
    :
    ( LETTER | '_' ) (NAMECHAR)*
    ;

REAL_NUMBER
  : NUMBER_VALUE  ( 'e' ( PLUS | MINUS )? DIGIT )?
  ;

fragment
NUMBER_VALUE
  : {numberDotValid()}?=> DIGIT DOT DIGIT?
  | DOT DIGIT
  | DIGIT
  ;

fragment NAMECHAR
    : LETTER | DIGIT | '.' | '-' | '_' |'%'
    ;

fragment DIGIT
  : '0' .. '9' ( '0' .. '9' )*
  ;

fragment LETTER
    : 'a'..'z'
    | 'A'..'Z'
    ;

I have passed the input as:

Create table emp (Table smallint not nulll);

I got an error mismatched input expecting Identifier. The error is on (Table smallint...) part. I know that tokens precedence is higher than the parser rules and it is currently matching the token Table. What is the right way to handle such types of issues?

Please help.

I guess it is beacuse the token 'table' is different to 'Table' that is recognized as an identifier, note the uppercase 'T' those tokens are not the same, you could make something like:

TABLE : 'table' | 'Table' | 'TABLE';

or to avoid combinations of upper and lower cases, something like:

TABLE: TABLE;
fragment T:
't' | 'T';
fragment A:
'a' | 'A';

and so on, that's the approach used by antlr too

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM