Miloš Lukačka
Miloš Lukačka

Reputation: 842

ANTLR, missmatched token exception, expected \u000F

when I run input

integer function () :
2+2+2;
integer x;
3-1-2;
end.

on my grammar:

evaluator
   :    type 'function' '(' ')' ':'
        (expression ';' | declaration ';')*
        'end' '.'
        EOF
   ;


declaration
    :   type IDENT (':=' expression)? ';'
    ;

type
    :   'integer'
    |   'double'
    |   'boolean'
    |   'void'
    ;

term
    :   IDENT
    |   '(' expression ')'
    |   INTEGER
    ;

unary
    :   ('+' | '-')* term
    ;

mult
    :   unary (('*' | '/' | 'mod') unary)*
    ;

expression
    :   mult (('+' | '-') mult)*
    ;

I get missmatched token exception on character 3 from the input, expected \u000F. When i erase line integer x; from the input, everything works fine. Any idea what \u000F means and why it is expected?

I tried rewriting every rule, nothing helps.

IDENT can be only letters, INTEGER only digits. expression is integers and '+' or '-' between them.

Thanks.

Upvotes: 0

Views: 595

Answers (1)

chollida
chollida

Reputation: 7894

Two things to check.

I think I see the problem. Your declaration rule eats the ; but your main rule (expression ';' | declaration ';')* also expects to get the semi colon. Remove the ; from your declaration rule.

ie rewrite your declaration rule to be:

declaration
    :   type IDENT (':=' expression)?  <- don't parse the semi colon here
    ;

One check the source of the file. Unicode character \u000F is SHIFT IN which isn't that common but can be used in irc environments to remove formatting. See this link

I often use a hex editor for verifying the file format is actually what I think it is. A text file can never lie to a hex editor:)

If you want to verify in aonther file you can usually create it by typeing ALT 15 at some other point in your file to see if ANTLR gives you the same error where you put the control character.

If its not the file encoding then I'd really go over your expression rule and lexer grammar to make sure there is nothing wrong with them. Because you didn't post them we can't help verify.

Upvotes: 1

Related Questions