tunnelvisionlabs / antlr4cs

The original, highly-optimized C# Target for ANTLR 4
Other
449 stars 103 forks source link

Grammar rule matching even though it shouldn't #340

Open tobbi opened 5 years ago

tobbi commented 5 years ago

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?

sharwell commented 5 years ago

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: