爱心技术专栏专题

Java编译器(二)

摘录:java 来源:java 加入时间:2006年08月19日
摘要:
Java编译器(二)

二、Mini Java的文法
BNF
Goal




::=




MainClass ( TypeDeclaration )* <EOF>





MainClass




::=
转载:转载请保留本信息,本文来自
http://www.51dibs.com
/html/2006/article/info16/a_f4a00959d7a68f84.htm

Java编译器(二)

站点:爱心种子小博士 关键字:Java编译器(二)

   
Java编译器(二)
二、Mini Java的文法
BNF
Goal




::=




MainClass ( TypeDeclaration )* <EOF>





MainClass




::=




"class" Identifier "{" "public" "static" "void" "main" "(" "String" "[" "]" Identifier ")" "{" PrintStatement "}" "}"





TypeDeclaration




::=




ClassDeclaration











|




ClassExtendsDeclaration





ClassDeclaration




::=




"class" Identifier "{" ( VarDeclaration )* ( MethodDeclaration )* "}"





ClassExtendsDeclaration




::=




"class" Identifier "extends" Identifier "{" ( VarDeclaration )* ( MethodDeclaration )* "}"





VarDeclaration




::=




Type Identifier ";"





MethodDeclaration




::=




"public" Type Identifier "(" ( FormalParameterList )? ")" "{" ( VarDeclaration )* ( Statement )* "return" Expression ";" "}"





FormalParameterList




::=




FormalParameter ( FormalParameterRest )*





FormalParameter




::=




Type Identifier





FormalParameterRest




::=




"," FormalParameter





Type




::=




ArrayType











|




BooleanType











|




IntegerType











|




Identifier





ArrayType




::=




"int" "[" "]"





BooleanType




::=




"boolean"





IntegerType




::=




"int"





Statement




::=




Block











|




AssignmentStatement











|




ArrayAssignmentStatement











|




IfStatement











|




WhileStatement











|




PrintStatement





Block




::=




"{" ( Statement )* "}"





AssignmentStatement




::=




Identifier "=" Expression ";"





ArrayAssignmentStatement




::=




Identifier "[" Expression "]" "=" Expression ";"





IfStatement




::=




"if" "(" Expression ")" Statement "else" Statement





WhileStatement




::=




"while" "(" Expression ")" Statement





PrintStatement




::=




"System.out.println" "(" Expression ")" ";"





Expression




::=




AndExpression











|




CompareExpression











|




PlusExpression











|




MinusExpression











|




TimesExpression











|




ArrayLookup











|




ArrayLength











|




MessageSend











|




PrimaryExpression





AndExpression




::=




PrimaryExpression "&&" PrimaryExpression





CompareExpression




::=




PrimaryExpression "<" PrimaryExpression





PlusExpression




::=




PrimaryExpression "+" PrimaryExpression





MinusExpression




::=




PrimaryExpression "-" PrimaryExpression





TimesExpression




::=




PrimaryExpression "*" PrimaryExpression





ArrayLookup




::=




PrimaryExpression "[" PrimaryExpression "]"





ArrayLength




::=




PrimaryExpression "." "length"





MessageSend




::=




PrimaryExpression "." Identifier "(" ( ExpressionList )? ")"





ExpressionList




::=




Expression ( ExpressionRest )*





ExpressionRest




::=




"," Expression





PrimaryExpression




::=




IntegerLiteral











|




TrueLiteral











|




FalseLiteral











|




Identifier











|




ThisExpression











|




ArrayAllocationExpression











|




AllocationExpression











|




NotExpression











|




BracketExpression





IntegerLiteral




::=




<INTEGER_LITERAL>





TrueLiteral




::=




"true"





FalseLiteral




::=




"false"





Identifier




::=




<IDENTIFIER>





ThisExpression




::=




"this"





ArrayAllocationExpression




::=




"new" "int" "[" Expression "]"





AllocationExpression




::=




"new" Identifier "(" ")"





NotExpression




::=




"!" Expression





BracketExpression




::=




"(" Expression ")"







客户服务中心信箱:[email protected] [email protected] 网站地图

声明

合作伙伴: