Docsity

Lex Specification - Advanced Compiler Design - Exam, Exams of Computer Science

Toggle sidebar
bg1
Undergraduate Exam
CSE-304 Compiler Design
Fall '97
Final Exam
December 16, 1997.
Duration: 2 hours, 45 minutes.
INSTRUCTIONS
Read the following carefully before answering any question.
The exam for graduatestudents is dierent from the exam for theundergraduatestudents. Make sure
you are readingthe correct question paper.
Doubtsaboutthe questions will be answered only in the rst 15 minutes of the exam. So, read the
questions carefully atthe beginningofthe exam.
Keep your answers br ief and precise.
Answer each question on a separatesheet of pap er. Writeyour name
and
your id number on the rst
sheet. Writeyour id number on thetop righthand corner of each additional sheet.
There are 8 questions in all, in 4 pages, for a total of 105 points. Themaximum score is 100.
GOOD LUCK!
1.
[Total: 10 points]
(a)
[5 points]
Writea
lex
specication to convert each occurrence of \
++
" in a C program to
\
+1
".
(b)
[5 points]
Writea
lex
specication to convert every continuous sequence of blank lines (a
blank line contains only white space characters) intoasingle line containing\
<P>
".
2.
[10 points]
L
A
T
E
Xand HTML are twodocument formatting languages. Wewantto build
a translator to convert documents structured usingL
A
T
E
Xinto HTML. In L
A
T
E
X, thetext to
be formatted is specied between
\begin{document}
and
\end{document}
and in HTML, it
is specied between
<html>
and
</html>
.InL
A
T
E
X,alist of(unnumbered) bullet items is
specied us ing
\begin{itemize}
and
\end{itemize}
and in HTML thelistisgiven using
<UL>
and
</UL>
. Eachitem in the list begins with
\item
in L
A
T
E
X, and with
<LI>
in HTML.
A sample translation of a L
A
T
E
Xdocumentto HTML i s shown below:
L
A
T
E
X
:
HTML:
\begin{document}
This is a sample latex document.
A list of bullet items can be created
as follows.
\begin{itemize}
\item Start an {\em itemize}
environment;
\item Place each item in with
a {\em item} command.
\end{itemize}
\end{document}
<html>
This is a sample latex document.
A list of bullet items can be created
as follows.
<UL>
<LI> Start an <em>itemize</em>
environment;
<LI> Place each item in with
a <em>item</em> command.
</UL>
</html>
Describe howyou will structure a system to translateL
A
T
E
Xdocumentsto HTML. Explain
whattools you will use to construct such a translator, andwhy.
1
Docsity.com
pf3
pf4

Partial preview of the text

Download Lex Specification - Advanced Compiler Design - Exam and more Exams Computer Science in PDF only on Docsity!

Undergraduate Exam

CSE-304 Compiler De s ign

Fall ' Final Exam Decemb er 16, 1997. Duration: 2 hours, 45 minute s.

INSTRUCTIONS

Read the following carefully b efore answer ing any que stion.  The exam for graduate students i s di erent f rom the exam for the undergraduate students. Make sure you are reading the correct que stion pap er.  Doubts ab out the que stions will b e answere d only in the rst 15 minute s of the exam. So, read the que stions carefully at the b eginning of the exam.  Keep your answers br ief and preci s e.  Answer each que stion on a s eparate sheet of pap er. Wr ite your name and your id numb er on the rst sheet. Wr ite your id numb er on the top r ight hand cor ner of each additional sheet.  There are 8 que stions in all, in 4 page s, for a total of 105 p oints. The maximum score i s 100.

GOOD LUCK!

  1. [Total: 10 p oints]

(a) [5 p oints] Wr ite a lex sp eci cation to convert each o ccurrence of ++" in a C program to + 1 ". (b) [5 p oints] Wr ite a lex sp eci cation to convert every continuous s equence of blank line s (a blank line contains only white space characters) into a s ingle line containing <P>".

  1. [10 p oints] LATEX and HTML are two do cument formatting language s. We want to build a translator to convert do cuments structure d us ing LATEX into HTML. In LATEX, the text to b e formatte d i s sp eci e d b etween \begin{document} and \end{document} and in HTML, it i s sp eci e d b etween and . In LATEX, a li st of (unnumb ere d) bullet items i s sp eci e d us ing \begin{itemize} and \end{itemize} and in HTML the li st i s given us ing
      and
    . Each item in the li st b egins with \item in LATEX, and with
  2. in HTML. A sample translation of a LATEX do cument to HTML i s shown b elow: LATEX: HTML: \begin{document} This is a sample latex document. A list of bullet items can be created as follows. \begin{itemize} \item Start an {\em itemize} environment; \item Place each item in with a {\em item} command. \end{itemize} \end{document}

This is a sample latex document. A list of bullet items can be created as follows.

  • Start an itemize environment;
  • Place each item in with a item command.
De scr ib e how you will structure a system to translate LATEX do cuments to HTML. Explain what to ols you will us e to construct such a translator, and why.

Undergraduate Exam

(^) 3. [Total: 10 p oints]

(a) [5 p oints] Let L b e the s et of all bit str ings (i.e, str ings over alphab et  = f 0 ; 1 g) that are divi s ible by 4. Can L b e repre s ente d by a regular expre ss ion? Justify. (b) [5 p oints] Let L^0 b e the s et of all bit str ings (i.e, str ings over alphab et  = f 0 ; 1 g) that are divi s ible by 3. Can L^0 b e repre s ente d by a regular expre ss ion? Justify.

  1. [Total: 20 p oints] Cons ider the following grammar G 4 :

E ! E + E E ! E  E E ! id

(a) [5 p oints] Compute the FIRST and FOLLOW s ets for E. (b) [5 p oints] Is G 4 an LL(1) grammar? Justify. (c) [5 p oints] Compute the LR(0) s ets of items for G 4. (d) [5 p oints] Which state s in the SLR(1) action table for G 4 has re duce actions? On what symb ols are the s e re duce actions done?

  1. [Total: 10 p oints]

(a) [5 p oints] Cons ider the following grammar G 5 that generate s binary numb ers repre s enting integers:

S ! L L ! L B L ! B B ! 0 B ! 1

Let an attr ibute val give the value of the binary numb er generate d by S. For example, on input 110 , S.val = 6. Determine S.val us ing syntax-directe d de nitions. (b) [5 p oints] Is val an inher ite d attr ibute of S? Is it an inher ite d attr ibute of L or B? Justify.

  1. [Total: 15 p oints]

(a) [3 p oints] Explain why eld and metho d name s in Decaf cannot b e re solve d until typ e s are known. That i s, why cannot one determine which entity i s repre s ente d by a[i].x until the typ e of a[i] i s known? (b) [4 p oints] When pars ing Decaf programs, we can encounter a var iable x in some expre ss ion such that x i s not declare d in the current scop e, nor in any enclos ing scop e. Explain why thi s i s not an error.

Undergraduate Exam

(^) (c) [5 p oints] Cons ider the grammar G^008 der ive d by adding

E ! E + = E

to grammar G^09. Mo dify the syntax directe d de ntion in (b) to include co de generation for expre ss ions of the form x += y.

END OF EXAM