There is a level of correctness that is deeper than grammar. There is a level of correctness that is deeper than grammar

Similar documents
Context-sensitive Analysis. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

Context-sensitive Analysis

Grammars. CS434 Lecture 15 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Syntactic Directed Translation

Context-sensitive Analysis

CS 406/534 Compiler Construction LR(1) Parsing and CSA

Syntactic Directed Translation

Context-sensitive Analysis Part II

Context-sensitive Analysis Part II Chapter 4 (up to Section 4.3)

Type Checking. Error Checking

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables

Syntax Directed Translation

intermediate code generator syntax tree token stream intermediate representation parser checker tree

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Syntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans

Type systems. Static typing

5. Semantic Analysis!

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Intermediate Representations

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

COMP 181. Prelude. Prelude. Summary of parsing. A Hierarchy of Grammar Classes. More power? Syntax-directed translation. Analysis

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

More On Syntax Directed Translation

Intermediate Code Generation Part II

Syntax-Directed Translation

Context-sensitive Analysis Part IV Ad-hoc syntax-directed translation, Symbol Tables, andtypes

Static Checking and Type Systems

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

Intermediate Code Generation

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

1 Lexical Considerations

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Semantic Analysis Attribute Grammars

Syntax-Directed Translation Part I

Concepts Introduced in Chapter 6

CMSC 330: Organization of Programming Languages

Syntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

Syntax-Directed Translation. Introduction

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

Concepts Introduced in Chapter 6

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Syntax-Directed Translation

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Semantic Analysis with Attribute Grammars Part 1

Semantic analysis. Syntax tree is decorated with typing- and other context dependent

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Syntax-Directed Translation Part II

The compilation process is driven by the syntactic structure of the program as discovered by the parser

COP5621 Exam 3 - Spring 2005

Semantic Analysis. Lecture 9. February 7, 2018

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Semantic Analysis. Compiler Architecture

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Semantic Analysis and Type Checking

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 309

Computing Inside The Parser Syntax-Directed Translation. Comp 412

The role of semantic analysis in a compiler

Compiler Principle and Technology. Prof. Dongming LU April 15th, 2019

Principles of Programming Languages

Short Notes of CS201

Computer Components. Software{ User Programs. Operating System. Hardware

Semantics of programming languages

CS201 - Introduction to Programming Glossary By

Semantic actions for expressions

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Programming Languages Third Edition. Chapter 7 Basic Semantics

Lecture 14 Sections Mon, Mar 2, 2009

Lexical Considerations

TDDD55 - Compilers and Interpreters Lesson 3

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

Lexical Considerations

Type Checking. Chapter 6, Section 6.3, 6.5

Syntax Directed Translation

Syntax-Directed Translation

CSCI Compiler Design

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412

Part 5 Program Analysis Principles and Techniques

Compilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1

Semantic actions for declarations and expressions

Semantic Analysis with Attribute Grammars Part 3

Semantic actions for declarations and expressions. Monday, September 28, 15

Symbol Tables. For compile-time efficiency, compilers often use a symbol table: associates lexical names (symbols) with their attributes

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Chapter 4 :: Semantic Analysis

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1

Transcription:

Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,b,c,d) int a, b, c, d; { } What is wrong with this program? (let me count the ways ) fee() { int f[3],g[0], h, i, j, k; char *p; fie(h,i, ab,j, k); k = f * i + j; h = g[7]; printf( <%s,%s>.\n, p,q); p = 0; } CMSC430 Spring 2007 Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,b,c,d) int a, b, c, d; { } fee() { int f[3],g[0], h, i, j, k; char *p; fie(h,i, ab,j, k); k = f * i + j; h = g[7]; printf( <%s,%s>.\n, p,q); p = 0; } What is wrong with this program? (let me count the ways ) declared g[0], used g[7] wrong number of args to fie() ab is not an int wrong dimension on use of f undeclared variable q 0 is not a character string All of these are deeper than syntax To generate code, we need to understand its meaning! CMSC430 Spring 2007 2

