CCS 305

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

MASENO UNIVERSITY

UNIVERSITY EXAMINATIONS 2019/2020

THIRD YEAR FIRST SEMESTER EXAMINATIONS FOR THE


DEGREE OF BACHELOR OF SCIENCE IN COMPUTER SCIENCE

MAIN CAMPUS

CCS 305: INTRODUCTION TO COMPILER


CONSTRUCTION

Date: 28th November, 2019 Time: 3.30 - 6.30 pm

INSTRUCTIONS:

• Answer ALL questions in SECTION A and any other TWO from SECTION B
• Write your registration number on all sheets of the answer book used.
• Use a NEW PAGE FOR EVERY QUESTION attempted, and indicate number
on the space provided on the page of the answer sheet.
• Fasten together all loose answer sheets used.
• Mobile phones and PDAs are NOT allowed in the examination room.

MASENO UNIVERSITY ISO 9001 :2008 CERTIFIED e


SECTION A: ANSWER ALL QUESTIONS

Question One (30 marks)

a) State and briefly explain five types of errors that can be


encountered during the compilation phases.
(5 marks)
b) State and briefly explain four major types of data structure in a
compiler (4 marks)
c) Backus-Naur Form (BNF) for simple arithmetic expressions
may be represented as:
• < exp > -t < exp > + < exp > I < exp > - < exp > I < exp > * <
exp > I < exp > 1 < exp > I( < exp > ) I < number>
• < number> -t < digit> I < digit> < number>
• <digit> -t 0 11 I 2 r 3 1415 I 6 I 7 I 8 I 9.
Write a leftmost derivation of 8 + 6 - 4 * 7/2 then draw its equivalent parse tree. (6 marks)

d) Use appropriate symbolic notation to explain the components


of a context-free grammar.
(5 marks)
e) Explain the purpose of a symbol table in the compilation
process, by stating typical attributes that can be defined for variables, procedures and functions.
(5 marks)
f) Compare compilers and interpreters as programming
language translation mechanisms.
(5 marks)

2
SECTION B: ANSWER ANY TWO QUESTIONS

Question Two.(20 marks)

a) FirstO and FollowO functions are very necessary in order to


define LL(1) grammars. State the rules required to compute First(X) and Follow(X), where X is a
symbol in the production of a given grammar.
(8 marks)
b) Consider the grammar G with productions:
C ~ cD I Ed
D ~ EFC Ie
E~ £ If
F~g
i) Generate the First sets and Follow sets (4 marks)
ii) Use the Generated First sets and Follow sets to develop LL(1) parsing table. (4 marks)
iii) Use the LL(1) parsing table with the LL(1) parsing algorithm to show the sequence of
operations on input string cgfd (4 marks)

Question Three (20 marks)

a) Explain how a shift-reduce parser operates, clearly describing


the role of stack, and various actions performed during the parsing process.
(6 marks)
b) Use a simple context-free production to explain how handles
are generated, then state their importance in shift-reduce parsing.
(3 marks)
c) Given the production of a context-free grammar G
E~E+E
E ~E*E
E ~(E)
E~i

..
i) Derive a top-down rightmost derivation of G
(3 marks)
H), Use a table containinq stack values, input string and action "
values to show the steps performed when analyzing the input string: i + i * i(i)
(8 marks)

Question Four (20 marks)

a) Use simple diagrams to distinguish between deterministic


finite state automaton (DFA) and non deterministic finite state automaton (NFA),
(6 marks)
I
'-- b) Build a scanner represented by the following regular
expressions using DFA (5 marks)
while-keyword = while
Identifier = [a- z][a- zO - 9]*
c) Develop a state transition table for the above scanner
(5 marks)
d) Given the grammar G with the following production rules, S
~a I as I bS, represent the language denoted by the grammar in regular expression, then
determine whether the following strings can be generated by the grammar (i) babaaba (ii)
aaabbbab (iii) bbaaba (4 marks)

Question Five (20 marks)

a) Consider the language L consisting of all strings waver the


alphabet {c,h} such that w contains hhh or cccc (i,e" at least 3 h in sequence, or at least 4 c in
sequence, or both), For example, the strings chchhh, cccchc, hhhcccch, hhh, etc. belong to the
language L,
i) Construct a regular expression for L
(4 marks)
ii) Construct from the regular expression an NFA recognizing L
(5 marks)

"
iii) Construct a DFA recognizing L, either by deriving from the
NFA of question (ii), or by constructing one directly.
(6 marks),.

b) The following production rules represent a regular grammar:


S~aA
A -----?aA
A -----? bB
B -----?bB

i) Determine the regular language defined by the above


grammar. (3 marks)
ii) List four strings of at least five terminals that can be accepted
by the language. (2 marks)

You might also like