CSCI Compiler Design

Similar documents
CSCI Compiler Design

CSCI Compiler Design

CMPSC 160 Translation of Programming Languages. Three-Address Code

CSCI565 Compiler Design

CSCI565 Compiler Design

CSCI Compiler Design

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

Formal Languages and Compilers Lecture X Intermediate Code Generation

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

Syntax-Directed Translation

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

CS2210: Compiler Construction. Code Generation

Intermediate Code Generation

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona

Syntactic Directed Translation

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

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

CS606- compiler instruction Solved MCQS From Midterm Papers

Intermediate Code Generation

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

QUESTIONS RELATED TO UNIT I, II And III

More On Syntax Directed Translation

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

Question Points Score

Lexical and Syntax Analysis. Bottom-Up Parsing

Intermediate Code Generation

CS 406/534 Compiler Construction Putting It All Together

Question Bank. 10CS63:Compiler Design

CS 536 Midterm Exam Spring 2013

2068 (I) Attempt all questions.

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Bottom-Up Parsing. Lecture 11-12

CSE302: Compiler Design

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

CSCE 531, Spring 2015 Final Exam Answer Key

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.

CSE 401 Midterm Exam Sample Solution 2/11/15

Syntax-Directed Translation Part II

Intermediate Representations

PRINCIPLES OF COMPILER DESIGN

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

shift-reduce parsing

Conflicts in LR Parsing and More LR Parsing Types

Intermediate Code Generation

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

CS5363 Final Review. cs5363 1

Lecture 8: Deterministic Bottom-Up Parsing

VIVA QUESTIONS WITH ANSWERS

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

Downloaded from Page 1. LR Parsing

S Y N T A X A N A L Y S I S LR

Syntax-Directed Translation. Lecture 14

LECTURE 3. Compiler Phases

Module 25 Control Flow statements and Boolean Expressions

Summary: Semantic Analysis

COP4020 Spring 2011 Midterm Exam

Lecture 7: Deterministic Bottom-Up Parsing

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

LR Parsing LALR Parser Generators

CSE P 501 Exam 8/5/04

Syntax Directed Translation

COP5621 Exam 3 - Spring 2005

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

CS 406: Syntax Directed Translation

MODULE 14 SLR PARSER LR(0) ITEMS

Bottom-Up Parsing. Lecture 11-12

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

CS 314 Principles of Programming Languages

CSCI312 Principles of Programming Languages

Dixita Kagathara Page 1

Intermediate Code Generation

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

MIDTERM EXAM (Solutions)

UNIT III & IV. Bottom up parsing

LR Parsing LALR Parser Generators

Compiler Construction

LR Parsing Techniques

CSE302: Compiler Design

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

Problem Score Max Score 1 Syntax directed translation & type

CSCI Compiler Design

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

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

CSE 5317 Midterm Examination 4 March Solutions

Chapter 4 :: Semantic Analysis

CSc 453 Intermediate Code Generation

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

Time : 1 Hour Max Marks : 30

HW 3: Bottom-Up Parsing Techniques

Abstract Syntax Trees & Top-Down Parsing

Intermediate Code Generation

Compiler Construction

Transcription:

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 [25 points] Conser the CFG fragment with non-terminal symbols {S, A, B}, with start symbol S, terminal symbols { } and the productions P listed below. (1) S A $ (2) A B (3) B B (4) B Questions: a) [15 points] Compute the DFA that recognizes the LR(0) sets of items for this grammar and construct the corresponding LR(0) parsing table. Comment on the nature of the conflicts. b) [10 points] Rewrite the grammar to avo the conflicts found in b). If there are any additional conflicts describe how the SLR table-construction algorithm would resolve them. a) Below is the DFA for the LR(0) sets of items and the corresponding LR(0) table. S! A $ A! B B! B B B A B S A $ A B B B A B B B State Action Goto $ A B shift goto goto accept shift reduce (2/3) reduce (2/3) reduce (4) reduce (4) As can be observed there are two reduce/reduce conflicts in state corresponding to the productions (2) and (3). We can try to address these conflicts by computing the FOLLOW sets for the non-terminals A and B and see if we can solve them. FOLLOW(A) = { $ }, FOLLOW(B) = { } Name: Number: 1 of 7

