Docsity

Document Formatting Languages - Advanced Compiler Design - Exam, Exams of Computer Science

Toggle sidebar
bg1
Undergraduate Exam
CSE-304 Compiler Design
Fall '98
Final Exam
December 17, 1998.
Duration: 2 hours, 50 minutes.
ID Number :
Name :
INSTRUCTIONS
Read the following carefully before answering any question.
๎˜
Please ๎˜Œll in your ID number and Name in the space provided above.
๎˜
DO NOT OPEN THE EXAM BOOKLET UNTIL INSTRUCTED TO DO SO!
๎˜
The exam for undergraduate students is di๎˜‹erent from the exam for the graduate students.
Make sure you are reading the correct question paper.
๎˜
Read all questions carefully at the beginning of the exam. Doubts ab out the questions will be
answered only in the ๎˜Œrst 30 minutes of the exam.
๎˜
Write your answers in the space provided. Keep answers brief and precise.
๎˜
The exam consists of 7 questions, in 11 pages (including this page) for a total of 100 points.
GOOD LUCK!
Question Max. Points Points Scored
1. 10
2. 10
3. 15
4. 20
5. 14
6. 16
7. 15
Total 100
1
Undergraduate Exam
1.
[10 points]
HTML and L
A
T
E
X are two document formatting languages. Wewant to build a translator
to convert documents structured using HTML to L
A
T
E
X. In HTML, the text of the document to
be formatted is speci๎˜Œed between
<html>
and
</html>
; in L
A
T
E
X, the text is speci๎˜Œed between
\begin{document}
and
\end{document}
. In HTML, a list of unnumbered bullet items is given
using
<UL>
and
</UL>
;inL
A
T
E
X, the list is speci๎˜Œed using
\begin{itemize}
and
\end{itemize}
. In
HTML, a list of numbered bullet items is given using
<OL>
and
</OL>
;inL
A
T
E
X, the list is speci๎˜Œed
using
\begin{enumerate}
and
\end{enumerate}
. Each item in the list begins with with
<LI>
in
HTML, and
\item
in L
A
T
E
X.
Font changing commands in HTML are speci๎˜Œed as follows: the text to be displayed in
bold face
,
italics
, or
teletype
are marked with
<B>
,
<I>
, and
<TT>
, respectively, in the beginning, and
</B>
,
</I>
, and
</TT>
, respectively, in the end. In L
A
T
E
X, the text with changed font begins with
{\bf
,
{\it
and
{\tt
respectively, and the change of font persists until the matching
}
is found.
A sample translation of a HTML documenttoL
A
T
E
Xisshown below:
HTML: L
A
T
E
X
:
Output (Formatted) Text
<html>
This is a sample document.
A list of bullet items
can be created as follows:
<UL>
<LI> Start an <b>itemize</b>
environment;
<LI> Place each item with
an <em>item</em>
command.
</UL>
</html>
\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 document.
A list of bullet items can be
created as follows.
๎˜
Start an
itemize
environment;
๎˜
Place each item
with an
item
com-
mand.
a. (7 p oints)
List the smallest set of tools you will use to construct a translator to convert input
HTML documents into L
A
T
E
X documents. Explain your choice.
You may assume that all input documents are well formed HTML documents: i.e., they are
correct with respect to HTML syntax.
b. (3 p oints)
If the input documents may not all be well formed HTML documents, will the tools
you chose in part
[a]
above be still su๎˜Žcient to construct a translator? Explain.
2
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Document Formatting Languages - Advanced Compiler Design - Exam and more Exams Computer Science in PDF only on Docsity!

Undergraduate Exam

CSE-

Compiler

Design

Fall^ '98 Final^ Exam Decemb^ er^ 17,

Duration:

2 hours,

50 minutes.

ID^ Num

b^ er^ : Name^ :

INSTR

UCTIONS

Read^ the

following

carefully

b^ efore^

answering

any^ question.

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

GOOD

LUCK!

Question

Max.

Points

Poin

ts^ Scored

1.^

2.^

3.^

4.^

5.^

6.^

7.^

Total^

1.^ [10^ p^ Undergraduate Exam

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

HTML,^

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

HTML,^

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

A LTX,^ E

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

HTML^

do^ cumen

ts^ into^ L

ATX^ doE

cuments.

Explain

your^ choice.

You^ ma

y^ assume

that^ all

input^ do

cuments

are^ well

formed

HTML

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

HTML^

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

ASCI^ I^

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

3.^ [Total:^ Undergraduate Exam

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

^ 3,^ `'^

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

(2^ +^ 4)

^6 will^

b^ e^ written

in^ p^ ost

x^ as^2

4 +^6 

a.^ (2^ p

oints)^

Write^ the

in^ x^ expression

((4^ ^

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)

b.^ (7 Undergraduate Exam

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)

7.^ [Total:^ Undergraduate Exam

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

(^ E^ ;^ E

;^ E^ )^ S

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