Docsity

Midterm Exam Questions - Programming Language Foundations | 22C 185, Exams of Programming Languages

Toggle sidebar
bg1
22C:185 — Fall 2004
page 1 of 2
Mid-term Exam
Open book/notes
1. [30 points]
The context-free grammar (S = {a, b, c, '+', '*', '(', ')'}), non-terminals E (start symbol)
and I short for Expression and Identifier, respectively)
E Æ E+E | E*E | (E) | I
I Æ a | b | c
generates
all
well-formed arithmetic expressions involving the operators + and *,
parentheses, and the identifiers a, b, and c. However, it is ambiguous.
(a) show why this grammar is ambiguous [15 points]
(b) construct an equivalent unambiguous grammar — include justification that your
grammar is both equivalent and unambiguous [15 points].
2. [35 points]
For this problem you are to devise an attribute grammar whose terminal alphabet is {0,1}
and having a Boolean-valued attribute called 'valid' (and other attributes if you wish) that
is true for the root node of a derivation tree just in case the derived string is of the form
0k1w (k≥0) for wŒ{0,1}* where the longest substring(s) of
consecutive
'0's in w (there
could be several) has length exactly k. Each such string 0k1w (and no doubt others)
should be derivable with your grammar. For instance, 0010100100 is valid, while
0010110 and 0010100011 are invalid. Your answer should cover the following:
(a) besides those required above, what strings are derivable with your context-free
grammar component,
(b) characterize each attribute you introduce as either synthesized or inherited and
state any dependencies in the order of their evaluation,
(c) explain why the 'valid' attribute evaluates to true for the proper strings, and false for
all others that are derivable.
pf2

Partial preview of the text

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

22C:185 — Fall 2004

page 1 of 2

Mid-term Exam

Open book/notes

1. [30 points]

The context-free grammar (S = {a, b, c, '+', '*', '(', ')'}), non-terminals E (start symbol)

and I short for Expression and Identifier, respectively)

E Æ E+E | E*E | (E) | I

I Æ a | b | c

generates all well-formed arithmetic expressions involving the operators + and *,

parentheses, and the identifiers a, b, and c. However, it is ambiguous.

(a) show why this grammar is ambiguous [15 points]

(b) construct an equivalent unambiguous grammar — include justification that your

grammar is both equivalent and unambiguous [15 points].

2. [35 points]

For this problem you are to devise an attribute grammar whose terminal alphabet is {0,1}

and having a Boolean-valued attribute called 'valid' (and other attributes if you wish) that

is true for the root node of a derivation tree just in case the derived string is of the form

k 1w (k≥0) for wŒ{0,1} ***** where the longest substring(s) of consecutive '0's in w (there

could be several) has length exactly k. Each such string 0

k 1w (and no doubt others)

should be derivable with your grammar. For instance, 0010100100 is valid, while

0010110 and 0010100011 are invalid. Your answer should cover the following:

(a) besides those required above, what strings are derivable with your context-free

grammar component,

(b) characterize each attribute you introduce as either synthesized or inherited and

state any dependencies in the order of their evaluation,

(c) explain why the 'valid' attribute evaluates to true for the proper strings, and false for

all others that are derivable.

22C:185 — Fall 2004

page 2 of 2

3. [35 points]

Modify the formal denotational definition of Wren (Figure 9.11 of our text, page 291) to

add the semantics for the following command:

::= watch do end watch

The informal description of the desired semantics is that the execution of the watch-

command causes no immediate effect, but after its execution, any subsequent change to

the will cause the immediate execution of the command following do , and

after its completion, execution resumes normally.

This new command introduces an additional reserved word and context conditions (e.g.,

the watch-variable must be a declared variable), but you are not responsible for these

issues in this problem. You need only make changes in the denotational semantics (i.e.,

domains and equations) to capture the informally stated semantics. Justify that the effect

of your changes accomplishes the required behavior.