10 Problem Set 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CS143 Handout 10

Autumn 2007 October 10, 2007


Problem Set 1: Lexing and LL(1)

This week’s assignment consists of written exercises on regular expressions, formal


grammars, and top-down parsing. You are to write up solutions and hand them in on
paper.

Due: October 17th in class

Problem 1
a. Construct a DFA that accepts all base 3 numbers (consisting of digits 0, 1, and 2)
which are multiples of 4.
b. Construct a DFA that accepts all strings which are valid C comments, where */ may
appear within the body of the comment, provided it appears inside double quotes.
For example, /* ab "cd */" ef */ would be a valid comment. You may label
edges with flex character classes such as [^*/] and [^"]. (You needn’t be so careful
about escaping flex meta-characters.)

Problem 2
Convert the following NFA to a DFA. For each DFA state, indicate the set of NFA states
to which is corresponds. Make sure you shod the initial state and the final states in the
constructed DFA.
ε

start ε ε ε
1 2 3 4

x z y

ε ε
5 6 7

Problem 3
Consider the following grammar for regular expressions:

R→RR
R→R|R
R → R*
R → (R)
R → id
2

The start symbol is R. Note that R is the only nonterminal, and the set of terminals is

{ id, |, *, (, ) }.

a. Show that this grammar is ambiguous. Give an example to illustrate the


ambiguity.
b. How would the ambiguity cause problems when interpreting the parse trees?
c. Construct an LL(1) grammar for regular expressions that gives * higher
precedence than |.

Problem 4
Consider the following context-free grammar:

Nonterminal Set = {S, G, R, L, C}


Terminal Set = {n, s, c, y}
Production Set = { S → G, G → R, G → G R, R → n L s, L → C, L → L c C, C → ε, C → C y}

The start symbol is S.


a. Is this grammar ambiguous?
b. Explain why the grammar is not LL(1).
c. Transform the grammar into a LL(1) grammar that accepts the same language.
d. Develop the parse table for a top-down predictive parser for your LL(1)
grammar.
e. Trace through the parse for the input nycys.
f. Sketch the functions required for a recursive-descent parser for your LL(1)
grammar.
g. Is the language described by the grammar regular—that is, can it be generated by
a regular expression even though we’ve expressed it using a CFG?

Grading
The problem set is worth 40 points. You will be graded on the correctness and clarity of
your answers. You may use just one late day on this assignment.

Collaboration on problem sets


Although you can discuss ideas with others, you must submit your own independent
solution and properly credit anyone you worked with.

You might also like