版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Semantic analysisParsing 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
2、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 pro
3、grams. 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 tableA large part of semantic analysis consists of tracking variable/function/type declarations .As we enter each new identifier in our symbol table, we nee
4、d 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 performedExamples of the things we check in thesemantic analysis phase. The type of th
5、e 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.
6、 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
7、 program, we need to determine if the identifier is accessible at thatpoint in the program. This is called scope checkingOne 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. Ma
8、ny 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-directe
9、d translationrefers 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 c
10、ompiler 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
11、 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 tocompute the attribute values associated with each grammar symbol in a productio
12、n. The attributevalue for a parse node may depend on information from its children nodes below or its siblings andparent 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 l
13、eft-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
14、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 attributesL attribute grammars,for every production AX1X2Xn,each attribute is a synthe
15、sized attribute or is a Inherited attribute of Xj(1jn),and the Inherited attribute of Xj(1jn) depends on the Inherited attribute of A or depends on the attributes of X1,X2,Xj-1 in every semantics ruleWe can implement syntax-directed translation in either a top-down or a bottom-up parser.Access to at
16、tributes 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
17、, 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 ermediate representationMost compilers translate the source first to some form of intermediate representation and convert from there into machine code
18、. 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, cle
19、aner separation between the front and back ends, adds possibilities for pilation, and works well with many advancedoptimization techniques.Intermediate representations are usually categorized according to where they fall between a high-level language like C, and machine code. IRs that are close to a
20、 high-level language are called high-level IRs, and IRs that are close to assembly are called low-level IRs. For example, a high-level IR might preserve things like array subscripts or field accesses whereas a low-level IR converts those into explicit addresses and offsets. abstract syntax tree You can think of a parse tree as an example of a high-level intermediate representation. In fact, it is often p
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 湖南省株洲市2026-2027学年高一上学期第一次月考数学自编卷(范围:人教A版必修一第1-2单元)(解析版)
- 2026年09月28日 济南市历下区人才测评中心 罗氏制药 医药销售专员 26人
- 江西省九江市匡庐星瀚高级中学2026-2027学年高一上学期开学考试数学试题(含答案)
- 河南省信阳市光山县第二高级中学2026-2027学年度上学期高一地理月考试题( 含答案)
- 广东省江门市2027届九年级上学期阶段性素养摸底历史试卷
- 2025年国企笔试题库及答案
- 临床医学概论中的内分泌疾病的影像学与诊断
- 痤疮的分类与临床表现
- 2026初中毕业班骨干班主任工作汇报课件:青春期学生的心理疏导
- 2026新学期职业院校年级组长工作经验分享课件-班级文化建设的实践探索
- 2026年中考语文一轮复习:修辞手法 专项练习题汇编(含答案解析)
- 会计领军人才选拔考试试题及答案
- 麻醉与免疫功能调节研究
- 综采二队职工应知应会考试题库及答案
- 《人力资源管理概论(第三版)》完整全套教学课件-1-172
- 传统文化创意产品在高中生消费市场中的营销渠道研究教学研究课题报告
- 肠鸣音听诊的部位课件
- 司法鉴定人资格考试题库及答案
- 国企安全员笔试试题题库及答案解析
- 美团送餐员合同协议书
- 贵阳市安全生产事故隐患排查治理规定
评论
0/150
提交评论