Beyond Syntax To generate code, the compiler needs to answer many questions Is x a scalar, an array, or a function? Is x declared? Are there names that are not declared? Declared but not used? Which declaration of x does each use reference? Is the expression x * y + z type-consistent? In a[i,j,k], does a have three dimensions? Where can z be stored? (register, local, global, heap, static) In f 5, how should 5 be represented? How many arguments does fie() take? What about printf ()? Does *p reference the result of a malloc()? Do p & q refer to the same memory location? Is x defined before it is used? These are beyond a CFG CMSC430 Spring 2007 3 Beyond Syntax These questions are part of context-sensitive analysis Answers depend on values, not parts of speech Questions & answers involve non-local information Answers may involve computation How can we answer these questions? Use formal methods > Context-sensitive grammars? > Syntax directed translation using attribute grammars? (attributed grammars?) Use ad-hoc techniques > Symbol tables > Ad-hoc code (action routines) In scanning & parsing, formalism won; different story here. CMSC430 Spring 2007 4 2

Beyond Syntax Telling the story using Syntax Directed Definitions with attribute grammars: The attribute grammar formalism is important > Succinctly makes many points clear > Sets the stage for actual, ad-hoc practice The problems with attribute grammars motivate practice > Non-local computation > Need for centralized information Some folks in the community still argue for attribute grammars > Knowledge is power > Information is immunization We will cover attribute grammars, then move on to ad-hoc ideas CMSC430 Spring 2007 5 Attribute grammars Generalization of context-free grammar Each grammar symbol has an associated set of attributes Augment grammar with rules that define values High-level specification, independent of evaluation scheme Dependences between attributes Values are computed from constants & other attributes Synthesized attribute - value computed from children Inherited attribute - value computed from siblings & parent CMSC430 Spring 2007 6 3

Syntax Directed Definitions (SDD) What is an attribute grammar? A context-free grammar augmented with a set of rules Each symbol in the derivation has a set of values, or attributes The rules specify how to compute a value for each attribute Example grammar Number Sign Sign + 0 This grammar describes signed binary numbers We would like to augment it with rules that compute the decimal value of each valid input string CMSC430 Spring 2007 7 Examples For Number Sign Number Sign For 0 Number Sign Sign Sign Sign Sign Sign 0 Sign 0 0 Number Sign 0 We will use these two throughout the lecture CMSC430 Spring 2007 8 4

Attribute Grammars Add rules to compute the decimal value of a signed binary number Productions Attribution Rules Number Sign.pos 0 Number.val If Sign.neg then.val else.val Sign + Sign.neg false Sign.neg true 0.pos 0.pos +.pos 0.pos 0.val.val +.val.pos.pos.val.val 0.val 0.val 2.pos Symbol Number Sign Attributes val neg pos, val pos, val CMSC430 Spring 2007 9 Back to the Examples For Number.val.val Number neg true.pos 0 Sign.val.val.pos 0.val 2.pos One possible evaluation order:.pos 2 Sign.neg 3.pos 4.val 5.val 6 Number.val Other orders are possible Knuth suggested a data-flow model for evaluation Independent attributes first Others in order as input values become available Evaluation order must be consistent with the attribute dependence graph Rules + parse tree imply an attribute CMSC430 Spring 2007 dependence graph 0 5

Back to the Examples Number val: 5 Sign neg: true pos: 2 pos: 2 pos: 0 pos: val: 0 pos: 0 val: 5 pos: 0 val: For 0 This is the complete attribute dependence graph for 0. It shows the flow of all attribute values in the example. Some flow downward inherited attributes Some flow upward synthesized attributes A rule may use attributes in the parent, children, or siblings of a node CMSC430 Spring 2007 The Rules of the Game Attributes associated with nodes in parse tree Rules are value assignments associated with productions Attribute is defined once, using local information Label identical terms in production for uniqueness Rules & parse tree define an attribute dependence graph > Graph must be non-circular This produces a high-level, functional specification Synthesized attribute > Depends on values from children Inherited attribute > Depends on values from siblings & parent CMSC430 Spring 2007 2 6

