




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、-1-阶段四 语义分析-2-项目需求遍历语法分析的输出结果-语法分析树;构造符号表和信息表;进行语义错误检查,输出语义错误信息;把函数标识符和语句中的变量在符号表中的地址信息回填到语法树中; 输出符号表。 -3-语法分析器的开发所处位置词法分析器语法分析器 语义分析源程序目标程序Token序列语法树目标代码生成器符号表及回填后的语法树源程序形式上的识别和处理 编译程序最实质性工作 -4-一、编译原理内容-5-一、Semantic ErrorsUse after Declaration( Declaration related )标识符没有声明; 重复声明;如何检查?每当遇到新声明的标识符,查符
2、号表如果当前有效的所有标识符中有相同名字的,则是重复声明错误;否则生成它的属性信息,保存到符号表中;每当遇到标识符的使用,查符号表如果没有找到,说明该标识符没有声明; 否则, 得到该标识符的属性,进行进一步分析;-6-Semantic ErrorsType related errors( Type related )各种条件表达式的类型不是布尔类型;运算符的分量类型不相容;赋值语句左右类型不相容;形实参类型不相容;函数调用参数个数不同;f(.)中f不是函数标识符;下标表达式(数组)不是合法类型;函数说明和函数返回类型不相容;-7-二、Symbol TableIdentifier NameAtt
3、ributes xint, variable, pvoid, function, (int i), .-8-Symbol TableInternal representation of IdentifiersInternal representation of TypesInternal representation of Values-9-Internal representation of IdentifiersDifferent Types of IdentifiersConstant nameType nameVariable nameFunction nameProcedure na
4、meField nametypedef enum constKind, typeKind, varKind, procKind, funcKind, fieldKind idKid;-10-Internal representation of IdentifiersAttributes of Different Types of IdentifiersConstant: (类型,值)Type: (类型)Variable: (类型, 层数,偏移)Function: (返回类型, 形参定义, 代码地址, 空间大小, .)Procedure: (形参定义, 代码地址, 空间大小, .)Field:
5、(类型, 偏移, 所在结构类型)-11-Attributes for Constant IdentifierTypePtrKindValueconstKindTypePtrpoint to the internal representation of the type of the constant;KindThe kind of identifier, here should be constKind;ValueThe value of the constant;-12-Attributes for Type IdentifierTypePtrKindtypeKindTypePtrpoint
6、 to the internal representation of the type;KindThe kind of identifier, here should be typeKind;-13-Attributes for Variable IdentifierTypePtrKindAccessLevelOffsetvarKindTypePtrpoint to the internal representation of the type of the variable;KindThe kind of identifier, here should be varKind;Access:
7、(dir, indir);Level: 层数Offset:偏移量-14-Attributes for Field IdentifierTypePtrKindOffsetPtrfieldKindTypePtrpoint to the internal representation of the type of the field;KindThe kind of identifier, here should be fieldKind;Offset: 该域名针对所在结构类型的偏移量;Ptr: refers to next field;-15-Attributes for Real Proc/Fun
8、c Identifier(实在过程或者函数)TypePtrKindClassLevelParamCode Size ForwardroutKindactualTypePtr: point to the internal representation of the type of func(void for proc);Kind: the kind of identifier, here should be routKind;Class: actual refers to real procedure or function;Level: 层数where the identifier defin
9、ed;Param: refers to a list of information for formal parameters;Code: refers to the start address of the target code for the procedure or function;Size: the size of the space;Forward: true for forward declaration;-16-Attributes for Formal Proc/Func Identifier (形参过程或者函数)TypePtrKindClassLevelParamOffs
10、et routKindformalTypePtr: point to the internal representation of the type of func(void for proc);Kind: the kind of identifier, here should be routKind;Class: formal refers to formal procedure or function;Level: 层数where the identifier defined;Param: refers to a list of information for formal paramet
11、ers;Offset: 在形参列表中的偏移;-17-层数(level)过程/函数的定义可以嵌套主程序的层数为0;偏移量(offset)执行过程/函数的调用时, 需要为其中的变量分配空间;偏移量指的是变量针对其所在过程/函数的空间的首地址的偏移量;Levels and offsets for Variables (including parameters)-18-Levels and offsets for Variables (including parameters)Program A var x, y: integer;Begin End.Procedure P() var k:array
12、1.9 of real; m: integer;Begin End;Procedure R()var x:real;Begin End;Function f(i:integer)var j:integer;Begin End;层数为0层数为1层数为2层数为1x : (0, 0)y : (0, 1)k : (1, 0)m : (1,18)i : (2, 0)j : (2, 1)x : (1, 0)-19-Levels and offsets for Variables (including parameters)typedef struct int number; char name10; ex
13、ample; int i;example p; real x;void p(int i) real x, y; void main() 层数为0层数为1层数为1i : (0, 0)p: (0, 1)x : (0, 12)i : (1, 0)x : (1, 1)y : (1, 3)-20-Internal representation of TypesTypes for Programming LanguagesBasic typeIntegerRealBoolChar Structural typeArrayStructureUnionEnumeration(枚举类型)Pointer-21-I
14、nternal representation of Basic TypesintPtrintSizeintTySize KindboolPtrboolSizeboolTyrealPtrrealSizerealTycharPtrcharSizecharTy-22-Internal representation of Arraysize = sizeof(ElemTyp) (Up-Low+1)ElemTy: pointer to its element type;Size Kind LowUpElemTyarrayTyarray 2.9 of integer;(8, arrayTy, 2, 9,
15、intPtr)int 7;(7, arrayTy, 0, 6, intPtr)-23-Internal representation of StructureSize Kind BodystrutTyBody: 指向结构体中域定义链表Size: 所有域的类型的size的总和;BodyOffsetNexttypedef struct int i; char name10; real x; example;FieldNameFieldType-24-Internal representation of UnionSize Kind BodyunionTyBody: 指向联合体中域定义链表Size:
16、 所有域的类型的size中最大值;BodyFieldTypeNextFieldNametypedef union int i; char name10; real x; test;-25-Internal representation of PointerSize Kind BaseTypeptrTyBaseType: 指向指针的基类型;Size:指针的空间大小(通常为一个单元);-26-Internal representation of EnumerationSize Kind EListenumTyEList: 枚举常量链表;Size:枚举类型值的空间大小(通常为一个单元);-27-In
17、ternal representation of Values可表示的值IntegerRealFalse, true(通常对应0和1)char: ASCII码值枚举类型: 对应整数结构值数组结构联合指针-28-struct symbtablechar idname10;AttributeIR attrIR;struct symbtable * next;SymbTable;符号表的数据结构 -29-typedef struct struct typeIR * idtype;/*指向标识符的类型内部表示*/IdKind kind;/*标识符的类型*/union struct AccessKind
18、 access;int level; int off;VarAttr; /*变量标识符的属性*/structint level;ParamTable * param; /*参数表*/int code;int size;ProcAttr; /*过程名标识符的属性*/More;/*标识符的不同类型有不同的属性*/AttributeIR;typedef IdKind=typeKind, varKind, procKind -30-struct typeIR int size; /*类型所占空间大小*/ TypeKindkind; union struct struct typeIR* indexTy
19、; struct typeIR * elemTy; ArrayAttr; fieldChain * body; /*记录类型中的域链*/ More;TypeIR;typedef Typekind=intTy,charTy ,arrayTy, recordTy, boolTy -31-三、Organization of Symbol TableScope of identifiers (作用域)Localized Symbol Table (符号表的局部化)Globalized Symbol Table (符号表的全局化)Interface Functions for Symbol Table
20、(接口函数)-32- 几乎在编译程序工作的全部过程中,都需要对符号表进行频繁的访问,查表和填表等操作,是编译程序的一笔很大的开销。因此,合理地组织符号表,并相应地选择好查表和填表的方法,是提高编译程序工作效率的重要一环。 -33-Scope of Identifier作用域(scope)An identifier has its scope in a program;A scope for an identifier is a piece of program where it is visible or effective; 一个标识符的作用域是该标识符有效的一段程序, 称为程序的局部化单位
21、, 通常一个程序局部化单位是一个子程序(函数)或者分程序;一个标识符的作用域从声明该标识符的位置开始到其所在的局部化单位的结束(其中要去掉其内部声明的同名标识符的作用域);特别地, 域名的作用域是包含该域名的结构或者联合体;-34-Scope of Identifierint i , j ;void test(int j) real x ; int x ; . void main() char i ; int i ; Question: what about “test”and “main”? -35-Dealing with Symbol Tableint i , j ;void test
22、( int j ) real x ; j int x ; . void main() char i ; int i ; j: (intPtr, varKind, 0, 1, dir)i: (intPtr, varKind, 0, 0, dir)test: (voidPtr, routKind, 0, )j: (intPtr, varKind, 1, 0, dir)x: (realPtr, varKind, 1, 1, dir)x: (intPtr, varKind, 1, ?, dir)main: (voidPtr, routKind, 0, )i: (charPtr, varKind, 1,
23、 0, dir)i: (intPtr, varKind, 1, 1, dir)-36-Dealing with Symbol Table During Semantic AnalysisIdentifier declaration(标识符声明)Check whether declared already by searching symbol table;If yes, error;If no, establish internal representation, insert into symbol table;Identifier usage(标识符使用)Check whether dec
24、lared by searching symbol table;If yes, get its attributes for further semantic analysis;If no, error;Exit from 局部化单位, delete identifiers that are declared in it;-37-Organization of Symbol TableEasy for searching顺序查找折半查找散列表(hash table)Reflect the scope of identifiers, guarantee that each time the ef
25、fective attributes for the identifiers can be found;局部化: 每个局部化单位的符号表作为一个独立的表处理, 即把每个局部化单位的符号表作为建表和查表单位;全局化:把整个程序的符号表统一处理;-38-局部化符号表Scope栈保存当前所有局部化单位符号表的首地址;局部化实现原理:进入局部化单位,建立一个新的空符号表,并将地址压入Scope栈;遇到定义性标识符(声明), 查当前符号表判定是否有重复定义,如果没有则将其属性登记到当前符号表中;遇到使用性标识符,查符号表(从当前符号表查,如果没有,再依次查scope栈中下一个符号表,如果都没有,没有声明
26、错;否则,找到对应的属性);结束一个局部化单位时,删除当前符号表, 弹出scope栈顶元素;每个局部化单位的符号表可以是线性表; 二叉树; 散列表-39-局部化符号表int i , j ;void test ( int j ) real x ; j int x ; . void main() char i ; int i ; Scope栈0-40-全局化符号表全局化实现原理整个程序用一个符号表, 该符号表的组织可以是线性表; 二叉树; 散列表每个局部化单位对应一个唯一的局部化编号num;符号表的表项为(num, id, attributes);初始化: CurrentNum = 0;进入局部化
27、单位, CurrentNum+;遇到定义性标识符(声明),检查所有对应CurrentNum的表项,判定是否有重复定义,如果没有则将其属性及其CurrentNum登记到符号表中;遇到使用性标识符,查符号表,如果都没有,没有声明错;否则,找到对应的属性;结束一个局部化单位时,删除所有CurrentNum对应的表项, CurrentNum -;-41-全局化符号表Search for attributes for identifier XDepend on how to organize the symbol table?The main idea(1)Num = CurrentNum; (2) s
28、earch for (X, Num, attr) item in Symbol table, If does exist return attr;If not exist, Num-; if Num0 return false else goto (2);-42-全部化符号表int i , j ;void test ( int j ) real x ; j int x ; . void main() char i ; int i ; ( 0, id, attributes )Symbol Table-43-Interfaces for Symbol TableCreate symbol tableDestroy symbol tableAppend one item to symbol tableFind entry to an identifierFind attrib
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 酶免法和胶体金法对丙肝抗体的检测结果比较分析
- 循证护理跌倒预防
- “光储一体”绿色营业厅初步设计方案
- 脑梗死术后饮食护理
- 自觉预防校园贷
- 2025年会计职称考试《初级会计实务》错题复盘强化实战试题
- 2025年软件设计师专业考试模拟试卷:移动应用开发技术试题
- 2025年一建《机电工程管理与实务》考试质量控制与验收题库解题技巧
- 2025年专升本艺术概论考试模拟卷(艺术鉴赏能力测评与提升试题)
- 2025年小学语文毕业升学考试全真模拟卷(文学名著阅读)-名著阅读与历史知识
- 应用文写作-第四章公务文书(请示报告)课件
- Premiere-视频剪辑操作-课件
- PDCA降低I类切口感染发生率
- 麻醉药理学阿片类镇痛药PPT
- 新湘版小学科学四年级下册教案(全册)
- 食品生产企业落实主体责任培训
- 药铺微信宣传方案
- 宿舍楼消防火灾应急疏散预案与宿舍消防安全管理制度
- 外研版(一起)英语二年级下册 Module4Unit2 What’s he doing 教案
- 《红楼梦》专题(文化)
- 三级妇幼保健院评审标准实施细则(保健院正确发展方向)
评论
0/150
提交评论