Compiler construction 从miniJava语法中消除歧义

Compiler construction 从miniJava语法中消除歧义,compiler-construction,compiler-errors,antlr,ambiguity,ambiguous,Compiler Construction,Compiler Errors,Antlr,Ambiguity,Ambiguous,我们的语法似乎有一些歧义,但我们真的不知道如何消除它们。我们真的需要有人一步一步地教我们。例如,在我们的语法中,如何消除“this”、“public”、“private”的歧义 你能解释一下你语法中的歧义吗? Program: ( ClassDeclaration )* 'eot'; ClassDeclaration: 'class' ID '{' ( FieldDeclaration | MethodDeclaration )* '}' ; FieldDeclaration:

我们的语法似乎有一些歧义,但我们真的不知道如何消除它们。我们真的需要有人一步一步地教我们。例如,在我们的语法中,如何消除“this”、“public”、“private”的歧义


你能解释一下你语法中的歧义吗?
    Program: ( ClassDeclaration )*  'eot';
ClassDeclaration:  'class'   ID '{' ( FieldDeclaration | MethodDeclaration )* '}' ;
FieldDeclaration: Declarators   ID';' ;
MethodDeclaration: Declarators   ID'(' ParameterList? ')' 
'{' Statement* ( 'returnExpression' ';' )? '}' ;
    Declarators : ( 'public'|  'private')?  'static'? Type ;
Type : PrimType | ClassType | ArrType; 
PrimType :  INT|  'boolean'|  'void';
ClassType : ID;
ArrType : ( INT| ClassType ) '[' ']' ;
ParameterList : Type   ID( ',' Type   ID)* ;
ArgumentList : Expression ( ',' Expression )* ;
Reference : ( 'this'|   ID) ( '.'   ID)* ;
Statement : 
'{' Statement* '}'
| Type   ID'=' Expression ';' 
| Reference ( '[' Expression ']' )? '=' Expression ';'
| Reference '(' ArgumentList? ')' ';' 
|  'if''(' Expression ')' Statement ( 'elseStatement' )? 
|  'while''(' Expression ')' Statement ;

Expression : 
Reference ( '['Expression ']')? 
| Reference '('ArgumentList? ')'
|  'unopExpression' 
|   'binopExpression' 
| '('Expression ')'
|  ( ID|FLOAT)|  'true'|  'false'
|  'new'(  ID'('')'|  INT'['Expression ']'|   ID'['Expression ']') ;

ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;

INT :   '0'..'9'+
    ;

FLOAT
    :   ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
    |   '.' ('0'..'9')+ EXPONENT?
    |   ('0'..'9')+ EXPONENT
    ;

COMMENT
    :   '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
    |   '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
    ;
    WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;

STRING
    :  '\'' ( ESC_SEQ | ~('\\'|'\'') )* '\''
    ;

CHAR:  '\'' ( ESC_SEQ | ~('\''|'\\') ) '\''
    ;

fragment
EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;

fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;

fragment
ESC_SEQ
    :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
    |   UNICODE_ESC
    |   OCTAL_ESC
    ;

fragment
OCTAL_ESC
    :   '\\' ('0'..'3') ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7')
    ;

fragment
UNICODE_ESC
    :   '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
    ;