简体   繁体   中英

Is there a way to avoid g4 tokenize a variable name as a laxer rule when we want?

I defined some lexer rules as given below:

    DATE:  D A T E  ;
    ID :  '&'*? IDENTIFIER ;
    IDENTIFIER  : [a-zA-Z_] [a-zA-Z_0-9]*;```

But for the line of coding as given below:

    keep date column1 column2;

Because in here the date is a variable name instead of a keyword DATE. So my question is that is it possible for me to let g4 to treat the date as a lexer rule of ID but not a DATE?

The ANTLR Lexer is, in no way, influenced by your parser rules.

It operates directly against the input stream of characters, and, if multiple rules match a sequence of characters, the tie is broken by these two rules.

1 - The rule that matches the longest stream of input characters will take precedence. (In your case the IDENTIFIER rule and the DATE rule, both match the "date" sequence of characters.

2 - If two rules match the same length character sequence, the first rule "wins". (In your case, the DATE rule occurs first, so the "date" sequence of characters will be recognized as a DATE token.

It makes absolutely no difference that a parse rule might be looking for an IDENTIFIER ; the Lexer has tokenized the input without influence from the parser rules, and the parser rules match the input stream of tokens generated from the Lexer.

IF you want "date" in this context to be acceptable, then you'll need to have your parser rule accept both an IDENTIFIER and a DATE token in that parser rule.

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