Problem Set for Compiler Construction - Fall 2008 | COMP 412, Assignments of Computer Science

Material Type: Assignment; Class: COMPILER CONSTRUCTION; Subject: Computer Science; University: Rice University; Term: Fall 2008;

Typology: Assignments

Pre 2010

Uploaded on 08/16/2009

koofers-user-7o4-1
koofers-user-7o4-1 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMP 412 Fall 2008
Homework 2 Due: Monday, Oct 6th
Problem 1 (20 points)
Consider the grammar
Sโ†’(L)|a
Lโ†’L , S |S
(a) What are the terminals, nonterminals and start symbol?
(b) Find parse trees for the following sentences:
i) (a, a)
ii) (a, (a, a))
(c) Construct the leftmost derivation for each of the sentences in (b)
(d) Construct the rightmost derivation for each of the sentences in (b)
Problem 2 (20 points)
Consider the grammar
Sโ†’aSbS |bS aS |๎˜
(a) Show the grammar is ambiguous by constructing two different leftmost derivations for
the sentence abab.
(b) Construct the corresponding rightmost derivations for abab.
(c) Construct the corresponding parse trees from parts (a) and (b) for abab.
(d) What can you conclude from the parse trees in part (d)?
Problem 3 (20 points)
A grammar symbol Xis defunct if there is no derivation of the form Sโ‡’โˆ—wXy โ‡’โˆ—wxy,
where XโˆˆVand w, x, y โˆˆฮฃโˆ—. That is, Xcan never appear in the derivation of some
sentence.
(a) Write an algorithm to eliminate all productions containing defunct symbols from the
grammar.
(b) Apply your algorithm to the grammar below. Describe the steps your algorithm takes.
Sโ†’0|A
Aโ†’AB
Bโ†’1
1
pf2

Partial preview of the text

Download Problem Set for Compiler Construction - Fall 2008 | COMP 412 and more Assignments Computer Science in PDF only on Docsity!

COMP 412 Fall 2008 Homework 2 Due: Monday, Oct 6th

Problem 1 (20 points) Consider the grammar S โ†’ ( L ) | a L โ†’ L , S | S (a) What are the terminals, nonterminals and start symbol? (b) Find parse trees for the following sentences: i) (a, a) ii) (a, (a, a)) (c) Construct the leftmost derivation for each of the sentences in (b) (d) Construct the rightmost derivation for each of the sentences in (b)

Problem 2 (20 points) Consider the grammar S โ†’ aSbS | bSaS |  (a) Show the grammar is ambiguous by constructing two different leftmost derivations for the sentence abab. (b) Construct the corresponding rightmost derivations for abab. (c) Construct the corresponding parse trees from parts (a) and (b) for abab. (d) What can you conclude from the parse trees in part (d)?

Problem 3 (20 points) A grammar symbol X is defunct if there is no derivation of the form S โ‡’โˆ—^ wXy โ‡’โˆ—^ wxy, where X โˆˆ V and w, x, y โˆˆ ฮฃโˆ—. That is, X can never appear in the derivation of some sentence. (a) Write an algorithm to eliminate all productions containing defunct symbols from the grammar. (b) Apply your algorithm to the grammar below. Describe the steps your algorithm takes. S โ†’ 0 | A A โ†’ AB B โ†’ 1 1

Problem 4 (20 points) Section 3.3, question 2 (page 730 in EAC)

Problem 5 (20 points) Section 3.5, question 1 (pages 731 & 732 in EAC)