3
votes

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?

1

1 Answers

6
votes

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.