Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CPSC 388 – Compiler Design and Construction
Semantics Static semantics Dynamic semantics attribute grammars
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
1 Compiler Construction Intermediate Code Generation.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
Chapter 6 Type Checking Section 0 Overview 1.Static Checking Check that the source program follows both the syntactic and semantic conventions of the source.
Compiler Construction
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 Beyond syntax analysis An identifier named x has been recognized. Is x a scalar, array or function? How big is x? If x is a function, how many and what.
Lecture # 17 Syntax Directed Definition. 2 Translation Schemes A translation scheme is a CF grammar embedded with semantic actions rest  + term { print(“+”)
Honors Compilers Semantic Analysis and Attribute Grammars Mar 5th 2002.
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
Chapter 6 Type Checking Section 0 Overview
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
Syntax-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Semantic Analysis Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Semantic Analysis1 Checking what parsers cannot.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Overview of Previous Lesson(s) Over View  An ambiguous grammar which fails to be LR and thus is not in any of the classes of grammars i.e SLR, LALR.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
Overview of Previous Lesson(s) Over View  In syntax-directed translation 1 st we construct a parse tree or a syntax tree then compute the values of.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Syntax Directed Definition and Syntax directed Translation
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 9 Ahmed Ezzat Semantic Analysis.
Lecture 9 Symbol Table and Attributed Grammars
Semantic Analysis Chapter 4.
Context-Sensitive Analysis
A Simple Syntax-Directed Translator
Constructing Precedence Table
Chapter 5 Syntax Directed Translation
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Syntax-Directed Translation Part I
Semantic Analysis Chapter 6.
CS 3304 Comparative Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 4 Action Routines.
SYNTAX DIRECTED DEFINITION
Compiler Construction
Chapter 5 Syntax Directed Translation
Compiler Construction
Presentation transcript:

Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Outline Syntax-directed specifications of language semantics –Syntax-directed definitions –Syntax-directed translation schemes Semantic analysis –Focus on type analysis

SYNTAX-DIRECTED SPECIFICATIONS OF LANGUAGE SEMANTICS

Overview of syntax-directed specifications of semantics Semantics can be expressed by: –Attaching attributes to grammar symbols –Specifying how to compute those attributes: By adding semantic rules, we get a syntax-directed definition (SDD) By adding semantic actions, we get a syntax- directed translation scheme (SDT)

Examples of attributes: values of evaluated subtrees Grammar: E → E + T E → T T → num Grammar: E → E + T E → T T → num E E+ T T num E.val = 7 E.val = 3 T.val = 3 num.val = 4 num.val = 3 String: String: 3 + 4

Examples of attributes: (line, column) in source file Grammar: E → E + T E → T T → num Grammar: E → E + T E → T T → num E E+ T T num E.coord = (1,1) to (1,6) E.coord = (1,1) to (1,2) T.coord = (1,1) to (1,2) num.coord = (1,5) to (1,6) num.coord = (1,1) to (1,2) String: 3 ❏ + ❏ 4 String: 3 ❏ + ❏ 4 +.coord = (1,3) to (1,4)

Examples of attributes: data types Grammar: E → E + T E → T T → num Grammar: E → E + T E → T T → num E E+ T T num E.type = int T.type = int num.type = int String: String: 3 + 4

SDDs vs. SDTs SDDs Do not specify any evaluation order for the semantic rules The rules appear at the end of the production bodies The semantic rules may only have controlled side effects Mostly useful for specification SDTs Explicitly specify an evaluation order for the semantic actions The actions may appear anywhere in the production bodies The semantic actions may be arbitrary code fragments Mostly useful for implementation

Syntax-directed definitions SDD of values of evaluated subtrees: ProductionRules E → E 1 + TE.val = E 1.val + T.val E → TE.val = T.val T → numT.val = num.val Each node has an attribute val holding the value of its evaluated subtree

Types of attributes An attribute at a parse tree node N may be of two kinds: –Synthesized Computed in terms of attributes at the children of N and/or other attributes at N –Inherited Assigned to N at the parent of N

Annotated parse tree for “3 + 4” E.val = 7 E.val = 3 T.val = 3 num.val = 3 T.val = 4 num.val = 4 +

Inherited attributes After eliminating left recursion from the previous grammar: ProductionRules E → T E'E'.inh = T.val E.val = E'.syn E' → + T E' 1 E' 1.inh = E'.inh + T.val E'.syn = E' 1.syn E' → ε E'.syn = E'.inh T → numT.val = num.val

Annotated parse tree for “3 + 4” T.val = 3 E.val = 7 num.val = 3 E'.inh = 3 E'.syn = 7 + T.val = 4 E'.inh = 7 E'.syn = 7 num.val = 4 ε

Classes of SDDs S-attributed SDDs: –Only synthesized attributes L-attributed SDDs: –Inherited attributes depend on attributes of symbols to the left in the production (including the head) –Attributes at a node N can also depend on other attributes at N, if it does not introduce dependency cycles In the first lab, you used an L-attributed SDD In the third lab, you will use an S-attributed SDD L S

