×
DREAM IT. BELIEVE IT. ACHIEVE IT.
--Your friends at LectureNotes
Close

Solution to Previous Year Exam Questions for Compiler Design - CD of UCLA by Ratnakar babu Mikkili

by Ratnakar Babu Mikkili
Type: PYQ SolutionInstitute: University of California Los Angeles, LA Course: B.Tech Specialization: Computer Science EngineeringDownloads: 2Views: 50Uploaded: 2 months agoAdd to Favourite

0 User(s)

Share it with your friends

Suggested Materials

Leave your Comments

Contributors

Ratnakar Babu Mikkili
Ratnakar Babu Mikkili
University of Southern California CSCI565 – Compiler Design Midterm Exam - Solution CSCI 565 – Compiler Design Spring 2010 Solution to the Midterm Exam _________________________________________________________________________ Problem 1: LR Top-Down Parsing [30 points] Given the following CFG grammar G = ({S,A,B}, S, {a, b, x), P) with P: (1) S → A (2) S → xb (3) A → aAb (4) A → B (5) B → x For this grammar answer the following questions: a) [10 pts] b) [05 pts] c) [05 pts] d) [05 pts] e) [05 pts] Compute the set of LR(1) items for this grammar and the corresponding DFA. Do not forget to augment the grammar with the default initial production S’ → S$ as the production (0). Construct the corresponding LR parsing table. Would this grammar be LR(0)? Why or why not? (Note: you do not need to construct the set of LR(0) items but rather look at the ones you have already made for LR(1) o answer this question. Show the stack contents, the input and the rules used during parsing for the input string w = axb$ Would this grammar be suitable to be parsed using a top-down LL parsing method? Why? Solution: a) The set of LR(1) items and the corresponding DFA are shown below: 1 of 6
University of Southern California CSCI565 – Compiler Design Midterm Exam - Solution b) The parsing table is as shown below: State 0 1 2 3 4 5 6 7 8 9 10 11 12 c) a s5 b terminals x s3 S g1 goto A g2 B g4 g7 g8 acc r(1) r(5) r(4) s6 s6 s6 $ s9 s12 r(4) r(5) s11 r(3) g10 r(3) If we were to compute the set of LR(0) items for state s3 we would have a shift-reduce conflict as on “b” as in this method for building the parse table we would have a shift action and for all the input tokens but the second item in the s3 set of items would suggest a reduction. This grammar is thus not LR(0) parseable. d) The stack contents for the input string w = axb$. e) This grammar does not have the LL(1) property as FIRST(A) and FIRST(xb) both productions of S will have “x” as a common terminal symbol. 2 of 6
University of Southern California CSCI565 – Compiler Design Midterm Exam - Solution Problem 2: Syntax-Directed Translation [30 points] Consider the following syntax-directed definition over the grammar defined by G = ({S, A, Sign}, S, {‘,’, ‘-‘, ‘+’, ‘n’}, P) with P the set of production and the corresponding semantic rules depicted below. There is a special terminal symbol “n” that is lexically matched by any string of one numeric digit and whose value is the numeric value of its decimal representation. For the non-terminal symbols in G we have defined two attributes, sign and value. The non-terminal A has these two attributes whereas S only has the value attribute and Sign only has the sign attribute. S → A Sign Sign → + Sign → A →n A → A1 , n || || || || || S.val = A.val; A.sign = Sign.sign; print(A.val); Sign.sign = 1 Sign.sign = 0 A.val = value(n) A1.sign = A.sign; if(A.sign = 1) then A.val = min (A1.val,value(n)); else A.val = max(A1.val,value(n)); For this Syntax-Directed Definition answer to the following questions: a) [05 pts] Explain the overall operation of this syntax-directed definition and indicate (with a brief explanation) which of the attributes are either synthesized or inherited. b) [10 pts] Give an attributed parse tree for the source string “5,2,3-“ and evaluate the attributes in the attributed parse tree depicting the order in which the attributes need to be evaluated (if more than one order is possible indicate one.) c) [15 pts] Suggest a modified grammar and actions exclusively using synthesized attributes. Explain its basic operation. Solution: a) This syntax-directed definition computes the maximum or minimum of a sequence of integers depending on the suffix ‘-‘ or ‘+’ respectively. As to the attributes, clearly the sign attribute for the Sign non-terminal is synthesized. The val attribute is also synthesized but the sign attribute for A is inherited as it flows from “right-to-left” (thus from top to bottom in the parse tree) for a production of A. b) The parse tree and the corresponding attribute values as well as an evaluation order is as shown below. c) The idea is to decouple at the top the two situations, computing a minimum or maximum based on the value of the Sign non-terminal. To accomplish this we have two very similar non-terminal symbols B and C as shown below, which replace the A terminal symbol. Note also the presence of two productions for S for the two computation functions, min and max and the absence of the non-terminal Sign. All attributes are now synthesized as shown in the figure below. 3 of 6
University of Southern California S S B B C C →B+ →C→n → B1 , n →n → C1 , n || || || || || || CSCI565 – Compiler Design Midterm Exam - Solution S.val = B.val; print(B.val) S.val = C.val; print(C.val) B.val = value(n) B.val = max(B1.val,value(n)) C.val = value(n) C.val = min(C1.val,value(n)) Problem 3: Intermediate Code Generation [40 points] Suppose that your language supports the dynamic allocation of single-dimensional vectors or arrays and that the grammar productions are as shown below: vec_expr → id [ expr ] expr → id S → id = vec_expr S → vec_expr = expr where expr is a generic expression with attributes place and code that indicate, respectively the temporary variable where the result of the evaluation of the expression will reside and the corresponding code. Note that because an array reference might appear on the left-hand-side or on the right-hand-side of an assignment, you need to use a base address and an index to evaluate the expression in the context of an assignment statement. Regarding the code generation for these array accesses answer the following questions: a) [15 pts] Define an SDT for the generation of intermediate code that computes the value of vec_expr. Given that the sizes of the array are only known at run-time (after allocation) you need to have access to a dope vector structure. Assume you know the address of this vector given by the variable reference “dyn_vec(id.name)”. The first location of this auxiliary array holds the base address of the vector (and index 0) and the second location of this vector holds the size of the single dimension of the vector (at index 1). b) [15 pts] Show the results of your work on the statement “v = a[i]” indicating the AST with the values of the attributes. c) [10 pts] Redo a) for the case where you want to include code that checks the bounds of the accesses to the vector. For instance when generating code for “a[i]” you need to check if the index value (in this case “i”) is both nonnegative and less than the dimension of the vector. 4 of 6

Lecture Notes