Mini Java编译器(三)
2005-3-23 7:13:17 作者:模板天下收集整理 来源:未知 网友评论 0 条
论坛
三、属性翻译文法
主类声明(MainClass)
::= "class" ↑
@fillMemory↓↑
@fillTable↓,
@fillTree↓
"{" "public" "static" "void" "main" "(" "String" "[" "]"
↑,=,=StringArrayType
@fillTable↓,
"{" "}" "}"
类声明(ClassDeclaration)
::= "class" ↑
@checkTable↓
@fillMemory↓↑
@fillTable↓,
@fillTree↓=
"{" ( VarDeclaration )* ( MethodDeclaration )* "}"
类继承声明(ClassExtendsDeclaration)
::= "class" ↑
@checkTable↓
"extends" ↑
@checkTree↓
@fillMemory↓↑
@fillTable↓,,
@fillTree↓=,=
"{" ( VarDeclaration )* ( MethodDeclaration )* "}"
变量声明(VarDeclaration)
::= ↑
↑, “;”
@checkTable↓,
@fillMemory↓↑
@fillTable↓,,,
↑ ::= ↑=ArrayType
|↑=BooleanType
|↑=IntegerType
::= ↑=ObjectType
@checkTable↓
↑, “;”
@checkTable↓,
@fillTable↓,,
方法声明(MethodDeclaration)
::= "public" ↑
↑,
@checkTable↓,
@fillMemory↓↑
@fillTable↓,,,
"(" ( )? ")"
"{" ( )* ( )* "return"
↑,
@checkType↓, ";" "}"
↑ ::= ↑=ArrayType
|↑=BooleanType
|↑=IntegerType
::=
( )*
::= ↑
↑,
@checkTable↓,
@fillTable↓,
::= "public" ↑=ObjectType
@checkTable↓
↑,
@checkTable↓,
@fillMemory↓↑
@fillTable↓,,,
"(" ( )? ")"
"{" ( )* ( )* "return"
↑,
@checkType↓, ";" "}"
↑ ::= ↑=ArrayType
|↑=BooleanType
|↑=IntegerType
::=
( )*
::= ↑=ObjectType
@checkTable↓
↑,
@checkTable↓,
@fillMemory↓↑
@fillTable↓,,
::= ","
赋值语句(AssignmentStatement)
::= ↑id_name @checkTable↓↑
“=” ↑, “;”
@checkType↓,
@generateCode↓=stmt_assign,
数组元素赋值语句(ArrayAssignmentStatement)
::= ↑ @checkTable↓↑
@checkType↓,ArrayType
"[" ↑,
@checkType↓,IntegerType
"]" "=" ↑,
@checkType↓,IntegerType ";"
@generateCode↓=stmt_array_assign,
if语句(IfStatement)
:= "if" "(" ↑,
@checkType↓,BooleanType
")" "else"
@generateCode↓=stmt_if,
while语句(WhileStatemen)
::= "while" "(" ↑,
@checkType↓,BooleanType
")"
@generateCode↓=stmt_while,
打印语句(PrintStatement)
::= "System.out.println" "("
↑,
@checkType↓,IntegerType
")" ";"
@generateCode↓=stmt_print,
属性及动作解释
动作
动作
作用
@checkTable↓,
检查标识符的合法性(遍历符号表)
@checkTable↓
检查类名是否已声明
@checkTable↓↑
检查变量是否已声明,若已声明返回变量类型
@checkTable↓
检查类型是否已声明
@fillTable↓,
将标识符插入符号表
@fillTable↓,,
将子类插入符号表,同时复制父类的方法和类变量至子类
@fillTable↓,
将标识符插入符号表
@fillTable↓,,,
将标识符插入符号表
@fillMemory↓↑
登记内存,返回地址
@generateCode↓,
生成伪代码
@checkType↓,
检查类型合法性(遍历继承树、符号表)
@checkType↓,
检查变量类型与表达式类型是否匹配
@checkTree↓,
检查child_name是否是parent_name的子类(遍历继承树)
@checkTree↓
检查child_name是否已存在于继承树内
@fillTree↓,
将child_name插入继承树中parent_name下
@fillTree↓