Docsity

Programming Languages and Compilers - Midterm Questions with Solutions | COMPSCI 164, Exams of Computer Science

Toggle sidebar
bg1
CS 164 Programming Languages and Compilers Handout 9-sol
Midterm I Solutions
Please read all instrutions (inluding these) arefully.
There are six questions on the exam, some with multiple parts. You have 1:20 to work on the
exam.
The exam is losed book, but you may refer to your four sheets of prepared notes.
Please write your answers in the spae provided on the exam, and learly mark your solutions.
You may use the baks of the exam pages as srath paper. Please do not use any additional
srath paper.
Solutions will be graded on orretness and larity. Eah problem has a relatively simple and
straightforward solution. You may get as few as 0 points for a question if your solution is far
more ompliated than neessary. Partial solutions will be graded for partial redit.
NAME:
SID or SS#:
Cirle the time of your setion: 9:00 10:00 11:00 12:00
1:00 2:00 3:00 4:00
Problem Max points Points
1 15
2 15
3 25
4 10
5 25
6 10
TOTAL 100
Spring 00 page 1 of 8
pf3
pf4
pf5
pf8

Partial preview of the text

Download Programming Languages and Compilers - Midterm Questions with Solutions | COMPSCI 164 and more Exams Computer Science in PDF only on Docsity!

Midterm I Solutions

 Please read all instru tions (in luding these) arefully.

 There are six questions on the exam, some with multiple parts. You have 1:20 to work on the exam.

 The exam is losed b o ok, but you may refer to your four sheets of prepared notes.

 Please write your answers in the spa e provided on the exam, and learly mark your solutions. You may use the ba ks of the exam pages as s rat h pap er. Please do not use any additional s rat h pap er.

 Solutions will b e graded on orre tness and larity. Ea h problem has a relatively simple and straightforward solution. You may get as few as 0 p oints for a question if your solution is far more ompli ated than ne essary. Partial solutions will b e graded for partial redit.

NAME:

SID or SS#:

Cir le the time of your se tion: 9:00 10:00 11:00 12:

Problem Max p oints Points 1 15 2 15 3 25 4 10 5 25 6 10 TOTAL 100

  1. First and Follow Sets (15 p oints)

Consider the following three grammars

1 : A! B C First(A) = f x g Fol low(A) = f x, $ g B! Ax j x First(B) = f x g Fol low(B) = f y g C! y C j y First(C) = f y g Fol low(C) = f x, $ g 2 : A! B C First(A) = f x, y g Fol low(A) = f x, $ g B! Ax j x j  First(B) = f x, y,  g Fol low(B) = f y g C! y C j y First(C) = f y g Fol low(C) = f x, $ g 3 : A! B C First(A) = f x, y,  g Fol low(A) = f x, $ g B! Ax j x j  First(B) = f x, y,  g Fol low(B) = f x, y, $ g C! y C j y j  First(C) = f y,  g Fol low(C) = f x, $ g

For ea h of the following statements ab out First and Follow sets, indi ate for whi h grammar(s) (1, 2, or 3) the statement is orre t. Ea h statement is orre t for one or more grammars|b e sure to list all the grammars for whi h a statement is orre t. Note that grammar 3 is a small extension of grammar 2, whi h is a small extension of grammar

  1. You may nd it helpful to ompute the First and Follow sets b eginning with the simplest grammar.

 F ir st(A) = fx; y g = f 2 g  F ol l ow (A) = f$; xg = f 1, 2, 3 g  F ol l ow (B ) = f$; x; y g = f 3 g  F ir st(C ) = fy g = f 1, 2 g  F ol l ow (C ) = f$; xg = f 1, 2, 3 g

  1. LR Parsing (25 p oints)

In these problems we des rib e a DFA for a epting viable pre xes of a grammar. Your assignment is to give a grammar whose DFA re ognizing viable pre xes satis es the des ription. Assume the DFA's we des rib e ontain LR(0) items|no lo okaheads are involved. Do not aug- ment your grammar with an extra pro du tion S! S 0. Just nd any grammar that works. Many people misunderstood this problem. Reread the first paragraph arefully. Instead of giving a grammar whose DFA for re ognizing viable prefixes mat hes the given des ription, many people gave a grammar that generates a (regular) language that has a DFA mat hing the des ription. Noti e that these answers don't really make sense, in any ase, sin e there are many possible DFAs for the same language. It is a oin iden e that the languages generated by the grammars for parts (a) and (b) happen to have DFAs that mat h the des riptions.

