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

Size: px
Start display at page:

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

Transcription

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

2 Adding Attributes We connect the grammar rules with attributes E.g. to implement type-checking or code generation A step from pure parsing towards translation input tokens parse tree dependency graph evaluation Parse-tree with attributes: annotated parse-tree Creating the tree: annotation or decoration The attributes are evaluated via semantic rules The order is specified by the dependency graph Parse tree and depend. graph may be implicit E.g. in a recursive descent parser the structure of the code tells the evaluation order (parameters, return values) Laszlo Böszörmenyi Compilers Attributed Grammars - 2

3 Syntax-Directed Definitions (SDD) An SDD is a cfg + attributes + semantic rules Attributes associated to symbols Rules associated to productions X.c denotes attribute c of node X in the parse tree Set of rules b := f(c 1, c 2,... c n ) associated to A α c 1, c 2, c 3,... c n are the attributes belonging to the symbols of the production b may an attribute of A (A.b: synthesized attribute) or an attribute of a symbol from α (α i.b: inherited attribute) f is a side-effect free function If side-effect needed (e.g. print result): dummy attribute Laszlo Böszörmenyi Compilers Attributed Grammars - 3

4 Synthesized and Inherited Attributes Synthesized attribute A.c of A α depends on The children of node A and itself Can be evaluated in any bottom-up order (e.g. depth-first) Also terminals may have synthesized attributes Lexical values (e.g. number), computed by the lexical analyzer Inherited attribute B.c of A α 1 Bα 2 depends on The father and siblings of node B and itself Terminals may not have inherited attributes (per definition) Combined attributes Evaluation order not trivial May even contain cycles! Production Sem. Rules A.s A B A.s:= B.i B.i B.i:= A.s+1 Laszlo Böszörmenyi Compilers Attributed Grammars - 4

5 Dependency Graph If b:= f(c), i.e. b depends on c c must be computed before b In the dependency graph: c b Example, A XY A.a = f(x.x, Y.y) (synthesized) X.x = g(a.a, Y.y) (inherited) Dep. Gr. defines a partial order A Y X and Y A X are both corrects orders A topological sort creates a total order m 1, m 2,... m k If m i m k then evaluate m i before m k For the other cases the top. sort creates arbitrary order No topological sort exists, if the graph contains cycles Laszlo Böszörmenyi Compilers Attributed Grammars - 5 X.x A.a Y.y X.x A.a Y.y

6 S-attributed Definitions Contain only synthesized attributes Evaluated depth-first (post order) L.du = 19 3 * dfvisit (node n) { E.v = 19 nl for each { (child m of n from left) dfvisit(m); } evaluate attributes of n; } E 1.v = 15 + T.v = 4 Production Semantic Rule 1. L E nl L.du:= E.v ~print(e.v) 2. E E 1 + T E.v:= E 1.v + T.v 3. E T E.v:= T.v 4. T T 1 * F T.v:= T 1.v * F.v 5. T F T.v:= F.v 6. F (E) F.v:= E.v 7. F digit F.v:= digit.v T.v = 15 F.v = 4 T 1.v = 3 * F.v = 5 d.v = 4 F.v = 3 d.v = 3 d.v = 5 Post-order (Polish) notation 3*5+4 35* *(5+4) 354+* 27 Laszlo Böszörmenyi Compilers Attributed Grammars - 6

7 Bottom-up Evaluation for S-attr. Def.s With a recursive descent (LL(1)) parser Evaluation at ascending from the recursion With an LR (shift-reduce) parser Evaluation at reduction We push the input on the state stack (shift) If the top of stack is the right side of a production, we replace it by the left-hand non-terminal (reduction) See more at bottom-up parsing Evaluation of an expression grammar We push the operands on an expression stack We pop them for evaluation and push the result back A stack-machine may be implemented in hw. or JVM Laszlo Böszörmenyi Compilers Attributed Grammars - 7

