简体   繁体   中英

Antlr 4 TEXT and DIGIT together

I am parsing a SQL like language and I have problems with strings that starts with a number:

SELECT 90userN is parsed to SELECT 90 AS userN

Since I remove the whitespaces, it somehow gets the digits as the name and the string as the alias.

I don't know even where to start.

Grammar:

result_column  :    '*'                             
            |       table_name '.' '*'                  
            |       table_name '.' any_name             
            |       expr                                


any_name :      keyword
            |   IDENTIFIER
            |   STRING_LITERAL
            |   '(' any_name ')'
    ;

    expr:   literal_value;

literal_value : 
                NUMERIC_LITERAL
            |   STRING_LITERAL
            |   DATE_LITERAL
            |   IDENTIFIER
            |   NULL
;

IDENTIFIER :    
          '"' (~'"' | '""')* '"'
        | '`' (~'`' | '``')* '`'
        | '[' ~']'* ']'
        | [a-zA-Z_] [a-zA-Z_0-9]*;
STRING_LITERAL : '\'' ( ~'\'' | '\'\'' )* '\'' ;
NUMERIC_LITERAL : 
    DIGIT+ ( '.' DIGIT* )? ( E [-+]? DIGIT+ )?
    | '.' DIGIT+ ( E [-+]? DIGIT+ )? ;
DATE_LITERAL:   DIGIT DIGIT DIGIT DIGIT '-' DIGIT DIGIT '-' DIGIT DIGIT;

Identifiers in SQL can not start with numbers and that is really clear in the last alternative of your IDENTIFIER rule: [a-zA-Z_] [a-zA-Z_0-9]*;

I think you are already using it, but refer to the SQLite4 grammar example

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