(a) The DFA onsists of one state and no transitions.

S! "

The state ontains the item S! : (b) The DFA onsists of two states s 1 and s 2 and there is one transition from the start state s 1 to s 2.

S! a

State s 1 ontains the item S! :a and state s 2 ontains the item S! a: ( ) The DFA onsists of three states s 1 , s 2 , and s 3. There is one transition from the start state s 1 to s 2 , one transition from s 2 to itself, and one transition from s 2 to s 3. There were several similar a eptable answers to this problem.

S! aS

S! aS j "

S! aS j a

The first grammar generates the empty language (it generates no finite strings). For the first grammar, the states are

s 1 : S! :aS s 2 : S! a:S s 3 : S! aS: S! :aS

The se ond grammar generates the language ontaining all strings of a's, and the third generates all strings of a's of length at least 1.

  1. Syntax-Dire ted Translation (10 p oints)

For the following grammar for arithmeti expressions de ne a syntax-dire ted translation that re ords the sign (POS or NEG) of ea h sub expression E in the attribute E :sig n. Write your semanti a tions to the right of the orresp onding pro du tion. You may assume that all integers are non-zero (so you don't need to worry ab out the sign of zero).

E! unsig ned integ er f E:sign = POS; g

j +E 1 f E:sign = E 1 :sign; g

j E 1 f E:sign = (E 1 :sign == POS)? NEG : POS; g

j E 1  E 2 f E:sign = (E 1 :sign == E 2 :sign)? POS : NEG; g

Common mistakes in answers to this problem were:

 Not realizing that a sign of an unsigned integer is always POS.  Assuming that +E and -E for e the sign of the result to be POS and NEG respe tively (the grammar is for arithmeti expressions, after all).  Using operations on POS and NEG without defining them.  Getting the sign logi in E 1  E 2 wrong.

(b) Give a leftmost derivation of the string aebb.

A leftmost derivation of the string aebb is

S! aS! aAb! aX Y Z b! aY Z b! aZ b! aeS b! aeAbb! aebb

Sin e the grammar is LL(1) (no onfli ts in the above parsing table) the grammar is not ambiguous and this leftmost derivation is unique.

( ) Show that if we add the pro du tion X! bS that the grammar is no longer LL(1).

If we add the produ tion X! bS then the following F ir st and F ol l ow sets hange: F ir st(X ) = fb; ; g F ir st(A) = fb; ; d; e; g

Sin e b 2 F ir st(X ) we have that b 2 F ir st(X Y Z ) and we will therefore have a onfli t in the LL(1) parsing table. The entry at [A; b℄ will ontain both entries A!  and A! X Y Z. This makes the grammar not LL(1). Note that the grammar is still unambiguous!

  1. Error Re overy (10 p oints)

Consider the following grammar for strings of ab's:

A! abA j 

(a) Augment the grammar with pro du tions that allow a parser to re ognize all erroneous strings as well as valid strings. The non-terminal ERROR should derive the suÆx starting at the rst erroneous terminal. More pre isely, in the ase that the input is erroneous, then ERROR should  derive a suÆx s of the string su h that if s is deleted, then the string is in the original language, and  derive the shortest su h suÆx. You may use as many new pro du tions and non-terminals b esides ERROR as you like. Assume that a and b are the only p ossible terminals.

A! abA j  j ERROR ERROR! a j bX j aaX X! aX j bX j 

(b) What language do es your augmented grammar generate?

The augmented grammar generates (a + b)^.

Common mistakes in answers to this problem were:  hanging the existing produ tions;  treating ERROR as a terminal rather than a non-terminal;  deriving a prefix rather than a suffix of the string;  writing produ tions for ERROR that didn't over all possible erroneous suffixes.