三、属性翻译文法
主类声明(MainClass)
| <MainClass> | ::= | “class” <Identifier>↑<id_name1> |
| @fillMemory↓<id_name1>↑<id_address> | ||
| @fillTable↓<id_name1>,<id_address> | ||
| @fillTree↓<id_name1> | ||
| “{” “public” “static” “void” “main” “(” “String” “[” “]” | ||
| <Identifier>↑<id_name2>,<id_belongsTo>=<id_name1>,<id_type>=StringArrayType | ||
| @fillTable↓<id_name2>,<id_belongsTo> | ||
| “{” <PrintStatement> “}” “}” |
类声明(ClassDeclaration)
| <ClassDeclaration> | ::= | “class” <Identifier>↑<id_name> |
| @checkTable↓<id_name> | ||
| @fillMemory↓<id_name1>↑<id_address> | ||
| @fillTable↓<id_name>,<id_address> | ||
| @fillTree↓<node_name>=<id_name> | ||
| “{” ( VarDeclaration )* ( MethodDeclaration )* “}” |
类继承声明(ClassExtendsDeclaration)
| <ClassExtendsDeclaration> | ::= | “class” <Identifier>↑<id_name1> |
| @checkTable↓<id_name1> | ||
| “extends” <Identifier>↑<id_name2> | ||
| @checkTree↓<id_name2> | ||
| @fillMemory↓<id_name1>↑<id_address> | ||
| @fillTable↓<id_name1>,<id_name2>,<id_address> | ||
| @fillTree↓<child_name>=<id_name1>,<parent_name>=<id_name2> | ||
| “{” ( VarDeclaration )* ( MethodDeclaration )* “}” |
变量声明(VarDeclaration)
| <VarDeclaration> | ::= | <Type>↑<id_type> |
| <Idetifier>↑<id_name>,<id_belongsTo> “;” | ||
| @checkTable↓<id_name>,<id_belongsTo> | ||
| @fillMemory↓<id_name>↑<id_address> | ||
| @fillTable↓<id_name>,<id_type>,<id_belongsTo>,<address> | ||
| <Type>↑<id_type> | ::= | <ArrayType>↑<id_type>=ArrayType |
| |<BooleanType>↑<id_type>=BooleanType | ||
| |<IntegerType>↑<id_type>=IntegerType |
方法声明(MethodDeclaration)
| <MethodDeclaration> | ::= | “public” <Type>↑<id_type> |
| <Identifier>↑<id_name>,<id_belongsTo> | ||
| @checkTable↓<id_name>,<id_belongsTo> | ||
| @fillMemory↓<id_name>↑<id_address> | ||
| @fillTable↓<id_name>,<id_type>,<id_belongsTo>,<id_address> | ||
| “(” ( <FormalParameterList> )? “)” | ||
| “{” ( <VarDeclaration> )* ( <Statement> )* “return” | ||
| <Expression>↑<exp_type>,<exp_value> | ||
| @checkType↓<id_type>,<exp_type> “;” “}” | ||
| <Type>↑<id_type> | ::= | <ArrayType>↑<id_type>=ArrayType |
| |<BooleanType>↑<id_type>=BooleanType | ||
| |<IntegerType>↑<id_type>=IntegerType | ||
| <FormalParameterList> | ::= | <FormalParameter> |
| ( <FormalParameterRest> )* | ||
| <FormalParameter> | ::= | <Type>↑<id_type> |
| <Identifier>↑<id_name>,<id_belongsTo> | ||
| @checkTable↓<id_name>,<id_belongsTo> | ||
| @fillTable↓<id_name>,<id_type> | ||
| <FormalParameterRest> | ::= | “,” <FormalParameter> |
赋值语句(AssignmentStatement)
| <AssignmentStatement> | ::= | <Identifier>↑id_name@checkTable↓<id_name>↑<id_type> |
| “=” <Expression>↑<exp_type>,<exp_value> “;” | ||
| @checkType↓<id_type>,<exp_type> | ||
| @generateCode↓<stmt_type>=stmt_assign,<extra_para> |
数组元素赋值语句(ArrayAssignmentStatement)
| <ArrayAssignmentStatement> | ::= | <Identifier>↑<id_name>@checkTable↓<id_name>↑<id_type> |
| @checkType↓<id_type>,ArrayType | ||
| “[” <Expression>↑<exp_type1>,<exp_value1> | ||
| @checkType↓<exp_type1>,IntegerType | ||
| “]” “=” <Expression>↑<exp_type2>,<exp_value2> | ||
| @checkType↓<exp_type2>,IntegerType “;” | ||
| @generateCode↓<stmt_type>=stmt_array_assign,<extra_para> |
if语句(IfStatement)
| <IfStatement> | ::= | “if” “(” <Expression>↑<exp_type>,<exp_value> |
| @checkType↓<exp_type>,BooleanType | ||
| “)” <Statement> “else” <Statement> | ||
| @generateCode↓<stmt_type>=stmt_if,<extra_para> |
while语句(WhileStatemen)
| <WhileStatement> | ::= | “while” “(” <Expression>↑<exp_type>,<exp_value> |
| @checkType↓<exp_type>,BooleanType | ||
| “)” <Statement> | ||
| @generateCode↓<stmt_type>=stmt_while,<extra_para> |
打印语句(PrintStatement)
| <PrintStatement> | ::= | “System.out.println” “(“ |
| <Expression>↑<exp_type>,<exp_value> | ||
| @checkType↓<exp_type>,IntegerType | ||
| “)” “;” | ||
| @generateCode↓<stmt_type>=stmt_print,<extra_para> |
属性及动作解释
动作
| 动作 | 作用 |
|---|---|
| @checkTable↓<id_name>,<id_level> | 检查标识符的合法性(遍历符号表) |
| @checkTable↓<class_name> | 检查类名是否已声明 |
| @checkTable↓<id_name>↑<id_type> | 检查变量是否已声明,若已声明返回变量类型 |
| @checkTable↓<id_type> | 检查类型是否已声明 |
| @fillTable↓<id_name1>,<id_address> | 将标识符插入符号表 |
| @fillTable↓<child_name>,<parent_name>,<id_address> | 将子类插入符号表,同时复制父类的方法和类变量至子类 |
| @fillTable↓<id_name>,<id_belongsTo> | 将标识符插入符号表 |
| @fillTable↓<id_name>,<id_sort>,<id_level>,<id_type> | 将标识符插入符号表 |
| @fillMemory↓<id_name>↑<id_address> | 登记内存,返回地址 |
| @generateCode↓<stmt_type>,<extra_para> | 生成伪代码 |
| @checkType↓<child_name>,<parent_name> | 检查类型合法性(遍历继承树、符号表) |
| @checkType↓<id_type>,<exp_type> | 检查变量类型与表达式类型是否匹配 |
| @checkTree↓<child_name>,<parent_name> | 检查child_name是否是parent_name的子类(遍历继承树) |
| @checkTree↓<class_name> | 检查child_name是否已存在于继承树内 |
| @fillTree↓<child_name>,<parent_name> | 将child_name插入继承树中parent_name下 |
| @fillTree↓<node_name> | 将node_name插入继承树中(父类为Object) |
属性
| 属性 | 数据类型(针对Java语言) | 含义 |
|---|---|---|
| id_name | String | 标识符名称 |
| id_level | int | 标识符的层次结构 |
| id_belongsTo | String | 所属层次名称,(见附表) |
| id_address | int | 内存地址 |
| stmt_type | int | 操作类型(见附表) |
| class_name | String | 类名 |
| extra_para | Object | 附加参数 |
| child_name | String | 子类名称 |
| parent_name | String | 父类名称 |
| class_name | String | 类名 |
| id_type | compiler.type.Type | 变量类型类型(见附表) |
| para_list | java.util.List | 参数表 |
| exp_value | int或boolean或任何程序中已定义的对象类型 | 表达式值 |
| exp_type | compiler.type.Type | 表达式类型 |
符号表结构
| 名称 | 数据类型(针对Java语言) | 含义 |
|---|---|---|
| name | String | 标识符名称 |
| sort | String | 标识符类别 |
| type | compiler.type.Type | 变量类型 |
| belongsTo | String | 归属信息(见附表) |
| address | int | 存储地址 |
内存分配表结构
| 名称 | 数据类型(针对Java语言) | 含义 |
|---|---|---|
| address | int | 标识符地址 |
| sort | String | 标识符类别 |
| name | String | 标识符名称 |
| value | Object | 标识符值 |
附表
stmt_type
| 名称 | 值 | 含义 |
|---|---|---|
| stmt_assign | 1 | 赋值语句 |
| stmt_array_assign | 2 | 数组元素赋值语句 |
| stmt_if | 3 | if语句 |
| stmt_while | 4 | while语句 |
| stmt_print | 5 | 打印语句 |
id_type
| 类 | 含义 | size(字长(bytes)) |
|---|---|---|
| ArrayType | 整型数组类型 | 4 |
| BooleanType | 布尔类型 | 1 |
| IntegerType | 整型类型 | 4 |
| ObjectType | 对象类型 | 不确定 |
| NonVariableType | 非变量 | 0 |
id_belongsTo
| 标识符类型 | “belongsTo”属性内容 |
|---|---|
| 类 | 无(分析时以文件为单位) |
| 方法 | 类名 |
| 类变量 | 类名 |
| 方法内变量 | 方法名 |
| 参数 | 方法名 |
-- EOF --
除非注明(如“转载”、“[zz]”等),本博文章皆为原创内容,转载时请注明: 「转载自程序员的信仰©」
本文链接地址:Mini Java编译器(三)
Today on history:
【2012】2012年政府工作报告 脱水版
【2009】如何让Netbeans以英文模式启动
【2006】程序员是如何捕猎大象的 [zz]