University of Southern California CSCI565 Compiler Design Mterm Exam Solution - Spring 2014 Using this knowledge, we can now construct the corresponding SLR table as shown below: State Action Goto $ A B shift goto goto accept shift reduce (3) reduce (2) reduce (4) reduce (4) As can be seen we are able to resolve the conflicts using this SLR table construction algorithm. b) Another way to resolve this conflict is by observing a 'replication' of the RHS of production (3) and thus eliminate the non-terminal A and all of its productions using the appropriate substitution. The resulting grammar is as shown below. There was a trick here that one has to be careful about. Just replacing the non-terminal A with B in production (1) would not lead to the same language (see below). (1) S B $ (2) B B (3) B S B $ S! B $ B! B B B B S! B $ B B B B State Action Goto $ B shift shift goto shift accept reduce (2) reduce (2) reduce (3) reduce (3) With this revised grammar there is no conflict and no need to use the SLR table construction algorithm to resolve them. As alluded to above the transformed grammar: (1) S B $ (2) B B (3) B is not equivalent to the original grammar as the language generated by the original grammar would not include the word "" and this transformed one would. Name: Number: 2 of 7

University of Southern California CSCI565 Compiler Design Mterm Exam Solution - Spring 2014 Problem 2: Syntax-Directed Translation [30 points] In Pascal a programmer can declare two integer variables a and b with the syntax var a, b : integer This declaration might be described with the following grammar: VarDecl 'var' IDList ':' TypeID IDList IDList ',' ID ID where IDList derives a comma separated list of variable names and TypeID derives a val Pascal type. You may find it necessary to rewrite the grammar. (a) [10 points] Write an attribute grammar that assigns the correct data type to each declared variable and show the values of your attributes for the example given above (consering that the TypeID for an integer is say 4). (b) [05 points] Determine an evaluation order of the attributes irrespective of the way the parse tree is constructed. (c) [05 points] How would you change the grammar to ensure that all the attributes would be synthesized attributes? (d) [10 points] Modify the original grammar to eliminate the left-recursion, and describe how to implement the corresponding attributive grammar using a table-based LL predictive parser. a) We define an attribute grammar with an integer (or enumerated if you prefer) attribute named type for the non-terminal symbols Id and IDList as well as the attribute value for the terminal symbol TypeID. The grammar productions and corresponding rules are as shown below where we have ignored the VarDecl non-terminal symbols as it plays no rule in the evaluation process. VarDecl 'var' IDList ':' TypeID IDList.type = TypeID.value IDList 0 IDList 1 ',' ID IDList 1.type = IDList 0.type; ID.type = IDList 0.type; ID ID.type = IDList 0.type; b) As can be observed the attribute type is an inherited attribute being first generated by the rule associated with the first production of the grammar, which is subsequently propagated down the parse tree along the IDList nodes. c) One can change the grammar without changing the accepted language as shown below where we have also depicted the revised attribute rules, now only involving synthesized attributes. VarDecl 'var' ID List ID.type = List.type List 0 ',' ID List 1 ID.type = List 1.type; List 0.type = List 1.type ':' TypeID List 0.type = TypeID.value Name: Number: 3 of 7

University of Southern California CSCI565 Compiler Design Mterm Exam Solution - Spring 2014 d) One can change the grammar as shown below where we have eliminated the left-recursion. On the right-hand se we show a sample AST using this grammar for a declaration of two variables of a generic type. VarDecl 'var' IDList ':' TypeID IDList ID IDList' IDList' ',' ID IDList' ε VarDeclList 'var' IDList ':' TypeID ID IDList' ',' ID IDList' Regarding the implementation of the attributed grammar resulting after this transformation we are left with the implementation of an attributive grammar that can be evaluated using an LL tablebased parsing algorithm as follows. When the top production is expanded the value of the inherited attribute of typeid is left on the stack at the very bottom (we assume here that being a terminal symbol its attribute value is a constant). Then 'var' is shifted and removed from the stack, leaving IDList exposed. Once that is expanded we can 'access' the value of typeid two position down in the stack and set the attribute of IDList' as this is pushed onto the stack. With ID now on top, the attribute of IDList' is just underneath it. The situation repeats itself for the other production of IDList'. The inherited attributes can be evaluated and set on the correct symbols once they are passed onto the stack. Name: Number: 4 of 7

