简体   繁体   中英

Antlr3: Could not match token in parser rules which is used in lexer rule

I have lexer rules in Antlr3 as:

HYPHEN : '-';

TOKEN : HYPHEN CHARS;

CHARS : 'a' ..'z';

Parser rule is as:

exp : CHARS | some complex expression;
parser_rule : exp HYPHEN exp;

If I try to match 'abc-abc' with parser_rule, It fails. Because lexer creates TOKEN for HYPHEN exp. How can I match it correctly with parser_rule.

In ANTLR lexer, the lexer rule that can match the longest sub-sequence of input is used. So your input

abc-abc

will be tokenized as

CHARS("abc")
TOKEN("-abc")

and therefore will not match the expected CHARS HYPHEN CHARS .

You should consider making TOKEN a parser rule instead of lexer 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