8 Value Stack Production Evaluation stack 1. L E nl print( top() ) 2. E E 1 + T push( pop() + pop() ) 3. E T 4. T T 1 * F push( pop() * pop() ) 5. T F 6. F digit push (digit) L.du = 19 E.v = 19 3 * nl E 1.v = 15 + T.v = 4 T.v = 15 F.v = 4 T 1.v = 3 * F.v = 5 d.v = 4 F.v = 3 d.v = 3 d.v = 5 Input State Value Production 3*5+4 nl - - *5+4 nl 3 *5+4 nl F 3 F digit *5+4 nl T 3 T F 5+4 nl T* 3 +4 nl T* nl T*F 3 5 F digit +4 nl T 15 T T * F +4 nl E 15 E T 4 nl E+ 15 nl E+4 15 nl E+F 15 4 F digit nl E+T T F nl E 19 E E + T E nl 19 L 19 print Laszlo Böszörmenyi Compilers Attributed Grammars - 8

9 Inherited Attributes - Example Express well context sensitivity Nodes inherit from father + siblings Example type declaration T.type is synthesized L.in is inherited T.type=real real id 1, id 2, id 3 D L.in=real Production Semantic Rule 1. D T L L.in := T.type 2. T int T.type := integer real L.in=real L.in=real,, id id id 3.type=real 3. T real T.type := real 4. L L 1, id L 1. in := L.in addtype (id.entry, L.in) 5. L id addtype(id.entry, L.in) id id 1.type=real id 2.type=real Laszlo Böszörmenyi Compilers Attributed Grammars - 9

10 L-attributed Definitions Edges in the dep. graph only from left to right ( ) Attributes must be either 1. Synthesized, or 2. Inherited, depending only from father and left siblings In any A X 1, X 2,... X n the attribute X i.a may use only a) Inherited attributes associated with A b) Inherited and synthesized attributes associated with X 1, X 2,... X i-1 dfvisit (node n) { for each (child m of n, from the left) { Compute the inherited attributes of m; dfvisit (m); } Compute the synthesized attributes of n n m 1 m 2 m 3 m 21 m 22 m 23 Laszlo Böszörmenyi Compilers Attributed Grammars - 10

11 L-attributed - example Inherited attributes can also help if syntax and parse tree do not match Example, non-left-recursive grammar for expressions like 3 * 5 T.v = 15 Production Semantic Rule 1. T FT T.inh:= F.val T.val := T.syn 2. T *FT 1 T 1.inh:= T.inh * F.val T.syn:= T 1.syn 3. T ε T.syn:= T 1.inh 4. F digit F.val:= digit.lexval F.v = 3 d.v = 3 T.inh = 3 T.syn = 15 * F.v = 5 T 1.inh = 15 T.syn = 15 d.v = 5 ε Laszlo Böszörmenyi Compilers Attributed Grammars - 11

12 Syntax Trees Syntax trees are compact parse trees Intermediate nodes represent programming constructs (e.g. operators for an expression grammar) Chains are merged L.v = 19 E.v = 19 nl 3 * E 1.v = 15 + T.v = 4 T.v = 15 F.v = 4 T 1.v = 3 * F.v = 5 d.v = 4 * F.v = 3 d.v = 5 d.v = 3 Laszlo Böszörmenyi Compilers Attributed Grammars - 12

13 Constructing a syntax tree - example Help functions 1. mknode (op, left, right) creates node for an operator 2. mkleaf (id, entry) creates node for an identifier 3. mkleaf (num, val) creates node for a number Tree is built bottom-up Production Semantic Rule E E 1 + T E.nptr := mknode( +, E 1.nptr, T.nptr) a 4 + c + E E 1 T E.nptr := mknode( -, E 1.nptr, T.nptr) E T E.nptr := T.nptr - id T (E) T id T.nptr := E.nptr T.nptr := mkleaf(id, id.entry) id n c T num T.nptr := mkleaf(num, num.val) a 4 Laszlo Böszörmenyi Compilers Attributed Grammars - 13

14 Laszlo Böszörmenyi Compilers Attributed Grammars - 14 Syntax DAGs Common expressions computed only once Expressions must be free of side effect Example: a + a * (b c) + (b c) * d + + a * * a - b c - b c d + + * * a - b c d

15 Translation Schemes A translation scheme is a cfg + semantic actions The order is explicitly specified Actions are put in { } at the place of execution Example infix postfix conversion E T R R addop T { print(addop.lexeme) } R 1 R ε T num { print (num.val)} T E 9 pr(9) - 5 R T pr(-) pr(5) + 2 R T pr(+) pr(2) R ε Laszlo Böszörmenyi Compilers Attributed Grammars - 15

16 Proper Placing of Semantic Actions For synthesized attributes always the right end E.g. T T 1 * F {T.val:= T 1.val * F.val} If also inherited attributes available 1. Inherited attributes of symbol X on the right side of a production must be evaluated before the actions of X 2. Synthesized attributes of a symbol to the right must not be used 3. Synthesized attributes of a left-hand non-terminal must be computed at last action quite right Example S A 1 {A 1.inh:= 1; A 2.inh:= 2; } A 2 A a {print(a.inh)} S {A 1.inh:= 1} A 1 {A 2.inh:= 2; } A 2 WRONG CORRECT a S A 1 A 2 pr(?) a Laszlo Böszörmenyi Compilers Attributed Grammars - 16

17 Left-recursion and Semantic Actions (1) Let be X.x, Y.y synthesized attributes A A 1 Y { A.a := g(a 1.a, Y.y) } A X { A.a := f(x.x) } Elimination of the left-recursion (as known) Let be R the new non-terminal for right-recursion A XR R YR ε Including semantic actions A X { R.i:= f(x.x) } R { A.a:= R.s } R Y{ R 1.i:= g(r.i, Y.y) } R 1 { R.s:= R 1.s } R ε { R.s := R.i } Laszlo Böszörmenyi Compilers Attributed Grammars - 17

18 Left-recursion and Semantic Actions (2) Let be A X Y Z a sentential form A.a, X.x, Y.y and Z.z are synthesized and computed bottom-up R.i is inherited and computed at descending R.s is synthesized and used only to pass the value upwards A.a:= R.s A.a:= h(g(f(x.x)), Y.y), Z.z) X R.i:= f(x.x) A.a:= g(f(x.x), Y.y) Z Y R.i:= g(f(x.x), Y.y) A.a:= f(x.x) Y X A X {A.a:= f(x.x)} A A Y {A.a:= g(f(x.x), Y.y)} A A Z {A.a:= h(g(f(x.x), Y.y), Z.z)} Z R.i:= h(g(f(x.x), Y.y), Z.z) ε Laszlo Böszörmenyi Compilers Attributed Grammars - 18

