Docsity

Midterm Exam Sample Solutions for Programming Language Foundations | 22C 185, Exams of Programming Languages

Toggle sidebar
bg1
22C:185: Fall 2004
page 1 of 3
Midterm Exam
Sample Solutions
Problem 1.
Any of several instances of ambiguity could be identified as a response. For one,
consider a command of the form while b do c1; c2. This has two structural descriptions.
command command
while expr do command command ; command
b command ; command while expr do command c2
c1 c2 b c1
Another example would be a command of the form if b1 then if b2 then c1 else c2 that
has two structural descriptions.
command command
if expr then command if expr then command else command
b1 if expr then command else command b1 if expr then command c2
b2 c1 c2 b2 c1
pf3

Partial preview of the text

Download Midterm Exam Sample Solutions for Programming Language Foundations | 22C 185 and more Exams Programming Languages in PDF only on Docsity!

Midterm Exam Sample Solutions Problem 1. Any of several instances of ambiguity could be identified as a response. For one, consider a command of the form while b do c1; c2. This has two structural descriptions. command command while expr do command command ; command b command ; command while expr do command c c1 c2 b c Another example would be a command of the form if b1 then if b2 then c1 else c2 that has two structural descriptions. command command if expr then command if expr then command else command b1 if expr then command else command b1 if expr then command c b2 c1 c2 b2 c

Problem 2. The first production in this attribute grammar is replaced by two new rules, namely

::= 1. 2 e 3 , and

::= 1. 2 e- 3.

These productions provide for the addition of a binary exponent suffix. All the other productions and their attribute rules are unchanged. Note that in the examples in the problem statement, the exponent value is used as the multiplier (or dividend). This is in contrast with common usage where the multiplier has the exponent part as the power of the base -- this may have been distracting and was not enough simpler to justify it, sorry. Either of these choices was given full credit (and differ technically in a very small way). For consistency with the problem statement, the attribute evaluation rules provided here take the former approach (just put Val( 3 ) as the exponent of 2 for the latter). Syntax rules Attribute evaluation ::=

1. 2

e 3 Val() fl

(Val( 1 )

Val(<binary!digits> 2 ) 2 Len(<binary!digits>^2 )

) * Val( 3 )

::=

1. 2

e- 3 Val() fl

(Val( 1 )

Val(<binary!digits> 2 ) 2 Len(<binary!digits>^2 )

) / Val( 3 )

Since the Val() attribute is correctly determined by the original attribute grammar rules, the only need is to adjust Val(). Adding a positive or negative exponent requires the value of the numeral preceding the exponent to be multiplied/divided by the appropriate value. This is determined using the Val() attribute, and then applied as indicated above.