CSCI565 Compiler Design

Size: px
Start display at page:

Download "CSCI565 Compiler Design"

Transcription

1 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 scalar or single dimensional arrays in a style similar to PASCAL where the type specifier and its storage size indications precedes the variable entifiers. To simply the implementation we only allow some basic types. Also integer stands for a terminal symbol with an integer value attribute named ival. VarDeclaration TypeSpecifier Dimensions IdentifierList TypeSpecifier Dimensions IdentifierList ';' vo char float int double '[' integer ']' ε ',' IdentifierList a. [15 points] Without rewriting the grammar develop an attributive grammar and syntax-directed definition (SDD) that inserts all the symbols associated with the variable declaration in a symbol table as part of the semantic action associated with the VarDeclaration production. Your solution should also check, and report, duplicate variable symbols, in which case only the first occurrence should be inserted in the symbol table. b. [10 points] Show the values of your attributes as well as the order in which they need to be evaluated for the snippet of code fragment "int [6] a, b;". Can the evaluation of your attributes be carried out in a single pass over the parse tree? Why or why not? a. There are many possible solutions to this simple problem. Here we present an L-attributed grammar solution with the list of attributes and corresponding types as follows: Dimensions: basesize (integer, inherited); size (integer, synthesized) TypeSpecifier: basesize (integer, synthesized) IdentifierList: list (list of symbols, synthesized) vardeclaration: list (list of symbols, synthesized); size (integer, synthesized) using these attributes we can define the semantics rules as follows: VarDeclaration TypeSpecifier Dimensions IdentifierList ';' { for each in IdentifierList.list do currentsymboltable.insert(, Dimensions.size); end for TypeSpecifier vo { TypeSpecifier.baseSize = 0; char { TypeSpecifier.baseSize = 1; float { TypeSpecifier.baseSize = 4; int { TypeSpecifier.baseSize = 4; double { TypeSpecifier.baseSize = 8; Dimensions '[' integer ']' { Dimensions.Size = Dimensions.baseSize * integer.ival; /* array case */ ε { Dimensions.Size = Dimensions.baseSize; /* scalar case */ IdentifierList 0 ',' IdentifierList 1 { IdentifierList.list 0 = appendandcheck(identifierlist.list 1, ); { IdentifierList.list = { 1 of 7

2 b. Using these rules one possible evaluation order is from left-to right following a depth-first search pre-order traversal. VarDeclaration list = {a,b size = 24 TypeSpecifier Dimensions IdentifierList ';' list = {a,b basesize = 4 basesize = 4 size = 6 x 4 int '[' integer ']' Id list = {a ',' IdentifierList list = {b 6 a Id list = {b b 2 of 7

3 Problem 2: Static-Single Assignment Representation [15 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 =...; a =...; i =...; L1: if(i > 0) { if(a < 0){ d = 0; b = 0; else { 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. Notice that in fact there is no loop in this code. a 1 =... b 1 = 0 i 1 =... d 1 =... L: i 2 = φ(i 1,i 3 ) b 2 = φ(b 1,b 5 ) if (i 2 <= 0) then goto Lbreak if (a 1 >= 0) then goto X d 2 = 0 b 3 = 0 goto Y X: b 4 = b d 3 = 1 Y: i 3 = i 2-1 b 5 = φ(b 3,b 4 ) if (i 3 < 0) then goto Lbreak goto L1 Lbreak: b 5 = φ(b 2,b 5 ) x 1 = a 1 y 1 = b 5 As can be observed by inspection each use has a single definition point that reaches it and each value is defined only once. This is particularly tricky for variable 'b'. 3 of 7

4 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 * m + f3(y,z); 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 12 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 kind f3 symbol type size var b integer 4 param m integer 4 param n integer 4 b) For the statement in line 12 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 f3 the search for symbols always begins in the symbol table for f3. In this statement "b = a*(x + 1);" the symbol b refers to the local variable of procedure f3, the symbol a to the local variable in procedure f2 and the x symbol is undefined. This is a semantic error, which the compiler needs to report as this symbol is out of scope. 4 of 7

5 Problem 4: Intermediate Code Generation [30 points] Conser the code generation scheme for expressions described in class. Assume that the grammar does allow for pointer dereferencing expression to consists of a simple entifier such as a->f1 or b.f2 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 s f1 and f2 being referenced in thee expressions. In this context answer the following: a) [20 points] Derived a SDT code generation scheme that handles simple pointer references such as the ones above as well as more complicated ones with multiple pointer indirections, e.g., a->f1->f2. Not that you can combine the "->" and the "." operators. 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 a = b->f1 + c.f2 assuming that b and c are declared in the C programming language as shown below. typedef struct { int f1; B; typedef struct { int y; int f2; C; int a; B* b; C* c; a) Below we depict a possible parse tree that will help us structure the grammar and corresponding productions, attributes and semantic rules for this problem. All the attributes should be synthesized as that helps the integration with a bottom-up parser and if not also with a single bottom-up traversal of the tree. type: pointer: place: code: symbol boolean temporary name list of instructions '=' Using these attributes we also defined a set of simple auxiliary functions, namely: b f2 type :: getoffset(user-defined type, _name); symboltable :: gettype(symbol_name); symboltable :: gettypeof Field(type_name, _name); a f1 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; 5 of 7

6 exp { exp.code =.code; exp.type =.type; exp.pointer =.pointer; exp.place =.place; '.' 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) Using the attribute grammar outlined above for the expression: '=' place= a t3 = c; t4=t3+4; t4 = *t4; t5 = t3 + t4; c = t5; place= a code= null '+' place= t5 t3 = c; t4=t3+4; t4 = *t4; t5 = t3 + t4; a exp place= t2 place= 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 pointer= true code= 't1 = b' f1 type= C* place= t3 pointer= true code= 't3 = c' f2 b c 6 of 7

7 Problem 5: Back-patching of Loop Constructs [20 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 repeat-while construct using the production below and also taking into account continue and break statements. Argue that your solution works for the case of nested loops and break and continue statements at different nesting levels. (1) S repeat L while E; (2) S continue; (3) S break; (4) L S ; 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. We have seen in class a possible approach to this SDT scheme is to have additional synthesized attributes for the statements, respectively a nextlist a skiplist and a breaklist. In the skiplist are the addresses of unresolved goto instructions that correspond to continue statements whereas in the break list are the addresses of unresolved goto instructions that correspond to break statements. The nextlist corresponds to addresses of unresolved goto instructions that follow the regular control-flow, as it is the case of regular instructions or if-then-else constructs. While the skiplist need to be patched with the addresses of the first instruction of the current nesting level, i.e. the first instructions that evaluates the control predicate of the loop, the breaklist needs to be patched with the first address following the current S construct. This cannot be immediately recognized at this level in the back-patching and thus the address of the goto in the breaklist is passed up as part of the synthesized attribute nextlist of S. (1) S repeat M 1 L while M 2 E; { backpatch(l.nextlist,m 2.quad); backpatch(l.skiplist,m 2.quad); backpatch(e.truelist.m 1.quad); S.nextlist = merge(e.falselist,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.quad = nextaddr; (7) M 2 ε { M 2.quad = nextaddr; Regarding the first production, the first back-patching command fills in the places where the control in L is transferred to the next iteration, that is, to the evaluation of the conditional E that is given by the M 2.quad value. The second backpatching command links the places where the evaluation of E is false to M 1.quad that is to the top of the loop. Next we merge the places where are goto instructions with the E.falselist as both these have addresses where the goto instructions will transfer control to the first instruction following the loop. The continue generates a single entry in a skiplist whereas the break generates a single entry in a breaklist of the corresponding S symbol. Regarding the sequencing of statement in production (4) we have to link the addresses from continue instructions in S with the first instruction in the predicate of the while construct in which the continue is nested. This is accomplished by using the skiplist attribute. The addresses that correspond to break instructions in either and S or L 2 need to be merged whereas the L 1.nextlist is simply the locations that need to be filled in with the addresses after L 2 which is only known at the next level up. Note that nested loop will have the break instruction just to the nest level up (see the role of L.breaklist in (5) and S.nextlist in (1) and then the S.nextlist in (4) where it is patched to M 2.quad. 7 of 7

CSCI565 Compiler Design

CSCI565 Compiler Design 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

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

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

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

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

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

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

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

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

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

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

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

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules. Outline Type Checking General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

More information

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference Type Checking Outline General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

More 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

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

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

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

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

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

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

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

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

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

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

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

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

Time : 1 Hour Max Marks : 30

Time : 1 Hour Max Marks : 30 Total No. of Questions : 6 P4890 B.E/ Insem.- 74 B.E ( Computer Engg) PRINCIPLES OF MODERN COMPILER DESIGN (2012 Pattern) (Semester I) Time : 1 Hour Max Marks : 30 Q.1 a) Explain need of symbol table with

More 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

Compiler Construction Assignment 3 Spring 2018

Compiler Construction Assignment 3 Spring 2018 Compiler Construction Assignment 3 Spring 2018 Robert van Engelen µc for the JVM µc (micro-c) is a small C-inspired programming language. In this assignment we will implement a compiler in C++ for µc.

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

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

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

Single-pass Static Semantic Check for Efficient Translation in YAPL

Single-pass Static Semantic Check for Efficient Translation in YAPL Single-pass Static Semantic Check for Efficient Translation in YAPL Zafiris Karaiskos, Panajotis Katsaros and Constantine Lazos Department of Informatics, Aristotle University Thessaloniki, 54124, Greece

More information

A simple syntax-directed

A simple syntax-directed Syntax-directed is a grammaroriented compiling technique Programming languages: Syntax: what its programs look like? Semantic: what its programs mean? 1 A simple syntax-directed Lexical Syntax Character

More information

FORM 2 (Please put your name and form # on the scantron!!!!)

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam 2: FORM 2 (Please put your name and form # on the scantron!!!!) True (A)/False(B) (2 pts each): 1. Recursive algorithms tend to be less efficient than iterative algorithms. 2. A recursive function

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

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

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

SEMANTIC ANALYSIS TYPES AND DECLARATIONS SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether

More 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

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

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11 ... 1/16 CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11 Alice E. Fischer February 3, 2016 ... 2/16 Outline Basic Types and Diagrams ... 3/16 Basic Types and Diagrams Types in C C has

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

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

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

CMPSC 160 Translation of Programming Languages. Three-Address Code

CMPSC 160 Translation of Programming Languages. Three-Address Code CMPSC 160 Translation of Programming Languages Lectures 16: Code Generation: Three- Address Code Three-Address Code Each instruction can have at most three operands Each operand corresponds to a memory

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

Lecture08: Scope and Lexical Address

Lecture08: Scope and Lexical Address Lecture08: Scope and Lexical Address Free and Bound Variables (EOPL 1.3.1) Given an expression E, does a particular variable reference x appear free or bound in that expression? Definition: A variable

More information

Chapter 6 Intermediate Code Generation

Chapter 6 Intermediate Code Generation Chapter 6 Intermediate Code Generation Outline Variants of Syntax Trees Three-address code Types and declarations Translation of expressions Type checking Control flow Backpatching Introduction Intermediate

More information

Lexical Considerations

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

More information

Programming, numerics and optimization

Programming, numerics and optimization Programming, numerics and optimization Lecture A-2: Programming basics II Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428 March

More information

CS 415 Midterm Exam Spring SOLUTION

CS 415 Midterm Exam Spring SOLUTION CS 415 Midterm Exam Spring 2005 - SOLUTION Name 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

More information

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e CS-3160 Concepts of Programming Languages Spring 2015 EXAM #1 (Chapters 1-6) Name: SCORES MC: /75 PROB #1: /15 PROB #2: /10 TOTAL: /100 Multiple Choice Responses Each multiple choice question in the separate

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

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End

More information

CPS 506 Comparative Programming Languages. Syntax Specification

CPS 506 Comparative Programming Languages. Syntax Specification CPS 506 Comparative Programming Languages Syntax Specification Compiling Process Steps Program Lexical Analysis Convert characters into a stream of tokens Lexical Analysis Syntactic Analysis Send tokens

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

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

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

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

Review of the C Programming Language

Review of the C Programming Language Review of the C Programming Language Prof. James L. Frankel Harvard University Version of 11:55 AM 22-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Reference Manual for the

More information

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu. Symbol Tables ASU Textbook Chapter 7.6, 6.5 and 6.3 Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 Definitions Symbol table: A data structure used by a compiler to keep track

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

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

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

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

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

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

COS 140: Foundations of Computer Science

COS 140: Foundations of Computer Science COS 140: Foundations of Computer Science Variables and Primitive Data Types Fall 2017 Introduction 3 What is a variable?......................................................... 3 Variable attributes..........................................................

More information

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler. More detailed overview of compiler front end Structure of a compiler Today we ll take a quick look at typical parts of a compiler. This is to give a feeling for the overall structure. source program lexical

More information

The PCAT Programming Language Reference Manual

The PCAT Programming Language Reference Manual The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University September 27, 1995 (revised October 15, 2002) 1 Introduction The PCAT language

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

More information

Ambiguity and Errors Syntax-Directed Translation

Ambiguity and Errors Syntax-Directed Translation Outline Ambiguity (revisited) Ambiguity and rrors Syntax-Directed Translation xtensions of CFG for parsing Precedence declarations rror handling Semantic actions Constructing a parse tree CS780(Prasad)

More information

Semantic Analysis and Type Checking

Semantic Analysis and Type Checking Semantic Analysis and Type Checking 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

More information

CSE P 501 Exam 8/5/04

CSE P 501 Exam 8/5/04 Name There are 7 questions worth a total of 65 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. You may refer to the following references: Course

More information

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. Lecture 09: Data Abstraction ++ Parsing Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. program text Parser AST Processor Compilers (and some interpreters)

More information

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Lecture 18 1 Announcements

More information