19 Left-recursion and Actions - Example E E 1 + T {E.v:= E 1.v + T.v} E E 1 - T {E.v:= E 1.v - T.v} E T {E.v:= T.v} T (E) {T.v:= E.v} T num {T.v:= num.v} Without left-recursion E T {R.i:= T.v} R {E.v:= R.s} R +T {R 1.i:= R.i +T.v} R 1 {R.s:= R 1.s} R -T {R 1.i:= R.i - T.v} R 1 {R.s:= R 1.s} R ε {R.s:= R.i} T (E) {T.v:= E.v} T num {T.v:= num.v} E.v:= 6 T.v:=9 R.i:= T.v = T.v:= R 1.i:= R.i-T.v = T.v:= 2 R 1.i:= R.i+T.v 2 R.s:= R.i 6 ε The inherited attributes are computed at descending The computed values are copied into a synthesized attribute at ascending 6 Laszlo Böszörmenyi Compilers Attributed Grammars - 19

20 Creating a syntax tree from a tr. scheme E.n a-4+c R E T {R.i:= T.nptr} R {E.nptr:= R.s} R addop T {R 1.i:= T.n - T.n R.i mknode(addop, R.i, T.nptr} R 1 {R.s:= R 1.s} + + T.n R.i R ε {R.s:= R.i} T (E) {T.nptr:= E.nptr} - id ε T id {T. nptr:= mkleaf(id, id.v} T num {T. nptr:= mkleaf(num, num.v} id n c a 4 Laszlo Böszörmenyi Compilers Attributed Grammars - 20

21 Predictive Parser with Attributes 1. non-terminal A, create a function, using The inherited attributes as parameters The synthesized attributes as return value 2. The code for each production does a) If X V T X.x is a synthesized attribute: Store X.x and continue to read the input b) If B V N then create a function call c:= B(b 1, b 2, b k ), where b i are the variables storing the inherited attributes of B and c is the synthesized attribute of B c) In the case of actions Replace the references to attributes by references to the corresponding variables Laszlo Böszörmenyi Compilers Attributed Grammars - 21