Using Attribute Grammars Attribute grammars can specify context-sensitive actions Take values from syntax Perform computations with values Insert tests, logic, Synthesized Attributes Use values from children & from constants S-attributed grammars Evaluate in a single bottom-up pass Good match to LR parsing Inherited Attributes Use values from parent, constants, & siblings directly express context can rewrite to avoid them Thought to be more natural Not easily done at parse time We want to use both kinds of attribute CMSC430 Spring 2007 3 Evaluation Methods Dynamic, dependence-based methods Build the parse tree Build the dependence graph Topological sort the dependence graph Define attributes in topological order Rule-based methods Analyze rules at compiler-generation time Determine a fixed (static) ordering Evaluate nodes in that order Imagine a diminutive person who knows how to evaluate attributed trees... (treewalk) Oblivious methods (passes, dataflow) Ignore rules & parse tree Pick a convenient order (at design time) & use it CMSC430 Spring 2007 4 7

Back to the Example Number Sign 0 For 0 CMSC430 Spring 2007 5 Back to the Example Number val: Sign neg: pos: val: pos: 0 val: pos: val: pos: val: pos: val: 0 pos: val: For 0 CMSC430 Spring 2007 6 8

Back to the Example Number val: 5 Sign neg: true pos: 0 val: 5 Inherited Attributes pos: pos: 0 val: pos: 2 pos: 2 0 pos: val: 0 For 0 CMSC430 Spring 2007 7 Back to the Example Number val: 5 Sign neg: true pos: 0 val: 5 Synthesized attributes pos: pos: 0 val: pos: 2 pos: 2 0 pos: val: 0 For 0 CMSC430 Spring 2007 8 9

Back to the Example Number val: 5 Sign neg: true pos: 0 val: 5 Synthesized attributes pos: pos: 0 val: pos: 2 pos: 2 0 pos: val: 0 For 0 CMSC430 Spring 2007 9 Back to the Example Number val: 5 Sign neg: true pos: pos: 0 val: 5 pos: 0 val: If we show the computation... & then peel away the parse tree... pos: 2 pos: 2 0 pos: val: 0 For 0 CMSC430 Spring 2007 20 0

Back to the Example val: 5 All that is left is the attribute dependence graph. neg: true pos: 0 val: 5 This succinctly represents the flow of values in the problem instance. pos: 2 pos: 2 pos: 0 pos: val: 0 pos: 0 val: For 0 The dynamic methods sort this graph to find independent values, then work along graph edges. The rule-based methods try to discover good orders by analyzing the rules. The oblivious methods ignore the structure of this graph. The dependence graph must be acyclic CMSC430 Spring 2007 2 Circularity We can only evaluate acyclic instances We can prove that some grammars can only generate instances with acyclic dependence graphs Largest such class is strongly non-circular grammars (SNC ) SNC grammars can be tested in polynomial time Failing the SNC test is not conclusive Many evaluation methods discover circularity dynamically Bad property for a compiler to have SNC grammars were first defined by Kennedy & Warren CMSC430 Spring 2007 22

A Circular Attribute Grammar Productions Attribution Rules Number.a 0 0.a 0.a + 0.b.b.c.b +.val 0.b 0.a + 0.c +.val 0.val 0.val 2.pos CMSC430 Spring 2007 23 An Extended Example Grammar for a basic block ( 4.3.3) Block 0 Block Assign Assign Assign Ident = Expr ; Expr 0 Expr + Term Expr Term Term Term 0 Term * Factor Term / Factor Factor Factor ( Expr ) Number Identifier Let s estimate cycle counts Each operation has a COST Add them, bottom up Assume a load per value Assume no reuse Simple problem for an AG Hey, this looks useful! CMSC430 Spring 2007 24 2

