(8.2)--Samuel's Ch6_2编译原理与实践英文版.ppt





《(8.2)--Samuel's Ch6_2编译原理与实践英文版.ppt》由会员分享,可在线阅读,更多相关《(8.2)--Samuel's Ch6_2编译原理与实践英文版.ppt(61页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 6Semantic Analysis Symbol Table Manager&Type Checking some some symbolssymbols must must be be collected collected and and be be put put together together into into tablesSymbol Table.tablesSymbol Table.There There are are two two functions functions in in symbol symbol table,table,the the f
2、irst first one one is is to to help help check check if if the the semantic semantic is is correct,correct,the the second second one one is is to to assist generating the code.assist generating the code.To To sum sum up,up,this this chapter chapter would would introduce introduce the the functions,f
3、unctions,content,structurecontent,structure and and operationoperation of symbol table.of symbol table.6.1 The Functions of Symbol Tables The The functions functions of symbol table are:of symbol table are:(1 1)Store informationStore information (2 2)Types CheckingTypes Checking (3 3)Data AddressDat
4、a AddressStore information:Store information:BeforeBefore store store information,information,we we firstlyfirstly should should divided divided the the data data into into different different types,types,then then put put them them into into the the corresponded corresponded tables.tables.Sometimes
5、 Sometimes information information is is stored stored in in table table during during lexical lexical analysis,sometimes it is done in semantic analysis.analysis,sometimes it is done in semantic analysis.If If the the data data is is an an identifier,identifier,it it would would be be stored stored
6、 in in an an identifier identifier tabletable,else else if if the the data data is is a a constant,constant,it it will will be be put put into into a a constant table.constant table.Types CheckingA A compilercompiler uses uses a a symbol symbol table table to to keep keep track track of of the the t
7、ype type and binding information about names.and binding information about names.The The symbol symbol table table is is searched searched every every time time by by a a name name that that is is encountered in the source text.encountered in the source text.If If a a new new name name or or new new
8、 information information about about an an existing existing name name is discovered,the table is changed.is discovered,the table is changed.Data Address:When When a a data data was was stored,stored,the the datas datas address address in in the the table table was also recorded as an attribute of t
9、he table.was also recorded as an attribute of the table.In In the the period period of of code code generation generation in in compiler,compiler,the the data data address address can can be be obtained obtained from from the the symbol symbol table table and and can can be be used directly.used dir
10、ectly.6.2 The Attribute of Symbol Table Symbol table is created according to Symbol table is created according to symbols namesymbols name,so the,so the keyword of the symbol table is symbols name.Except name keyword of the symbol table is symbols name.Except name attribute,there are also six attrib
11、utes of a symbol in symbol attribute,there are also six attributes of a symbol in symbol table using for semantic checking.table using for semantic checking.1.1.Constant declarationsConstant declarations const int size=199;const int size=199;2.2.Type declarationsType declarationsstruct Entrystruct E
12、ntry char*name;char*name;int count;int count;struct Entry*next;struct Entry*next;3.3.Variable declarationsVariable declarationsint a,b100;int a,b100;4.4.Function declarationsFunction declarations9Basic DeclarationsThe six attributes of a symbol as follow:Attribute of kindAttribute of kindAttribute o
13、f dimension or sizeAttribute of dimension or sizeAttribute of parameterAttribute of parameterAttribute of addressAttribute of addressAttribute of level in programAttribute of level in programAttribute of line position in source programAttribute of line position in source programAttribute of Attribut
14、e of kindkind Kind Kind can can describe describe the the characteristics characteristics of of a a symbol.symbol.Every Every symbol symbol has has its its own own type,type,such such as as constant,constant,variable variable and and procedure.procedure.Attribute of Attribute of dimension or sizedim
15、ension or size If If an an identifier identifier is is a a name name of of an an array,array,then then we we should should store store the the dimension dimension and and boundary boundary of of identifier identifier value value as as an an attribute of it.attribute of it.Attribute of Attribute of p
16、arameterparameter If If an an identifier identifier is is a a name name of of a a procedure,procedure,the the parameters parameters of of the procedure should be recorded as an attribute.the procedure should be recorded as an attribute.Attribute of Attribute of addressaddress In In source source pro
17、gram,program,an an identifier identifier or or a a constant constant are are often often related related with with a a unit unit address address to to accomplish accomplish some some operation.operation.So So the addresses of them are put in symbol table as an attribute.the addresses of them are put
18、 in symbol table as an attribute.Attribute of Attribute of levellevel in program in program Symbol Symbol in in program program or or subprogram,subprogram,the the level level of of its its position position in in program program should should be be written written as as an an attribute attribute in
19、 in symbol symbol table.table.In In some some source source languages,languages,identifiers identifiers can can have have same same name name in in different different level level of of program,program,so so attribute attribute of of level level in in program program could could help help recognize
20、recognize them.them.A A blockblock in in a a programming programming language language is is any any construct construct that that can can contain declarations.contain declarations.In In C,C,the the blocks blocks are are files,files,function function declarations,declarations,compound compound state
21、ments,structures,class definitions(in OO languages).statements,structures,class definitions(in OO languages).A A language language is is block block structuredstructured if if it it permits permits the the nesting nesting of of blocks blocks inside inside other other blocks blocks and and if if the
22、the scope scope of of declarations declarations in in a a block block are are limited limited to to that that block block and and other other blocks blocks contained contained in in that block,subject to the that block,subject to the most closely nested rulemost closely nested rule:given given sever
23、al several different different declarations declarations of of the the same same name,name,the the declaration declaration that that applies applies to to a a reference reference is is the the one one in in the the most most closely nested block to the reference.closely nested block to the reference
24、.14Block Structureint i,j;int i,j;int f(int size)int f(int size)char i,temp;char i,temp;double j;double j;char*j;char*j;15ExampleAttribute ofAttribute of line line position in source programposition in source program Sometimes Sometimes the the line line position position of of symbol symbol in in s
25、ource source program program is is needed needed to to be be stored stored as as an an attribute attribute in in symbol symbol table in order to obtain the correct semantic checking.table in order to obtain the correct semantic checking.Attribute of Attribute of constantconstant value value If If th
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 8.2-Samuel's Ch6_2编译原理与实践英文版 8.2 Samuel Ch6_2 编译 原理 实践 英文

限制150内