22 Predictive Parser with Attributes - Example PROCEDURE R(i : Node) : Node = CONST AddOp = SET OF Scanner.Symbol {plus, minus}; VAR addop: Scanner.Symbol; BEGIN IF sym IN AddOp THEN (* R addop TR *) addop := sym; Scanner.GetSym(); RETURN R(mknode(addop, i, T())); ELSE (* R ε *) RETURN i; END; (*IF sym*) END R; R addop T {R 1.i:= mknode(addop, R.i, T.nptr)} R 1 {R.s:= R 1.i} R ε {R.s:= R.i} PROCEDURE T(): Node = BEGIN... END T; (*No inh. attr; no parameters*) Laszlo Böszörmenyi Compilers Attributed Grammars - 22

Syntax-Directed Translation

Syntax-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 information

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

Syntax-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 information

Syntax-Directed Translation

Syntax-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 information

Principles of Programming Languages

Principles 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 information

Syntax-Directed Translation

Syntax-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 information

[Syntax Directed Translation] Bikash Balami

[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 information

Syntax-Directed Translation Part I

Syntax-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 information

Abstract Syntax Tree

Abstract Syntax Tree Abstract Syntax Tree Condensed form of parse tree, useful for representing language constructs. The production S if B then s1 else s2 may appear as if-then-else B s1 s2 1 Abstract Syntax tree Chain of

More information

Syntax Directed Translation

Syntax 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 information

Principles of Programming Languages

Principles 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 information

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

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 309 PART 4 - SYNTAX DIRECTED TRANSLATION F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2016 109 / 309 Setting Translation of context-free languages Information attributes of grammar symbols Values

More information

Syntax-Directed Translation. Introduction

Syntax-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 information

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 8a Syntax-directed Transla1on Elias Athanasopoulos

ΕΠΛ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 information

COMPILER CONSTRUCTION (Evaluation Orders for SDD s)

COMPILER 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 information

Syntax-Directed Translation Part II

Syntax-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 information

Syntax-Directed Translation

Syntax-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 information

5. Syntax-Directed Definitions & Type Analysis

5. Syntax-Directed Definitions & Type Analysis 5. Syntax-Directed Definitions & Type Analysis Eva Rose Kristoffer Rose NYU Courant Institute Compiler Construction (CSCI-GA.2130-001) http://cs.nyu.edu/courses/spring15/csci-ga.2130-001/lecture-5.pdf

More information

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

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 264 PART 4 - SYNTAX DIRECTED TRANSLATION F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2015 109 / 264 Setting Translation of context-free languages Information attributes of grammar symbols Values

More information

Syntax Directed Translation

Syntax Directed Translation Syntax Directed Translation 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 type of arguments does

More information

Semantic Analysis Attribute Grammars

Semantic 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

Syntactic Directed Translation

Syntactic Directed Translation Syntactic Directed Translation Translation Schemes Copyright 2016, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission

More information

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

Syntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans Syntax-Directed Translation CS3300 - Compiler Design Syntax Directed Translation V. Krishna Nandivada IIT Madras Attach rules or program fragments to productions in a grammar. Syntax directed definition

More information

Abstract Syntax Trees Synthetic and Inherited Attributes

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 information

COP5621 Exam 3 - Spring 2005

COP5621 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 information

Semantic 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? 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 information

Lecture Compiler Construction

Lecture Compiler Construction Lecture Compiler Construction Franz Wotawa wotawa@ist.tugraz.at Institute for Software Technology Technische Universität Graz Inffeldgasse 16b/2, A-8010 Graz, Austria Summer term 2017 F. Wotawa (IST @

More information

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram CS6660 COMPILER DESIGN Question Bank UNIT I-INTRODUCTION TO COMPILERS 1. Define compiler. 2. Differentiate compiler and interpreter. 3. What is a language processing system? 4. List four software tools

More information

Summary: Semantic Analysis

Summary: Semantic Analysis Summary: Semantic Analysis 1 Basic Concepts When SA is performed: Semantic Analysis may be performed: In a two-pass compiler: after syntactic analysis is finished, the semantic analyser if called with

More information

A programming language requires two major definitions A simple one pass compiler

A 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 information

CSE302: Compiler Design

CSE302: 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

Syntax-Directed Translation

Syntax-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 information

Chapter 4 :: Semantic Analysis

Chapter 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 information

Chapter 4. Action Routines

Chapter 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 information

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

UNIT-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 information

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

Compiler 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 information

More On Syntax Directed Translation

More On Syntax Directed Translation More On Syntax Directed Translation 1 Types of Attributes We have productions of the form: A X 1 X 2 X 3... X n with semantic rules of the form: b:= f(c 1, c 2, c 3,..., c n ) where b and the c s are attributes

More information

CS 406: Syntax Directed Translation

CS 406: Syntax Directed Translation CS 406: Syntax Directed Translation Stefan D. Bruda Winter 2015 SYNTAX DIRECTED TRANSLATION Syntax-directed translation the source language translation is completely driven by the parser The parsing process

More information

We now allow any grammar symbol X to have attributes. The attribute a of symbol X is denoted X.a

We 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 information

Computer 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 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 information

Syntax-Directed Translation. Lecture 14

Syntax-Directed Translation. Lecture 14 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik) 9/27/2006 Prof. Hilfinger, Lecture 14 1 Motivation: parser as a translator syntax-directed translation stream of tokens parser ASTs,

More information

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

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask? Syntax-directed translation Context-sensitive analysis The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the

More information

Lecture 14 Sections Mon, Mar 2, 2009

Lecture 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 information

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

COP4020 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 information

A Simple Syntax-Directed Translator

A 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 information

UNIT IV INTERMEDIATE CODE GENERATION

UNIT 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 information

Question Bank. 10CS63:Compiler Design

Question 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 information

Semantic Analysis. Role of Semantic Analysis

Semantic 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 information

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

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis Semantic Processing The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the program based on its syntactic structure

More information

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous. Section A 1. What do you meant by parser and its types? A parser for grammar G is a program that takes as input a string w and produces as output either a parse tree for w, if w is a sentence of G, or

More information

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701) Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov 2014 Compiler Design (170701) Question Bank / Assignment Unit 1: INTRODUCTION TO COMPILING

