tunnelvisionlabs/antlr4cs

Grammar rule matching even though it shouldn't

Opened this issue · 1 comments

tobbi commented

I have specified the following grammar:
https://pastebin.com/mzwv0czc

I'm testing it out with the following C# code:

var stream = new AntlrInputStream(plausiregel);
var lexer = new plausiregelGrammarLexer(stream);
var tokens = new CommonTokenStream(lexer);
var parser = new plausiregelGrammarParser(tokens);
var ctx = parser.plausiregel();
var eof = ctx.Eof();

The input is as follows:
G:238859 == G:238859 > 100

Because I specified a mandatory EOF token after the formel rule, the plausiregel formula should be null because the input string does not match due to trailing garbage after the formel rule. However, only the EOF token is null.

Am I incorrect with my assessment?

Parser rules do not always return null if an error occurred (it attempts to recover from errors). There should be a property of ctx you can check to see if an error occurred. There are other options as well:

  • You can register an error listener and use that to verify that no errors were reported, while still allowing recovery
  • You can use BailErrorListener to throw an exception if an error occurs