Docsity

Midterm Exam 1 with Solution - Programming Languages and Compilers | COMPSCI 164, Exams of Computer Science

Toggle sidebar
bg1
Midterm I
CS164, Spring 2004
February 26, 2003
Please read all instrutions (inluding these) arefully.
Write your name, login, SID, and irle the setion time.
There are 7 pages in this exam and 4 questions, eah with multiple parts. Some questions
span multiple pages. All questions have some easy parts and some hard parts. If you get
stuk on a question move on and ome bak to it later.
You have 1 hour and 20 minutes to work on the exam.
The exam is losed bo ok, but you may refer to your two pages of handwritten 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. We might dedut points if your solution is far more ompliated
than neessary. Partial solutions will b e graded for partial redit.
LOGIN: Solutions
NAME:
SID:
Cirle the time of your setion: Tue 3:00 Tue 4:00 Wed 10:00 Wed 11:00 Wed 1:00 Wed 2:00
Problem Max points Points
1 20
2 15
3 50
4 15
TOTAL 100
1
pf3
pf4
pf5

Partial preview of the text

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

Midterm I

CS164, Spring 2004

February 26, 2003

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

 Write your name, login, SID, and ir le the se tion time.

 There are 7 pages in this exam and 4 questions, ea h with multiple parts. Some questions span multiple pages. All questions have some easy parts and some hard parts. If you get stu k on a question move on and ome ba k to it later.

 You have 1 hour and 20 minutes to work on the exam.

 The exam is losed b o ok, but you may refer to your two pages of handwritten 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. We might dedu t p oints if your solution is far more ompli ated than ne essary. Partial solutions will b e graded for partial redit.

LOGIN: Solutions

NAME:

SID:

Cir le the time of your se tion: Tue 3:00 Tue 4:00 Wed 10:00 Wed 11:00 Wed 1:00 Wed 2:

Problem Max p oints Points 1 20 2 15 3 50 4 15 TOTAL 100

1 Finite Automata and Regular Expressions (20 p oints)

(a) (10 p oints) Draw a 4-state DFA over the alphab et  = fa; bg that a epts the language onsisting of strings with an even numb er of a (this in ludes zero a), or an o dd numb er of b. Solution:

(b) Consider the following DFA over the alphab et  = fa; bg.

i. (5 p oints) Lab el the transitions of the following NFA so that it a epts the same language as the DFA. Your NFA should transform to the given DFA by applying the NFA-to-DFA onversion algorithm given in le ture.

ii. (5 p oints ) Write the regular expression for this language. Among the several p ossi- ble answers, write the one that would transform to this NFA by applying the regular expression-to-NFA onversion algorithm given in le ture. Solution: (ba)^ j a

3 LR Parsing and Ambiguity (50 p oints)

Consider the following grammar over the terminals +, (the negation op erator) and id.

S! E E! E + E j E j id

Below is a partial DFA for this grammar.

(a) (21 p oints) Complete the ab ove DFA. You need to do the following:

 Complete state 0 by p erforming losure on the item listed.  Fill in all elements of states 1 and 5, and the lo okahead items in states 3, 4 and 6.  Fill in the missing transition lab els on all edges.  Write the ne essary \redu e by ..." lab els on states.

(b) (4 p oints) For ea h state with a on i t, list the state, the lo okahead token, and the typ e of on i t (i.e. shift-redu e on i t, or redu e-redu e on i t). Solution:

State Lo okahead Typ e 2 + shift-redu e 6 + shift-redu e

( ) (6 p oints) Draw all the parse trees for the string id + id + id. Solution:

(d) (3 p oints) Is this grammar ambiguous? Why or why not? Solution:

Yes, this grammar is ambiguous b e ause the string id + id + id has three parse trees.

4 Grammars (15 p oints)

Consider two LL(1) grammars, ea h with a single non-terminal, and without  pro du tions. The non-terminal for the rst grammar is A and for the se ond one is B. Consider a third grammar obtained by merging the pro du tions of the two grammars and adding the pro du tion S! A j B. Answer the following questions, and explain your answers.

(a) (5 p oints) Is it p ossible to derive the empty string from S in the third grammar? Solution:

No, S an derive the empty string i either A or B an derive the empty string.

(b) (5 p oints) Write the ne essary and suÆ ient ondition(s) that the two original grammars must satisfy, in order to ensure that the third grammar is unambiguous. Do not give examples of grammars for A and B, but give instead a general ondition that applies to all grammars of the form mentioned ab ove. Solution:

The interse tion of the languages a epted by A and B should b e empty, i.e.

L(A) \ L(B ) = ;

Here L(A) denotes the language a epted by grammar A.

( ) (5 p oints) Write the ne essary and suÆ ient ondition(s) that the two original grammars must satisfy, in order to ensure that the third grammar is LL(1). Do not give examples of grammars for A and B, but give instead a general ondition that applies to all grammars of the form mentioned ab ove. Solution:

The interse tion of the rst sets of A and B should b e empty, i.e.

F ir st(A) \ F ir st(B ) = ;