• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

Mini Java编译器(三)

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 16次 | 进入软件测试论坛讨论

领测软件测试网 三、属性翻译文法
主类声明(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”属性内容



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

方法
类名

类变量
类名

方法内变量
方法名

参数
方法名








文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网