Docsity

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

Toggle sidebar
bg1
CS 164 Programming Languages and Compilers Handout 8
Midterm I
Please read all instructions (including these) carefully.
There are six questions on the exam, each worth between 15 and 30 points. You have 3 hours
to work on the exam.
The exam is closed book, but you may refer to your four sheets of prepared 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. There are no \tricky" problems on the
exam|each 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 complicated than necessary.
NAME: Sample solution
SID or SS#:
Problem Max points Points
1 15
2 15
3 20
4 20
5 30
6 30
TOTAL 130
Fall 95 page 1 of 11
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

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

Midterm I

 Please read all instructions (including these) carefully.

 There are six questions on the exam, each worth b etween 15 and 30 p oints. You have 3 hours to work on the exam.

 The exam is closed b o ok, but you may refer to your four sheets of prepared 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 pap er. Please do not use any additional scratch pap er.

 Solutions will b e graded on correctness and clarity. There are no \tricky" problems on the exam|each 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 complicated than necessary.

NAME: Sample solution

SID or SS#:

Problem Max p oints Points 1 15 2 15 3 20 4 20 5 30 6 30 TOTAL 130

  1. Regular Expressions (15 p oints) The following description of integer constants in C++ ap- p ears on page 480 of \The C++ Programming Language."

An integer constant consisting of a sequence of digits is taken to b e decimal (base ten) unless it b egins with 0 (digit zero). A sequence of digits starting with 0 is taken to b e an o ctal integer (base eight). The digits 8 and 9 are not o ctal digits. A sequence of digits preceded by 0x or 0X is taken to b e a hexadecimal integer (base sixteen). The hexadecimal digits include a or A through f or F : : :. The typ e of an integer dep ends on its : : : sux. [An integer may have no sux.] : : : If [the integer] is suxed by u or U, its typ e is : : :. If it is suxed by l or L, its typ e is : : :. [Either or b oth suxes may app ear at most once each in either order.]

Give a regular expression for C++ integer constants. Use only the op erations +, , and con- catenation in your expression (no sp ecial ex notation). You may also use parentheses and  (which are not op erations). For convenience, you may name a regular expression R by writing name = R.

[spaces are not significant]

octal = 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 nonzero = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 decimal = 0 + nonzero hex = decimal + a + A + b + B + c + C + d + D + e + E + f + F

optu = u + U + epsilon optl = l + L + epsilon suffix = (optu optl)+(optl optu)

number = (octal octal) + (nozero decimal) + ((0x + 0X) hex hex*) integer = number suffix

[Note: We also allowed (0x hex*)]

  1. Grammars (20 p oints)

Give a context-free grammar for each of the following languages.

(a) Any string of x's followed by an equal numb er of y's. 7 points

S -> xSy | epsilon

(b) Any string of x's of length n followed by a string of y's of length 2 n + 1, for any n  0. 7 points

S -> xSyy | y

(c) Any string of x's and y's with an equal numb er of x's and y's. The x's and y's may app ear in any order. 6 points

S -> xSyS | ySxS | epsilon

  1. Top-Down Parsing (20 p oints)

Consider the following grammar for a subset of English sentences. The nonterminals are S; NP; VP; AP. The terminals are the, noun, adjective, verb. The start symb ol is S.

S! NP VP j NP VP NP NP! the AP noun AP! AP adjective j  VP! verb

Give an LL(1) grammar that generates the same language as this grammar. Show the parsing table for the grammar you pro duce. a) Left factor

S! NP VP NPopt NPopt! NP j  NP! the AP noun AP! AP adjective j  VP! verb

b) Eliminate left recursion

S! NP VP NPopt NPopt! NP j  NP! the AP noun AP! adjective AP j  VP! verb

c) Construct table the noun verb adjective $ S S! NP VP NPopt NPopt NPopt! NP NPopt!  NP NP! the AP noun AP AP!  AP! adjective AP VP VP! verb

(a) Give the grammar used in constructing the DFA. 5 points

S -> id | string | S + S | id := S

(b) Show that the grammar is ambiguous.

5 points

The string id + id + id has two parse trees:

S

S

id

  • S

S + S

id id

id

S

S + S

id S + S

id

(c) Which of the following strings are viable pre xes? For each string that is a viable pre x, give a sux that pro duces a right sentential form. i. S 2 points Yes: suffix: $

ii. + S + 1 point No.

iii. S + id := 2 points Yes: suffix: id $

(d) Which states of the DFA have con icts and of what kind (shift-reduce or reduce-reduce)? Be sure to state clearly the cause of the con ict in each case. 5 points

State 7 has a shift-reduce conflict on input +.

State 8 has a shift-reduce conflict on input +.

(e) Which con icts should b e resolved in which way to give the := op erator higher precedence than +? 5 points

Reduce should be chosen in state 8.

(f ) Which con icts should b e resolved in which way to cause the + op erator to asso ciate to the right? 5 points

Shift should be chosen in state 7.

(S + S #)

(S #)

(S' #)

(c) Consider a new action insert t where t is a terminal. The meaning of the action is to insert t b efore the next input token (i.e., t b ecomes the next input). Give insert actions that implement each of the following lo cal recovery strategies. Be sure to say which entries in the action table have the insert actions. i. If the input contains an id followed by a string, insert an := b etween them.

action(2, string) = insert :=

ii. If the input contains two consecutive strings, insert a + b etween them. action(4, string) = insert +

iii. If the input contains a string followed by an id, insert a + b etween them. action(4, id) = insert +