More information

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COMP-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 information

LR Parsing LALR Parser Generators

LR Parsing LALR Parser Generators LR Parsing LALR Parser Generators Outline Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the

More information

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI UNIT I - LEXICAL ANALYSIS 1. What is the role of Lexical Analyzer? [NOV 2014] 2. Write

More information

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

COP4020 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 information

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

10/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 information

COP4020 Spring 2011 Midterm Exam

COP4020 Spring 2011 Midterm Exam COP4020 Spring 2011 Midterm Exam Name: (Please print Put the answers on these sheets. Use additional sheets when necessary or write on the back. Show how you derived your answer (this is required for full

More information

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1

NARESHKUMAR.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 information

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

Semantic 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 information

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG Attribute Evaluation Order Determining attribute 2 expected_type evaluation order actual_type actual_type for any attribute grammar is a 5 complex problem, 1 [1] [2] requiring

More information

LR Parsing LALR Parser Generators

LR Parsing LALR Parser Generators Outline LR Parsing LALR Parser Generators Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the

More information

Chapter 4 - Semantic Analysis. June 2, 2015

Chapter 4 - Semantic Analysis. June 2, 2015 Chapter 4 - Semantic Analysis June 2, 2015 The role of the semantic analyzer Compilers use semantic analysis to enforce the static semantic rules of a language It is hard to generalize the exact boundaries

More information

Abstract Syntax Trees & Top-Down Parsing

Abstract 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 information

Abstract Syntax Trees & Top-Down Parsing

Abstract 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 information

DEPARTMENT OF INFORMATION TECHNOLOGY / COMPUTER SCIENCE AND ENGINEERING UNIT -1-INTRODUCTION TO COMPILERS 2 MARK QUESTIONS

DEPARTMENT OF INFORMATION TECHNOLOGY / COMPUTER SCIENCE AND ENGINEERING UNIT -1-INTRODUCTION TO COMPILERS 2 MARK QUESTIONS BHARATHIDASAN ENGINEERING COLLEGE DEPARTMENT OF INFORMATION TECHNOLOGY / COMPUTER SCIENCE AND ENGINEERING Year & Semester : III & VI Degree & Branch : B.E (CSE) /B.Tech (Information Technology) Subject

More information

Programming Languages

Programming 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 information

VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur

VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur 603203. DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Year & Semester : III & VI Section : CSE 1 & 2 Subject Code : CS6660 Subject Name : COMPILER

More information

Abstract Syntax Trees & Top-Down Parsing

Abstract 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 information

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation Concepts Introduced in Chapter 2 A more detailed overview of the compilation process. Parsing Scanning Semantic Analysis Syntax-Directed Translation Intermediate Code Generation Context-Free Grammar A

More information

2068 (I) Attempt all questions.

2068 (I) Attempt all questions. 2068 (I) 1. What do you mean by compiler? How source program analyzed? Explain in brief. 2. Discuss the role of symbol table in compiler design. 3. Convert the regular expression 0 + (1 + 0)* 00 first

More information

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

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8] Code No: R05311201 Set No. 1 1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8] 2. Compute the FIRST and FOLLOW sets of each

