Reputation: 305
I'm currently developing a parser for an old proprietary markup-like language which has to be converted to a newer standard. I'm using ANTLR 4.8 to generate C# parsers, which I use with official Antlr4.Runtime.Standard
.
The parser grammar starts with the entry rule like this:
parser grammar ParstParser;
options {
tokenVocab=ParstLexer;
}
report
:
input
lines
fields
mod
head?
body
foot?
EOF
;
[...]
Testing the grammar with grun
or with the official ANTLR plugin in Rider, it parses my dummy file just fine (sorry for hiding the markup code, but it's a property of the company I'm working for):
Using C# I wrote a builder for an higher-level model which accepts contexts from the ANTLR parse tree, but the parse fails with an InputMismatchException
, which is logged on the console like this:
line 20:0 mismatched input '<EOF>' expecting 'HEAD'
My dummy C# entrypoint is something like the following:
public static class Program
{
public static void Main()
{
var lexer = new ParstLexer(new AntlrInputStream(Examples.ExampleResources.DUMMY));
var tokens = new CommonTokenStream(lexer);
var parser = new ParstParser(tokens);
var parseTree = parser.report();
var modelBuilder = new ModelBuilder();
modelBuilder.AddInput(new InputBlock(
parseTree.input().vars().squote_string().GetText().Trim('\''),
Examples.ExampleResources.PSTARKIV522));
modelBuilder.AddLines(parseTree.lines());
modelBuilder.AddFields(parseTree.fields());
modelBuilder.AddMod(parseTree.mod());
modelBuilder.AddHead(parser.head());
modelBuilder.AddBody(parser.body());
modelBuilder.AddFoot(parser.foot());
var model = modelBuilder.GetModel();
Console.WriteLine(JsonConvert.SerializeObject(model));
}
}
I can't figure out why I'm experiencing this behavior.
I actually searched about this error and I found many people having this kind of error for many different problems; I tried playing around with the EOF token (e.g. not explicitly declaring it, or moving it in a wrapper rule around report
), but with no results.
The fact that Java-based tools like grun
or the Rider plugin don't complain unlike my C# code does makes me think that the problem can be contestualized in the C# target or in my own Main()
, but I can't figure out where.
Upvotes: 0
Views: 614
Reputation: 305
The problem was related to a duplicated parser call: for a mistake in referencing the right object in my C# code, I was calling the parser again instead of the tree generated by the first parser run.
The parser started again from the head
parser rule (because that was the one I was requesting with the head()
method), but the input was already been consumed by the first invocation of the parser, so the parser was looking for the HEAD
token which opens head
and it was instead finding the end of the input EOF
.
Upvotes: 0