An Extended Example Adding attribution rules (continued) Block0 Block Assign Block0.cost Block.cost + Assign.cost Assign Block0.cost Assign.cost Assign Ident = Expr ; Assign.cost COST(store) + Expr.cost Expr0 Expr + Term Expr0.cost Expr.cost + COST(add) + Term.cost Expr Term Expr0.cost Expr.cost + COST(add) + Term.cost Term Expr0.cost Term.cost Term0 Term * Factor Term0.cost Term.cost + COST(mult ) + Factor.cost Term / Factor Term0.cost Term.cost + COST(div) + Factor.cost Factor Term0.cost Factor.cost Factor ( Expr ) Factor.cost Expr.cost Number Factor.cost COST(loadi) Identifier Factor.cost COST(load) All the attributes are synthesized! CMSC430 Spring 2007 25 An Extended Example Properties of the example grammar All attributes are synthesized S-attributed grammar Rules can be evaluated bottom-up in a single pass > Good fit to bottom-up, shift/reduce parser Easily understood solution Seems to fit the problem well What about an improvement? Values are loaded only once per block (not at each use) Need to track which values have been already loaded CMSC430 Spring 2007 26 3

A Better Execution Model Adding load tracking Need sets Before and After for each production Must be initialized, updated, and passed around the tree Factor ( Expr ) Factor. cost Expr.co st ; Expr.B efore Factor. Before ; Factor. After Expr.Af ter Number Factor. cost COST(loadi) ; Factor. After Factor. Before Identifi er If (Identifi er.name Factor. Before) then Factor. cost COST(load); Factor. After Factor. Before Identifi er.name else Factor. cost 0 Factor. After Factor. Before This looks more complex! CMSC430 Spring 2007 27 A Better Execution Model Load tracking adds complexity But, most of it is in the copy rules Every production needs rules to copy Before & After A sample production Expr 0 Expr + T erm Expr 0.cost Expr.cost + COST(add) + Term.cost ; Expr.Before Expr 0.Before ; Term.Before Expr.After; Expr 0.After Term.After These copy rules multiply rapidly Each creates an instance of the set Lots of work, lots of space, lots of rules to write CMSC430 Spring 2007 28 4

An Even Better Model What about accounting for finite register sets? Before & After must be of limited size Adds complexity to Factor Identifier Requires more complex initialization Jump from tracking loads to tracking registers is small Copy rules are already in place Some local code to perform the allocation Next Curing these problems with ad-hoc syntax-directed translation CMSC430 Spring 2007 29 Remember the previous example? Grammar for a basic block Block 0 Block Assign Assign Assign Ident = Expr ; Expr 0 Expr + Term Expr Term Term Term 0 Term * Factor Term / Factor Factor Factor ( Expr ) Number Identifier Let s estimate cycle counts Each operation has a COST Add them, bottom up Assume a load per value Assume no reuse Simple problem for an AG Hey, this looks useful! CMSC430 Spring 2007 30 5

And Its Extensions Tracking loads Introduced Before and After sets to record loads Added 2 copy rules per production > Serialized evaluation into execution order Made the whole attribute grammar large & cumbersome Finite register set Complicated one production (Factor Identifier) Needed a little fancier initialization Changes were quite limited Why is one change hard and the other easy? CMSC430 Spring 2007 3 The Moral of the Story Non-local computation needed lots of supporting rules Complex local computation was relatively easy The Problems Copy rules increase cognitive overhead Copy rules increase space requirements > Need copies of attributes > Can use pointers, for even more cognitive overhead Result is an attributed tree (somewhat subtle points) > Must build the parse tree > Must search tree for answers CMSC430 Spring 2007 32 6

Addressing the Problem If you gave this problem to a chief programmer Introduce a central repository for facts Table of names > Field in table for loaded/not loaded state Avoids all the copy rules, allocation & storage headaches All inter-assignment attribute flow is through table > Clean, efficient implementation > Good techniques for implementing the table (hashing) > When its done, information is in the table! (no navigation) > Cures most of the problems Unfortunately, this design violates the functional paradigm > Do we care? CMSC430 Spring 2007 33 The Realist s Alternative Ad-hoc syntax-directed translation Associate a snippet of code with each production At each reduction, the corresponding snippet runs Allowing arbitrary code provides complete flexibility > Includes ability to do tasteless & bad things To make this work Need names for attributes of each symbol on lhs & rhs > Typically, one attribute passed through parser + arbitrary code (structures, globals, statics, ) > Yacc introduced $$, $, $2, $n, left to right A ::= B C {$$ = concat($,$2)} Need an evaluation scheme > Fits nicely into LR() parsing algorithm CMSC430 Spring 2007 34 7

