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