More information

It parses an input string of tokens by tracing out the steps in a leftmost derivation.

It parses an input string of tokens by tracing out the steps in a leftmost derivation. It parses an input string of tokens by tracing out CS 4203 Compiler Theory the steps in a leftmost derivation. CHAPTER 4: TOP-DOWN PARSING Part1 And the implied traversal of the parse tree is a preorder

More information

Semantic Analysis with Attribute Grammars Part 3

Semantic Analysis with Attribute Grammars Part 3 with Attribute Grammars Part 3 Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler Design Outline of the Lecture Introduction

More information

Attribute Grammars. Attribute Grammars

Attribute Grammars. Attribute Grammars Attribute Grammars Definitions: synthesized, inherited, dependence graph Example: syntax-directed translation S-attributed grammars -attributed grammars Bottom Up euation of inherited attributes Top Down

More information

Introduction to Syntax Directed Translation and Top-Down Parsers

Introduction to Syntax Directed Translation and Top-Down Parsers Introduction to Syntax Directed Translation and Top-Down Parsers 1 Attributes and Semantic Rules Let s associate attributes with grammar symbols, and semantic rules with productions. This gives us a syntax

More information

4. Semantic Processing and Attributed Grammars

4. 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 information

Compiler Construction

Compiler Construction Compiler Construction Collection of exercises Version February 7, 26 Abbreviations NFA. Non-deterministic finite automaton DFA. Deterministic finite automaton Lexical analysis. Construct deterministic

More information

Syntax Directed Translation

Syntax Directed Translation CS143 Handout 16 Summer 2011 July 6 th, 2011 Syntax Directed Translation Handout written by Maggie Johnson and revised by Julie Zelenski. Syntax-directed translation refers to a method of compiler implementation

More information

Compiler Lab. Introduction to tools Lex and Yacc

Compiler 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 information

Wednesday, September 9, 15. Parsers

Wednesday, September 9, 15. Parsers Parsers What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

More information

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs: What is a parser Parsers A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

More information

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology exam Compiler Construction in4303 April 9, 2010 14.00-15.30 This exam (6 pages) consists of 52 True/False

More information

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing Roadmap > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing The role of the parser > performs context-free syntax analysis > guides

