Jeppe
Jeppe

Reputation: 11

ANTLR: MismatchedTokenException with similar literals

I have the following

rule : A B;

A : 'a_e' | 'a';
B : '_b';

Input:

a_b    //dont work
a_e_b  //works

Why is the lexer having trouble matching this? When ANTLR matches the 'a_' in 'a_b' shouldnt it backtrack or use lookahead or something to see it cant match a token A and then decide to match token A as 'a' and then procede to match token B as '_b'?

I think ive missunderstood something very basic about how antlr works. Ive tried to read up on it in the ANTLR doc and google. But i have little experience wokring with lexers and parsers.

Thank you very much for any help.

Upvotes: 1

Views: 912

Answers (1)

Graeme Newlands
Graeme Newlands

Reputation: 73

You need to use a syntactic predicate to distinguish the 'a', '_', 'e' and 'b'.

The following will work:

grammar T;

rule : A B;

B : '_b';
A :     ('a_e')=>'a_e'
    | 'a'  ;

This parses 'a_e_b' and 'a_b' as you expect.

Recommend checking chapter 13 of The Definitive ANTLR Reference.

Upvotes: 1

Related Questions