Reworking the Example (with load tracking) Block 0 Block Assign Assign cost 0; Assign Ident = Expr ; cost cost + COST(store); Expr 0 Expr + Term cost cost + COST(add); Expr Term cost cost + COST(sub); Term Term 0 Term * Factor cost cost + COST(mult); Term / Factor cost cost + COST(div); Factor Factor ( Expr ) Number cost cost + COST(loadi); Identifier { i hash(identifier); if (Table[i].loaded = false) then { cost cost + COST(load); Table[i].loaded true; } } This looks cleaner & simpler! CMSC430 Spring 2007 35 Example Building a Parse Tree Assume constructors for each node Assume stack holds pointers to nodes Assume yacc syntax Goal Expr $$ = $; Expr Expr + Term $$ = MakeAddNode($,$3); Expr Term $$ = MakeSubNode($,$3); Term $$ = $; Term Term * Factor $$ = MakeMulNode($,$3); Term / Factor $$ = MakeDivNode($,$3); Factor $$ = $; Factor ( Expr ) $$ = $; number $$ = MakeNumNode(token); id $$ = MakeIdNode(token); CMSC430 Spring 2007 36 8

Reality Most parsers are based on this ad-hoc style of context-sensitive analysis Advantages Addresses the shortcomings of the AG paradigm Efficient, flexible Disadvantages Must write the code with little assistance Programmer deals directly with the details Most parser generators support a yacc-like notation CMSC430 Spring 2007 37 Typical Uses Building a symbol table > Enter declaration information as processed > At end of declaration syntax, do some post processing > Use table to check errors as parsing progresses Note: this assumes table is global Simple error checking/type checking > Define before use lookup on reference > Dimension, type,... check as encountered > Type conformability of expression bottom-up walk > Procedure interfaces are harder Build a representation for parameter list & types Create list of sites to check Either check offline, or handle the cases for arbitrary orderings CMSC430 Spring 2007 38 9

Is This Really Ad-hoc? Relationship between practice and attribute grammars Similarities Both rules & actions associated with productions Application order determined by tools, not author (Somewhat) abstract names for symbols Differences Actions applied as a unit; not true for AG rules Anything goes in ad-hoc actions; AG rules are functional AG rules are higher level than ad-hoc actions CMSC430 Spring 2007 39 Limitations Forced to evaluate in a given order: postorder > Left to right only > Bottom up only Implications > Declarations before uses > Context information cannot be passed down How do you know what rule you are called from within? Example: cannot pass bit position from right down : $$ = 2*$ + $2 > Could you use globals? In this case we could get the position from the left, which is not much help (and it requires initialization) CMSC430 Spring 2007 40 20

Alternative Strategy Build Abstract Syntax Tree > Use tree walk routines > Use visitor design pattern to add functionality TreeNodeVisitor VisitAssignment(AssignmentNode) VisitVariableRef(VariableRefNode) TypeCheckVisitor AnalysisVisitor VisitAssignment(AssignmentNode) VisitVariableRef(VariableRefNode) VisitAssignment(AssignmentNode) VisitVariableRef(VariableRefNode) CMSC430 Spring 2007 4 Visitor Treewalk I Parallel structure of tree: > Separates treewalk code from node handling code > Facilitates processing change without change to tree structure TreeNode Accept(NodeVisitor) AssignmentNode VariableRefNode Accept(NodeVisitor v) Accept(NodeVisitor v) v.visitassignment(this) v.visitvariableref(this) CMSC430 Spring 2007 42 2

