Antlr Lexer Parse error

114 views Asked by At

Antlr version: antlr-dotnet-tool-3.5.0.2

TestGrammar.g

lexer grammar TestGrammar;

options
{
    language=CSharp3;   
    backtrack=true; 
}

DOT
    :   '.'
    ;

NUMBER
    :   ( '0'..'9' )+ ('.' ( '0'..'9' )+)?
    ;

WS  
    : (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=Hidden;}
    ;

C# code:

        var lexer = new TestGrammar(new ANTLRStringStream("2..3"));
        while (true)
        {
            var token = lexer.NextToken();
            Console.WriteLine(token);

            if (token.Type == -1)
                break;
        }

Result:

[@-1,3:3='3',<5>,1:3]
[@-1,4:4='<EOF>',<-1>,1:4]

So, I test this grammar with input

2..3

I expect that the result will be the following:

NUMBER["2"] DOT["."] DOT["."] NUMBER["3"]

So, what am I doing wrong? Thank you!

1

There are 1 answers

0
Bart Kiers On

Testing with the Java target:

TestGrammar lexer = new TestGrammar(new ANTLRInputStream("2..3"));

for (Token t : lexer.getAllTokens()) {
  System.out.printf("%s -> %s\n", t.getText(), TestGrammar.VOCABULARY.getSymbolicName(t.getType()));
}

produces the following output:

2 -> NUMBER
. -> DOT
. -> DOT
3 -> NUMBER

So, the grammar is correct. I doubt that the C# runtime would produce anything other than what I posted (with the C# equivalent test class). If it still doesn't work, please edit your question and add some code that demonstrates how to reproduce the error(s) you get.