0
votes

I'm learning antlr. I've gotten it working with Java but can't get anything to work in C#. I've created the simplest case I can find using an example from The Definitive Antlr Book and I'm still getting problems.

Here is my code and my grammar. I've hardcoded "hello parr" as the input and get

line 1:6 mismatched input 'parr' expecting ID

PreParser.g4

grammar PreParser; 
import PreParseLex;

r: 'hello' ID;

PreParserLex.g4

lexer grammar PreParseLex;

ID: [a-z]+;
WS: [ \t\r\n]+ -> skip ;

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Tree;

namespace PreparseApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string expression = "hello parr";
            AntlrInputStream input = new AntlrInputStream(expression);

            PreParseLex lexer = new PreParseLex(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            PreParserParser parser = new PreParserParser(tokens);
            IParseTree tree = parser.r();

            Console.WriteLine("Done");
        }
    }
}

I don't understand why 'parr' doesn't ID. Ideas?

1
If you have something working in the Java version that doesn't work in the C# version, you probably want to post a bug report on the C# project's issue tracker: github.com/tunnelvisionlabs/antlr4cs/issuesSam Harwell
I've created an issue.Gary

1 Answers

0
votes

Sam Harwell answered this question. I used the wrong class for the lexer in C#. I needed to use PreParseLexer rather than PreParseLex. See his full response.