Exam3-2005 - COP5621 Exam 3 Spring 2005(Please print Name Put the answers on these sheets Use additional sheets when necessary Show how you derived

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COP5621 Exam 3 - Spring 2005 (Please print) Name: 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) (2) (3) (4) (5) (6) (7) one two three four attributes synthesized S (8) (9) (10) (11) (12) (13) (14) syntax-directed definitions semantic actions abstract syntax trees translation schemes parse trees inherited L (a) There are two notations for associating semantic rules and actions with grammar productions: 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 3. Given the following grammar for an expression: E E E T T T F F → → → → → → → → E+T E-T T T*F T/F F (E) 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) 2 4. Below is a grammar expressed in Yacc defining the syntax of a decimal constant. Add semantic actions to calculate the value of the constant and print the value to standard output. You may assume that Yacc’s stack type (YYSTYPE) is int.(15 points) decimal : digits ; digits : digits digit | digit ; digit : ’0’ | ’1’ ... | ’9’ ; 5. What are the three proposed evaluation methods for semantic rules to decorate parse trees? (5 points) 3 6. Consider the following syntax-directed definition: D T T T L L V V → → → → → → → → TL int real ^T1 V L1 , V id id [ T ] L.type := T.type T.type := int T.type := real T.type := pointer(T1 .type) V.type := L.type L1 .type := L.type; V.type := L.type enter(id.entry, V.type) 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 id1 , id2 [int] (c) Is the syntax-directed definition S-attributed? Is it L-attributed? (5 points) 4 7. Consider the following syntax-directed definition: E → E1 + T E→T T → num E.val := E1 .val + T.val E.val := T.val 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) 5 8. Suppose we build an interpreter in Yacc to evaluate integer and floating point expressions. The following fragment is part of this Yacc specification: %union { Symbol *sym; struct { int type; union { int float } val; } rec; } %token <sym> ID %type <rec> expr i; f; %% expr : expr ’+’ expr { ... | ID } { if ($1->type == Tint) { $$.type = Tint; $$.val.i = $1->val.i; } else { $$.type = Tfloat; $$.val.f = $1->val.f; } } ; The expr nonterminal has a type and a val field containing the type and value, respectively. Give the Yacc semantic actions to evaluate the sum ’+’ on integers and floats.(10 points) 6 9. (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 S → id := E E → E1 or M E2 backpatch(E.t, M.q); backpatch(E.f, nextquad) backpatch(. . . ) emit(id.place := 0); emit(goto nextquad+1); backpatch(. . . ) emit(id.place := 1) ... E → E1 and M E2 ... E → ( E1 ) ... E → not E1 ... E → id M → ￿ E.t := makelist(nextquad); emit(if id.place goto ); E.f := makelist(nextquad); emit(goto ) 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) 7 ...
View Full Document

This note was uploaded on 02/01/2012 for the course COP 5621 taught by Professor Vanengelen during the Spring '11 term at FSU.

Ask a homework question - tutors are online