CS164 Midterm I Exam, Spring 2004, Exams of Programming Languages

The instructions and questions for the cs164 midterm i exam held in spring 2004. The exam covers topics such as finite automata and regular expressions, ll parsing, lr parsing and ambiguity, and grammars. Students are required to solve multiple-choice and open-ended questions within a given time limit.

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shailaja_987c
shailaja_987c 🇮🇳

4.3

(34)

217 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Midterm I
CS164, Spring 2004
February 26, 2003
Please read all instructions (including these) carefully.
Write your name, login, SID, and circle the section time.
There are 7 pages in this exam and 4 questions, each with multiple parts. Some questions
span multiple pages. All questions have some easy parts and some hard parts. If you get
stuck on a question move on and come back to it later.
You have 1 hour and 20 minutes to work on the exam.
The exam is closed book, but you may refer to your two pages of handwritten notes.
Please write your answers in the space provided on the exam, and clearly mark your solutions.
You may use the backs of the exam pages as scratch paper. Please do not use any additional
scratch paper.
Solutions will be graded on correctness and clarity. Each problem has a relatively simple and
straightforward solution. We might deduct points if your solution is far more complicated
than necessary. Partial solutions will be graded for partial credit.
LOGIN:
NAME:
SID:
Circle the time of your section: 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 CS164 Midterm I Exam, Spring 2004 and more Exams Programming Languages in PDF only on Docsity!

Midterm I

CS164, Spring 2004

February 26, 2003

  • Please read all instructions (including these) carefully.
  • Write your name, login, SID, and circle the section time.
  • There are 7 pages in this exam and 4 questions, each with multiple parts. Some questions span multiple pages. All questions have some easy parts and some hard parts. If you get stuck on a question move on and come back to it later.
  • You have 1 hour and 20 minutes to work on the exam.
  • The exam is closed book, but you may refer to your two pages of handwritten notes.
  • Please write your answers in the space provided on the exam, and clearly mark your solutions. You may use the backs of the exam pages as scratch paper. Please do not use any additional scratch paper.
  • Solutions will be graded on correctness and clarity. Each problem has a relatively simple and straightforward solution. We might deduct points if your solution is far more complicated than necessary. Partial solutions will be graded for partial credit.

LOGIN:

NAME:

SID:

Circle the time of your section: Tue 3:00 Tue 4:00 Wed 10:00 Wed 11:00 Wed 1:00 Wed 2:

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

1 Finite Automata and Regular Expressions (20 points)

(a) Draw a 4-state DFA over the alphabet Σ = {a, b} that accepts the language consisting of strings with an even number of a (this includes zero a), or an odd number of b.

(b) Consider the following DFA over the alphabet Σ = {a, b}.

i. Label the transitions of the following NFA so that it accepts the same language as the DFA. Your NFA should transform to the given DFA by applying the NFA-to-DFA conversion algorithm given in lecture.

ii. Write the regular expression for this language. Among the several possible answers, write the one that would transform to this NFA by applying the regular expression-to-NFA conversion algorithm given in lecture.

3 LR Parsing and Ambiguity (50 points)

Consider the following grammar over the terminals +, − (the negation operator) and id.

S → E E → E + E | − E | id

Below is a partial DFA for this grammar.

(a) Complete the above DFA. You need to do the following:

  • Complete state 0 by performing closure on the item listed.
  • Fill in all elements of states 1 and 5, and the lookahead items in states 3, 4 and 6.
  • Fill in the missing transition labels on all edges.
  • Write the necessary “reduce by ...” labels on states.

(b) For each state with a conflict, list the state, the lookahead token, and the type of conflict (i.e. shift-reduce conflict, or reduce-reduce conflict).

(c) Draw all the parse trees for the string id + − id + id.

(d) Is this grammar ambiguous? Why or why not?

4 Grammars (15 points)

Consider two LL(1) grammars, each with a single non-terminal, and without  productions. The non-terminal for the first grammar is A and for the second one is B. Consider a third grammar obtained by merging the productions of the two grammars and adding the production S → A | B. Answer the following questions, and explain your answers.

(a) Is it possible to derive the empty string from S in the third grammar?

(b) Write the necessary and sufficient condition(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 condition that applies to all grammars of the form mentioned above.

(c) Write the necessary and sufficient condition(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 condition that applies to all grammars of the form mentioned above.