Example of non-L-attributed SDD ProductionSemantic rules A → B CA.syn = B.syn; B.inh = f(C.syn, A.syn)

Example SDD Grammar for mathematical functions: E → E + E E → E * E E → num | x | ( E ) Goal: specify an SDD for the translation of an expression into its derivative (as a string), recalling the rules: x’ = 1 n’ = 0 (f * g)’ = f’ * g + f * g’ (f + g)’ = f’ + g’

Example SDD ProductionRules E → E 1 + E 2 E.expr = E 1.expr || ”+” || E 2.expr E.der = ”(” || E 1.der || ”+” || E 2.der || ”)” E → E 1 * E 2 E.expr = E 1.expr || ”*” || E 2. expr E.der = ”(” || E 1.der || ”*” || E 2. expr || ”+” || E 1.expr || ”*” || E 2.der || ”)” E → xE.expr = ”x” E.der = ”1” E → numE.expr = num.val E.der = ”0” E → ( E 1 )E.expr = ”(” || E 1.expr || ”)” E.der = E 1.der Where || is the string concatenation operator

Exercise (1) Draw the parse tree for the string 2*(x + x) Then decorate it using the SDD on the previous slide.

Exercise (2) Complete the following SDD of values of evaluated subtrees. ProductionRules E → T E‘E'.inh = T.val E.val = E'.syn E' → + T E' 1 E' 1.inh = E'.inh + T.val E'.syn = E' 1.syn E' → εE'.syn = E'.inh T → F T'? T' → * F T' 1 ? T' → ε? F → numF.val = num.val F → ( E )?

SEMANTIC ANALYSIS

Semantic analysis Why? –Not all errors are lexical or syntactical –Needed to generate correct code When? –Can be done during parsing (semantic actions) –Easier in separate passes on some intermediate program representation

Semantic analysis – name analysis Examples of name analyses from trac42: –Is a referenced variable declared? –Is a variable uniquely declared in the scope? –Is a called function declared/defined? void my_func(void) { int x = y + 23 * my_fnuc(); char x = ‘x’; }

Semantic analysis – type analysis Examples of type analyses from trac42: –Is an operator applied to operands of the right types? –Is a function called with the right number of arguments? –Are the function arguments of the right types? int my_func(int arg) { int x = arg * “bla bla bla”; int y = my_func(23, 78); return my_func(37.8); }

More examples of semantic analyses Is a “break” statement enclosed in a loop or a switch? (C, C++, C#, Java…) From ADA: The beginning and end of blocks should be tagged with the same name

Static vs. dynamic checks Static checks are done during compilation –Static type checks requires type specifications by the programmer or type inference by the compiler Dynamic checks are done during runtime –Dynamic type checks require type information to be carried with data objects. Examples: A ” type” member in structs Vpointers and vtables in OO languages Trac42 needs only static checks

Type analysis Type information is gathered from: –Declarations of variables and functions char str[256]; int some_func(float arg); –Format of constants x = 15.7f; c = ’a’; z = 98ul; A type system specifies how to assign type attributes to program parts –More on this in the next lecture

What is a type? Specification of: –The size needed to store a data object –How to interpret the stored data Examples: –Unsigned short: needs 16 bits and is interpreted as a number from 0 to –Signed short: needs 16 bits and is interpreted as a number from to 32767

Using types for code generation unsigned x; unsigned y = 24; unsigned z = 6; x = y / z; … divl -8(%ebp) … int x; int y = 24; int z = 6; x = y / z; … idivl -8(%ebp) …

Type conversions Changes the type of a data object Explicit: int a = 12; float b = (float) a; void* p_v = (void*) &a; int* p_i = (int*) p_v; Implicit (coercions): inferred by the compiler: char a = 12; int b = a; float x = 17; Trac42 does not have type conversions

Representing types Type expressions: –Basic types, e.g., int, char, float void – No value error – Erroneous type –Type names –Type constructors. Examples: int* p;pointer(int) int a[27];array(27, int); int f(char a, float b);char × float → int

Representing types – type constructors pointer(T) –Pointer to an object of type T array(I, T) –Array with I nr of elements of type T T 1 × T 2 –Product of types T 1 and T 2

Representing types – type constructors Records. Similar to products, but includes the member name. –Example: struct A { int a; char b; }; record((a x integer) x (b x char)) T 1 → T 2 –Function taking the type T 1 as argument and returning T 2 –T 1 is often a product type

Tree representation of type expressions Example: –int* my_func(char a, char b); –Type of my_func: char × char → pointer(int) × → char pointer int

Exercise (3) Write the type expression and draw it as a tree for the function char** your_func(int a, char* b, float c);

Conclusion SDDs and SDTs are two similar ways to attach semantics to a grammar Attributes on grammar symbols can be synthesized or inherited Data types are needed to guard against errors and to generate correct code Types can be represented as type expressions

Next time More type analysis