Arthur Attout
Arthur Attout

Reputation: 2916

Why is ANTLR C# autogenerated parser not matching EOF?

I have a very simple grammar that (I think) should only allow additions of two elements like 1+1 or 2+3

grammar dumbCalculator;
expression: simple_add EOF;
simple_add: INT ADD INT;

INT:('0'..'9');
ADD : '+';

I generate my C# classes using the official ANTLR jar file

java -jar "antlr-4.9-complete.jar" C:\Users\Me\source\repos\ConsoleApp2\ConsoleApp2\dumbCalculator.g4 -o C:\Users\Me\source\repos\ConsoleApp2\ConsoleApp2\Dumb -Dlanguage=CSharp -no-listener -visitor

No matter what I try, the parser keeps adding the trailing elements although they shouldn't be allowed.

For example "1+1+1" gets parsed properly as an AST :

expression
    simple_add
        1
        +
        1
    +
    1

Although I specifically wrote that expression must be simple_add then EOF and simple_add is just INT ADD INT. I have no idea why the rest is being accepted, I expect ANTLR to throw an exception on this.

This is how I test my parser :

var inputStream = new AntlrInputStream("1+1+1");
var lexer = new dumbCalculatorLexer(inputStream);
lexer.RemoveErrorListeners();
lexer.AddErrorListener(new ThrowExceptionErrorListener());
var commonTokenStream = new CommonTokenStream(lexer);
var parser = new dumbCalculatorParser(commonTokenStream);
parser.RemoveErrorListeners();
parser.AddErrorListener(new ThrowExceptionErrorListener());
var ex = parser.expression();
ExploreAST(ex);

Why is the rest of the output being accepted ?

Upvotes: 0

Views: 86

Answers (1)

Arthur Attout
Arthur Attout

Reputation: 2916

Classical scenario, I find my error 5 minutes after posting on Stack Overflow.

For anyone encountering a similar scenario, this happened because I did not explicitly set the ErrorHandler on my parser.

Naively, I expected all the AddErrorListener to handle the errors, but somehow there's a specific thing to do if you need the errors to be handled before visiting the tree.

I needed to add

parser.ErrorHandler = new BailErrorStrategy();

After this, I indeed got the exceptions on wrong input strings.

This is probably not the right thing to do, I'll let someone who knows ANTLR better to comment on this.

Upvotes: 1

Related Questions