资源预览内容
第1页 / 共224页
第2页 / 共224页
第3页 / 共224页
第4页 / 共224页
第5页 / 共224页
第6页 / 共224页
第7页 / 共224页
第8页 / 共224页
第9页 / 共224页
第10页 / 共224页
亲,该文档总共224页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
zhangjinghrbeu.edu.cn,1,编译原理与实践,Compiler Principle and Implementation,中英双语版,张菁 著 / 黄维通 Baikunth nath 审,Chapter 6 Symbol Table Manager and Type Checking,Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University,zhangjinghrbeu.edu.cn,3,some symbols must be collected and be put together into tablesSymbol Table. There are two functions in symbol table, the first one is to help check if the semantic is correct, the second one is to assist generating the code. To sum up, this chapter would introduce the functions, content, structure and operation of symbol table.,zhangjinghrbeu.edu.cn,4,6.1 The Functions of Symbol Tables,The functions of symbol table are: (1) Store information (2)Types Checking (3) Data Address,zhangjinghrbeu.edu.cn,5,Store information: Before store information, we firstly should divided the data into different types, then put them into the corresponded tables. Sometimes information is stored in table during lexical analysis, sometimes it is done in semantic analysis. . If the data is an identifier, it would be stored in an identifier table,else if the data is a constant, it will be put into a constant table.,zhangjinghrbeu.edu.cn,6,Types Checking A compiler uses a symbol table to keep track of the type and binding information about names. The symbol table is searched every time by a name that is encountered in the source text. If a new name or new information about an existing name is discovered, the table is changed.,zhangjinghrbeu.edu.cn,7,Data Address: When a data was stored, the datas address in the table was also recorded as an attribute of the table. In the period of code generation in compiler, the data address can be obtained from the symbol table and can be used directly. .,zhangjinghrbeu.edu.cn,8,6.2 The Attribute of Symbol Table,Symbol table is created according to symbols name, so the keyword of the symbol table is symbols name. Except name attribute, there are also six attributes of a symbol in symbol table using for semantic checking. .,zhangjinghrbeu.edu.cn,9,The six attributes of a symbol as follow:,Attribute of kind Attribute of dimension or size Attribute of parameter Attribute of address Attribute of level in program Attribute of line position in source program,zhangjinghrbeu.edu.cn,10,Attribute of kind Kind can describe the characteristics of a symbol. Every symbol has its own type, such as constant, variable and procedure. . Attribute of dimension or size If an identifier is a name of an array, then we should store the dimension and boundary of identifier value as an attribute of it. .,zhangjinghrbeu.edu.cn,11,Attribute of parameter If an identifier is a name of a procedure, the parameters of the procedure should be recorded as an attribute. . Attribute of address In source program, an identifier or a constant are often related with a unit address to accomplish some operation. So the addresses of them are put in symbol table as an attribute. .,zhangjinghrbeu.edu.cn,12,Attribute of level in program Symbol in program or subprogram, the level of its position in program should be written as an attribute in symbol table. In some source languages, identifiers can have same name in different level of program, so attribute of level in program could help recognize them. . Attribute of line position in source program Sometimes the line position of symbol in source program is needed to be stored as an attribute in symbol table in order to obtain the correct semantic checking. .,zhangjinghrbeu.edu.cn,13,Attribute of constant value If the symbol is a constant, then the value of it should been stored as a value attribute in symbol table. . Attributes above are not all the attributes a symbol table has, some symbols used for special may has particular attributes. To sum up, the function of symbol table is for checking up if the semantic in source program is correct.,zhangjinghrbeu.edu.cn,14,Example 6.1,The definition of constants is: Const pi=3.14 r=10 Its symbol table is: NameKindValuepireal3.14rint10,Name,pi,r,Kind,real,int,Value,3.14,10,zhangjinghrbeu.edu.cn,15,Example 6.2,The definition of variables is: Var i, j: integer; x, y: real; Its symbol table is,zhangjinghrbeu.edu.cn,16,Example 6.3,The definition of procedure is: Procedure Max;Var h , l: integer; 2 in size attribute means there are two parameters in the procedure,zhangjinghrbeu.edu.cn,17,6.3 The Design of Symbol Table,This section, we begin to discuss the storage and the scope of symbols in various parts of a program. Then we study how to design the structure of a symbol attribute in local symbol table. .,zhangjinghrbeu.edu.cn,18,In order to obtain high running efficient and save the storage of symbol table, we should take into account a lot of things. Firstly, what are the features o
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号