Practice Problems: - Qii) Aaab Io
Practice Problems: - Qii) Aaab Io
I. Regular Expressions. Problem 1. Consider the following regular expression divided into parts: 1
(ualbb)
2.3
For each string below either write that it is generated by the regular expression or that it is not generated by it: 2.; ),
(i)(jJ(3
(ii) b b b a a a b a b a
EJo
Qii)aaab io
(lv) b baa baa b b
Wa
(v)(C3fj)faa H. Context-free grammars, ambiguity and precedence. Problem 1. Consider the following grammar with two operators. # and S. Capital letters denote nonterminals, and small letters denote terminals. B::=B#EIBSEIE E ::= B a I b (i) This grammar is ambiguous. U that the grammar is ambiguous. the shortest sequence of terminals possible, show cc; Ct.
B
(ii (b)
t
is the precedence of # over 5? Choose one (a) # has higher precedence than as equal precedence with S. and (c) # has lower precedence than S.
La
(lii) Giveevidencetosupportyouranswertopart.
34ccvLpcortcJ QSQtiftt)U
1
Q4
Cccc{r vet,(
OCt
(iv)
S oppose that we wanted to add a unary operator % with lower precedence than #. Add parentheses to the following expression to show how it would be evaluated giver these precedences
7a#b)
(v) Modify the original grammar so that it is not ambiguous making the fewest changes r. to rules possible o) c r ) U
-
t- L
Problerr.i 1. Consider the zfthe ri-else grammar below. S and E denote nonterminals and z. 1. h and a denote terminals:
S
::=
iLtSeS
iEtS
(1
the first
two productions
j
/
-
(
UL,
c)
$,r I
ci
Km
(ii) Construct the LL(1) parsing table for the grammar from (i). Show your FIRST and FOLLOW sets for relevant sequences of symbols. r---.
L
.-
t.
-r----
c:
(iii) This grammar is not LL(i). Is it possible to resolve the conflict(s) in such a way that the LL(1) parser will parse Jthen-else statements normally (i.e., an else will he associated with the nearest then)? Justify your answer.
jC1
.
iZ JEizJz ZiL1
.1
.
(ii) Construct the CFSM for the original grammar. Make sure that von show each state. fill in all items in the state, label all transitions, and write the necessary Reduce by. labels on states.
-
(iii) This grammar is not SLR(1). For all states with a conflict, write the state, the type of conflict (e.g., shift-reduce, reduce-reduce conflict) and the lookahead token that triggers the conflict, Is it possible to resolve the conflicts in such a way that the LR parser will parse zf-then-eise expressions normally?
-
(j
c.
Problem 2. Consider the following grammar: S [S Y[ a Y + S Z Z b z I SY
-
..
S, Y and Z are noriterrninals and a, b, c, + , and ] are terminals. (i) Compute the FIRST and FOLLOW sets for S. Y and Z.
C. FItT
:
c
(,-
(
(7)
.
fILv:..
c
Problem 3. Consider the following grammar over terminals +. - and id: S-E EE+EI EJid (i) Construct the CFSM for this grammar. You need to (a) show all states, (b) show all items in a state, including the computation of closure, (c) show all transitions with labels, and (d) write all necessary reduce by... labels on states.
__-
____
1
-
;eL !---t
zzL:;
L ftU
; !
%r
b__i
Ii,
e
ah
fr
1)
4 L-. ::-
l a
r
---t
tThj
it3cC
(ii) For each state with a conflict list the state. the lookahead token awl the type of conflict (i.e.. shift-reduce, or reduce-reduce). :..;(j,J r, ub
.
I:
t H 1 ,,t
itCtstC
, ,
a
I
a;;
F
:
.
L
L.
C.
1:
I
V
I.
C-S
IA-t_ Icl+U
la
pcus wes.
Suppose that we want the string Id + Id + Id to have only one possible rightmost derivation (call this property P): S E E+E E+E+E E+E+id E-i-Id-i-Id -E+Id+Id * -Id+id+id (v) Draw the parse tree for this derivation.
IS
e
;J.
(vi) Describe in English the precedence and associativity needed to ensure property P.
ko-s
s t
4- i
f4Rk 4
(vii) For each conflict in the CFSM, explain how the conflict should be resolved to ensure property P (as a shift or as a reduce). ci,. -i, reckia
Tt
5 i i
EEEOpid Op + I *
-
Give an attribute grammar which translates postfix expressions into parenthesized infix ex / will be translated into (A/(A+ B)).
1(i
.Ji
r
1
4Ji
L S
frI
Li<i
s
ch-
fr I tfr pII.
.
vt..
c4
I +
c3
I-
b t
tO
ij
:-
_O_
-,
/
V
LL
r( L
kc
IA
IAI
VI. Prolog P1. Given the following Prolog clauses defining predicate f () answer the questions below. The built-in predicate integer() returns true when its argument is an integer or a variable bound to an integer value, else fails. The built-in predicate var() returns true when its argument is an unbound variable, else fails.
,
integer(L), L
var(L),
>
0,
M is L-1,
f(Y,M),
f(Y,M),
L is M+1.
Write what answer is returned by the following queries (notice we are looking for the first answer only): i. ?- f([1,2,3),x).
ii.?-
f([1,2,3],4).
iii.
?-
f(X,3).
iv. ?- f(X,Y).
v. Describe briefly (1-2 sentences) what the f predicate calculates.
V. Scoping P1. The interesting thing about this language is that procedures whose names are in upper-case use dynamic scoping to lookup non-local variables, whereas procedures whose names are in lower-case use lexical scoping to lookup non-local variables. You will trace the execution of this program and answer the following 2 questions: (i) What is the output of this program? procedure
main 0
:=
// all initially 0
:=
C;
if
mod (b
0)
then
QO;
procedure
} {
lexical-link dynamic-link
procedure Q()
integer b a++; b++;
:=
0;
C++;
}-
print #4, a, b,
}--end main
C;
lexical-link dynamic-link
(ii) Show the run-time stack when execution reaches at the right. You may not need to use all the frames.
/***/
U\
lexical-link dynamic-link
I
procedure
lv
lexical-link dynamic-link
Ce
I
4)).
p
V.)
4- I I
a
c
a
fl
.fl
C
It
I
:j
C
4,
dt
t.I
1
I
LiC
a
a
.S
4 j
I
t c
CLr.
ci
.1
IbIj .)
I:
1.
frs
It
I
V
,_
4.jt
C.
I
--.
WI
.4;
7
1
if
4ibet
_
)1 ti
t
I
C
(t
.fJ
F
C
II
I
4t C 1
/ P
II 41
JI
. jr
ii,
:
(C
tC e
c
ci a ,
ft
ji
k%j
ac e
t4