Confused by error types: missing token vs. no viable alternative

19 views
Skip to first unread message

Martin d'Anjou

unread,
Jan 28, 2020, 10:33:51 PM1/28/20
to antlr-discussion
I am trying to deal with errors, and I have a case that I do not understand.

The lexer is:
lexer grammar L;
MACHINE
: 'machine';
DEFAULT
: 'default';
MACDEF
: 'macdef';
ID
: ~('\\r' | '\\n' | ' ' | '\\t')+;

The parser is:
parser grammar G;
netrc
: (machineEntry | macro)* defaultEntry? macro*;
machineEntry
: MACHINE ID;
defaultEntry
: DEFAULT;
macro
: MACDEF ID;

When I feed the string "machine", I get what I expect:
line 1:7 missing ID at '<EOF>'

When I feed the string "macdef", I get no viable alternative, which seems wrong to me:
line 1:6 no viable alternative at input 'macdef'

I am very confused as to why I am getting two different error messages for what appears to be the same user error (missing ID after the keyword).

Any help appreciated, thanks in advance.
Martin

Mike Lischke

unread,
Jan 29, 2020, 3:37:36 AM1/29/20
to antlr-discussion

The lexer is:
lexer grammar L;
MACHINE
: 'machine';
DEFAULT
: 'default';
MACDEF
: 'macdef';
ID
: ~('\\r' | '\\n' | ' ' | '\\t')+;

The parser is:
parser grammar G;
netrc
: (machineEntry | macro)* defaultEntry? macro*;
machineEntry
: MACHINE ID;
defaultEntry
: DEFAULT;
macro
: MACDEF ID;

When I feed the string "machine", I get what I expect:
line 1:7 missing ID at '<EOF>'

When I feed the string "macdef", I get no viable alternative, which seems wrong to me:
line 1:6 no viable alternative at input 'macdef'

I am very confused as to why I am getting two different error messages for what appears to be the same user error (missing ID after the keyword).

That may be the result of the way the prediction. You can check it by stepping through the recognition process and see where it fails in the first and where in the second case.

Your netrc rule can match `macro` in two different ways. Without an ID neither of the two occurrences of `macro` can be matched, which fails the prediction already on enter of netrc. That means the prediction already fails and produces the no viable alt error.

The input `machine` on the other hand can clearly be determined as being part of the `machineEntry` rule and thus the parser will try to match that. The prediction works find at this point. The input `machine` is then matched, but since the ID is missing the parser will issue an error (missing symbol).


Martin d'Anjou

unread,
Jan 29, 2020, 1:51:20 PM1/29/20
to antlr-discussion
You are correct. It fails in adaptivePredict.
I do not understand why you say "Without an ID neither of the two occurrences of `macro` can be matched". Is there an ambiguity I am not seeing?

Martin

Martin d'Anjou

unread,
Jan 29, 2020, 3:12:28 PM1/29/20
to antlr-discussion
In any case, I decided to simplify the grammar.
I'll catch the problem (no machineEntry after defaultEntry) in the visitor instead.

Thanks Mike.

Mike Lischke

unread,
Jan 30, 2020, 4:30:16 AM1/30/20
to antlr-di...@googlegroups.com
You are correct. It fails in adaptivePredict.
I do not understand why you say "Without an ID neither of the two occurrences of `macro` can be matched". Is there an ambiguity I am not seeing?

The rule netrc consists of only 3 optional parts. The subrule macro can be matched by the first and the third part, that's the ambiguity.
Reply all
Reply to author
Forward
0 new messages