Skip to content

Mismatched input error: inaccurate list of expected tokens #4899

@hackathlete

Description

@hackathlete

It appears that the list of expected tokens can sometimes be inaccurate when a mismatched input is detected.

Following grammar can be used to reproduce the issue:

grammar ErrorReportingTest;

file
  : (assign)+ EOF
  ;
assign
  : aggregate LE expr SEMI
  ;
aggregate
  : LPAREN ( el_others | ( el+=expr ( COMMA el+=expr )* ( COMMA el_others )? ) ) RPAREN
  ;
el_others
  : OTHERS ARROW expr
  ;
expr
  : '-' expr
  | expr '*' expr
  | expr ('+'|'-') expr
  | ID
  | INT
  | '(' expr ')'
  ;

OTHERS : 'others';
LE     : '<=';
ARROW  : '=>';
COMMA  : ',';
SEMI   : ';';
LPAREN : '(';
RPAREN : ')';
INT    : [0-9]+;
ID     : LETTER (LETTER | [0-9])*;
fragment
LETTER : [a-zA-Z];
WS     : [ \t\n\r]+ -> skip;
SL_COMMENT : '//' .*? '\n' -> skip;

The following examples show valid code lines that illustrate the language described:

(t, e, r) <= 0;
(others => 9) <= 9;
(t, e, r, others => 9) <= 0;

If there is a "mismachted input" error in the code, then the expected tokens are reported in the error message.

Expected
The correct error message is displayed for the incomplete code snippet "(t, e, r)" as the only possible token is '<='.

(t, e, r)
line 2:0 mismatched input '<EOF>' expecting '<='

Unexpected
From my point of view, the error message for the incomplete code snippets "(others => 9)" and "(t, e, r, others => 9)" shall be the same as above. But, the expected token list contains other tokens and the '<=' is missing.

(others => 9)
line 2:0 mismatched input '<EOF>' expecting {'-', '*', '+', ')'}
(t, e, r, others => 9)
line 2:0 mismatched input '<EOF>' expecting {'-', '*', '+', ')'}

The RecognitionException gives back different offendingState numbers. I would expect that the offendingState is equal for all shown code lines.
To me, it seems as there may be a problem in the generated parser. However, has anybody a hint how to fix (work around) this issue in my grammar?

ANTLR4 version: v4.13.2
Target language: Java

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions