I have an existing grammar I'm converting to Antlr4 using the C# output. It's failing on the first token with the message mismatched input 'Begin' expecting ANY_WORD. Strangely though when I print out the tokens to see what token it is being recognised as, it indicates it has identified it correctly.
string text = File.ReadAllText(fi.FullName);
var fs = new Antlr4.Runtime.AntlrInputStream(text);
var lexer = new TestLexer(fs);
var tokens = new Antlr4.Runtime.CommonTokenStream(lexer);
tokens.Fill();
var tokenList = tokens.GetTokens();
yRLogger.Info("Printing tokens");
foreach(IToken tok in tokenList)
{
Logger.Info(tok.ToString()); //prints [@0,13:17='Begin',<28>,1:13] 28 In TestLexer.tokens, 28 corresponds to ANY_WORD
}
var parser = new TestParser(tokens);
parser.start(); //throws 'mismatched input 'Begin' expecting ANY_WORD. '
Checking the same grammar and input in AntlrWorks also behaves as expected - it finds the first token without an error.
From TestParser.tokens ANY_WORD=28
.
The input file is
Begin parsing now
The Antlr file is
start : ANY_WORD+;
ANY_WORD
:
~('\r'|'\n'|' '|'\t'|'\u000C')+
;
WS : (' '|'\t'|'\u000C') -> skip
;