0
votes

I'm trying to wtire sort of CSharp grammar in ANTLR4. Here is the grammar: http://pastie.org/9197255

Here is the test class:

public class Test
{
   public static void Main()
   {
      System.Console.WriteLine("Hello, World!");
   }
}

And here is the error: line 1:0 mismatched input 'public' expecting ACCESS_MODIFIER

What's the problem?

1

1 Answers

1
votes

The input public is matched by both the lexer rules IDENTIFIER and ACCESS_MODIFIER. ANTLR resolves such an ambiguity by choosing the first one declared in the grammar. In other words, for your grammar, the input public will always be an IDENTIFIER and never be an ACCESS_MODIFIER.

You can resolve this by moving the IDENTIFIER rule after the rules for other keywords.