Docsity

Midterm I Solutions for Programming Languages and Compilers | COMPSCI 164, Exams of Computer Science

Toggle sidebar
bg1
Midterm I Solutions
CS164, Spring 2006
February 23, 2006
•Please read all instructions (including these) carefully.
•Write your name, login, SID, and circle the section time.
•There are 8 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: Fri 10:00 Fri 11:00 Fri 2:00
Problem Max points Points
1 6
2 30
3 20
4 44
TOTAL 100
1
pf3
pf4
pf5
pf8

Partial preview of the text

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

Midterm I Solutions

CS164, Spring 2006

February 23, 2006

  • Please read all instructions (including these) carefully.
  • Write your name, login, SID, and circle the section time.
  • There are 8 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: Fri 10:00 Fri 11:00 Fri 2:

Problem Max points Points 1 6 2 30 3 20 4 44 TOTAL 100

1 Miscellaneous (6 points)

(a) Fill the missing right-hand side for the production of A in the grammar below such that the rules form a LL(1) grammar. S → A a A | A b A

A → Solution: A → 

Consider the following grammar:

S → a S | b S | a b a A

A → ε | a A | b A

(b) Is the above grammar LL(1)? Why or why not? Solution: It is not LL(1) because on non-terminal S and lookahead a there are two produc- tions possible (a S and a b a A).

(c) Show an NFA that accepts the language of this grammar. Solution: The solution is shown below. You can solve this problem without figuring out the language before: each non-terminal corresponds to an NFA state,  transitions mark the accepting states, each production corresponds to a transition. This works for all grammars whose productions have at most one non-terminal on the right-hand-side, occurring at the end.

a S

a, b

A

b a

a, b

In the following questions the alphabet is { 0 , 1 }. Write TRUE if the language is regular and FALSE otherwise. Explain your answers in one or two sentences.

(c) The language of strings with no occurrences of the substring “010”. Solution: TRUE. The complement of a regular language is regular. Take the DFA that recognizes the 010 substring and swap rejecting and accepting states.

(d) The language of strings with more occurrences of 0 than 1.

Solution: FALSE. This would require counting up for a 0 and down for a 1. Unbounded counting cannot be done with finite automata.

(e) The language of strings whose length is a multiple of 3. Solution: TRUE. Counting modulo N requires only N states.

(f) [Harder] The language of strings whose number of occurrences of 0 minus number of occur- rences of 1 is a multiple of 3. Solution: TRUE. This is also a counting modulo 3 problem, with counting up for a 0 and down for a 1.

(g) [Harder] The language of strings of terminals and non-terminals that can appear on the parsing stack during an LR(1) parse (stack is read from the bottom of the stack). Here the alphabet is the set of terminals and non-terminals of the grammar being parsed. Solution: TRUE. After all we use a DFA to process the contents of the stack and decide whether to shift or to reduce. Take than DFA and make every state an accepting state.

3 LL Parsing (20 points)

Consider the following grammar with terminals (, [, ), and ].

S → T S | [S]S | )S |  T → (X) X → T X | [X]X | 

(a) [Long] Fill in the table with the First and Follow sets for the non-terminals.

First Follow S ( [ )  $ ]

T ( ( [ ) ] $

X ( [  ) ]

(b) [Depends on (a)] Fill in the LL(1) parsing table.

( [ ) ] $

S T S [ S ] S ) S  

T ( X )

X T X [ X ] X  

(c) [Depends on (b)] Is this grammar LL(1)? Solution: Yes, because every cell in the table above has at most one entry.

S   E , $

E   int , $/if/+

E   E if E , $/if/+

E   E + E ,$/if/+

E  int  , $/if/+

reduce by E  E if E on $/if/+ reduce by^ E^ ^ E^ +^ E^ on^ $/if/+

int int

reduce by E  int on $/if/+

ACCEPT on $

0 int

1

2

3

4

(^5 )

S  E  , $

E  E  if E , $/if/+

if (^) E  E E , $/if/+

E (^) E

E

E  E if  E , $/if/+

E   int , $/if/+

E   E if E , $/if/+

E   EE , $/if/+

E  E E , $/if/+

E   int , $/if/+

E   E if E , $/if/+

E   EE , $/if/+

E  E if E  , $/if/+

E  E  if E , $/if/+

E  E E , $/if/+

E  EE  , $/if/+

E  E  if E , $/if/+

E  E E , $/if/+

if if + +

(d) [Does not depend on (b)] Assume that we disambiguate the grammar by saying that + is left-associative and binds more tightly than if, and if is right associative. Show the correct parse tree for the string int + int if int + int if int Solution:

S

E

E

E (^) E

int int

if (^) E

E (^) if E

E + E

int int

int

(e) [Depends on (b)] For each of the conflicts that you have identified above, show how it must be resolved to achieve the disambiguation described above. For a shift/reduce conflict say whether we should shift or reduce. For a reduce/reduce conflict say which reduction we should choose. Solution: State 5: shift on “if”, shift on “+”. State 6: reduce on “if”, reduce on “+”.