《编译原理》课程教学资源:第8章Review

Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis, where we delve deeper to check whether they form a sensible set of instructions in the programming language For a program to be semantically correct, all variables functions, classes, etc are properly defined expressions and variables are used in ways that respect the type system, access control isnt violated, and so on Semantic analysis is the next-to-last phase of the front end and the compilers last chance to weed out incorrect programs We need to ensure the program is well-formed enough to continue on to the next phase where we generate code
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis, where we delve deeper to check whether they form a sensible set of instructions in the programming language. For a program to be semantically correct, all variables, functions, classes, etc. are properly defined, expressions and variables are used in ways that respect the type system, access control isn’t violated, and so on. Semantic analysis is the next-to-last phase of the front end and the compiler’s last chance to weed out incorrect programs. We need to ensure the program is well-formed enough to continue on to the next phase where we generate code

semantic analysis and symbol table a large part of semantic analysis consists of tracking variable/function/type declarations. As we enter each new identifier in our symbol table. we need to record the type information of the declaration Then, as we continue parsing the rest of the program, we make sure that the type of each identifier and expression is respected in terms of the operations being performed
semantic analysis and symbol table A large part of semantic analysis consists of tracking variable/function/type declarations .As we enter each new identifier in our symbol table, we need to record the type information of the declaration. Then, as we continue parsing the rest of the program, we make sure that the type of each identifier and expression is respected in terms of the operations being performed

Examples of the things we check in the semantic analysis phase The type of the right-side expression of an assignment statement should match the type of the left-side, and the left-side needs to be a properly declared and assignable identifier (i.e. not some sort of constant) The parameters of a function should match the arguments of a function call in both number and type The language may require that identifiers are unique disallowing a global variable and function of the same name The operands to multiplication operation will need to be of numeric type, perhaps even the exact same type depending on the strictness of the language
Examples of the things we check in the semantic analysis phase. . The type of the right-side expression of an assignment statement should match the type of the left-side, and the left-side needs to be a properly declared and assignable identifier (i.e. not some sort of constant). The parameters of a function should match the arguments of a function call in both number and type. The language may require that identifiers are unique, disallowing a global variable and function of the same name. The operands to multiplication operation will need to be of numeric type, perhaps even the exact same type depending on the strictness of the language

As we encounter identifiers in a program, we need to determine if the identifier is accessible at that point in the program This is called scope checking One additional issue in semantic analysis is dealing with scopes A scope is a section of program text enclosed by basic program delimiters, e.g,() in C, or begin-end in Pascal. Many languages allow nested scopes that are scopes defined within other scopes. The scope defined by the innermost such unit is called the current scope. The scope defined by the current scope and by any enclosing brogram units are known as open scopes. any pi other scope is a closed scope
As we encounter identifiers in a program, we need to determine if the identifier is accessible at that point in the program. This is called scope checking One additional issue in semantic analysis is dealing with scopes. . A scope is a section of program text enclosed by basic program delimiters, e.g., {} in C, or begin-end in Pascal. Many languages allow nested scopes that are scopes defined within other scopes. The scope defined by the innermost such unit is called the current scope. The scope defined by the current scope and by any enclosing program units are known as open scopes. Any other scope is a closed scope

Syntax-directed translation refers to a method of compiler implementation where the source language translation is completely driven by the parser. In other words, the parsing process and parse trees are used to direct semantic analysis and the translation of the source program This can be a separate phase of a compiler or we can augment our conventional grammar with information to control the semantic analysis and translation. Such grammars are called attribute grammars
Syntax-directed translation refers to a method of compiler implementation where the source language translation is completely driven by the parser. In other words, the parsing process and parse trees are used to direct semantic analysis and the translation of the source program. This can be a separate phase of a compiler or we can augment our conventional grammar with information to control the semantic analysis and translation. Such grammars are called attribute grammars

