Mini Java编译器(三)

王朝java/jsp·作者佚名  2006-01-31
窄屏简体版  字體: |||超大  

三、属性翻译文法主类声明(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

<VarDeclaration> ::= <Type>↑<id_type>=ObjectType

@checkTable↓<id_type>

<Idetifier>↑<id_name>,<id_belongsTo> “;”

@checkTable↓<id_name>,<id_belongsTo>

@fillTable↓<id_name>,<id_type>,<id_belongsTo>

方法声明(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>

<MethodDeclaration> ::= "public" <Type>↑<id_type>=ObjectType

@checkTable↓<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>=ObjectType

@checkTable↓<id_type>

<Identifier>↑<id_name>,<id_belongsTo>

@checkTable↓<id_name>,<id_belongsTo>

@fillMemory↓<id_name>↑<id_address>

@fillTable↓<id_name>,<id_type>,<id_address>

<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”属性内容

无(分析时以文件为单位)

方法

类名

类变量

类名

方法内变量

方法名

参数

方法名

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航