metacircle
metacircle

Reputation: 2568

ANTLR Grammar Issue

I have a problem with an ANTLR Grammar and I just don't see my problem, been trying to figure it out for over an hour now. I stripped my problem down to this simple grammar:

        grammar TestGrammar;

        options {
          language = Java;
          k=2;
        }

        compu_method :  '/begin COMPU_METHOD'  NAME  NAME  NAME  NAME
              (
              (formula)
              |('COEFFS' realnumber realnumber realnumber realnumber realnumber realnumber)
              )*
              '/end COMPU_METHOD';

            formula : '/begin FORMULA' (.)* '/end FORMULA';

            realnumber: (INT | FLOAT);

        NAME : LETTER (LETTER|'0'..'9'|'['|']'|'.')* ; 
        fragment LETTER : 'A'..'Z' | 'a'..'z' | '_' ; 

        INT : MINUS? ('0' | '1'..'9' '0'..'9'*) ; 
        FLOAT : MINUS? ('0'..'9')+ '.' ('0'..'9')* Exponent? | MINUS? '.' ('0'..'9')+ Exponent? | MINUS? ('0'..'9')+ Exponent ; 
        MINUS : '-' ; 
        fragment Exponent : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;

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

And the input I am trying to match is:

/begin COMPU_METHOD
        foo
        foo
        foo
        foo
        COEFFS 0.000000 1.000000 0.000000 0.000000 0.000000 1.000000
/end COMPU_METHOD

The Interpreter always gives me "mismatched input 'COEFFS' expecting '\u0005'"

But why?

If I change the order of (formula) and ('COEFFS' ...) in my grammer it's fine, I just don't get why?

Upvotes: 1

Views: 252

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170138

After fixing the errors (there's an invalid N token in compu_method, and you forgot a semi colon after the formula rule), I have no problem parsing the input. I get the following parse tree:

enter image description here

Note that I used the debugger, and not the interpreter (which is buggy). So your problem is probably that you used the interpreter.

Upvotes: 1

Related Questions