我们的语法似乎有些含糊不清。但我们真的不知道如何删除它们。我们真的需要有人一步一步地教我们。例如,在我们的语法中,我们如何消除“这个”,“公共”,“私人”的歧义......
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
;