Weird error reporting: extraneous input '<EOF>'

See original GitHub issue

We found a weird case where the default error recovery strategy reports the EOF token as being an extraneous token.

Grammar:

grammar Test;

program
    : declarations? expression EOF;

declarations
    : declaration+;

declaration
    : 'let' declarator (',' declarator)* ';';

declarator
    : ID '=' expression ;

expression
    : ID
    | NUMBER
    | BOOLEAN
;

NUMBER: [0-9]+;
BOOLEAN: 'true' | 'false';
ID: [a-zA-Z_][a-zA-Z_0-9]*;
WS: (' ' | '\n' | '\r' | '\t')+ -> channel(HIDDEN);

Scenario:

public class Test {
    public static void main(String[] args) throws Exception {
        CharStream input = CharStreams.fromString("let a = 1, b = ;\ntrue");
        TestLexer lex = new TestLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lex);
        TestParser parser = new TestParser(tokens);
        parser.program();
    }
}

Console output:

line 1:15 extraneous input ';' expecting {NUMBER, BOOLEAN, ID}
line 2:4 extraneous input '<EOF>' expecting {',', ';'}

I tracked down the method of the error recovery strategy that reports the EOF token as being extraneous. I believe the correct here would be to report a token mismatch rather than an extraneous token.

I’d be glad to provide a PR if welcome, but I need some guidance on how to proceed.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
marcospassoscommented, Oct 5, 2019

Yeah, we can fix it on our side, not a big deal. The motivation behind opening this PR is that whenever we find something which we believe to be an improvement, we always bring it up in the hope of helping other ANTLR users. But if your judgment is that the risk does not worth the benefit, we can keep it as is.

0reactions
marcospassoscommented, Oct 5, 2019

I am closing in favor of stability.

Thank you for your support, @parrt.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Delta Time Travel with SQL error extraneous input '0 ...
spark.sql("SELECT * FROM RIDES.YELLOW_TAXI VERSION AS OF 0"). It throws the error: ParseException: extraneous input '0' expecting {<EOF>, ...
Read more >
strange error messages when running rule in plugin while it's ...
I encounter strange error messages when running rules in PyCharm ... line 1:3 extraneous input ' ' expecting NAME line 1:11 mismatched input ......
Read more >
Strange error "mismatched input '<EOF>' expecting ';' on line 0"
Hi, I've a really a strange problem (working with XText 1.0.1 and Eclipse 3.6 -> Eclipse Text file encoding is UTF-8)
Read more >
ANTLR4, "extraneous input" messages? - Google Groups
To me, this says that all of the top components are optional. Mostly I do this by appending * to the parser syntax,...
Read more >
Pyspark SQL Error - mismatched input 'FROM' expecting <EOF>
Hello All, I am executing a python script in AWS EMR (Linux) which executes a sql inside or below snippet of code and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found