·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » Mini Java编译器(二)??语法(BNF)

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

类别: JAVA教程  评论数:0 总得分:0
二、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 ")"



(待续)



-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1