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    /

A shift-reduce parser for this language processes the string

        i    *    i    #    @    i    /

and performs the following actions:

Now for the questions:

Login:
   
 
 
 
 



Login:

2.    [9 points] Consider the following NFA:



Login:


 

4.    [9 points] Consider the following grammar:

I am interested in finding the maximum number of '+' operations in any expression (expr) of a given program.  For example, in the answer should be 2 (this assumes that ID denotes 1-character identifiers.)
    Fill in the recursive-descent compiler on the next page so that the input program gets checked for syntactic correctness and 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()  returns the syntactic category of the next (as yet unprocessed) symbol of the input:
        one of the values

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

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", _________);

}



_____ prog () {





}



_____ stmt () {







}



_____ assign () {





}



_____ expr () {







}


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 should not be necessary to build LALR(1) machines to answer any of these questions!

 
 
 
 
 



Login: