5. Syntax-Directed Definitions & Type Analysis
|
|
- Gary Ramsey
- 6 years ago
- Views:
Transcription
1 5. Syntax-Directed Definitions & Type Analysis Eva Rose Kristoffer Rose NYU Courant Institute Compiler Construction (CSCI-GA ) March 2, 2015 Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
2 1 Introduction 2 SDD: Syntax-Directed Definition 3 Type Systems 4 Types in Programming Languages 5 Types in the Compiler 6 Project Milestone 2 Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
3 Context Tokens Tree Tree source program IR IR IR Lexical Analysis Syntax Analysis Semantic Analysis Symbol Table Intermediate Representation Generator Optimizer Code Generator Machine-Dependent Code Optimizer target machine code Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
4 From Abstract Syntax Tree (AST) position = initial + rate * 60 = id, 1 + id, 2 id, 3 num, 60 id lexeme 1 position 2 initial 3 rate Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
5 Type Checking Gives Annotated AST... id, 1 t = float = t = float + id, 2 t = float t = float id lexeme t 1 position float 2 initial float 3 rate float t = float id, 3 num, 60 t = float t = int Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
6 1 Introduction 2 SDD: Syntax-Directed Definition 3 Type Systems 4 Types in Programming Languages 5 Types in the Compiler 6 Project Milestone 2 Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
7 Syntax-Directed Definition (SDD) Grammar Given a context-free grammar. Attributes Each grammar symbol (terminal or nonterminal) has a set of attributes associated. Semantic Rules Each production has a set of semantic rules associated for computing the attributes. If X is a symbol, a is an attribute, then X.a denotes the value of a at node X. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
8 Synthesized and Inherited Attributes Synthesized attributes at node N are defined only in terms of the attribute values of the children of N, and N itself. Inherited attributes at node N is defined only in terms of attribute values at N s parent, N itself, and N s siblings. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
9 Example: SDD for Desk Calculator PRODUCTION SEMANTIC RULE 1. L E $ L.val = E.val 2. E E 1 + T E.val = E 1.val + T.val 3. E T E.val = T.val 4. T T 1 F T.val = T 1.val F.val 5. T F T.val = F.val 6. F (E) F.val = E.val 7. F digit F.val = digit.lexval val and lexval are synthesized attributes. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
10 Example: SDD for Desk Calculator PRODUCTION SEMANTIC RULE 1. L E $ L.val = E.val 2. E E 1 + T E.val = E 1.val + T.val 3. E T E.val = T.val 4. T T 1 F T.val = T 1.val F.val 5. T F T.val = F.val 6. F (E) F.val = E.val 7. F digit F.val = digit.lexval val and lexval are synthesized attributes. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
11 Characteristics of Desk Calculator S-attributed (only synthesized attributes) attribute grammar (SDD/ no side-effects) S-attribute calculations: any bottom-up parsing is good (depth-first tree traversal). Note: In parser generator YACC, S-attributes are directly annotatable (as semantic actions). Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
12 Characteristics of Desk Calculator S-attributed (only synthesized attributes) attribute grammar (SDD/ no side-effects) S-attribute calculations: any bottom-up parsing is good (depth-first tree traversal). Note: In parser generator YACC, S-attributes are directly annotatable (as semantic actions). Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
13 Introduction SDD: Syntax-Directed Definition Type Systems Types in Programming Languages Types in the Compiler Project Milestone 2 Tree Traversal: Depth-First Post-order (default). Pre-order. Binary trees: also in-order. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
14 Exercise: Desk Calculator Give annotated parse trees for the following expressions: (3 + 4) (5 + 6) $ (4 + 5) $ (Parse trees showing attributes and their values) Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
15 Exercise Construct an SDD that generates a (synthesized) attribute t in prefix notation for each expression expr, for arithmetic expressions. (You only need to consider: +,, and expressions). Prefix notation is where the operator comes before its operands; e.g., x y is the prefix notation for x y. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
16 Solution: SDD for Infix Notation into Prefix Notation PRODUCTION SEMANTIC RULE expr expr 1 + term 2 expr.t = + expr 1.t term 2.t expr expr 1 term 2 expr.t = expr 1.t term 2.t expr expr 1 term 2 expr.t = expr 1.t term 2.t expr term expr.t = term.t term 0 term.t = 0 term 1 term.t = term 9 term.t = 9 Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
17 Solution: SDD for Infix Notation into Prefix Notation PRODUCTION SEMANTIC RULE expr expr 1 + term 2 expr.t = + expr 1.t term 2.t expr expr 1 term 2 expr.t = expr 1.t term 2.t expr expr 1 term 2 expr.t = expr 1.t term 2.t expr term expr.t = term.t term 0 term.t = 0 term 1 term.t = term 9 term.t = 9 Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
18 Solution: SDD for Infix Notation into Prefix Notation PRODUCTION SEMANTIC RULE expr expr 1 + term 2 expr.t = + expr 1.t term 2.t expr expr 1 term 2 expr.t = expr 1.t term 2.t expr expr 1 term 2 expr.t = expr 1.t term 2.t expr term expr.t = term.t term 0 term.t = 0 term 1 term.t = term 9 term.t = 9 Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
19 Solution: SDD for Infix Notation into Prefix Notation PRODUCTION SEMANTIC RULE expr expr 1 + term 2 expr.t = + expr 1.t term 2.t expr expr 1 term 2 expr.t = expr 1.t term 2.t expr expr 1 term 2 expr.t = expr 1.t term 2.t expr term expr.t = term.t term 0 term.t = 0 term 1 term.t = term 9 term.t = 9 Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
20 Exercise: Mingling with Inherited Attributes... PRODUCTION SEMANTIC RULES 1. T F T T.inh = F.val; T.val = T.syn T F T 1 T ɛ T 1.inh = T.inh F.val; T.syn = T 1.syn T.syn = T.inh 4. F digit F.val = digit.lexval inh is inherited, and val, syn are synthesized. Can you explain why? Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
21 Dependency Graphs Dependency graphs depicts the flow of information among attributes. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
22 Introduction SDD: Syntax-Directed Definition Type Systems Types in Programming Languages Types in the Compiler Project Milestone 2 Example: Attributes Parse tree for 3 5 based on SDD: F 3 val digit 1 lexval T 9 val inh 5 8 syn T F 4 val 6 T inh 7 syn digit 2 lexval ɛ Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
23 Introduction SDD: Syntax-Directed Definition Type Systems Types in Programming Languages Types in the Compiler Project Milestone 2 Example: Attributes with Dependency Graph Parse tree for 3 5 based on SDD: F 3 val digit 1 lexval T 9 val inh 5 8 syn T F 4 val 6 T inh 7 syn digit 2 lexval ɛ Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
24 Dependency Graphs Central problem: Determining the efficient evaluation order for the attribute instances in a given parse tree. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
25 Calculating Attributes Cycles possible! PRODUCTION SEMANTIC RULE A B A.s = B.i; B.i = A.s + 1 When dependency graphs have cycles, evaluation is stuck! Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
26 Well-behaved SDD Classes Given an SDD, hard to tell if there exists a parse tree whose dependency graphs have cycles! Safe way to go: use classes of SDDs that guarantee an evaluation order: S-Attributed Definitions all attributes are synthesized. L-Attributed Definitions left restrictions on dependency graph edges. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
27 Well-behaved SDD Classes Given an SDD, hard to tell if there exists a parse tree whose dependency graphs have cycles! Safe way to go: use classes of SDDs that guarantee an evaluation order: S-Attributed Definitions all attributes are synthesized. L-Attributed Definitions left restrictions on dependency graph edges. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
28 L-Attributed Definitions Suppose an attribute rule for A X 1 X 2... X i... X n produces inherited X i.a attribute. The rule may only use: inherited attributes associated with the head (A), inherited or synthesized attributes associated with the occurences of symbols X 1, X 2,..., X i 1 located left of X i. Inherited or synthesized attributes associated with the occurence of X i itself, as long as not part of a dependency graph cycle! or, the produced attribute is synthesized. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
29 Example: Mingling with Inherited Attributes... Decide if the SDD is L-attributed and argue : PRODUCTION SEMANTIC RULES 1. T F T T.inh = F.val; T.val = T.syn T F T 1 T ɛ T 1.inh = T.inh F.val; T.syn = T 1.syn T.syn = T.inh 4. F digit F.val = digit.lexval inh is inherited, and val, syn are synthesized. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
30 Example: Mingling with Inherited Attributes... Decide if the SDD is L-attributed and argue : PRODUCTION SEMANTIC RULES 1. T F T T.inh = F.val; T.val = T.syn T F T 1 T ɛ T 1.inh = T.inh F.val; T.syn = T 1.syn T.syn = T.inh 4. F digit F.val = digit.lexval inh is inherited, and val, syn are synthesized. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
31 Example: Mingling with Inherited Attributes... Decide if the SDD is L-attributed and argue : PRODUCTION SEMANTIC RULES 1. T F T T.inh = F.val; T.val = T.syn T F T 1 T ɛ T 1.inh = T.inh F.val; T.syn = T 1.syn T.syn = T.inh 4. F digit F.val = digit.lexval inh is inherited, and val, syn are synthesized. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
32 Another example The following SDD is NOT L-attributed. Why? PRODUCTION A B C SEMANTIC RULES A.s = B.b B.i = F(C.c, A.s) where B.i is inherited, and A.s, C.c are synthesized. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
33 Another example The following SDD is NOT L-attributed. Why? PRODUCTION A B C SEMANTIC RULES A.s = B.b B.i = F(C.c, A.s) Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
34 Construction of Abstract Syntax Trees TREE INTERIOR NODES GRAMMAR parse tree nonterminals concrete syntax abstract syntax tree programming constructs abstract syntax Example (abstract syntax) + 2 E E + E E E digit 9 5 Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
35 Construction of AST Example 1: S-attributed SDD for simple expresssions. PRODUCTION SEMANTIC RULE 1. E E 1 + T 2 E.node = new Node( +, E 1.node, T 2.node) 2. E E 1 T 2 E.node = new Node(, E 1.node, T 2.node) 3. E T E.node = T.node 4. T ( E ) T.node = E.node 5. T id T.node = new Leaf(id, id.entry) 6. T num T.node = new Leaf(num, num.entry) Show the AST for a 4 + c Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
36 Construction of AST Example 1: S-attributed SDD for simple expresssions. PRODUCTION SEMANTIC RULE 1. E E 1 + T 2 E.node = new Node( +, E 1.node, T 2.node) 2. E E 1 T 2 E.node = new Node(, E 1.node, T 2.node) 3. E T E.node = T.node 4. T ( E ) T.node = E.node 5. T id T.node = new Leaf(id, id.entry) 6. T num T.node = new Leaf(num, num.entry) Show the AST for a 4 + c Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
37 Construction of AST Example 2: L-attributed SDD for simple expresssions. PRODUCTION SEMANTIC RULE 1. E T E E.node = E.syn; E.inh = T.node 2. E + T E 1 E 1.inh = new Node( +, E.node, T.node) 3. E T E 1 E.syn = E 1.syn E 1.inh = new Node(, E.node, T.node) E.syn = E 1.syn 4. E ɛ E.syn = E.inh 5. T ( E ) T.node = E.node 6. T id T.node = new Leaf(id, id.entry) 7. T num T.node = new Leaf(num, num.entry) Show AST and dependency graph for a 4 + c Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
38 Construction of AST Example 2: L-attributed SDD for simple expresssions. PRODUCTION SEMANTIC RULE 1. E T E E.node = E.syn; E.inh = T.node 2. E + T E 1 E 1.inh = new Node( +, E.node, T.node) 3. E T E 1 E.syn = E 1.syn E 1.inh = new Node(, E.node, T.node) E.syn = E 1.syn 4. E ɛ E.syn = E.inh 5. T ( E ) T.node = E.node 6. T id T.node = new Leaf(id, id.entry) 7. T num T.node = new Leaf(num, num.entry) Show AST and dependency graph for a 4 + c Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
39 Syntax-Directed Translation of Type Expressions PRODUCTION SEMANTIC RULE 1 T B C T.syn = C.syn C.inh = B.syn 2 B int B.syn = integer 3 B float B.syn = integer 4 C [num]c 1 C.syn = array(num.val, C 1.syn) C 1.inh = C.inh 5 C ɛ C.syn = C.inh Show AST and dependencies for int[2][3] Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
40 Syntax-Directed Translation of Type Expressions PRODUCTION SEMANTIC RULE 1 T B C T.syn = C.syn C.inh = B.syn 2 B int B.syn = integer 3 B float B.syn = integer 4 C [num]c 1 C.syn = array(num.val, C 1.syn) C 1.inh = C.inh 5 C ɛ C.syn = C.inh Show AST and dependencies for int[2][3] Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
41 1 Introduction 2 SDD: Syntax-Directed Definition 3 Type Systems 4 Types in Programming Languages 5 Types in the Compiler 6 Project Milestone 2 Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
42 Type Checking Languages come with type system: Set of rules What are the basic value types? How can existing values be composed and decomposed to form new types? Compiler s job: Assign type expressions to each component Determine that these type expressions conform to type systems Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
43 Type Checking Languages come with type system: Set of rules What are the basic value types? How can existing values be composed and decomposed to form new types? Compiler s job: Assign type expressions to each component Determine that these type expressions conform to type systems Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
44 Purpose of Types Development Help (completion, documentation) Error detection (static and dynamic checks) Error Prevention (disambiguate operations) Optimization purposes (storage layout) Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
45 Dynamic vs Static Type Checking Type checking can be done dynamically for any language (i.e., at run-time) compiler generates code to do runtime checks Usually preferred to do type checking statically (i.e., at compile-time) Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
46 Properties of Type Checking A sound type system eliminates the need for dynamic checking Examples: OCaml, Haskell A language is strongly typed if all type errors are caught. Examples: Java, C#, Ruby, Python, OCaml, Haskell Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
47 Rules for Type Checking Type Synthesis Builds the type system of an expression from the types of subexpressions Requires names to be declared before use Type Inference Determines the type of a construct from the way it is used Complex Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
48 Type Synthesis (Denotational Formulation) if f : S T and x : S then f(x) : T f : S T f(x) : T x : S Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
49 Type Synthesis (Denotational Formulation) if f : S T and x : S then f(x) : T f : S T f(x) : T x : S Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
50 Type Inference (Denotational Formulation) f(x) : T S[ f : S T x : S ] Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
51 1 Introduction 2 SDD: Syntax-Directed Definition 3 Type Systems 4 Types in Programming Languages 5 Types in the Compiler 6 Project Milestone 2 Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
52 Values in Programming Languages Some examples of types: short, int, long, char, bool, float,... int[2][3], struct Link, String,... class names,... Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
53 Type Concepts primitive types (boolean, integral, floating point, chars, strings*) composite types (arrays, records, strings*) reference types (pointers, object references) abstract data types aka. ADT (class) subtype (class hierarchies) recursive types (linked lists) function types (ordering)... Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
54 Type Concepts primitive types (boolean, integral, floating point, chars, strings*) composite types (arrays, records, strings*) reference types (pointers, object references) abstract data types aka. ADT (class) subtype (class hierarchies) recursive types (linked lists) function types (ordering)... Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
55 Type Specification Language manual Compiler manual Machine architecture Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
56 Type Specification Language manual Compiler manual Machine architecture Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
57 Type Specification Language manual Compiler manual Machine architecture Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
58 Some (Modern) Language Type Implementation Details Type Implementation (bit) Language Specifications byte 8 JVM-Spec: 8 short 16 (or 32) JVM-Spec: 16, C/C++ unspec. char 8 or 16 JVM-Spec: 16, C/C++: 8 int 32 or 64 JVM-Spec: 32, C/C++ 16 long 64 (or 128) JVM-Spec: 64, C/C++ 32 float 32 (or 64) JVM-Spec: 32, C/C++ unspec. double 64 (or 128) JVM-Spec: 64, C/C++ unspec. C: part of system read limits.h for min and max specifications. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
59 Traditional/Old System (C-compiler) Type Details Type Implementation (bit) short 8 int 16 long 32 Today: unix systems : specify integers as 64 bit window systems : specify integers as 32 bit computers (AMD64): ALU datapath, registers are 64 bit, mobile phones (ARM): ditto, but 32 bit (moving to 64) Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
60 Type Conversions Widening (preservation) often implicit (coercions). Narrowing (lossy) often explicit (casts). Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
61 Introduction SDD: Syntax-Directed Definition Type Systems Types in Programming Languages Types in the Compiler Project Milestone 2 Widening Conversion Hierarchy (primitive Java types) short byte double float long int char Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
62 Introduction SDD: Syntax-Directed Definition Type Systems Types in Programming Languages Types in the Compiler Project Milestone 2 Narrowing Conversion Hierarchy (primitive Java types) byte double float long int short char Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
63 Example Type Conversion I 1 float position; 2 int initial; 3 float rate; position = initial + rate * 60.00; Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
64 Example Type Conversion II 1 float position; 2 int initial; 3 float rate; position = initial + rate * 60; Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
65 Example Type Conversion III 1 int position; 2 int initial; 3 float rate; position = initial + rate * 60; Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
66 Explicit Conversions (cast) Tell the compiler that you MEAN this type conversion (type cast) position = initial + (( int) rate) * 60; Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
67 Example Java Conversions... 1 "U" "U" 3 " friends are " + true 4 true + " types" Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
68 Example Java Conversions... 1 "U" + 2 OK "U" 3 " friends are " + true 4 true + " types" Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
69 Example Java Conversions... 1 "U" + 2 OK "U" Fail 3 " friends are " + true 4 true + " types" Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
70 Example Java Conversions... 1 "U" + 2 OK "U" Fail 3 " friends are " + true OK 4 true + " types" Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
71 Example Java Conversions... 1 "U" + 2 OK "U" Fail 3 " friends are " + true OK 4 true + " types" Fail Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
72 Operator and Function Overloading + meaning addition or string concatenation (Java) user defined functions (Java) 1 void err() {...} 2 void err(string s) {...} Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
73 Operator and Function Overloading + meaning addition or string concatenation (Java) user defined functions (Java) 1 void err() {...} 2 void err(string s) {...} Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
74 1 Introduction 2 SDD: Syntax-Directed Definition 3 Type Systems 4 Types in Programming Languages 5 Types in the Compiler 6 Project Milestone 2 Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
75 Type Equivalence Approaches What are the values in a type? Approach Denotational Abstract Data Types (ADT) Definition set of values (Type Theory) set of operations (OO-systems) When are two types considered the same? Approach Denotational Abstract Data Types (ADT) Named typing Definition set of values (Type Theory) set of operations (OO-systems) set of names Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
76 Type Expressions (TE) A way so assign structure to types: primitive types are TEs, type constructor operator assigned to a TE. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
77 TE Examples short, int, long, float, char,... T[] Map S,T S T S T Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
78 Type structure: AST for int[2][3] T B C (1) B int float (2) C [num]c ɛ (3) T int [.]. 2 [.]. 3 ɛ Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
79 Type structure: AST for int[2][3] T B C (1) B int float (2) C [num]c ɛ (3) T int [.]. 2 [.]. 3 ɛ Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
80 Type structure: type expression for int[2][3] array 2 array 3 int Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
81 Example: SDD for array TEs PRODUCTION T B C B int B float C [num]c 1 C ɛ SEMANTIC RULES T.t = C.t; C.b = B.t B.t = integer B.t = float C.t = array(num.val, C 1.t); C 1.b = C.b C.t = C.b b-inherited; t-synthesized Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
82 Example: SDD for array TEs PRODUCTION T B C B int B float C [num]c 1 C ɛ SEMANTIC RULES T.t = C.t; C.b = B.t B.t = integer B.t = float C.t = array(num.val, C 1.t); C 1.b = C.b C.t = C.b b-inherited; t-synthesized Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
83 Example: SDD with Type Storage Attribute PRODUCTION SEMANTIC RULES T BC T.t = C.t; T.width = C.width; C.b = B.t; w = B.width; B int B.t = integer; B.width = 4; B float B.t = float; B.width = 8; C [num]c 1 C.t = array(num.val, C 1.t); C.width = num.val C 1.width; C ɛ C.t = C.b; C.width = w; b inherited; t, width synthesized Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
84 Example SDD: Declarations and Environment Attribute PRODUCTION SEMANTIC RULES S T 1 id 2 = E 3 ; S 4 E 3.e = S.e; S 4.e = Extend(S.e, id 2.sym, T 1 ) (1 E E 1 + E 2 E 1.e = E.e; E 2.e = E.e (2 id 1 id 1.t = Lookup(E.e, id 1.sym) (3 Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
85 1 Introduction 2 SDD: Syntax-Directed Definition 3 Type Systems 4 Types in Programming Languages 5 Types in the Compiler 6 Project Milestone 2 Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
86 Project Milestone 2 Project Milestone 2 Released! Two due dates: 3/9 (Monday) and 3/27 (Friday) Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
87 Project Milestone 2 Requirements for 3/9 1 SDD for type checking of JST: Pr2You-sdd.pdf... template is provided: Pr2Base-sdd.tex (or PDF). 2 Evaluation criteria: Are attributes explained? Are the dependencies between attributes explained? Are declarations recorded properly in type environments? Are all nodes that can be assigned a type annotated with one? Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
88 Project Milestone 2 Requirements for 3/9 1 SDD for type checking of JST: Pr2You-sdd.pdf... template is provided: Pr2Base-sdd.tex (or PDF). 2 Evaluation criteria: Are attributes explained? Are the dependencies between attributes explained? Are declarations recorded properly in type environments? Are all nodes that can be assigned a type annotated with one? Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
89 Project Milestone 2 Requirements for 3/27 1 Implementation: Pr2You.hx... template provided: Pr2Base.hx. 2 Documentation: Pr2You.pdf. 3 Evaluation criteria: HACS source is well structured and easy to read. Variation and difficulties documented. Planning documented. How to reproduce tests is documented. Choices documented. Collaborations documented. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
90 Project Milestone 2 Requirements for 3/27 1 Implementation: Pr2You.hx... template provided: Pr2Base.hx. 2 Documentation: Pr2You.pdf. 3 Evaluation criteria: HACS source is well structured and easy to read. Variation and difficulties documented. Planning documented. How to reproduce tests is documented. Choices documented. Collaborations documented. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
91 Project Milestone 2 Requirements for 3/27 1 Implementation: Pr2You.hx... template provided: Pr2Base.hx. 2 Documentation: Pr2You.pdf. 3 Evaluation criteria: HACS source is well structured and easy to read. Variation and difficulties documented. Planning documented. How to reproduce tests is documented. Choices documented. Collaborations documented. Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
92 Questions? Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA ) 5. Syntax-directed Definitions/Type Analysis March 2, / 67
Abstract Syntax Trees Synthetic and Inherited Attributes
Abstract Syntax Trees Synthetic and Inherited Attributes Lecture 22 Sections 5.1-5.2 Robb T. Koether Hampden-Sydney College Mon, Mar 16, 2015 Robb T. Koether (Hampden-Sydney College)Abstract Syntax TreesSynthetic
More informationSyntax Directed Translation
Syntax Directed Translation Rupesh Nasre. CS3300 Compiler Design IIT Madras Aug 2015 Character stream Lexical Analyzer Machine-Independent Code Optimizer F r o n t e n d Token stream Syntax Analyzer Syntax
More informationSyntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions
Concepts Introduced in Chapter 5 Syntax-Directed Definitions Translation Schemes Synthesized Attributes Inherited Attributes Dependency Graphs Syntax-Directed Translation Uses a grammar to direct the translation.
More informationCOMPILER CONSTRUCTION (Evaluation Orders for SDD s)
COMPILER CONSTRUCTION (Evaluation Orders for SDD s) Prof. K R Chowdhary Email: kr.chowdhary@jietjodhpur.ac.in Campus Director, JIET, Jodhpur Thursday 18 th October, 2018 kr chowdhary Code generation 1/
More informationSyntax-Directed Translation
Syntax-Directed Translation What is syntax-directed translation? The compilation process is driven by the syntax. The semantic routines perform interpretation based on the syntax structure. Attaching attributes
More informationCompilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1
Compilers 5. Attributed Grammars Laszlo Böszörmenyi Compilers Attributed Grammars - 1 Adding Attributes We connect the grammar rules with attributes E.g. to implement type-checking or code generation A
More informationSemantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.
SEMANTIC ANALYSIS: Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known. Parsing only verifies that the program consists of tokens
More informationType Checking. Chapter 6, Section 6.3, 6.5
Type Checking Chapter 6, Section 6.3, 6.5 Inside the Compiler: Front End Lexical analyzer (aka scanner) Converts ASCII or Unicode to a stream of tokens Syntax analyzer (aka parser) Creates a parse tree
More informationLecture 14 Sections Mon, Mar 2, 2009
Lecture 14 Sections 5.1-5.4 Hampden-Sydney College Mon, Mar 2, 2009 Outline 1 2 3 4 5 Parse A parse tree shows the grammatical structure of a statement. It includes all of the grammar symbols (terminals
More informationChapter 6 Intermediate Code Generation
Chapter 6 Intermediate Code Generation Outline Variants of Syntax Trees Three-address code Types and declarations Translation of expressions Type checking Control flow Backpatching Introduction Intermediate
More informationCOP5621 Exam 3 - Spring 2005
COP5621 Exam 3 - Spring 2005 Name: (Please print) Put the answers on these sheets. Use additional sheets when necessary. Show how you derived your answer when applicable (this is required for full cred
More informationSyntax-Directed Translation. Introduction
Syntax-Directed Translation Introduction Translation of languages guided by context-free grammars Attach attributes to the grammar symbols Values of the attributes are computed by semantic rules associated
More informationSyntax-Directed Translation
Syntax-Directed Translation 1 Syntax-Directed Translation 2 Syntax-Directed Translation 3 Syntax-Directed Translation In a syntax-directed definition, each production A α is associated with a set of semantic
More informationCSE 431S Type Checking. Washington University Spring 2013
CSE 431S Type Checking Washington University Spring 2013 Type Checking When are types checked? Statically at compile time Compiler does type checking during compilation Ideally eliminate runtime checks
More informationA Simple Syntax-Directed Translator
Chapter 2 A Simple Syntax-Directed Translator 1-1 Introduction The analysis phase of a compiler breaks up a source program into constituent pieces and produces an internal representation for it, called
More informationSemantic Analysis Attribute Grammars
Semantic Analysis Attribute Grammars Martin Sulzmann Martin Sulzmann Semantic Analysis Attribute Grammars 1 / 18 Syntax versus Semantics Syntax Analysis When is a program syntactically valid? Formalism:
More information[Syntax Directed Translation] Bikash Balami
1 [Syntax Directed Translation] Compiler Design and Construction (CSc 352) Compiled By Central Department of Computer Science and Information Technology (CDCSIT) Tribhuvan University, Kirtipur Kathmandu,
More informationCSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking
CSE450 Translation of Programming Languages Lecture 11: Semantic Analysis: Types & Type Checking Structure Project 1 - of a Project 2 - Compiler Today! Project 3 - Source Language Lexical Analyzer Syntax
More informationCOP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher
COP4020 Programming Languages Semantics Robert van Engelen & Chris Lacher Overview Static semantics Dynamic semantics Attribute grammars Abstract syntax trees Static Semantics Syntax concerns the form
More informationSEMANTIC ANALYSIS TYPES AND DECLARATIONS
SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether
More informationCOP4020 Programming Languages. Semantics Prof. Robert van Engelen
COP4020 Programming Languages Semantics Prof. Robert van Engelen Overview Static semantics Dynamic semantics Attribute grammars Abstract syntax trees COP4020 Spring 2011 2 Static Semantics Syntax concerns
More informationStatic Checking and Type Systems
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009 2 The Structure of our Compiler Revisited Character stream Lexical
More informationPrinciples of Programming Languages
Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 11! Syntax- Directed Transla>on The Structure of the
More informationStructure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.
More detailed overview of compiler front end Structure of a compiler Today we ll take a quick look at typical parts of a compiler. This is to give a feeling for the overall structure. source program lexical
More informationSyntax-Directed Translation
Syntax-Directed Translation 1 Syntax-Directed Translation 1. We associate information with the programming language constructs by attaching attributes to grammar symbols. 2. Values of these attributes
More informationSyntax-Directed Translation Part I
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011 2 The Structure of our Compiler Revisited Character stream
More informationCOMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou
COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! Any questions about the syllabus?! Course Material available at www.cs.unic.ac.cy/ioanna! Next time reading assignment [ALSU07]
More informationTypes. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus
Types Type checking What is a type? The notion varies from language to language Consensus A set of values A set of operations on those values Classes are one instantiation of the modern notion of type
More informationSyntax and Grammars 1 / 21
Syntax and Grammars 1 / 21 Outline What is a language? Abstract syntax and grammars Abstract syntax vs. concrete syntax Encoding grammars as Haskell data types What is a language? 2 / 21 What is a language?
More informationA programming language requires two major definitions A simple one pass compiler
A programming language requires two major definitions A simple one pass compiler [Syntax: what the language looks like A context-free grammar written in BNF (Backus-Naur Form) usually suffices. [Semantics:
More informationThe Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.
The Compiler So Far CSC 4181 Compiler Construction Scanner - Lexical analysis Detects inputs with illegal tokens e.g.: main 5 (); Parser - Syntactic analysis Detects inputs with ill-formed parse trees
More informationProgramming Languages
Programming Languages Tevfik Koşar Lecture - IX February 14 th, 2006 1 Roadmap Semantic Analysis Role of Semantic Analysis Static vs Dynamic Analysis Attribute Grammars Evaluating Attributes Decoration
More information10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.
Outline Semantic Analysis In Text: Chapter 3 Static semantics Attribute grammars Dynamic semantics Operational semantics Denotational semantics N. Meng, S. Arthur 2 Syntax vs. Semantics Syntax concerns
More informationPrinciples of Programming Languages
Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 10! Con:nua:on of the course Syntax- Directed Transla:on
More informationStatic and Dynamic Semantics
Copyright R.A. van Engelen, FSU Department of Computer Science, 2000 Semantic Analysis In this set of notes you will learn about: Static semantics Dynamic semantics Attribute grammars Abstract syntax trees
More informationIntermediate Code Generation
Intermediate Code Generation Rupesh Nasre. CS3300 Compiler Design IIT Madras Aug 2015 Character stream Lexical Analyzer Machine-Independent Code Optimizer F r o n t e n d Token stream Syntax Analyzer Syntax
More informationSemantic Analysis. Role of Semantic Analysis
Semantic Analysis Chapter 4 Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are semantic analysis (intermediate) code generation The principal job of the semantic
More informationCompilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam
Compilers Type checking Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Summary of parsing Parsing A solid foundation: context-free grammars A simple parser: LL(1) A more powerful parser:
More informationComputer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres
Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres dgriol@inf.uc3m.es Introduction He am a driver might be syntactically correct but semantically wrong. Semantic
More informationCompilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.
Compilerconstructie najaar 2012 http://www.liacs.nl/home/rvvliet/coco/ Rudy van Vliet kamer 124 Snellius, tel. 071-527 5777 rvvliet(at)liacs.nl college 5, dinsdag 9 oktober 2012 Static Type Checking 1
More informationChapter 4 :: Semantic Analysis
Chapter 4 :: Semantic Analysis Programming Language Pragmatics, Fourth Edition Michael L. Scott Copyright 2016 Elsevier 1 Chapter04_Semantic_Analysis_4e - Tue November 21, 2017 Role of Semantic Analysis
More informationFormal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table
Formal Languages and Compilers Lecture IX Semantic Analysis: Type Checking & Symbol Table Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/
More informationSyntax-Directed Translation
Syntax-Directed Translation ALSU Textbook Chapter 5.1 5.4, 4.8, 4.9 Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 What is syntax-directed translation? Definition: The compilation
More informationCS 430 Spring Mike Lam, Professor. Data Types and Type Checking
CS 430 Spring 2015 Mike Lam, Professor Data Types and Type Checking Type Systems Type system Rules about valid types, type compatibility, and how data values can be used Benefits of a robust type system
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More informationSemantic Analysis. Lecture 9. February 7, 2018
Semantic Analysis Lecture 9 February 7, 2018 Midterm 1 Compiler Stages 12 / 14 COOL Programming 10 / 12 Regular Languages 26 / 30 Context-free Languages 17 / 21 Parsing 20 / 23 Extra Credit 4 / 6 Average
More informationIntermediate Code Generation
Intermediate Code Generation Rupesh Nasre. CS3300 Compiler Design IIT Madras July 2018 Character stream Lexical Analyzer Machine-Independent Code Code Optimizer F r o n t e n d Token stream Syntax Analyzer
More informationTypes. What is a type?
Types What is a type? Type checking Type conversion Aggregates: strings, arrays, structures Enumeration types Subtypes Types, CS314 Fall 01 BGRyder 1 What is a type? A set of values and the valid operations
More informationType Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.
Outline Type Checking General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time
More informationSyntax-Directed Translation Part II
Syntax-Directed Translation Part II Chapter 5 Slides adapted from : Robert van Engelen, Florida State University Alessandro Artale, Free University of Bolzano Syntax-Directed Translation Schemes Syntax-directed
More informationEarlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl
Compilerconstructie najaar 2013 http://www.liacs.nl/home/rvvliet/coco/ Rudy van Vliet kamer 124 Snellius, tel. 071-527 5777 rvvliet(at)liacs(dot)nl college 1, dinsdag 3 september 2013 Overview 1 Why this
More informationWriting Evaluators MIF08. Laure Gonnord
Writing Evaluators MIF08 Laure Gonnord Laure.Gonnord@univ-lyon1.fr Evaluators, what for? Outline 1 Evaluators, what for? 2 Implementation Laure Gonnord (Lyon1/FST) Writing Evaluators 2 / 21 Evaluators,
More informationSemantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?
Semantic analysis and intermediate representations Which methods / formalisms are used in the various phases during the analysis? The task of this phase is to check the "static semantics" and generate
More informationOutline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference
Type Checking Outline General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time
More informationCSE302: Compiler Design
CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University April 24, 2007 Outline Recap Three
More informationType checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E
Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E if E then S while E do S S ; S Type checking of statements The purpose
More informationLECTURE 3. Compiler Phases
LECTURE 3 Compiler Phases COMPILER PHASES Compilation of a program proceeds through a fixed series of phases. Each phase uses an (intermediate) form of the program produced by an earlier phase. Subsequent
More informationWe now allow any grammar symbol X to have attributes. The attribute a of symbol X is denoted X.a
Attribute Grammars Attribute Grammars were invented by Don Knuth as a way to unify all of the stages of compiling into one. They give a formal way to pass semantic information (types, values, etc.) around
More informationCSC 467 Lecture 13-14: Semantic Analysis
CSC 467 Lecture 13-14: Semantic Analysis Recall Parsing is to translate token stream to parse tree Today How to build trees: syntax direction translation How to add information to trees: semantic analysis
More informationCOMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking
Agenda COMP 181 Type checking October 21, 2009 Next week OOPSLA: Object-oriented Programming Systems Languages and Applications One of the top PL conferences Monday (Oct 26 th ) In-class midterm Review
More informationUNIT IV INTERMEDIATE CODE GENERATION
UNIT IV INTERMEDIATE CODE GENERATION 2 Marks 1. Draw syntax tree for the expression a=b*-c+b*-c 2. Explain postfix notation. It is the linearized representation of syntax tree.it is a list of nodes of
More informationIntermediate Code Generation Part II
Intermediate Code Generation Part II Chapter 6: Type checking, Control Flow Slides adapted from : Robert van Engelen, Florida State University Static versus Dynamic Checking Static checking: the compiler
More informationAbstract Syntax Trees & Top-Down Parsing
Abstract Syntax Trees & Top-Down Parsing Review of Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationAbstract Syntax Trees & Top-Down Parsing
Review of Parsing Abstract Syntax Trees & Top-Down Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationIntermediate Code Generation
Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target
More informationCOS 320. Compiling Techniques
Topic 5: Types COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Types: potential benefits (I) 2 For programmers: help to eliminate common programming mistakes, particularly
More informationintermediate-code Generation
intermediate-code Generation }sequence of intermediate representations source program High Level intermediate Representation Low Level intermediate Representation Target Code e.g. C programming language
More informationData Types. Every program uses data, either explicitly or implicitly to arrive at a result.
Every program uses data, either explicitly or implicitly to arrive at a result. Data in a program is collected into data structures, and is manipulated by algorithms. Algorithms + Data Structures = Programs
More informationAbstract Syntax Trees & Top-Down Parsing
Review of Parsing Abstract Syntax Trees & Top-Down Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationPRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS
Objective PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS Explain what is meant by compiler. Explain how the compiler works. Describe various analysis of the source program. Describe the
More informationBuilding Compilers with Phoenix
Building Compilers with Phoenix Syntax-Directed Translation Structure of a Compiler Character Stream Intermediate Representation Lexical Analyzer Machine-Independent Optimizer token stream Intermediate
More informationNARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1
SEM / YEAR : VI / III CS2352 PRINCIPLES OF COMPLIERS DESIGN UNIT III INTERMEDIATE CODE GENERATION PART A 1. What are the benefits of intermediate code generation? (A.U May 2008) A Compiler for different
More informationCS558 Programming Languages
CS558 Programming Languages Fall 2016 Lecture 7a Andrew Tolmach Portland State University 1994-2016 Values and Types We divide the universe of values according to types A type is a set of values and a
More informationSemantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413
CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 13: Types and Type-Checking 19 Feb 07 Semantic Analysis Last time: Semantic errors related to scopes Symbol tables Name resolution This lecture:
More informationChapter 4. Action Routines
Chapter 4 Action Routines Syntax and Semantics In general: Syntax form Semantics meaning In programming languages: Syntax part of the language definition that can be described via a context-free grammar
More informationCompilers. Compiler Construction Tutorial The Front-end
Compilers Compiler Construction Tutorial The Front-end Salahaddin University College of Engineering Software Engineering Department 2011-2012 Amanj Sherwany http://www.amanj.me/wiki/doku.php?id=teaching:su:compilers
More informationExamples of attributes: values of evaluated subtrees, type information, source file coordinates,
1 2 3 Attributes can be added to the grammar symbols, and program fragments can be added as semantic actions to the grammar, to form a syntax-directed translation scheme. Some attributes may be set by
More informationJava Primer 1: Types, Classes and Operators
Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,
More informationOutline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring
Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,
More informationUNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.
UNIT-3 SYNTAX-DIRECTED TRANSLATION: A Grammar symbols are associated with attributes to associate information with the programming language constructs that they represent. Values of these attributes are
More information4. Semantic Processing and Attributed Grammars
4. Semantic Processing and Attributed Grammars 1 Semantic Processing The parser checks only the syntactic correctness of a program Tasks of semantic processing Checking context conditions - Declaration
More informationCS558 Programming Languages
CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and
More informationQuestion Bank. 10CS63:Compiler Design
Question Bank 10CS63:Compiler Design 1.Determine whether the following regular expressions define the same language? (ab)* and a*b* 2.List the properties of an operator grammar 3. Is macro processing a
More informationINF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright Instructors. What is a Data Type? A type is a collection of computational entities that share some common property Programming
More informationProf. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan
Language Processing Systems Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Semantic Analysis Compiler Architecture Front End Back End Source language Scanner (lexical analysis)
More informationCompiler Lab. Introduction to tools Lex and Yacc
Compiler Lab Introduction to tools Lex and Yacc Assignment1 Implement a simple calculator with tokens recognized using Lex/Flex and parsing and semantic actions done using Yacc/Bison. Calculator Input:
More informationTime : 1 Hour Max Marks : 30
Total No. of Questions : 6 P4890 B.E/ Insem.- 74 B.E ( Computer Engg) PRINCIPLES OF MODERN COMPILER DESIGN (2012 Pattern) (Semester I) Time : 1 Hour Max Marks : 30 Q.1 a) Explain need of symbol table with
More informationType Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413
Type Inference Systems CS412/CS413 Introduction to Compilers Tim Teitelbaum Type inference systems define types for all legal programs in a language Type inference systems are to type-checking: As regular
More informationContents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix
PGJC4_JSE8_OCA.book Page ix Monday, June 20, 2016 2:31 PM Contents Figures Tables Examples Foreword Preface xix xxi xxiii xxvii xxix 1 Basics of Java Programming 1 1.1 Introduction 2 1.2 Classes 2 Declaring
More informationError Handling Syntax-Directed Translation Recursive Descent Parsing
Error Handling Syntax-Directed Translation Recursive Descent Parsing Lecture 6 by Professor Vijay Ganesh) 1 Outline Recursive descent Extensions of CFG for parsing Precedence declarations Error handling
More information5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz
5. Semantic Analysis Mircea Lungu Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/
More information5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz
5. Semantic Analysis Mircea Lungu Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/
More informationCSE302: Compiler Design
CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University April 3, 2007 Outline Recap Syntax-directed
More informationΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 8a Syntax-directed Transla1on Elias Athanasopoulos
ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών Lecture 8a Syntax-directed Transla1on Elias Athanasopoulos eliasathan@cs.ucy.ac.cy Syntax-directed TranslaPon (SDT) Μετάφραση Κατευθυνόμενη από τη Σύνταξη We
More informationBuilding Compilers with Phoenix
Building Compilers with Phoenix Parser Generators: ANTLR History of ANTLR ANother Tool for Language Recognition Terence Parr's dissertation: Obtaining Practical Variants of LL(k) and LR(k) for k > 1 PCCTS:
More informationType Checking. Error Checking
Type Checking Error Checking Dynamic checking takes place while program is running Static checking takes place during compilation Type checks Flow-of-control checks Uniqueness checks Name-related checks
More informationCompiler Principle and Technology. Prof. Dongming LU April 15th, 2019
Compiler Principle and Technology Prof. Dongming LU April 15th, 2019 PART TWO 6. Semantic Analysis Contents Part One 6.1 Attributes and Attribute Grammars Part Two 6.2 Algorithms for Attribute Computation
More informationCSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1
CSE P 501 Compilers Static Semantics Hal Perkins Winter 2008 1/22/2008 2002-08 Hal Perkins & UW CSE I-1 Agenda Static semantics Types Attribute grammars Representing types Symbol tables Note: this covers
More informationSyntax-Directed Translation
Syntax-Directed Translation Grammar symbols are associated with attributes to associate information with the programming language constructs that they represent. Values of these attributes are evaluated
More informationSemantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University
Semantic Analysis CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Role of Semantic Analysis Syntax vs. Semantics: syntax concerns the form of a
More information