Docsity

Problems with Solutions - Programming Languages | CSCI 4430, Exams of Programming Languages

Toggle sidebar
bg1
Practice
Problems
I.
Regular
Expressions.
Problem
1.
Consider
the
following
regular
expression
divided
into
parts:
——1——
——2.1—— ——2.2—— ——2.3—— ——2.4——
(ualbb)’
((abiba)
(aaibb)*
(ablba)
(aabb))’
For
each
string
below
either
write
that
it
is
generated
by
the
regular
expression
or
that
it
is
not
generated
by
it:
£
),
2.3
2.;
•
(i)(€jJ(3€
•
(ii)
b
b
b
aaa
b
a
b
a
EJo
•Qii)aaab
io
•
(lv)
b
baa
baa
b
b
Wa
•
(v)(C’3fj)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
the
shortest
sequence
of
terminals
possible,
show
that
the
grammar
is
ambiguous.
cc;
Ct.
B
B
L—a
•
(ii
t
is
the
precedence of
#
over
5?
Choose one
(a)
#
has
higher precedence
than
(b)
#
as
equal
precedence with
S.
and
(c)
#
has
lower
precedence
than
S.
•
(lii)
Giveevidencetosupportyouranswertopart.
Ccc’c{r
OCt
34ccvLpcortcJ
QSQtiftt)U
Q4
vet,(
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Problems with Solutions - Programming Languages | CSCI 4430 and more Exams Programming Languages in PDF only on Docsity!

Practice

Problems

I.

Regular

Expressions.

Problem

Consider

the

following

regular

expression

divided^

into

parts:

(ualbb)’

((abiba)

(aaibb)*

(ablba)

(aabb))’

For

each

string

below

either

write

that^

it

is

(^) generated

by

the

regular expression

or^

that

it^

is

not

generated

by

it:

£

),

(^) 2.;

  • (^) (ii) (i)(€jJ(3€•

b

b b

a a a

b

a b

a

EJo

•Qii)aaab

(^) io

(lv)

b

baa

baa^

b

b

Wa

(v)(C’3fj)faa

H.

Context-free grammars,

ambiguity^

and

precedence.

Problem

Consider

the

following

grammar

with^

two

operators.

#^

and

S.^

Capital

letters

denote nonterminals,

and

small

letters

denote^

terminals.

E B::=B#EIBSEIE

B

Ia^

b

(i)

This grammar

is

ambiguous.

U

the

shortest

sequence

of

terminals^

possible,

show

that

the

grammar

is^

ambiguous.

cc;

Ct.

B

B

L—a

(ii

t is

the

precedence of

#^

over

Choose one

(a)

#^

has

higher precedence

than^

(b)

#^

as

equal

precedence with

S.

and

(c)

#^

has

lower

precedence

than

S.^

(lii)

Giveevidencetosupportyouranswertopart.

Ccc’c{r^

OCt

34ccvLpcortcJ

QSQtiftt)U

Q

vet,(

1

(iv)

Soppose

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

to rules (^) possible

r. r

c o

U

) )

-‘ t- L

III.
LL

and LR Grammars.

Problerr.i

Consider

the

zfthe

(^) ri-else

grammar

below.

S

and E

denote

nonterminals

and

z.

hand (^) a

denote terminals:

S

::= iLtSeS

iEtS

(

(i)•^ Rewrite

this grammar

to eliminate

(^) the common

(^) prefix (^) in the first two productions

for

S

j

(

ci /

UL,

c’)

$,r I^

Km -

C

(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: iZ

JEizJz

ZiL

(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.

jC

.

.

.

L

r

L

  • (^) (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.

;‘eL

!——---t


L f’tU

e

‘C’

L-’—’. 4

::-

c

a ‘‘‘l

---t

r tThj

(ii)

For

each

state

with

a

conflict

list

the

state.

the

lookahead

conflict

(^) (i.e..

shift-reduce,

or

reduce-reduce).

r,

ub

:—..;(j,J

.‘

1 % Ii, t a

it3c’C

a

1

__-—

r “‘‘

,

‘b__i

1 zzL:;

—; !

ah

a

%r

token fr

awl

the^

type

of

F

a;;

L

F L.

I

I.

SV

I” S

I:

,,‘t 1 Ht

itCtstC

(iii)

Draw

the

parse

tree(s)

for

this

expression:

U

—Id

+^

Id

, r

a

, .

I’

N

; :

C.

.

1:

C-

5

(iv)

ts

this

grammar

ambiguous?

Why

or

why

not?

IA-t_

(^) Icl+U

l’a

  • 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.

I

I

‘IS

J

;J.

  • (^) (vi)

Describe

in

English

the

precedence

and

associativity

needed

to

ensure

property

P.

e ko-s

ts”’

r

(^) f4Rk

4

4- i

(vii)

For

each conflict

in

the

CFSM,

explain

how

the^

conflict

should

be^

resolved

to

(^) ensure

property

P

(as

a

shift

or

as

a

reckiareduce).

ci,.

-i,

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

avariable bound

(^) to an integer

value, else (^) fails. The (^) built-in

predicate

var() (^) returns

(^) true

when (^) its argument

(^) is an unbound

(^) variable,

else (^) fails.

f([ ],o).

f([jY],L)

integer(L),

L

0, M is L-1,

f(Y,M),

L

is M÷1.

f([Y),L)

var(L),

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- sentences)

(^) what the (^) fpredicate

(^) calculates.

Practice

(^) Problems

(part 2>

V.

Scoping

P1.

The interesting

thing about this language

isthat 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 isthe output ofthis (^) program?

procedure main

main

0

icaI-liixx

{

integer

a,

b,

c

:= 0;

// all (^) initially

0

dynamic-link

xxxx

procedure

p()

{

integer

a

:=

print

a,

b,

C;

a++;

b+÷;

if (b mod 2 =

then

QO;

else

p () ;

procedure

print

“#2”,

a, b, c;

lexical-link

}- - end p;

dynamic-link

procedure

Q()

{

integer

b

:= 0;

a++;

b++;

C++;

print

“#3”,

a, (^) b,

c;/****/

}- -end

Q;

lexical-linkprocedure

n

print

“#4”,

a, b, C;

dynamic-link

}--end

main

(ii) Show (^) the (^) run-time

stack when execution^

reaches

/***/ using (^) the (^) frames

at the right. (^) You may not need to use all the (^) frames.

procedure

U\

dynamic-linklexical-link

I

)

lexical-linkprocedure

lv

dynamic-link