Docsity

Simplest DFA - Programming Languages and Compilers - Exams, Exams of Programming Languages

Toggle sidebar
bg1
UNIVERSITY OF CALIFORNIA
Department of Electrical Engineering
and Computer Sciences
Computer Science Division
CS 164 P.N. Hilfinger
Spring 2005
CS 164: Midterm
Name: Login: .
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
spaces 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 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 11 pages.
1. /8
2. /10
3. /
4. /9
5. /8
TOT /35
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Simplest DFA - Programming Languages and Compilers - Exams and more Exams Programming Languages in PDF only on Docsity!

UNIVERSITY OF CALIFORNIA

Department of Electrical Engineering and Computer Sciences Computer Science Division

CS 164 P.N. Hilfinger Spring 2005

CS 164: Midterm

Name: Login:.

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 spaces 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 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 11 pages.

  1. /
  2. /
  3. /
  4. /
  5. / TOT /
  1. [8 points] Suppose that string literals consist of one or more “stringlets” separated from each other by whitespace (one or more characters, each one of which is either a blank or a newline). Each stringlet consists of any sequence of characters (including newlines) between quotes (“), but with any occurrence of a quotation mark inside the string doubled. For example, “” and “””” are valid stringlets – the first denoting the empty string and the second denoting one quotation mark. Thus the following are valid string literals:
  2. “Hello, “ “world.”
  3. “I said, “”Hi”””
  4. “These are two lines”
  5. “These are “ “two” “ “ “lines”

a. Give a regular expression for these literals, using Lex notation (including definitions, if you want).

b. Produce the simplest DFA (yes, it must be deterministic) you can for these literals.

c. Consider the following Java (not C) program fragment.

class C { int g (int y, int z) { If (y > 0) { int r = y - z; return f (r – 1); } else { int q = z * y; return x + q; //<<< } }

int x = 2;

int f (int x) { return g ( x+1,2); }

String h (sting x) { return x + g (Integer.parseInst (x), 1); } }

what would the compiler’s symbol table look like while it is processing the body of g at the <<< comment?

d. For the same program as in part (c), would the behavior of the class change at all if Java were dynamically scoped? If so, then give an example (i.e. of a method call on an instance of C) that would exhibit the difference. Otherwise explain why there is no difference.

e. For the same program as in part (c), and again assuming normal Java scoping rules, would the behavior change if Java were dynamically typed? Why?

a. Using Bison notation, fill in the actions above to compute the right answer (that is so that the semantic value assigned to the start node is the desired number). Pretend that types have been declared for all the symbols: ID has type String and everything else is an integer. If there are nonterminals that don’t need values, don’t bother to assign them. Your actions must have no side effects (no global variables in particular).

b. 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 numbers right. Assume the following functions are available for your use:

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

‘=’ ID NUM FOR DO OD ‘;’ ‘-|’

name() returns the name (a String) of the next token whenever next () == ID

value() returns the integer value of the next token whenever next() == NUM

scan(T) checks that next () is T and reports and 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 local variables only. Do not introduce any new types.

Here is the parser skeleton. Remember no global variables; assign only to local variables; do not introduce new types. If needed, however you can introduce more functions (do mention somewhere what they parse).

void start() { printf ( “Number of assignments to x in program = %d\n”, prog());

}

int prog(){

int stmt () {

int _finally () {

Continued on the next page

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

@ # i -|

(the -| symbol is an end-of-file). A shift-reduce parser for the language processes the string

i # i @ i -|

And performs the following actions

  1. Shift #
  2. Reduce 1 symbol (from the stack), producing (the non terminal symbol) P
  3. Shift i
  4. Reduce 2 symbols, producing E
  5. Shift #
  6. Reduce 0 symbols producing P
  7. Shift i
  8. Reduce 2 symbols producing E
  9. Shift @ Then shift i
  10. Reduce 3 symbols producing E
  11. Reduce 3 symbols producing E
  12. Shift -| accepting the string

(When we say “reduce n symbols producing X” above, we main “remove n symbols (terminals or non-terminals) from the stack, and then push the non- terminal X”). Now for the questions:

a. What is the parse tree for the given string?

b. Fill in the following shift-reduce table with a plausible set of entries for the given input. No, we are not interested in the LALR(1) table necessarily, just any table that would behave as indicated on the given input (how it would (mis)behave on some other input is completely irrelevant). The starting state is 0; otherwise, you are free to choose your own state numbers. For each rn (reduce) entry that you have, provide the appropriate production on the right. Feel free to leave rows blank if you don’t need them. Likewise, you don’t need to use all of the rn entries we’ve given you. Important: there is more than one correct answer to this question!

Action/Goto State @^ # i -| E P

r.

r.

r.

r.

r.

r.

r.

r.