attribute grammars We augment a grammar by associating attributes with each grammar symbol that describes its properties An attribute has a name and an associated value-a string, a number, a type, a memory location, an assigned register, whatever information we need With each production in a grammar, we give semantic rules or actions. which describe how to compute the attribute values associated with each grammar symbol in a production. The attribute value for a parse node may depend on information from its children nodes below or its siblings and parent node above
attribute grammars. We augment a grammar by associating attributes with each grammar symbol that describes its properties. An attribute has a name and an associated value— a string, a number, a type, a memory location, an assigned register, whatever information we need. With each production in a grammar, we give semantic rules or actions, which describe how to compute the attribute values associated with each grammar symbol in a production. The attribute value for a parse node may depend on information from its children nodes below or its siblings and parent node above

There are two types of attributes we might encounter: synthesized or inherited Synthesized attributes are those attributes that are passed up a parse tree, i.e., the left-side attribute is computed from the right-side attributes. values for the attributes of terminals are usually supplied by the lexical analyzer and the synthesized ones are passed up from there Inherited attributes are those that are passed down a parse tree, i. e, the right-side attributes are derived from the left-side attributes(or other right-side attributes) These attributes are used for passing information about the context to nodes further down the tree
There are two types of attributes we might encounter: synthesized or inherited . Synthesized attributes are those attributes that are passed up a parse tree, i.e., the left-side attribute is computed from the right-side attributes. Values for the attributes of terminals are usually supplied by the lexical analyzer and the synthesized ones are passed up from there . Inherited attributes are those that are passed down a parse tree, i.e., the right-side attributes are derived from the left-side attributes (or other right-side attributes). These attributes are used for passing information about the context to nodes further down the tree

S- attribute grammars, Only have synthesized attributes L- attribute grammars, for every production A→XX2…Xn, each attribute is a synthesized attribute or is a nherited attribute of X (1≤≤n), and the nherited attribute of X (1sisn) depends on the Inherited attribute of a or depends on the attributes ofX, x 2 in every semantics rule
S- attribute grammars,Only have synthesized attributes L- attribute grammars, for every production A→X1X2…Xn,each attribute is a synthesized attribute or is a Inherited attribute of Xj (1≤j≤n),and the Inherited attribute of Xj (1≤j≤n) depends on the Inherited attribute of A or depends on the attributes of X1,X2,…, Xj-1 in every semantics rule

We can implement syntax-directed translation in either a top-down or a bottom-up parser Access to attributes in yacc The syntax $1, $2 is used to access the attribute of the nth token on the right side of the production The global variable yylval is set by the scanner and that value is saved with the token when placed on the parse stack. When a rule is reduced, a new state is placed on the stack, the default behavior is to just copy the attribute of $1 for that new state, this can be controlled by assigning to $S in the action for the rule
We can implement syntax-directed translation in either a top-down or a bottom-up parser. Access to attributes in yacc . The syntax $1, $2 is used to access the attribute of the nth token on the right side of the production. The global variable yylval is set by the scanner and that value is saved with the token when placed on the parse stack. When a rule is reduced, a new state is placed on the stack, the default behavior is to just copy the attribute of $1 for that new state, this can be controlled by assigning to $$ in the action for the rule

