CS164 Midterm Exam in Computer Science - University of California, Exams of Programming Languages

A midterm exam for the computer science 164 course offered by the university of california, berkeley's department of electrical engineering and computer sciences. The exam covers topics such as shift-reduce parsing, nfas, regular expressions, bnf grammars, and recursive descent parsing.

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shailaja_987c
shailaja_987c 🇮🇳

4.3

(34)

217 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIVERSITY OF CALIFORNIA
Department of Electrical Engineering
and Computer Sciences
Computer Science Division
P. N. Hilfinger
CS164
Fall 1997
CS 164: Midterm
Name: _______________________________________________ Login: _______
Please do not discuss the contents of this test with anyone before the evening of Friday, 10 October.
You have two hours to complete this test. Please put your login on each sheet, as indicated, in case pages get separated. Answer
all questions in the space provided on the exam paper. Show all work (but be sure to indicate your answers clearly.) The exam is
worth a total of 35+ points (out of the total of 200), distributed as indicated on the individual questions.
You may use any notes, books, or computers you please--anything inanimate. We suggest that you read all questions before
trying to answer any of them and work first on those about which you feel most confident.
You should have 5 problems on 8 pages.
1. __________/9
2. __________/9
3. __________/
4. __________/9
5. __________/8
TOT. __________/35
Login:
1. [9 points] A certain language has the following terminal symbols:
@ * # i /
1
pf3
pf4
pf5

Partial preview of the text

Download CS164 Midterm Exam in Computer Science - University of California and more Exams Programming Languages in PDF only on Docsity!

UNIVERSITY OF CALIFORNIA

Department of Electrical Engineering and Computer Sciences Computer Science Division

P. N. Hilfinger CS Fall 1997

CS 164: Midterm

Name: _______________________________________________ Login: _______

Please do not discuss the contents of this test with anyone before the evening of Friday, 10 October.

all questions in the space provided on the exam paper. Show all work (but be sure to indicate your answers clearly.) The exam is^ You have two hours to complete this test. Please put your login on each sheet, as indicated, in case pages get separated. Answer worth a total of 35+ points (out of the total of 200), distributed as indicated on the individual questions.You may use any notes, books, or computers you please--anything inanimate. We suggest that you read all questions before trying to answer any of them and work first on those about which you feel most confident. You should have 5 problems on 8 pages.

  1. __________/
  2. __________/
  3. __________/
  4. __________/
  5. __________/ TOT. __________/

Login:

1. [9 points] A certain language has the following terminal symbols:

@ * # i /

A shift-reduce parser for this language processes the string

i * i # @ i /

and performs the following actions:

1. Shift i.

2. Reduce 0 symbols (on the stack), producing (the non-terminal symbol) N. Then reduce 2 symbols, producing T. Then

reduce 1 symbol, producing F.

3. Shift *. Then shift i. Then shift #.

4. Reduce 1 symbol, producing N. Then reduce 2 symbols, producing T.

5. Shift @. Then shift i.

6. Reduce 0 symbols, producing N. Then reduce 2 symbols, producing T. Then reduce 1 symbol, producing F. Then

reduce 5 symbols, producing F.

7. Shift /.

8. Reduce 2 symbols, producing E and accepting the input.

Now for the questions:

a. As far as one can tell from this sample, what is the grammar for the language being parsed?

(Continued on next page)

Login:

b. What is the parse tree for this string?

Login:

2. [9 points] Consider the following NFA:

operations in any expression ( expr ) of a given program. For example, inI am interested in finding the maximum number of ' + '

Fill in the recursive-descent compiler the answer should be 2 (this assumes that on the next page so that the input program gets checked for syntactic correctness and^ ID^ denotes 1-character identifiers.)

the right number gets printed. Be careful: the grammar is not LL(1); you can change it as needed, just so long as you end up recognizing the same language and get the right numbers. Assume that the following functions are available for your use:

next() one of the values returns the syntactic category of the next (as yet unprocessed) symbol of the input:

advances to the next token.^ scan(T)^ checks that^ next()^ is^ T , and reports an error if not. It then

ERROR() reports an error.

Write your program on the next page without using global variables. All assignments should be to local variables only. Do not introduce any new types.

Login:

Here is the parser skeleton. Remember: no global variables; assign only to local variables; do not introduce new types.

void start() { printf ("Maximum operators in any expression = %d\n", _________); } _____ pr

Login:

5. [8 points] In the following, do not worry about syntax trees or semantic actions; just consider the language being recognized.

Symbols in single quotes or in all-upper-case are terminal symbols. It shouldanswer any of these questions! not be necessary to build LALR(1) machines to

a. I happened to have an LL(1) parser generator and put the following grammar through it:

The parser generator told me the grammar is not LL(1). Why not?

b. In the language Alphard, x.y could be written y(x). Assuming we want a LALR(1) parser, why do we get a

reduce/reduce conflict on the '(Warning: is it ambiguous?) ) ' token in the following grammar? Give an example of an input that runs into the error.

Login:

c. In the following grammar, there is a shift/reduce conflict on ' ( '. Bison says that the problem is in the following state:

Identify the bug in the grammar. Give an example of an input that runs into the error (that is, that requires making a shift/reduce decision for a state and symbol that have a conflict). Propose a simple change to the grammar (and the language it describes) to fix the problem (that is, make an informed guess as to what was really intended).