
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Professor: Milanova; Class: PROGRAMMING LANGUAGES; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Fall 2008;
Typology: Exams
1 / 32
This page cannot be seen from the preview
Don't miss anything!

























CSCI
(^2008)
**
(^) is
CSZI
(^) Final
I
Fall
(^2008)
Consider the
following
grammar
over terminals
(,jj,
and
is the starting
(^) symbol
of the
grammar.
I
X-iT-(X)
a)
( 5pts)**
Fill in
the (^) table
below
with
the
FIRST
and
FOLLOW
sets
for the
nonterminals
in this
grammar:
S
C (L
)
cL’
—___
b)
( (^) points)**
Fill in the
LL(1)
parsing
(^) table
below:
H
x TT S
c)
( points
Is this
grammar
(^) Explain
(^) briefly
(^) why
(^) or (^) why
not
1 L r
c’
ck
d
e
CSCI
(^4430)
Final
(^) Exam
Fall
(^008)
Consider
the
foowing
grammar
over
terminals
+,
and
if E E
E
a (
pts) (^) This
grammar
is ambiguous.
Show
two
parse trees
for
E
/ç
EE
/
FLr
..J
b) (
pts)
Consider
(^) the
partial
(i.e.
CFSM)
for this
grammar:
Complete
state
by performing
(^) closure
on the
item
(^) listed.
Fill in the
elements
of states
and
Fill (^) in the missing labels
(^) of them).
Fill in the (^) “reduce
by
labels
on states
and
6 below:
Label on
state
“reduce
by E
, E
Eon
Label
(^) at (^) state
“reduce
by (^) E
if Eon
(^) Final
(^) Exam
Fall
(^2008)
a) (2Opts’
What
does
the program
below
print,
assuming
that
each argument/parameter
is passed
according
to the qualifier
on it, Assume
in by value-result
that
all parameter
addresses
are
calculated
only ON
to the
procedure.
means
{
mt
a[4);
mt
h,
k,
j;
function
p(int
x
: by
mt
y
: by
mt
z : by
value-result)
{ j :=
4;
x*2;
z :=
x*3;
printa[y]
“in
p “,
h,
j,
k,
a[O..3];
initialize
aErn]
to
(3-rn)
a[O]
a[lJ=2;
a[2]=l;
a[3]=O;
h :=
1;
j :=
2;
:=
“before
“,
h,
j,
k,
a[O..3];
call
p(a[j],
(j-l),
a[h));
(***)
“after
“,
h,
j,
k,
a[O..3);
Output:
0
‘
(^) Exam
Fall
(^2008)
b) NAME______________________ (^) (5pts
How
(^) would
(^) you
(^) change
(^) the
(^) call
(^) to (^) p (^) at (^) (***)
so^ (^) that
(^) after
(^) returning
(^) from
(^) that
call.^ (^) a[2] would
(^) have
(^) been
modified
to^ (^6)
call
p(
(D
tL.):
c)
(^) (2Opts)
(^) Complete
(^) the
(^) following
(^) program fragment
(^) in (^) the
(^) shortest
(^) possible
(^) way
(^) (fewest
(^) and
shortest^
statements)
(^) so (^) that
(^) the
(^) output
(^) will
be^ (^) different
(^) if (^) parameter
passing^
(^) was
(^) by (^) reference
(^) versus
(^) by (^) value-
result.
Indicate
(^) in (^) each
(^) case
(^) what
(^) the (^) output
(^) would be.
procedure
p(integer
x)
{
(A
),
main
()^
{
/*
begin
main
program
integer
= 10,
= 20;
p (^) (A);
(^) , (^) A, (^) “B=”
(^) , B) ;^
A3ci,
iC
Output
n case
(^) of (^) pass
(^) by-reference:
A
(^) Output
(^) in (^) case
(^) of (^) pass by-value-result:
b
2t
AG)
E:O
(^) 443()
(Ama!
Exam^
Fall
(^) OO
Assume
(^) you
(^) have
(^) a (^) symbol
(^) table
(^) whose
(^) keys
(^) are
(^) variable
(^) names
(^) and
(^) whose
(^) entries
(^) are
(^) symbolic
(^) values
that (^) correspond
(^) to (^) the
(^) type
(^) of (^) that
(^) variable,
(^) In (^) our
(^) Scheme
(^) project and
(^) Python
(^) examples
(^) we (^) have
(^) seen
(^) how
to (^) search
(^) such
(^) a (^) table
(^) to (^) find
(^) the
(^) value
(^) corresponding
(^) to (^) a given
(^) key.
Here,
(^) we (^) want
(^) to (^) do (^) the
(^) opposite,
namely
(^) we (^) want
(^) a (^) function
(^) get
vars
(^) that
finds
all (^) the
(^) variables
n the (^) table
(^) of (^) a (^) given
(^) type
(^) (i.e.
value).
For (^) example,
in
(^) we
(^) would
(^) store
(^) the table
(^) as (^) a (^) list
((x
int)
(y
real)
(z
list)
(w
int))
Here
(^) the (^) first
(^) element
(^) of (^) each
(^) sublist
(^) is (^) the
(^) key
(^) (the
(^) variable) and the
(^) second
(^) element
(^) is (^) the (^) value
(^) (or
variable
(^) type).
The
(^) call
(get
vars
‘mt
int)
(y
real)
(z
list)
(w
int)))will
(^) return
the (^) list
(x
w)
Recall
(^) several
(^) higher
(^) order
(^) functions
(^) in (^) Scheme
(^) that
(^) may
(^) be (^) useful:
(define
(foldl
g ys
u)
(if
(null?
ys)
u (foldi
g (cdr
ys)
(g
u (car
ys)))))
(define
(foldr
op
ys
val)
(if
(null?
ys)
val
(op
(car
ys)
(foldr
op
(cdr
ys)
val))))
(define
(map
f ys)
(if
(null?
ys)
I
(cons
(f
(car
ys))
(map
f (cdr
ys)))))
a. Wrte
(^) a Scheme
(^) function
(^) get
vars
(^) for (^) finding
(^) all (^) the
(^) variables
(^) of (^) a (^) given
(^) type,
(define
(get
vars
type
ilist)
(y
9CL’
CiX
(CiS
.ai
I
(^) f:jia
Exam^
FaIl
(^2008)
Now
write
a^
(^) function
to accomplish
the
same
task,
Note:
the
following
built-in
operations
in
Python
on dictionaries
may
be of use:
,
,
()
update
(dict)
, len
(dict)
, indexing
or the following
builtin
(^) operations
on Python
lists:
(concatenate),
append(ele),
del
list[k]
(element
deletion),
indexing
[.
I
Function
returns
list of variables
dict
of type
def
get
vars
(type,
dict)
k/Lrt4 ( vcrS
CSCl 4430
Fail