1
votes

I have been hacking away at my ANTLR4 grammar trying to eliminate all of its ambiguities, lexer rule error at a time. Nothing I seem to do fixes the problems. These lexer rules in particular that give me the same error are as follows:

Identifier: Letter (Letter | Digit | Und)+;
Keyword   : Letter+;
Param: Number | Identifier;
Statement: Keyword Lpr Param+ Rpr;
Block: Lbc Statement+ Rbc;

As you might have noticed, one token they all have in common is Letter. This and other tokens are defined as:

fragment Digit: '0'..'9';
fragment Letter: ('A'..'Z');
Und: '_';
Lpr: '(';
Rpr: ')';

I can't find how this could cause an ambiguity, unless ANTLR doesn't allow for multiple definition with potentially identical outcomes.

1

1 Answers

0
votes

ANTLR only (really) works with unambiguous grammars. Certain left-recursions are allowed and predicates and lexer modes can be used to handle/avoid ambiguities.

ANTLR will, however, allow for multiple definition with potentially identical outcomes.

It does this by always selecting the first such conflicting rule. The remaining conflicting rules are shadowed, meaning that for all practical purposes, they do not exist. Almost certainly, a very undesirable result.