



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




ID^ Num
b^ er^ : Name^ :
^ Please
ll^ in^ y
our^ ID^
numb^ er
and^ Name
in^ the^ space
provided
ab^ ove.
^ DO^ NOT
OPEN
THE^ EXAM
BOOKLET
UNTIL
INSTR
UCTED
TO^ DO
SO!
^ The^ exam
for^ undergraduate
studen
ts^ is^ di
erent^
from^ the
exam^ for
the^ graduate
studen
ts.
Make^ sure
you^ are
reading
the^ correct
question
pap^ er.
^ Read
all^ questions
carefully
at^ the^
b^ eginning
of^ the^
exam.^ Doubts
ab^ out^ the
questions
will^ b^ e
answered
only^ in
the^ rst
30 min
utes^ of^
the^ exam.
^ Write
your^ answ
ers^ in^ the
space^ pro
vided.^ Keep
answers
brief^ and
precise.
^ The^ exam
consists
of^7 questions,
in^11 pages
(including
this^ page)
for^ a^ total
of^100
p^ oints.
Question
Max.
Points
Poin
ts^ Scored
Total^
oints]^ HTML
A and LT X^ are^ tE wo^ do^ cumen
t^ formatting
languages.
We^ wan
t^ to^ build
a^ translator
to^ convert
do^ cumen
ts^ structured
using^ HTML
A to LTE X.^ In^ HTML,
the^ text
of^ the^
do^ cumen
t^ to
b^ e^ formatted
is^ sp^ eci
ed^ b^ et
ween^
and^
A; in LT X,^ theE text^ is
sp^ eci^ ed
b^ etween
\begin{document}
and^ \end{document}
.^ In^ HTML,
a^ list^ of
unnum
b^ ered^ bullet
items^ is
given
using^
and^
A; in LTE X,^ the^ list
is^ sp^ eci
ed^ using
\begin{itemize}
and^ \end{itemize}
.^ In
a^ list^ of^
numb^ ered
bullet^ items
is^ given
using^
and^
A; in LTE X,^ the^ list
is^ sp^ eci
ed
using^ \begin{enumerate}
and^ \end{enumerate}
.^ Each^
item^ in^
the^ list^
b^ egins^ with
with^
in
and^ \item
A in LTX.E Font^ changing
commands
in^ HTML
are^ sp^ eci
ed^ as^ follo
ws:^ the^
text^ to^
b^ e^ displa
yed^ in^ b
old^ face
italics,^ or
teletype
are^ mark
ed^ with
,^ and^
,^ resp^ ectiv
ely,^ in^ the
b^ eginning,
and^
,^ and
resp^ ectiv
ely,^ in^ the
end.^ In
the^ text
with^ changed
font^ b^ egins
with^ {\bf
{\it^ and
{\tt^ resp
ectively,
and^ the
change^
of^ font^ p
ersists^ un
til^ the^ matc
hing^ }^ is
found.
A^ sample
translation
of^ a^ HTML
do^ cumen
At to LTE X^ is^ sho
wn^ b^ elo
w:
HTML:^
ALTX:^ E
Output^
(Formatted)
Text
This^ is^
a^ sample
document. A^ list^ of
bullet^
items can^ be^ created
as^ follows: ^ Start
an^ itemizeenvironment; ^ Place
each^ item
with an^ itemcommand.
\begin{document}This^ is^
a^ sample
document. A^ list^ of
bullet^
items can^ be^ created
as^ follows: \begin{itemize}\item^
Start^ an
{\bf^ itemize} environment;\item Place^ each
item^ with an^ {\em^
item}^ command. \end{itemize}\end{document}
This^ is^ a^ sample
do^ cumen
t. A^ list^ of^ bullet
items^ can
b^ e created^ as
follows. Start^ an^ itemize environment; Place^ each
item with^ an^
item^ com- mand.
a.^ (7^ p
oints)^
List^ the
smallest
set^ of^ to
ols^ you^
will^ use
to^ construct
a^ translator
to^ conv
ert^ input
do^ cumen
ts^ into^ L
ATX^ doE
cuments.
Explain
your^ choice.
You^ ma
y^ assume
that^ all
input^ do
cuments
are^ well
formed
do^ cumen
ts:^ i.e.,^
they^ are
correct^ with
resp^ ect
to^ HTML
syntax.
b.^ (3^ p
oints)^
If^ the^ input
do^ cumen
ts^ may^ not
all^ b^ e^ w
ell^ formed
do^ cumen
ts,^ will^ the
to^ ols
you^ chose
in^ part^
[a]^ ab^ ov
e^ b^ e^ still
sucien
t^ to^ construct
a^ translator?
Explain.
2
Docsity.com
10 p^ oin
ts]^ BigSoft
Inc.,^ the
world's
largest^
supplier
of^ useless
software,
announces
a^ new
pro^ duct
called^ antitrust
.^ The^ program
antitrust
converts
do^ cumen
ts^ (whic
h^ are^ plain
les)^ as
follows:^
each^ input
sentence
is^ copied
to^ the^ output
le,^ exc
ept^ those
sentenc
es^ that^ c
ontain^ the
word
competition
BigSoft^
claims^ that
antitrust
is^ written
completely
using^ Lex,
with^ no
C^ co^ de
other^ than
printf
's.
Is^ this^ p
ossible?
If^ so,^ sk
etch^ the
Lex^ sp^ eci
cation;
if^ not,^ explain
why^ not. 3
15 p^ oin
ts]^ The^
\normal"
way^ to
write^ arithmetic
expressions
is^ called
in^ x^ form,
since
op^ erators
are^ sandwic
hed^ b^ etw
een^ the^
op^ erands.
For^ example,
in^1 +^2
op^ erates
on^2 and
and^ `+'^
works^ on
1 and^ the
result^ of
the^ pro
duct.
The^ same
expression
can^ b^ e^ written
in^ post^
x^ form,^
as^1 2
^ +:^ the
op^ erands
precede
the^ op^ erators.
For^ instance
the^ in^
x^ expression
^6 will^
b^ e^ written
in^ p^ ost
x^ as^2
a.^ (2^ p
oints)^
Write^ the
in^ x^ expression
6))^ +^7 in
p^ ost^ x^
form.
b.^ (6^ p
oints)^
Can^ every
in^ x^ expression
b^ e^ conv
erted^ to
p^ ost^ x?
Justify.
d.^ (7^ p
oints)^
Let^ Lb^3
e^ a^ language
of^ p^ ost
x^ expressions
over^ integer
constan
ts,^ with
`'^ and
as^ binary
op^ erators
and^ ` '
as^ an^ unary
op^ erator.
Write^ a^
grammar
that^ describ
es^ Lprecisely^3
Docsity.com
14 p^ oin
ts]^ Consider
the^ follo
wing^ grammar,
G,^ that^5
de^ nes^
expressions
over^ integers
and
a^ sp^ ecial
identi^ er
x:
E^ !^
E^ +^ T E^ !^
T T^ !^
T^ *^ F T^ !^
F F^ !^
(^ E^ ) F^ !^
int F^ !^
x
The^ expressions
generated
by^ Gcan^5
b^ e^ view
ed^ as^ p^ olynomials
of^ x.
Consider
the^ problem
of^ di^ eren
tiating^ the
p^ olynomials
with^ resp
ect^ to^ x.
For^ instance,
for^ an^ input
expression
x^ the^ translation
should^
yield^1.^
The^ output
need^ not
b^ e^ simpli
ed.^ For
example,
x*x
may^ b^ e^
translated
into^ x*
+^ x*1.
Let^ dx^ b
e^ an^ attribute
of^ nonterminal
symb^ ols
E^ ,^ T^ and
F^ that^
holds^ the
derivativ
de (i.e., dx )^ of^ the
corresp^ onding
expression. a.^ (3^ p
oints)^
For^ each
of^ the^ follo
wing^ non
terminal
symb^ ols
state^ whether
dx^ is^ an
inherited
or
synthesized
attribute: E^ : T^ : F^ :
(continued
on^ next^
page)
p^ oints)
Write^ a
syntax^
directed
de^ nition
to^ compute
the^ value
of^ attribute
dx^ for^
grammar
G.^5 Hint:^ Use
the^ two
laws^ of
di^ eren
tiation:^
d^ (u+v^ )=^ dx^
d^ ud +^ dx^
d vand (^) dx (uv^ )=^ dx^
d^ v u +dx^
d^ u v .dx^
c.^ (4^ p
oints)^
Can^ the
attribute
dx^ b^ e^ computed
during^
parsing^
when^ a^
shift-reduce
parser^ is
used?
Why^ or^
why^ not?
Docsity.com
16 p^ oin
ts]^ Consider
the^ follo
wing^ Decaf
program:
class^ A
{public^ int^ i;public^ int^ m(int
i)^ {^ return
i;}
}class^ B
extends
A^ { public^ float
m; public^ float
i(float
x)^ {^ return
x;}
}class^ C
extends
B^ { public^ float
m(float
x){^ return
m;}
public^ int
i(int^ i){
return
i(i);}
}class^ D
extends
C^ { float^ i;int^ m;void^ f(){boolean
i; A^ a;^ B^ b;
C^ c;^ D^
d; a^ =^ new
A(); b^ =^ new
B(); c^ =^ new
C(); d^ =^ new
D(); Out.print(EXAMPLE
_EXPRESSI
ON^ /****/);
}}
The^ table
b^ elow^ lists
expressions
that^ ma
y^ app^ ear
in^ the^ program
in^ place
of^ EXAMPLE_EXPRESSION
(marked
with^ /****/
).^ In^ the
space^ pro
vided^ in
the^ table,
ll^ the^
typ^ e^ of^
each^ expression.
Fill^ at^ least
8 entries.
Each^ correct
entry^ in
the^ table
is^ worth
2 p^ oints
(maxim
um^16 p
oints).
EXAMPLE_EXPRESSION
Typ^ e
1 a.i 2 d.i 3 i 4 this.i 5 i(m) 6 m(i) 7 m(i+m(i)) 8 c.m(i) 9 c.m(b.i) 10 c.m(this.i)
15 p^ oin
ts]
a.^ (7^ p
oints)^
Give^ a^ syn
tax^ directed
de^ nition
to^ generate
co^ de^ for
the^ for
statemen
t^ (C^ or^ Decaf
whose^ syn
tax^ is^ giv
en^ by^ the
following
grammar
rule. S^ !^ for
In^ the^ ab
ove^ E^ stands
for^ expressions
and^ S^ for
statemen
ts.^ Assume
that^ co
de^ is^ an
attribute
of^ E^ and
S^ that^
represen
ts^ the^ generated
co^ de.^ Y
ou^ may^
generate
Cream^
co^ de^ (as
in^ the^ last
pro^ ject)
or^ Three-address
co^ de^ (as
in^ the^ text).
State^ an
y^ assumptions
you^ ma
y^ make^
ab^ out^ the
attributes
of^ S^ and
E^ clearly
.^ You^ ma
y^ (but
need^ not)
assume
that^ short-circuit
co^ de^ is^
generated
for^ E^.
(continued
on^ next^
page)
Docsity.com