



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




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 it and helpful for partial credit). You can collect 100 points in total for this exam. A bonus question for an addit ional 15 points is included. If the total number of points for this exam exceed 100, the excess points are ca rried over to the next exams.
(1) one (8) syntax-directed definitions (2) two (9) semantic actions (3) three (10) abstract syntax trees (4) four (11) translation schemes (5) attributes (12) parse trees (6) synthesized (13) inherited (7) S (14) L
(a) There are two notations for associating semantic rules and actions with grammar pro- ductions: and. (b) In translation schemes, are inserted within the right-hand sides of productions. (c) Quadruples are an implementation of -address code statements. (d) The larger class of -attributed definitions enable translations to be performed without explicit construction of a parse tree. (e) The values of attributes are passed upwards in a parse tree.
E โ E + T E โ E - T E โ T T โ T * F T โ T / F T โ F F โ ( E ) F โ id
represent the string
a*(b-c)+(b-c)
as a parse tree, an abstract syntax tree, and a DAG that is minimal. (15 points)
D โ T L L.type := T.type T โ int T.type := int T โ real T.type := real T โ ^T 1 T.type := pointer(T 1 .type) L โ V V.type := L.type L โ L 1 , V L 1 .type := L.type; V.type := L.type V โ id enter(id.entry, V.type) V โ id [ T ] enter(id.entry, array(V.type, T.type))
(a) Identify the synthesized and inherited attributes. (5 points) (b) Show the annotated parse tree for the sentence. (15 points) ^real id 1 , id 2 [int] (c) Is the syntax-directed definition S-attributed? Is it L-attributed? (5 points)
E โ E 1 + T E.val := E 1 .val + T.val E โ T E.val := T.val T โ num T.val := num.val
(a) Eliminate left recursion and show the converted translation scheme. (10 points) (b) Write a recursive descent parser for the converted translation scheme. (5 points)
S โ if E then M S backpatch(E.t, M.q); backpatch(E.f, nextquad) S โ id := E backpatch(... ) emit(id.place := 0); emit(goto nextquad+1); backpatch(... ) emit(id.place := 1) E โ E 1 or M E 2...
E โ E 1 and M E 2...
E โ not E 1...
E โ id E.t := makelist(nextquad); emit(if id.place goto ); E.f := makelist(nextquad); emit(goto ) M โ M.q := nextquad
(a) Complete the definition by completing the two backpatch operations in the assignment operation and the semantic rules for the not operator. (10 points) (b) Generate intermediate code for if not (a or b) then c := (a and b) The first three-address statement is numbered 0 (i.e. nextquad returns 0 the first time). You may assume that id.place represents the identifierโs name. Use additional sheets when necessary. (5 points)