More information

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Error 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 information

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing ICOM 4036 Programming Languages Lexical and Syntax Analysis Lexical Analysis The Parsing Problem Recursive-Descent Parsing Bottom-Up Parsing This lecture covers review questions 14-27 This lecture covers

More information

Final Term Papers 2013

Final Term Papers 2013 Solved by: Sahar (well wisher) Class BSCS 6 th Semester Subject CS606 (COMPILER CONSTRUCTION) Solution Type: Final Term Solved Subjective including Papers of Year : 2013,2012,2011,2010,2009 2006 Institute:

More information

QUESTIONS RELATED TO UNIT I, II And III

QUESTIONS RELATED TO UNIT I, II And III QUESTIONS RELATED TO UNIT I, II And III UNIT I 1. Define the role of input buffer in lexical analysis 2. Write regular expression to generate identifiers give examples. 3. Define the elements of production.

More information

Compiler Construction

Compiler Construction Compiled on 5/05/207 at 3:2pm Abbreviations NFA. Non-deterministic finite automaton DFA. Deterministic finite automaton Compiler Construction Collection of exercises Version May 5, 207 General Remarks

More information

Alternatives for semantic processing

Alternatives for semantic processing Semantic Processing Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies

More information

Semantic Analysis and Intermediate Code Generation

Semantic Analysis and Intermediate Code Generation DDD55 Compilers and Interpreters DDB44 Compiler Construction Semantic Analysis and Intermediate Code Generation Semantic Analysis and Intermediate Code Generation able management source program Lexical

More information

LECTURE NOTES ON COMPILER DESIGN P a g e 2

LECTURE NOTES ON COMPILER DESIGN P a g e 2 LECTURE NOTES ON COMPILER DESIGN P a g e 1 (PCCS4305) COMPILER DESIGN KISHORE KUMAR SAHU SR. LECTURER, DEPARTMENT OF INFORMATION TECHNOLOGY ROLAND INSTITUTE OF TECHNOLOGY, BERHAMPUR LECTURE NOTES ON COMPILER

More information

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages. Syntax Prof. Robert van Engelen COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview n Tokens and regular expressions n Syntax and context-free grammars n Grammar derivations n More about parse trees n Top-down and

More information

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F). CS 2210 Sample Midterm 1. Determine if each of the following claims is true (T) or false (F). F A language consists of a set of strings, its grammar structure, and a set of operations. (Note: a language

More information

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468 Parsers Xiaokang Qiu Purdue University ECE 468 August 31, 2018 What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure

More information

3. Parsing. Oscar Nierstrasz

3. Parsing. Oscar Nierstrasz 3. Parsing 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/ http://www.cs.purdue.edu/homes/hosking/

More information

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309 PART 3 - SYNTAX ANALYSIS F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2016 64 / 309 Goals Definition of the syntax of a programming language using context free grammars Methods for parsing

More information

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures) CS606- Compiler Construction MidTerm Papers Solved MCQS with Reference (1 to 22 lectures) by Arslan Arshad (Zain) FEB 21,2016 0300-2462284 http://lmshelp.blogspot.com/ Arslan.arshad01@gmail.com AKMP01

More information

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages. Syntax Prof. Robert van Engelen COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview Tokens and regular expressions Syntax and context-free grammars Grammar derivations More about parse trees Top-down and bottom-up

More information

Static and Dynamic Semantics

Static 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 information

Program Assignment 2 Due date: 10/20 12:30pm

Program Assignment 2 Due date: 10/20 12:30pm Decoration of parse tree for (1 + 3) * 2 N. Meng, S. Arthur 1 Program Assignment 2 Due date: 10/20 12:30pm Bitwise Manipulation of Hexidecimal Numbers CFG E E A bitwise OR E A A A ^ B bitwise XOR A B B

More information

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

Semantic analysis. Syntax tree is decorated with typing- and other context dependent Semantic analysis Semantic analysis Semantic analysis checks for the correctness of contextual dependences: { nds correspondence between declarations and usage of identiers, { performs type checking/inference,

More information