CSCI565 Compiler Design

Size: px
Start display at page:

Download "CSCI565 Compiler Design"

Transcription

1 CSCI565 Compiler Design Fall 2015 Homework 2 - Solution Problem 1: Attributive Grammar and Syntax-Directed Translation [30 points] In this problem you ned to develop a grammar for array variable expressions and perform checking of the type and number of indexing expressions of an array variable. You may assume that a given variable array has been previously declared along with the number of dimensions of the array. For example, if a variable a is declared as a twodimensional array of double values the array expression a[i] and a[i,j,k] are both incorrect. Your solution should also check that the type of the indexing expressions (in this case using the variables i, j and k are all integer. Show your work by indicating the order in which the attributes need to be evaluated and present two sample cases, one correctly typed and another incorrectly typed. A possible grammar for array expression is given below which is obviously incomplete. ArrayExpression ArrayIdentifier [ ExpressionList ']' ExpressionList Expression, ExpressionList ExpressionList Expression Expression... ArrayIdentifier Given these non-terminals we can define the following attributes and their types, Here the enumerated value used for the type attribute associated with the Expression is a simple unique entifier (possibly the index into a symbol table) that corresponds to the type of the corresponding expression. ArrayExpression: typechecked (boolean, synthesized) ArrayIdentifier: (string, synthesized) ExpressionList: length (integer, synthesized) ExpressionList: typelist (list of enumerated values, synthesized) Expression: type (enumerated, synthesized) Given these attributes, we can develop a set of rules as follows: ArrayExpression ArrayIdentifier [ ExpressionList ']' { decl = lookuparraydeclaration(arrayidentifier.); ArrayExpression.typechecked = true; if (decl.numberdimensions!= ExpressionList.length) ArrayExpression.typechecked = false; for all values v in ExpressionList.typelist { if (v!= integer){ ArrayExpression.typechecked = false; break; ExpressionList Expression, ExpressionList 1 { ExpressionList.typelist = AppendToList(ExpressionList 1.typelist,Expression.type); ExpressionList.length = ExpressionList 1.length + 1 ExpressionList Expression { ExpressionList.typelist = CreateList(Expression.type); ExpressionList.length = 1; Expression... { Expression.type = <cdode that determines the type> ; ArrayIdentifier { ArrayIdentifier. =.string; 1 of 7

2 University of Southern California CSCI565 Compiler Design Homework 2 - Solution Problem 2: Static-Single Assignment Representation [10 points] For the sequence of instructions shown below depict an SSA-form representation (as there could be more than one). Comment on the need to save all the values at the end of the loop and how the SSA representation helps you in your evaluation of the code. Do not forget to include the φ-functions. b = 0; d = 1; a =...; i =...; L1: if(i > 0) { d = 0; b = b + 1; d = 1; i = i - 1; if(i < 0) goto Lbreak; goto L1; Lbreak: x = a; y = b; A possible representation in SSA is as shown below where each value associated with each variable is denoted by a subscripted index. b 1 = 0 d 1 = 1 a 1 =... i 1 =... L1: i 2 = φ(i 1,i 3 ) b 2 = φ(b 1,b 3 ) if (i 2 <= 0) then goto Lbreak d 2 = 0 b 3 = b d 3 = 1 i 3 = i 2-1 if (i 3 < 0) then goto Lbreak goto L1 Lbreak: b 3 = φ(b 2,b 3 ) d 4 = φ(d 1,d 3 ) x 1 = a 1 y 1 = b 3 An important aspect of this representation is that it makes explicit the flow of values that reach a given point. In particular and for this case the value for the d variable is defined by two assignments denoted by d 4 = φ(d 1,d 3 ). Where and despite the intermediate assignment of the value 0 to the variable d, the surviving value is 1 and hence we can be guaranteed at the exit of this loop that the variable d has the 1 value. This is a form of data-flow analysis that is done via the SSA representation. 2 of 7

3 Problem 3: Symbol Table Organization [10 points] For the PASCAL code below answer the following questions: 01: procedure main 02: integer a, b, c; 03: procedure f1(w,x); 04: integer w, x; 05: f2(w,x); 06: end; 07: procedure f2(y,z); 08: integer a, y, z; 09: procedure f3(m,n); 10: integer b, m, n; 11: c = a * b + f4(m+1); 12: b = a * (x + 1); 13: end; 14: f3(c,z); 15: end; 16: function f4(k): integer; 17: integer k; 18: f4 := (k + 1); 19: end; 20:... 21: f1(a,b); 22: end; a) [05 points] Draw the symbol tables for each of the procedures in this code (including main) and show their nesting relationship by linking them via a pointer reference in the structure (or record) used to implement them in memory. Include the entries or s for the local variables, arguments and any other information you find relevant for the purposes of code generation, such as its type and location at run-time. b) [05 points] For the statement in line 18 what are the specific instance of the variables used in this statement the compiler needs to locate? Explain how the compiler obtains the data corresponding to each of these variables table at compile time. a) The figure below depicts the hierarchical structure of the procedure in this PASCAL program. main kind symbol type size var a integer 4 var b integer 4 var c integer 4 kind param param f1 symbol w x type size integer 4 integer 4 f2 kind symbol type size var a integer 4 param y integer 4 param z integer 4 f4 kind symbol type size param k integer 4 f3 kind symbol type size var b integer 4 param m integer 4 param n integer 4 b) For the statement in line 18 we simply follow the symbol table entries to find out the specific instance of each of the symbols. Given than the statement is located lexically inse the body of procedure f4 the search for symbols always begins in the symbol table for f4. In this statement "f4 := (k+1);" the symbol k refers to the local variable of procedure f4, so trivially, this will be accessible from the f4 s own activation record (AR) accessible via the ARP or stack pointer (SP) used in many processor architectures. 3 of 7

4 University of Southern California CSCI565 Compiler Design Homework 2 - Solution Problem 4: Intermediate Code Generation [25 points] Conser the code generation scheme for expressions described in class. Assume that the grammar does allow for referencing of union expressions and its s to consists of a simple entifier such as a.u1.f or b.u2.g where the symbol a refers to a reference or address of a C struct and b for a location of a struct in C. Assume for the purpose of this exercise that during a semantic analysis phase you have computed the offset of the first byte of each of the unions u1 and u2 being referenced in these expressions. In this context answer the following: a) [20 points] Derived a SDT code generation scheme that handles simple union references such as the ones above as well as more complicated ones with multiple nested union references, e.g., a.u1.f1. You need to include as part of your answer relevant productions of the grammar. b) [10 points] Show your code generation scheme for the simple expression c = a.u.f1 + b.u.f2 assuming that b and c are declared in the C programming language as shown below. typedef struct { union { int f1;; u;; int xa;; A;; typedef struct { int xb;; union { int f2;; u;; B;; int A B c;; a;; b;; a) Below is a possible parse tree that will help us structure the grammar and corresponding productions, attributes and semantic rules for this problem for a simple assignment b = a.u.f1. All the attributes should be synthesized as that helps the integration with a bottom-up parser and also with a single bottom-up traversal of the tree. type: pointer: place: code: symbol to be used with the symbol table boolean (true if this is a pointer ) temporary name list of instructions '=' Using these attributes, we also defined a set of simple auxiliary functions, namely: b '.' type :: getoffset(user-defined type, _name);; symboltable :: gettype(symbol_name);; symboltable :: gettypeof Field(type_name, _name);; '.' f1 u a 4 of 7

5 As to the semantic rules we can define them as follows: assign '=' exp 1 exp 1 exp 2 '+' exp 3 { t4 = newtemp();; assign.place = t4;; assign.code = append(exp 1.code, gen('t4 = exp 1';;));; assign.pointer = exp 1.pointer;; assign.type = igettype(.symbol);; { t3 = newtemp();; exp 1.place = t3;; exp 1.code = append(exp 2.code, exp 3.code,gen(t3 = exp 2.place + exp 3.place));; exp 1.pointer = exp 2.pointer;; exp 1.type = exp 2.type;; exp { exp.code =.code;; exp.type =.type;; exp.pointer =.pointer;; exp.place =.place;; 1 2 '->' 1 2 '.' 1 { offset = getfieldoffset( 2.type,.symbol);; t2 = newtemp();; 1.place = t2;; 1.type = gettypeoffield( 2.type,.symbol);; if( ispointertype( 2.type) ){ 1.code = append( 2.code,{ gen('t2 = 2.place + offset';; 't2 = *t2;;));; 1.pointer = true;; else { error { offset = getfieldoffset( 2.type,.symbol);; t2 = newtemp();; 1.place = t2;; 1.type = gettypeoffield( 2.type,.symbol);; if( ispointertype( 2.type) ){ error;; else { 1.code = append( 2.code,{ gen('t2 = 2.place + offset';; 't2 = *t2;;));; 1.pointer = ispointertype( 1.type);; { 1.type = gettype(.symbol);; 1.pointer = ispointertype(.symbol));; t1 = newtemp();; 1.place = t1;; 1.code = { gen('t1 =.symbol;;');; b) Nested unions behave the same as nested structure with the only difference that the offsets are computed in a different fashion. Once you have determined the offsets of each, the code generation scheme is entical. 5 of 7

6 University of Southern California CSCI565 Compiler Design Homework 2 - Solution '=' place= c code= t1 = a; t2=t1+0; t2 = *t2; t3 = b; t4=t3+4; t4 = *t4; t5 = t3 + t4; c = t5; place= a code= null '+' place= t5 code= t1 = b; t2=t1+0; t2 = *t2; t3 = c; t4=t3+4; t4 = *t4; t5 = t3 + t4; c exp place= t2 code= t1 = b; t2=t1+0; t2 = *t2; place= t2 code= t1 = b; t2=t1+0; t2 = *t2; exp place= t4 code= t3 = c; t4=t3+4; t4 = *t4; place= t4 code= t3 = c; t4=t3+4; t4 = *t4; '.' '->' '.' type= B place= t1 code= 't1 = a' u f1 '->' type= B place= t3 code= 't3 = b' u f2 a b 6 of 7

7 Problem 5: Back-patching of Loop Constructs [25 points] We have covered in class an SDT scheme to generated code using the back-patching technique for a while loop construct. In this exercise you will develop a similar scheme for the FORTRAN do-loop construct using the production below and also taking into account next and stop statements (these are similar to the continue and break statement in the C language. Further assume that the expression exp 1 and exp 2 have a place attribute and a specific code generation attribute where the code to evaluate them has been deposited. A further complication in FORTRAN is that the expressions that define the bounds of the loop are evaluated once on loop entry and temporaries are used instead to retain their values. The exp 1 and exp 2 are thus only evaluated on loop entry. Your solution needs to account for this by including temporary variables as part of the generated code. (1) S do var = exp 1, exp 2 L enddo (2) S next; (3) S stop; (4) L S \n L (4) L S Do not forget to show the augmented production with the marker non-terminal symbols, M and possibly N along with the corresponding rules for the additional symbols and productions. Argue for the correctness of your solution without necessarily having to show an example. This solution is further complicated by the fact that the code starts its execution with the evaluation of the expressions Exp 1 and Exp 2 that correspond to the bounds of the loop and these expressions need only and must only be evaluated once. The code generated as part of the main production starts at the address M 3.address. In order to control the execution of the loop, however, the production for M 1 inserts a jump to the code at the bottom of the loop where the test for execution will be made. It is thus assumed that the execution, when it reaches the code generated by production M 1 has already executed the code generated for the evaluation of Exp 1 and Exp 2. As such the correct values for these expression are already in the corresponding location attribute. The code jumps to the address denoted by M 3.address where the test fir the condition of execution of the loop is located and then jumps back to the body of the loop. (1) S do M 1 var = Exp 1, Exp 2 M 2 L M 3 enddo { t1 = newtemp(); t2 = newtemp(); gen( t 1 = Exp 1.place ); gen( t 2 = Exp 2.place ); gen( var. = t1 ); emit( goto M 2.address ); emit( t1 = t1 + 1; ); emit( var. = t1; ); gen( if t1 > t2 goto ); emit( goto M 2.address ); backpatch(newlist({m 1.address, M 3.address); backpatch(l.nextlist,m 3.address); backpatch(l.skiplist,m 3. address); S.nextlist = merge(makelist({m 1.address+6,L.breaklist)); S.breaklist = nil; (2) S continue; { S.skiplist = newlist(nextaddr()); emit( goto ); S.breaklist = nil; S.nextlist = nil; (3) S break; { S.breaklist = newlist(nextaddr()); emit( goto ); S.nextlist = nil; S.skiplist = nil; (4) L 1 S ; M 2 L 2 { backpatch(s.nextlist, M 2.quad); L 1.nextlist = L 2.nextlist; L 1.breaklist = merge(s.breaklist,l 2.breaklist); L 1.skiplist = merge(s.skiplist,l 2.skiplist); (5) L S { L.breaklist = S.breaklist; L.nextlist = S.nextlist; L.skiplist = S.skiplist; (6) M 1 ε { M 1.address = nextaddr; emit( goto ); (7) M 2 ε { M 2.address = nextaddr; emit( goto ); (8) M 3 ε { M 3.address = nextaddr; 7 of 7

CSCI565 Compiler Design

CSCI565 Compiler Design CSCI565 Compiler Design Spring 2015 Homework 2 - Solution Problem 1: Attributive Grammar and Syntax-Directed Translation [25 points] Conser the grammar fragment below. It allows for the declaration of

More information

CSCI Compiler Design

CSCI Compiler Design University of Southern California CSCI565 Compiler Design Mterm Exam Solution - Spring 2014 CSCI 565 - Compiler Design Spring 2014 Mterm Exam - Solution Problem 1: Context-Free-Grammars and Parsing Algorithms

More information

CSCI Compiler Design

CSCI Compiler Design University of Southern California CSCI565 Compiler Design Midterm Exam - Spring 26 CSCI 565 - Compiler Design Spring 26 Midterm Exam March, 26 at : PM in class (KAP 47) Duration: 2h 3 min. Please label

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Basic Approach and Application to Assignment and xpressions Array xpressions Boolean xpressions Copyright 2017, Pedro C. Diniz, all rights reserved. Students enrolled in the

More information

Intermediate Code Generation

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

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Basic Approach and Application to Assignment and xpressions Array xpressions Boolean xpressions Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the

More information

Intermediate Code Generation Part II

Intermediate Code Generation Part II 1 Intermediate Code Generation Part II Chapter 6 (1 st ed Chapter 8) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009 2 Advanced Intermediate Code Generation

More information

Module 26 Backpatching and Procedures

Module 26 Backpatching and Procedures Module 26 Backpatching and Procedures In this module, we would learn to generate three-address code for control flow statements using backpatching. We shall also discuss to combine Boolean expressions

More information

Intermediate Representations & Symbol Tables

Intermediate Representations & Symbol Tables Intermediate Representations & Symbol Tables Copyright 2014, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Control-Flow tatements hort-circuit Predicate valuation Back-Patching Copyright 2015, Pedro C. Diniz, all rights reserved. tudents enrolled in the Compilers class at the University

More information

CSCI Compiler Design

CSCI Compiler Design University of Southern California CSCI565 Compiler Design Midterm Exam - Fall 26 CSCI 565 - Compiler Design Fall 26 Midterm Exam Solution Problem : Context-Free-Grammars and Parsing Algorithms [4 points]

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Control-Flow tatements hort-circuit Predicate valuation Back-Patching Copyright 2011, Pedro C. Diniz, all rights reserved. tudents enrolled in the Compilers class at the University

More information

Intermediate Code Generation

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

Intermediate Code Generation

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

CSCI 565: Compiler Design and Implementation Spring 2006

CSCI 565: Compiler Design and Implementation Spring 2006 CSCI 565: Compiler Design and Implementation Spring 2006 Midterm Exam Solution Feb. 28, 2006 Problem 1: Attributive Grammar and Syntax Directed Translation [30 points] A language such as C allows for user

More information

LECTURE 3. Compiler Phases

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

Principles of Compiler Design

Principles of Compiler Design Principles of Compiler Design Intermediate Representation Compiler Lexical Analysis Syntax Analysis Semantic Analysis Source Program Token stream Abstract Syntax tree Unambiguous Program representation

More information

THEORY OF COMPILATION

THEORY OF COMPILATION Lecture 09 IR (ackpatching) THEORY OF COMPILATION Eran Yahav www.cs.technion.ac.il/~yahave/tocs2011/compilers-lec09.pptx Reference: Dragon 6.2,6.3,6.4,6.6 1 Recap Lexical analysis regular expressions identify

More information

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction Procedure Abstraction Run Time Environment Records Procedure Linkage Name Translation and Variable Access Copyright 2010, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at

More information

CS2210: Compiler Construction. Code Generation

CS2210: Compiler Construction. Code Generation Modern Compiler Project Fortran program Fortran s Lexer, Parser, and Static Checker Intermediate Code Generator IR MIPS Code Generator MIPS code C program C s Lexer, Parser, and Static Checker Intermediate

More information

CSCI Compiler Design

CSCI Compiler Design CSCI 565 - Compiler Design Spring 2015 Midterm Exam March 04, 2015 at 8:00 AM in class (RTH 217) Duration: 2h 30 min. Please label all pages you turn in with your name and student number. Name: Number:

More information

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code Code Generation Intermediate Code? Assembly Code Object Code (Machine Code) 1 Intermediate Code P-Code Three Address Code 2 Compiler Construction F6S 1 Intermediate Representation Abstract Syntax Tree

More information

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 STUDENT ID: INSTRUCTIONS Please write your student ID on this page. Do not write it or your name

More information

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Lecture - 20 Intermediate code generation Part-4 Run-time environments

More information

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access Run Time Environment Activation Records Procedure Linkage Name Translation and Variable Access Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University

More information

Semantic Actions and 3-Address Code Generation

Semantic Actions and 3-Address Code Generation Compiler Design 1 Semantic Actions and 3-Address Code Generation Compiler Design 2 Introduction We start with different constructs of the given grammar (laboratory assignment-5) and discuss semantic actions

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation 1 Intermediate Code Generation Translating source program into an intermediate language" Simple CPU Independent, yet, close in spirit to machine language Benefits Retargeting

More information

CSCE 531, Spring 2015 Final Exam Answer Key

CSCE 531, Spring 2015 Final Exam Answer Key CSCE 531, Spring 2015 Final Exam Answer Key 1. (40 points total) Consider the following grammar with start symbol S : S S S asb S T T T a T cs T ɛ (a) (10 points) Find FIRST(S), FIRST(T ), FOLLOW(S), and

More information

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

CMPT 379 Compilers. Anoop Sarkar.  11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC CMPT 379 Compilers Anoop Sarkar http://www.cs.sfu.ca/~anoop 11/13/07 1 TAC: Intermediate Representation Language Specific Language + Machine Independent Machine Dependent Front End AST Intermediate Code

More information

CSCI Compiler Design

CSCI Compiler Design CSCI 565 - Compiler Design Spring 2010 Final Exam - Solution May 07, 2010 at 1.30 PM in Room RTH 115 Duration: 2h 30 min. Please label all pages you turn in with your name and student number. Name: Number:

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Front-end intermediate code generation source program Lexical Analyzer Syntax Analyzer Semantic Analyzer Int. Code Generator intermediate representation Symbol Table Advantages

More information

CSCI565 Compiler Design

CSCI565 Compiler Design CSCI565 Compiler Design Spring 2011 Homework 4 Solution Due Date: April 6, 2011 in class Problem 1: Activation Records and Stack Layout [50 points] Consider the following C source program shown below.

More information

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (1 Principle of Compilers Lecture VIII: Intermediate Code Generation Alessandro Artale Faculty of Computer Science Free

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

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms SE352b Software Engineering Design Tools W3: Programming Paradigms Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa SE352b: Roadmap CASE Tools: Introduction System Programming Tools Programming Paradigms

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

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

Dixita Kagathara Page 1

Dixita Kagathara Page 1 2014 Sem-VII Intermediate Code Generation 1) What is intermediate code? Intermediate code is: The output of the parser and the input to the Code Generator. Relatively machine-independent: allows the compiler

More information

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz Compiler Design Fall 2015 Control-Flow Analysis Sample Exercises and Solutions Prof. Pedro C. Diniz USC / Information Sciences Institute 4676 Admiralty Way, Suite 1001 Marina del Rey, California 90292

More information

Concepts Introduced in Chapter 6

Concepts Introduced in Chapter 6 Concepts Introduced in Chapter 6 types of intermediate code representations translation of declarations arithmetic expressions boolean expressions flow-of-control statements backpatching EECS 665 Compiler

More information

CMSC430 Spring 2014 Midterm 2 Solutions

CMSC430 Spring 2014 Midterm 2 Solutions CMSC430 Spring 2014 Midterm 2 Solutions 1. (12 pts) Syntax directed translation & type checking Consider the following grammar fragment for an expression for C--: exp CONST IDENT 1 IDENT 2 [ exp 1 ] Assume

More information

Intermediate Representa.on

Intermediate Representa.on IR Intermediate Representa.on CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class Intermediate Representation Language Specific Language + Machine Independent Machine Dependent

More information

CS 432 Fall Mike Lam, Professor. Code Generation

CS 432 Fall Mike Lam, Professor. Code Generation CS 432 Fall 2015 Mike Lam, Professor Code Generation Compilers "Back end" Source code Tokens Syntax tree Machine code char data[20]; int main() { float x = 42.0; return 7; } 7f 45 4c 46 01 01 01 00 00

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

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

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

More information

Module 27 Switch-case statements and Run-time storage management

Module 27 Switch-case statements and Run-time storage management Module 27 Switch-case statements and Run-time storage management In this module we will discuss the pending constructs in generating three-address code namely switch-case statements. We will also discuss

More information

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation Acknowledgement CS3300 - Compiler Design Semantic Analysis - IR Generation V. Krishna Nandivada IIT Madras Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all

More information

Compiler Design. Spring Run-Time Environments. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Compiler Design. Spring Run-Time Environments. Sample Exercises and Solutions. Prof. Pedro C. Diniz University of Southern California (USC) Computer Science Department Compiler Design Spring 2014 Run-Time Environments Sample Exercises and Solutions Prof. Pedro C. Diniz USC / Information Sciences Institute

More information

Compilers. Compiler Construction Tutorial The Front-end

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

Generating 3-Address Code from an Attribute Grammar

Generating 3-Address Code from an Attribute Grammar Generating 3-Address Code from an Attribute Grammar Here is a small but realistic programming language with integer and boolean data, loops and conditionals, procedures with no arguments that compute but

More information

Run-time Environments

Run-time Environments Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

More information

Run-time Environments

Run-time Environments Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

More information

CSC 467 Lecture 13-14: Semantic Analysis

CSC 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 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

Type Checking. Chapter 6, Section 6.3, 6.5

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

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

The 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 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

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

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

Qualifying Exam in Programming Languages and Compilers

Qualifying Exam in Programming Languages and Compilers Qualifying Exam in Programming Languages and Compilers University of Wisconsin Fall 1991 Instructions This exam contains nine questions, divided into two parts. All students taking the exam should answer

More information

Languages and Compiler Design II IR Code Generation I

Languages and Compiler Design II IR Code Generation I Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring 2010 rev.: 4/16/2010 PSU CS322 HM 1 Agenda Grammar G1

More information

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 97 INSTRUCTIONS

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 97 INSTRUCTIONS RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 97 STUDENT ID: INSTRUCTIONS Please write your student ID on this page. Do not write it or your name

More information

Homework 1 Answers. CS 322 Compiler Construction Winter Quarter 2006

Homework 1 Answers. CS 322 Compiler Construction Winter Quarter 2006 Homework 1 Answers CS 322 Compiler Construction Winter Quarter 2006 Problem 1 m := 0 i := 0 L1: if i

More information

Decision Making in C

Decision Making in C Decision Making in C Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed

More information

Formal Languages and Compilers Lecture X Intermediate Code Generation

Formal Languages and Compilers Lecture X Intermediate Code Generation Formal Languages and Compilers Lecture X Intermediate Code Generation Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

More information

Concepts Introduced in Chapter 6

Concepts Introduced in Chapter 6 Concepts Introduced in Chapter 6 types of intermediate code representations translation of declarations arithmetic expressions boolean expressions flow-of-control statements backpatching EECS 665 Compiler

More information

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Compiling and Interpreting Programming. Overview of Compilers and Interpreters Copyright R.A. van Engelen, FSU Department of Computer Science, 2000 Overview of Compilers and Interpreters Common compiler and interpreter configurations Virtual machines Integrated programming environments

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

Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar

Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar anoop@cs.sfu.ca Only submit answers for questions marked with. Provide a makefile such that make compiles all your programs, and make

More information

Intermediate Representations

Intermediate Representations Compiler Design 1 Intermediate Representations Compiler Design 2 Front End & Back End The portion of the compiler that does scanning, parsing and static semantic analysis is called the front-end. The translation

More information

Intermediate Representations

Intermediate Representations Intermediate Representations A variety of intermediate representations are used in compilers Most common intermediate representations are: Abstract Syntax Tree Directed Acyclic Graph (DAG) Three-Address

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

LECTURE 18. Control Flow

LECTURE 18. Control Flow LECTURE 18 Control Flow CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear in a program text. Selection (or alternation): a

More information

Semantic actions for declarations and expressions

Semantic actions for declarations and expressions Semantic actions for declarations and expressions Semantic actions Semantic actions are routines called as productions (or parts of productions) are recognized Actions work together to build up intermediate

More information

CS536 Spring 2011 FINAL ID: Page 2 of 11

CS536 Spring 2011 FINAL ID: Page 2 of 11 CS536 Spring 2011 FINAL ID: Page 2 of 11 Question 2. (30 POINTS) Consider adding forward function declarations to the Little language. A forward function declaration is a function header (including its

More information

Run Time Environments

Run Time Environments Run Time Environments ALSU Textbook Chapter 7.1 7.3 Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 Preliminaries During the execution of a program, the same name in the source

More information

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division Fall, 2001 Prof. R. Fateman SUGGESTED S CS 164 Final Examination: December 18, 2001, 8-11AM

More information

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

Semantic actions for declarations and expressions. Monday, September 28, 15 Semantic actions for declarations and expressions Semantic actions Semantic actions are routines called as productions (or parts of productions) are recognized Actions work together to build up intermediate

More information

CSc 453 Intermediate Code Generation

CSc 453 Intermediate Code Generation CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson Overview Intermediate representations span the gap between the source and target languages: closer to target language;

More information

Problem Score Max Score 1 Syntax directed translation & type

Problem Score Max Score 1 Syntax directed translation & type CMSC430 Spring 2014 Midterm 2 Name Instructions You have 75 minutes for to take this exam. This exam has a total of 100 points. An average of 45 seconds per point. This is a closed book exam. No notes

More information

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B. CSE 3302 Test 1 1. Preprocessor macros are associated with: A. C B. Java C. JavaScript D. Pascal 2. (define x (lambda (y z) (+ y z))) is an example of: A. Applying an anonymous function B. Defining a function

More information

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

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

CSE 340 Fall 2014 Project 4

CSE 340 Fall 2014 Project 4 CSE 340 Fall 2014 Project 4 Due on Dec. 5, 2014 by 11:59 pm Abstract The goal of this project is to give you some hands-on experience with implementing a compiler. You will write a compiler for a simple

More information

5. Semantic Analysis!

5. Semantic Analysis! 5. Semantic Analysis! Prof. O. 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

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Intermediate codes are machine independent codes, but they are close to machine instructions The given program in a source language is converted to an equivalent program in

More information

CS 415 Midterm Exam Fall 2003

CS 415 Midterm Exam Fall 2003 CS 415 Midterm Exam Fall 2003 Name KEY Email Address Student ID # Pledge: This exam is closed note, closed book. Questions will be graded on quality of answer. Please supply the best answer you can to

More information

Chapter 7:: Data Types. Mid-Term Test. Mid-Term Test (cont.) Administrative Notes

Chapter 7:: Data Types. Mid-Term Test. Mid-Term Test (cont.) Administrative Notes Chapter 7:: Data Types Programming Language Pragmatics Michael L. Scott Administrative Notes Mid-Term Test Thursday, July 27 2006 at 11:30am No lecture before or after the mid-term test You are responsible

More information

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 4 Robert Grimm, New York University 1 Review Last week Control Structures Selection Loops 2 Outline Subprograms Calling Sequences Parameter Passing

More information

CS 321 Homework 4 due 1:30pm, Thursday, March 15, 2012 This homework specification is copyright by Andrew Tolmach. All rights reserved.

CS 321 Homework 4 due 1:30pm, Thursday, March 15, 2012 This homework specification is copyright by Andrew Tolmach. All rights reserved. CS 321 Homework 4 due 1:30pm, Thursday, March 15, 2012 This homework specification is copyright 2002-2012 by Andrew Tolmach. All rights reserved. Typechecking In this assignment, you will build a type-checker

More information

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13 Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1 What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down,

More information

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

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing

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

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 2 Thomas Wies New York University Review Last week Programming Languages Overview Syntax and Semantics Grammars and Regular Expressions High-level

More information

Today's Topics. CISC 458 Winter J.R. Cordy

Today's Topics. CISC 458 Winter J.R. Cordy Today's Topics Last Time Semantics - the meaning of program structures Stack model of expression evaluation, the Expression Stack (ES) Stack model of automatic storage, the Run Stack (RS) Today Managing

More information

Programming Languages Third Edition. Chapter 7 Basic Semantics

Programming Languages Third Edition. Chapter 7 Basic Semantics Programming Languages Third Edition Chapter 7 Basic Semantics Objectives Understand attributes, binding, and semantic functions Understand declarations, blocks, and scope Learn how to construct a symbol

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information

COP5621 Exam 4 - Spring 2005

COP5621 Exam 4 - Spring 2005 COP5621 Exam 4 - 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 credit

More information

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008. Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008. Outline. Abstractions for telling a computer how to do things. Computational Paradigms. Language Definition, Translation.

More information

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

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

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the Final Examination Please read all instructions (including these) carefully. Please print your name at the bottom of each page on the exam. There are nine questions on the exam, some in multiple parts.

More information