Docsity

CS 164: Exam 1 - Computer Science University of California, 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 2010
CS 164: Test #1
Name: Login:
You have one hour and twenty minutes 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 20+ points (out of the total of 200), distributed as indicated on the individual
questions.
You may use any notes or books you please—anything unresponsive. 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 8 problems on 8 pages.
1. /2
2. /1
3. /1
4. /3
5. /
6. /5
7. /4
8. /4
TOT /20
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download CS 164: Exam 1 - 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

CS 164 P. N. Hilfinger Spring 2010

CS 164: Test #

Name: Login:

You have one hour and twenty minutes 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 20+ points (out of the total of 200), distributed as indicated on the individual questions. You may use any notes or books you please—anything unresponsive. 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 8 problems on 8 pages.

TOT /

  1. [2 points] Consider the following very simple grammar:

p : s ⊣ s : /* Empty */ s : ’(’ s ’)’

Suppose that we have a shift-reduce parser for this grammar. Write a regular expression that describes the possible contents of the parsing stack just after a shift or reduce.

  1. [1 point] I have a language implementation that involves a Flex lexer. A new revision of the language standard requires me to double the number of different kinds of token returned by the lexer. What effect will this have on the lexer’s speed and why? (Assume that both new and old lexer action rules (the things in curly braces) all take about the same time to execute.)
  2. [1 point] From a given grammar, I am able to produce two different derivations of a certain string, S. Under what circumstances would this indicate that the grammar is ambiguous?
  1. [1 point] What is the name of the dealer you’d go to for ... answers oracular, Bogies spectacular, Tetrapods tragical, Mirrors so magical, Facts astronomical, Solemn or comical,...?
  2. [5 points] The following grammar describes the structure of a tree-like specification language:

spec : block ⊣

block : TAG ID attrs kids

attrs : /* EMPTY */ | attr attrs

kids : /* EMPTY */ | BEGIN blocklist

blocklist : END | block blocklist

attr : ID ’=’ NUM

Upper-case names and quoted symbols denote terminal symbols (returned by the lexer). Assume the following are already implemented:

The syntactic category of the next token (ID, BEGIN, END, ’=’, etc.).

def peek(): ...

If token is not None, check that peek() == token. Then discard the

current value of peek() and replace it with the next token in the input.

Indicate an error if the check fails.

def scan(token = None): ...

Report error

def ERROR(): ...

On the next page, complete a recursive-descent parser for this language. Do recognition only; don’t worry about semantic values.

def spec():

Complete the implementation here.

def block():

def attrs():

def kids():

def blocklist():

def attr():

b. Show as much as you can of the grammar corresponding to this derivation.

c. Is the grammar ambiguous? Why or why not?

d. Starting with p, show the first five derivation steps (the first five ‘⇒’ steps) in the leftmost derivation that corresponds to the same parse tree.

  1. [4 points] A very primitive text formatter for mathematics allows one to write things like “x^2+y_{i+1}^{3}-c_{q_{j}}” and get “x^2 + y^3 i+1 − cqj ”. The grammar below gives the syntax for the input language:

eqn : text { output( ) }

text : /EMPTY/ { }

| text item { }

item : GLYPH sub0 sup0 { }

sub0 : /EMPTY/ { }

| ’_’ ’{’ text ’}’ { }

sup0 : /EMPTY/ { }

| ’^’ ’{’ text ’}’ { }

The terminal symbols are GLYPH, ‘{’, ‘}’, ‘_’, and ‘^’. The object that you output is a box, a container of printable text, which is defined for you in the form of two functions:

box() returns an empty box.

add(B 1 , B 2 , x, y), where B 1 and B 2 are boxes and x and y are numbers, returns a new box containing B 1 ’s text, with B 2 ’s text positioned so that its lower-left corner is at coordinates (x, y) relative to B 1 ’s lower-left corner. Thus if B 1 contains the text “hello ” and B 2 contains the text “world”, then add(B 1 , B 2 , 6 , 1 ) would output as hello world.

For each GLYPH The lexical analyzer returns a box containing text of width 1. Superscripts and subscripts are one unit above and below the text they are attached to, respectively. We will not bother with decreasing the font size of subscripts and superscripts for this problem. Fill in the blanks above to achieve the desired effect. Your semantic actions must not have side effects, except to assign to $$. You may, however, use any Python values and data structures you’d like for semantic values (numbers, boxes, dictionaries, lists, etc.).