Languages and Compilers SProg og Oversttere Semantic Analysis

  • Slides: 14
Download presentation
Languages and Compilers (SProg og Oversættere) Semantic Analysis 1

Languages and Compilers (SProg og Oversættere) Semantic Analysis 1

Semantic Analysis – Describe the purpose of the Semantic analysis phase – Discuss Identification

Semantic Analysis – Describe the purpose of the Semantic analysis phase – Discuss Identification and type checking 2

The “Phases” of a Compiler Source Program Syntax Analysis Error Reports Abstract Syntax Tree

The “Phases” of a Compiler Source Program Syntax Analysis Error Reports Abstract Syntax Tree Contextual Analysis Error Reports Decorated Abstract Syntax Tree Code Generation Object Code 3

Multi Pass Compiler A multi pass compiler makes several passes over the program. The

Multi Pass Compiler A multi pass compiler makes several passes over the program. The output of a preceding phase is stored in a data structure and used by subsequent phases. Dependency diagram of a typical Multi Pass Compiler: Compiler Driver calls This module Syntactic Analyzer Contextual Analyzer Code Generator input output Source Text AST Decorated AST Object Code 4

Recap: Contextual Constraints Syntax rules alone are not enough to specify the format of

Recap: Contextual Constraints Syntax rules alone are not enough to specify the format of well-formed programs. Example 1: let const m~2; in m + x Undefined! Example 2: let const m~2 ; var n: Boolean in begin n : = m<4; n : = n+1 Type error! end Scope Rules Type Rules 5

Contextual Analysis -> Decorated AST Annotations: result of identification : type result of type

Contextual Analysis -> Decorated AST Annotations: result of identification : type result of type checking Program Let. Command Sequential. Declaration Assign. Command Binary. Expr : int Assign. Command Var. Decl Ident n Integer VName. Exp Int. Expr : char : int Simple. T Simple. V : char : int Simple. T Ident Char. Expr Ident c Char Ident Char. Lit Ident c ‘&’ n : int Ident Op Int. Lit n + 1 6

Nested Block Structure Nested A language exhibits nested block structure if blocks may be

Nested Block Structure Nested A language exhibits nested block structure if blocks may be nested one within another (typically with no upper bound on the level of nesting that is allowed). There can be any number of scope levels (depending on the level of nesting of blocks): Typical scope rules: • no identifier may be declared more than once within the same block (at the same level). • for any applied occurrence there must be a corresponding declaration, either within the same block or in a block in which it is nested. 7

Identification Table: Example let var a: Integer; var b: Boolean in begin. . .

Identification Table: Example let var a: Integer; var b: Boolean in begin. . . let var b: Integer; var c: Boolean in begin. . . end. . . let var d: Boolean; var e: Integer in begin let const x: 3 in. . . end Level Ident Attr 1 a (1) Level 1 b (2) Ident 1 a 1 b 2 c Level 1 1 2 2 Ident Attr a (1) Level b (2) 1 d (5) 1 e (6) 2 2 3 Ident a b d e x Attr (1) (2) (3) (4) Attr (1) (2) (5) (6) (7) 8

Type Checking For most statically typed programming languages, a bottom up algorithm over the

Type Checking For most statically typed programming languages, a bottom up algorithm over the AST: • Types of expression AST leaves are known immediately: – literals => obvious – variables => from the ID table – named constants => from the ID table • Types of internal nodes are inferred from the type of the children and the type rule for that kind of expression 9

Type Checking: How Does It Work Example: the type of a binary operation expressions

Type Checking: How Does It Work Example: the type of a binary operation expressions Type rule: If op is an operation of type T 1 x. T 2 ->R then E 1 op E 2 is type correct and of type R if E 1 and E 1 are type correct and have type compatible with T 1 and T 2 respectively Bin. Op bool Int. Expr int 3 Operator intxint->bool < Int. Expr int 4 10

Contextual Analysis Identification and type checking are combined into a depth-first traversal Program of

Contextual Analysis Identification and type checking are combined into a depth-first traversal Program of the abstract syntax tree. Let. Command Sequential. Declaration Sequential. Command Assign. Command Binary. Expression Var. Dec Simple. T Ident Char. Expr Simple. T Simple. V Ident n Integer c Vname. Expr Int. Expr Simple. V Ident Char. Lit Ident Op Int. Lit Char c ‘&’ n n + 1 11

Visitor • Solution using Visitor: – Visitor is an abstract class that has a

Visitor • Solution using Visitor: – Visitor is an abstract class that has a different method for each type of object on which it operates – Each operation is a subclass of Visitor and overloads the type-specific methods – Objects that are operated on, accept a Visitor and call back their type-specific method passing themselves as operands – Object types are independent of the operations that apply to them – New operations can be added without modifying the object types 12

Visitor Solution Node • Nodes accept visitors and call appropriate method of the visitor

Visitor Solution Node • Nodes accept visitors and call appropriate method of the visitor • Visitors implement the operations and have one method for each type of node they visit Accept( Node. Visitor v ) Variable. Ref. Node Assignment. Node Accept(Node. Visitor v) {v->Visit. Variable. Ref(this)} Accept(Node. Visitor v) {v->Visit. Assignment(this)} Node. Visitor Visit. Assignment( Assignment. Node ) Visit. Variable. Ref( Variable. Ref. Node ) Type. Checking. Visitor Visit. Assignment( Assignment. Node ) Visit. Variable. Ref( Variable. Ref. Node ) Code. Generating. Visitor Visit. Assignment( Assignment. Node ) Visit. Variable. Ref( Variable. Ref. Node ) 13

Why contextual analysis can be hard • Questions and answers involve non-local information •

Why contextual analysis can be hard • Questions and answers involve non-local information • Answers mostly depend on values, not syntax • Answers may involve computations Solution alternatives: • Abstract syntax tree – specify non-local computations by walking the tree • Identification tables (sometimes called symbol tables) – central store for facts + checking code • Language design – simplify language 14