Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANTLR - The following alternatives can never be matched

I am unable to understand why this simple grammar -

grammar Test;
file    :   ID;
ID  :   .*;

leads to this error -

Test.g:3:6: The following alternatives can never be matched: 1

Why is this happening?

like image 677
user869081 Avatar asked Dec 02 '25 16:12

user869081


1 Answers

Using .* and .+ at the end of a lexer rule is illegal in ANTLR:

ID : .*;       // illegal
ID : 'bar' .*; // illegal
ID : .* 'bar'; // legal

In parser rules, this is not the case.

like image 82
Bart Kiers Avatar answered Dec 07 '25 03:12

Bart Kiers