COMPILER CONSTRUCTION Principles and practice Kenneth C. louden
COMPILER CONSTRUCTION Principles and Practice Kenneth C. Louden
6. Semantic Analysis PART ONE
6. Semantic Analysis PART ONE
Semantic Analysis phase Purpose: compute additional information needed for compilation that is beyond the capabilities of Context Free Grammars and standard parsing algorithms Static semantic analysis: Take place prior to execution (Such as building a symbol table, performing type inference and type checking Classification analysis of a program required by the rules of the programming language to establish its correctness and to guarantee proper execution Analysis performed by a compiler to enhance the efficiency of execution of the translated program
• Semantic Analysis Phase – Purpose: compute additional information needed for compilation that is beyond the capabilities of ContextFree Grammars and Standard Parsing Algorithms – Static semantic analysis: Take place prior to execution (Such as building a symbol table、performing type inference and type checking) • Classification – Analysis of a program required by the rules of the programming language to establish its correctness and to guarantee proper execution – Analysis performed by a compiler to enhance the efficiency of execution of the translated program
Description of the static semantic analysis Attribute grammar identify attributes of language entities that must be computed and to write attribute equations or semantic rules that express how the computation of such attributes is related to the grammar rules of the language Which is most useful for languages that obey the principle of syntax-Directed Semantics Abstract syntax as represented by an abstract syntax tree
• Description of the static semantic analysis – Attribute grammar • identify attributes of language entities that must be computed • and to write attribute equations or semantic rules that express how the computation of such attributes is related to the grammar rules of the language – Which is most useful for languages that obey the principle of Syntax-Directed Semantics – Abstract syntax as represented by an abstract syntax tree
Implementation of the static semantic analysis Not as clearly expressible as parsing algorithms because of the addition problem caused by the timing of the analysis during the compilation process Multi-pass(more common) or single pass lead to totally different process Emphasis Attributes and attribute grammars Algorithms for attribute computation The symbol table Data types and type checking a semantic analyzer for the tiNy language
• Implementation of the static semantic analysis: – Not as clearly expressible as parsing algorithms because of the addition problem caused by the timing of the analysis during the compilation process – Multi-pass (more common) or single pass lead to totally different process • Emphasis: – Attributes and attribute grammars – Algorithms for attribute computation – The symbol table – Data types and type checking – A semantic analyzer for the TINY language
Contents Part One 6.1 Attributes and Attribute grammars 6.2 Algorithms for Attribute Computation Part two 6.3 The symbol table 6.4 Data Types and Type Checking 6.5 A Semantic Analyzer for the tINY Language
Contents Part One 6.1 Attributes and Attribute Grammars 6.2 Algorithms for Attribute Computation Part Two 6.3 The Symbol Table 6.4 Data Types and Type Checking 6.5 A Semantic Analyzer for the TINY Language
6.1 Attributes and attribute Grammars
6.1 Attributes and Attribute Grammars
Attributes Any property of a programming language construct such as The data type of a variable The value of an expression The location of a variable in memory The object code of a procedure The number of significant digits in a number Binding of the attribute The process of computing an attribute and associating its computed value with the language construct in question Binding time The time during the compilation/execution process when the binding of an attribute occurs Based on the difference of the binding time, attributes is divided nto Static attributes (be bound prior to execution and dynamic attributes (be bound during execution)
• Attributes – Any property of a programming language construct such as • The data type of a variable • The value of an expression • The location of a variable in memory • The object code of a procedure • The number of significant digits in a number • Binding of the attribute – The process of computing an attribute and associating its computed value with the language construct in question • Binding time – The time during the compilation/execution process when the binding of an attribute occurs – Based on the difference of the binding time, attributes is divided into Static attributes (be bound prior to execution) and Dynamic attributes (be bound during execution)
Example: The binding time and significance during compilation of the attributes Attribute computations are extremely varied ype checker In a language like C or Pascal, is an important part of semantic analysis While in a language like LiSP, data types are dynamic, LISP compiler must generate code to compute types and perform type checking during program execution The values of expressions Usually dynamic and the be computed during execution But sometime can also be evaluated during compilation (constant folding)
• Example: The binding time and significance during compilation of the attributes. – Attribute computations are extremely varied – Type checker • In a language like C or Pascal, is an important part of semantic analysis; • While in a language like LISP , data types are dynamic, LISP compiler must generate code to compute types and perform type checking during program execution. – The values of expressions • Usually dynamic and the be computed during execution; • But sometime can also be evaluated during compilation (constant folding)
Example: the binding time and significance during compilation of the attributes Attribute computations are extremely varied The allocation of a variable Either static(such as in FORTRAN77)or dynamic(such as in LISP) Sometimes it is a mixture of static and dynamic(such as in C and Pascal) depending on the language and properties of the variable itself. Object code of a procedure A static attribute, which is computed by the code generator Number of significant digits in a number Often not explicitly treated during compilation
• Example: the binding time and significance during compilation of the attributes. – Attribute computations are extremely varied – The allocation of a variable: • Either static (such as in FORTRAN77 ) or dynamic (such as in LISP), • Sometimes it is a mixture of static and dynamic (such as in C and Pascal) depending on the language and properties of the variable itself. – Object code of a procedure: • A static attribute, which is computed by the code generator – Number of significant digits in a number: • Often not explicitly treated during compilation