版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- it公司辭職報告范文
- 2025年貴陽貨運從業(yè)資格證試題答題器app
- 2025贛州市商品房買賣合同空白
- 上?,F(xiàn)代化工職業(yè)學院《大學化學A》2023-2024學年第一學期期末試卷
- 上海外國語大學賢達經(jīng)濟人文學院《社會化媒體營銷》2023-2024學年第一學期期末試卷
- 上海外國語大學賢達經(jīng)濟人文學院《空間三維建模與仿真》2023-2024學年第一學期期末試卷
- 2025擋墻承包合同
- 上海思博職業(yè)技術學院《光電材料》2023-2024學年第一學期期末試卷
- 課題申報書:構建“四三”學校德育模式的實踐研究
- 三年級下學期科學教學工作總結
- 2024年社區(qū)警務規(guī)范考試題庫
- 2024年7月國家開放大學法學本科《知識產(chǎn)權法》期末考試試題及答案
- 建設工程計價-001-國開機考復習資料
- 2022年全國應急普法知識競賽試題庫大全-中(多選題庫-共2部分-1)
- 神經(jīng)病學運動系統(tǒng)
- 北京市西城區(qū)2022-2023學年六年級上學期數(shù)學期末試卷(含答案)
- 妊娠合并甲減的護理
- 鋼管支撐強度及穩(wěn)定性驗算
- GB/T 5534-2024動植物油脂皂化值的測定
- 幼兒園手足口病教師培訓
評論
0/150
提交評論