0

当我运行输入

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

在我的语法上:

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)*
    ;

我从输入的字符中得到不匹配的令牌异常3,预期\u000F。当我从输入中删除行时integer x;,一切正常。知道这\u000F意味着什么以及为什么会这样吗?

我尝试重写每条规则,没有任何帮助。

IDENT 只能是字母,INTEGER 只能是数字。表达式是整数和它们之间的“+”或“-”。

谢谢。

4

1 回答 1

1

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.

于 2013-06-19T19:40:06.233 回答