简体   繁体   English

ANTLR 4中的EOF不匹配

[英]Mismatched EOF in ANTLR 4

I'm using ANTLR 4 (4.5.1) to create a parser and then do a semantic validation but I get EOF error when I test the parser. 我正在使用ANTLR 4(4.5.1)创建解析器,然后进行语义验证,但是在测试解析器时出现EOF错误。

This is the error 这是错误

linea 8:0 at [@10,49:48='<EOF>',<-1>,8:0]: mismatched input '<EOF>' expecting 'class'

And this is my input file. 这是我的输入文件。

class Program {

     int z;
    int c;
}

The workaround I'm using to fix this error is to repeat the whole input file inside like this: I don't understand why the following works, maybe OSX is the problem. 我用来解决此错误的解决方法是在整个内部重复这样的输入文件:我不明白为什么以下代码有效,也许OSX是问题所在。

class Program {

         int z;
        int c;
    }
class Program {

         int z;
        int c;
    }

I don't want to do this anymore, does anyone knows what is happening? 我不想再这样做了,有人知道发生了什么吗?

This is my grammar 这是我的语法

grammar program;

//*********************LEXER SPECIFICATION **************
STRUCT :  'struct' ;
TRUE :    'true' ;
FALSE :   'false' ;
VOID :    'void' ;
IF :      'if' ;
ELSE :    'else' ;
WHILE :   'while' ;
RETURN :  'return' ;
INT :     'int' ;
CHAR :    'char' ;
BOOLEAN : 'boolean' ;

fragment LETTER : ('a'..'z'|'A'..'Z') ;
fragment DIGIT :'0'..'9' ;
fragment ASCII : (' ' ..'~') | '\\' | '\'' | '\"' | '\t' | '\n' ;
//* \'
ID : LETTER ( LETTER | DIGIT )* ;
NUM : DIGIT ( DIGIT )* ;
Char : '\'' ASCII '\'';


WS : 
    [\t\r\n\f ]+ -> skip
    ;

COMMENT
    :   ( '//' ~[\r\n]* '\r'? '\n'  
        | '/*' .*? '*/'         
        ) -> skip
    ;                               

//************** PARSER SPECIFICATION **************

program
    : 'class' 'Program' '{' (declaration)* '}' 
    ;

declaration
    :   structDeclaration   #declarationStructDeclaration
    |   varDeclaration      #declarationVarDeclaration
    |   methodDeclaration   #declarationMethodDeclaration
    ;
varDeclaration
    :   varType ID ';'              #varDeclarationID
    |   varType ID '[' NUM ']' ';'  #varDeclarationArray
    ;

varDeclarationStruct
    :   varType ID ';'          #varDeclarationStructID
    |   varType ID '[' NUM ']' ';'  #varDeclarationStructArray
    ;

structDeclaration
    :   STRUCT ID '{' (varDeclarationStruct)* '}'
    ;

varType                     #varType
    :   INT                 #varTypeInt
    |   CHAR                #varTypeChar
    |   BOOLEAN             #varTypeBoolean
    |   STRUCT ID           #varTypeStruct
    |   structDeclaration   #varTypeStructDeclaration
    |   VOID                #varTypeVoid
    ;

methodDeclaration
    :   methodType ID '(' (params | ) ')' block
    ;


params
    :   params ',' parameter    #paramsParameterWithComma
    |   parameter       #paramsParameter
    ;

methodType
    :   INT             #methodTypeInt
    |   CHAR                #methodTypeChar
    |   BOOLEAN             #methodTypeBoolean
    |   VOID                #methodTypeVoid
    ;

parameter
    :   parameterType ID        #parameterID
    |   parameterType ID '[' NUM ']'    #parameterArray
    ;

parameterType
    :   INT                 #parameterTypeInt
    |   CHAR                #parameterTypeChar
    |   BOOLEAN             #parameterTypeBoolean
    ;


block
    :   '{' (varDeclaration | statement)* '}'
    ;

statement
    :   IF '(' expression ')' block (ELSE block)?   #statementIF
    |   WHILE '(' expression ')' block          #statementWhile
    |   'return' (expression | ) ';'            #statementReturn
    |   methodCall ';'                  #statementMethodCall
    |   block                       #statementBlock             
    |   location '=' expression ';'         #statementLocation
    |   (expression)?';'                #statementExpression
    ;

location
    :   (ID | ID '[' expression ']') ('.' locationMember)?
    ;

locationMember
    :   (ID | ID '[' expression ']')('.' locationMember)?
    ;


expression 
    :   andExpr             #expressionAndExpr
    |   expression cond_op_or andExpr   #expressionCondOpOr
    ;

andExpr
    :   eqExpr              #andExprEqExpr 
    |   andExpr cond_op_and eqExpr  #andExprCondOpAnd
    ;

eqExpr
    :   relationExpr            #eqExprRelationExpr
    |   eqExpr eq_op relationExpr   #eqExprEqOp
    ;

relationExpr
    :   addExpr             #relationExprAddExpr
    |   relationExpr rel_op addExpr     #relationExprRelOp
    ;


addExpr
    :   multExpr            #addExprMultExpr
    |   addExpr minusplus_op multExpr   #addExprMinusPlusOp
    ;

multExpr
    :   unaryExpr           #multExprUnary
    |   multExpr multdiv_op unaryExpr   #multExprMultDivOp
    ;


unaryExpr
    :   '('(INT|CHAR)')'  value     #unaryExprCast
    |   '-' value           #unaryExprMinus
    |   '!' value           #unaryExprNot
    |   value               #unaryExprOther
    ;

value
    :   location            #valueLocation
    |   methodCall          #valueMethodCall
    |   literal             #valueLiteral
    |   '(' expression ')'      #valueExprWithParent
    ;



methodCall
    :   ID '(' (arg (',' arg)*)? ')' 
    ;

arg
    :   expression
    ;


minusplus_op    
    :   '+'
    |   '-'
    ;

multdiv_op
    :   '*'
    |   '/'
    |   '%'
    ;

powmod_op
    :   '%'
    ;

rel_op
    :   '<'
    |   '>'
    |   '<='
    |   '>='
    ;

eq_op
    :   '=='
    |   '!='
    ;

cond_op_or : '||';
cond_op_and: '&&';

literal
    :   int_literal
    |   char_literal
    |   bool_literal
    ;

int_literal
    :   NUM
    ;

char_literal
    :   Char 
    ;

bool_literal
    :   'true'
    |   'false'
    ;

EDIT########### 编辑###########

I'm using the netbeans plugin ANTLR works and it is compiling the grammar with the 4.4 version and I'm using the jar 4.5.1, so I only get a warning. 我正在使用netbeans插件ANTLR,并且正在使用4.4版本编译语法,而我正在使用jar 4.5.1,所以我只得到警告。 I tried to compile it with the 4.5.1 version and now the error I'm receiving is this: 我尝试使用4.5.1版本进行编译,现在收到的错误是:

mismatched input 'class' expecting <EOF>

This happens when I duplicate the input. 当我重复输入时,会发生这种情况。 If I don't duplicate it, I receive the first error of this question. 如果不重复,则会收到该问题的第一个错误。

mismatched input '<EOF>' expecting 'class'

问题是我两次调用了visitor.program(),所以我需要将输入加倍。

I had the same problem with my grammar and I fixed it with using a "wrapper" around it... 我的语法也有同样的问题,我用一个“包装纸”解决了这个问题。

wrapper
  : programm EOF
  ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM