東南編譯原理全64講主講-附課件視頻教程_第1頁
東南編譯原理全64講主講-附課件視頻教程_第2頁
東南編譯原理全64講主講-附課件視頻教程_第3頁
東南編譯原理全64講主講-附課件視頻教程_第4頁
東南編譯原理全64講主講-附課件視頻教程_第5頁
已閱讀5頁,還剩7頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論