首页 > 学院 > 开发设计 > 正文

Mini Java编译器(二)——语法(BNF)

2019-11-18 14:23:51
字体:
来源:转载
供稿:网友

  二、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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表