University of Southern California CSCI565 Compiler Design Homework 2 - Solution Problem 3. Intermediate Code Generation [30 points] Suppose that your language had an exclusive-or (EXOR) logical operator and the corresponding syntax as shown below: E E 1 ^ E 2 Questions: (a) [10 points] Write an SDT scheme for this EXOR operator using the arithmetic-based representation of boolean expression under the assumption that the target machine architecture does include an EXOR logic instruction. Show your work for an illustrative expression. (b) [20 points] Write an SDT scheme using short-circuit evaluation for this logic operator. Notice that this is not as trivial as it sounds as the evaluation of the expression should only yield a true value if and only if only one of the operands is true. Show your work for an illustrative expression. a) For the arithmetic-based representation of boolean the code generation SDT would be a trivial modification of the rules described in class for the binary AND or OR operator as shown below. E E 1 ^ E 2 E.place = newtemp() E.code=append(E 1.code,E 2.code,gen(E.place=E 1.place^ E 2.place) E 1.nextstat = E.nexstat E 2.nextstat = E 1.laststat E.laststat = E 2.laststat + 1 For an expression of the form "x = (a < b) ^ (c > d)" the resulting generated intermediate code would be: 00: if a < b goto 03 01: t1 = 0 02: goto 04 03: t1 = 1 04: if c < d goto 07 05: t2 = 0 06: goto 08 07: t2 = 1 08: t3 = t1 ^ t2 b) The difficulty of this control-flow generation scheme is that after the evaluation of the first term of the EXOR one still needs to evaluate the second term. If the firs term evaluates to false, then we would like the goto labels of the code corresponding to the second term to be the same as the goto of the overall boolean expression. If, however, the first term evaluates to true, we would like these goto labels to be switched. An elegant way to deal with this is to use a single auxiliary boolean variable, say t, which is initialized to false. If the code corresponding to the first term of the expression evaluates to true, the corresponding target label could include an assignment of that variable to true. The evaluation of the second term of the expression should follow and should this evaluation hold true the value of the auxiliary variable should be reversed. Lastly, a single conditional goto Name: Number: 5 of 7

University of Southern California CSCI565 Compiler Design Homework 2 - Solution will transfer control to the true or false label of the overall boolean expression. The code layout below depicts this code structure. E1.true E1.false E2.true E2.false t = false E1.code t = true E2.code t = not t if t goto E.true goto E.false E1.true E1.false E2.true E2.false t = newtemp! E1.true = newlabel! E1.false = newlabel! E2.true = newlabel! E2.false = newlabel! E.code = append (! gen('t = false;'),! E1.code,! gen('e1.true: t = true;'),! gen('e1.false: '),!! E2.code,! gen('e2.true: t = not t;'),! gen('e2.false: if (t) goto E.true;'),! gen('goto E.false;')! ) For the sample example of a boolean expression used in a) this SDT scheme would generate the code below: 00: t = false 01: if a < b goto 03 02: goto 04 03: t = true 04: if c > d goto 06 05: goto 07 06: t = not t 07: if (t) goto E.true 08: goto E.false Another alternative code generation scheme would make partial use of the arithmetic-based approach by having the evaluation of the two boolean expression terms derive their results in two independent auxiliary variables, say t1 an t2, followed by a simple EXOR logical operand and the condition jump for the E.true and E.false labels in indicated by the inherited attributes. Name: Number: 6 of 7

University of Southern California CSCI565 Compiler Design Homework 2 - Solution Problem 4. SSA 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 =...; Lloop: if(i > 0) { if(a < 0){ b = i+1; } else { d = 1; } i = i - 1; if(i < 0) goto Lbreak; goto Lloop; } 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 =...; a 1 =...; i 1 =...; X: i 2 = φ(i 1,i 3 ) if (i 2 > 0) then goto Y if (a 1 >= 0) then goto Z b 2 = i 2 + 1 goto Z d 2 = 1 Z: i 3 = i 2-1 if (i 3 > 0) then goto Y goto X Y: b 3 = φ(b 1,b 2 ) x 1 = a 1 y 1 = b 3 As can be observed by inspection each use has a single definition point that reaches it and each value is defined only once. Name: Number: 7 of 7