intermediate representation Most compilers translate the source first to some form of intermediate representation and convert from there into machine code. The intermediate representation is a machine- and languageindependent version of the original source code. Although converting the code twice introduces another step and thus incurs loss in compiler efficiency, use of an intermediate representation provides advantages in increased abstraction. cleaner separation between the front and back ends, adds possibilities for re-targeting/cross- compilation, and works well with many advanced optimization techniques
intermediate representation Most compilers translate the source first to some form of intermediate representation and convert from there into machine code. The intermediate representation is a machine- and languageindependent version of the original source code. Although converting the code twice introduces another step and thus incurs loss in compiler efficiency, use of an intermediate representation provides advantages in increased abstraction, cleaner separation between the front and back ends, adds possibilities for re-targeting/crosscompilation, and works well with many advanced optimization techniques
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《编译原理》课程教学资源:第5章练习答案.doc
- 《编译原理》课程教学资源:29 TAC examples.pdf
- 《编译原理》课程教学资源:作业及answer.doc
- 《编译原理》课程教学资源:前后端图.doc
- 《编译原理》课程教学资源:复习题1.doc
- 中国人民大学:《数据库系统概论 An Introduction to Database System》课程教学资源(PPT课件讲稿),共三篇,十章).ppt
- 《数据库系统原理与应用》教程教学资源(第二版)数据库概论练习.doc
- 《数据库系统原理与应用》教程教学资源(PPT课件讲稿,第二版)第16章 信息系统的开发过程.ppt
- 《数据库系统原理与应用》教程教学资源(PPT课件讲稿,第二版)第15章 数据仓库技术.ppt
- 《数据库系统原理与应用》教程教学资源(PPT课件讲稿,第二版)第14章 分布式数据库技术.ppt
- 《数据库系统原理与应用》教程教学资源(PPT课件讲稿,第二版)第13章 事务和并发控制.ppt
- 《数据库系统原理与应用》教程教学资源(PPT课件讲稿,第二版)第12章 查询处理技术.ppt
- 《数据库系统原理与应用》教程教学资源(PPT课件讲稿,第二版)第11章 索引和散列技术.ppt
- 《数据库系统原理与应用》教程教学资源(PPT课件讲稿,第二版)第10章 SQL语言高级功能.ppt
- 《数据库系统原理与应用》教程教学资源(PPT课件讲稿,第二版)第9章 SQL语言初步.ppt
- 《数据库系统原理与应用》教程教学资源(PPT课件讲稿,第二版)第8章 Datalog语言.ppt
- 《数据库系统原理与应用》教程教学资源(PPT课件讲稿,第二版)第7章 关系代数基本理论.ppt
- 《数据库系统原理与应用》教程教学资源(PPT课件讲稿,第二版)第6章 关系模式的规范化设计.ppt
- 《数据库系统原理与应用》教程教学资源(PPT课件讲稿,第二版)第5章 关系模型.ppt
- 《数据库系统原理与应用》教程教学资源(PPT课件讲稿,第二版)第4章 数据库建模ODL方法.ppt
- 《编译原理》课程教学资源:java图.doc
- 《编译原理》课程教学资源:TAC.rtf
- 《编译原理》课程教学资源:第四章练习答案.ppt
- 《编译原理》编译原理实验三,四讲稿.ppt
- 《编译原理》课程教学资源:第10章review.ppt
- 《编译原理》课程教学资源:教学计划.doc
- 《编译原理》课程教学资源:第八章 语法制导翻译和中间代码生成.ppt
- 《编译原理》课程教学资源:第二章 PL/0编译程序.ppt
- 《编译原理》课程教学资源:第九章 符号表.ppt
- 《编译原理》课程教学资源:第六章 LR分析程序及其自动构造.ppt
- 《编译原理》课程教学资源:第三章 词法分析.ppt
- 《编译原理》课程教学资源:第十二章 代码生成.ppt
- 《编译原理》课程教学资源:第十一章 代码优化.ppt
- 《编译原理》课程教学资源:第十章 目标程序运行时的组织.ppt
- 《编译原理》课程教学资源:第四章 文法和语言.ppt
- 《编译原理》课程教学资源:第五章 LL(1)文法及其分析程序.ppt
- 《编译原理》课程教学资源:第一章 概述.ppt
- 河南经贸职业学院:《ASP.NET动态网站开发》课程教学资源(PPT课件)第二十一讲 ASP.NE增强服务器控件.ppt
- 河南经贸职业学院:《ASP.NET动态网站开发》课程教学资源(PPT课件)第十五讲 DataAdapter对象.ppt
- 河南经贸职业学院:《ASP.NET动态网站开发》课程教学资源(PPT课件)第十四讲 DataReader对象的使用.ppt