二、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 “)” |
-- EOF --
除非注明(如“转载”、“[zz]”等),本博文章皆为原创内容,转载时请注明: 「转载自程序员的信仰©」
本文链接地址:Mini Java编译器(二)
Today on history:
【2012】2012年政府工作报告 脱水版
【2009】如何让Netbeans以英文模式启动
【2006】程序员是如何捕猎大象的 [zz]
发表回复