Visitor Treewalk II VisitAssignment(aNodePtr) // preprocess assignment (anodeptr->rhs)->accept(this); // postprocess rhs info; (anodeptr->lhs)->accept(this); // postprocess assignment; Refers to current visitor! To start the process: AnalysisVisitor a; treeroot->accept(a); CMSC430 Spring 2007 43 Summary: Strategies for Context-Sensitive Analysis Attribute Grammars > Pros: Formal, powerful, can deal with propagation strategies > Cons: Too many copy rules, no global tables, works on parse tree Postorder Code Execution > Pros: Simple and functional, can be specified in grammar (Yacc) but does not require parse tree > Cons: Rigid evaluation order, no context inheritance Generalized Tree Walk > Pros: Full power and generality, operates on abstract syntax tree (using Visitor pattern) > Cons: Requires specific code for each tree node type, more complicated CMSC430 Spring 2007 44 22

Type systems Types Values that share a set of common properties defined by language and/or programmer Type system. set of types in a programming language, and 2. rules that use types to specify program behavior Example type rules If operands of addition are of type integer, then result is of type integer The result of the unary & operator is a pointer to the object referred to by the operand Advantages of typed languages Ensure run-time safety Expressiveness (overloading, polymorphism) Provide information for code generation CMSC430 Spring 2007 45 Type checking Type checker Enforces rules of type system May be strong/weak, static/dynamic Static type checking Performed at compile time Early detection, no run-time overhead Not always possible (e.g., A[i]) Dynamic type checking Performed at run time More flexible, rapid prototyping Overhead to check run-time type tags CMSC430 Spring 2007 46 23

Type expressions Type expressions Used to represent the type of a language construct Describes both language and programmer types Examples Basic types: integer, real, character,... Constructed types: arrays, records, pointers, functions,... Constructing new types arrays array(..0, T) records T x T2 pointers pointer(t) functions T x T2 x T n CMSC430 Spring 2007 47 A simple type checker Using a synthesized attribute grammar, we will describe a type checker for arrays, pointers, statements, and functions. Grammar for source language: P ::= L ; E L ::= L ; D D D ::= D ; E id: T T ::= char integer array [num] of T T E ::= literal num id E mod E E[E] E Basic types char, integer, typeerror assume all arrays start at, e.g., array [256] of char results in the type expression array(..256,char) builds a pointer type, so integer results in the type expression pointer(integer) CMSC430 Spring 2007 48 24

Type checking example Partial attribute grammar for the type system L ::= L D L ::= D D ::= id: T {addtype(id.entry, T.type) } T ::= char {T.type char } T ::= integer {T.type integer } T ::= T { T.type pointer(t.type) } T ::= array [num] of T { T.type array(:..num.val, T.type) } CMSC430 Spring 2007 49 Type checking expressions Each expression is assigned a type using rules associated with the grammar. E ::= literal { E.type char } E ::= num { E.type integer } E ::= id { E.type lookup(id.entry) } E ::= E mod E2 { E.type if E.type = integer and E 2.type = integer then integer else typeerror} E ::= E [E2] { E.type if E 2.type = integer and E.type = array(s,t) then t else typeerror } E ::= E { E.type if E.type = pointer then t such that pointer(e,t) else typeerror } CMSC430 Spring 2007 50 25

Type checking statements Statements do not typically have values, therefore we assign them the type void. If an error is detected within the statement, it gets type typeerror. S ::= id E { S.type if id.type = E.type then void else typeerror } S ::= if E then S S ::= while E do S S ::= S ; S 2 { S.type if E.type = boolean then S.type else typeerror } { S.type if E.type = boolean then S.type else typeerror } { S.type if S.type = void then S 2.type else typeerror } CMSC430 Spring 2007 5 Type checking functions We add two new productions to the grammar to represent function declarations and applications T ::= T T declaration E ::= E ( E ) application To capture the argument and return type, we use T ::= T T 2 {T.type (T.type T 2.type) } E ::= E ( E 2 ) { E.type if E.type = s t and E 2.type = s then t else typeerror } Note: We could avoid functions addtype and lookup by adding a new inherited attribute symboltable which was a set that kept track of declarations, and was passed down parse tree. (How?) CMSC430 Spring 2007 52 26