Docsity

9 Problems on Compiler Construction - Examination 3 | COP 5621, Exams of Computer Science

Toggle sidebar
bg1
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 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. Match the terms below with the given sentences so as to best complete each sentence. Use
no term more than once. Some terms will go unused. (10 points)
(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.
2. Give one reason why compilers generate intermediate code (rather than generating machine
code directly). (5 points)
1
pf3
pf4
pf5

Partial preview of the text

Download 9 Problems on Compiler Construction - Examination 3 | COP 5621 and more Exams Computer Science in PDF only on Docsity!

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 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. Match the terms below with the given sentences so as to best complete each sentence. Use no term more than once. Some terms will go unused. (10 points)

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

  1. Give one reason why compilers generate intermediate code (rather than generating machine code directly). (5 points)
  1. Given the following grammar for an expression:

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)

  1. Consider the following syntax-directed definition:

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)

  1. Consider the following syntax-directed definition:

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)

  1. (bonus question). (15 points) The following syntax-directed definition uses the backpatching technique to generate short-circuit code for Boolean expressions. The right-hand side of an assignment can be a Boolean expression. A 1 is to be stored in the assigned variable when the Boolean expression evaluates to true and a 0 is to be stored when the expression is false.

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 โ†’ ( E 1 )...

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)