SPRING END SEMESTER EXAMINATION-2024
6th Semester, B. Tech.
COMPILER DESIGN
CS 3008
MODEL ANSWER
(For 2022 (L.E.), 2021 & Previous Admitted Batches)
Time: 2 Hours 30 Minutes Full Marks: 50
Answer any FIVE questions.
Question paper consists of two SECTIONS i.e. A and B.
Section A is compulsory.
Attempt any Four question from Sections B.
The figures in the margin indicate full marks.
Candidates are required to give their answers in their own words as far as practicable
and all parts of a question should be answered at one place only.
SECTION-A (Learning levels 1)
1. Answer the following questions. [1
10]
(a) Find the FIRST of all the non-terminals in the following
grammar:
𝑆→ 𝐴𝑎𝐴𝑏 | 𝐵𝑏𝐵𝑎
𝐴→ ε
𝐵→ε
Ans: 𝐹𝐼𝑅𝑆𝑇(𝑆) = {𝑎, 𝑏}
𝐹𝐼𝑅𝑆𝑇(𝐴) = {ε}
𝐹𝐼𝑅𝑆𝑇(𝐵) = {ε}
(b) Differentiate between Synthesized and Inherited attributes in
syntax-directed definition (SDD).
Ans: A synthesized attribute for a nonterminal A at a parse-tree node N
is defined by a semantic rule associated with the production at N.
Note that the production must have A as its head. A synthesized
attribute at node N is defined only in terms of attribute values at
the children of N and at N itself.
An inherited attribute for a nonterminal B at a parse-tree node N
is defined by a semantic rule associated with the production at the
parent of N. Note that the production must have B as a symbol in
its body. An inherited attribute at node N is defined only in terms
of attribute values at N's parent, N itself, and N's siblings.
(c) Find the viable prefixes of the right sentential form of “aaAb”
using the following grammar:
𝑆→ 𝐴𝐴
𝐴→ 𝑎𝐴 | 𝑏
Ans: Viable prefixes of the right sentential form of “aaAb” are a, aa,
aaA.
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
(d) Which phase of the compiler detects the error in the following C
code:
#include <stdio.h>
void main() {
int3num= 1234;
}
Ans: Lexical Analysis Phase(if the student treat the statement
‘int 3num’) / Syntax Analysis Phase
(e) What are the positives of non-inclusion of lexical analyzer in
parser?
Ans: Some of the positives of non-inclusion of lexical analyzer in parser
are: Simplicity of design, Improving compiler efficiency,
Enhancing compiler portability.
(f) Consider the following SDT:
P1: 𝐴→𝐵 + 𝐶 {𝐴. 𝑣𝑎𝑙 = 𝐵. 𝑣𝑎𝑙 + 𝐶. 𝑣𝑎𝑙}
P2: 𝐴→𝐵∙𝐶 {𝐴. 𝑣𝑎𝑙 = 𝐵. 𝑣𝑎𝑙∗𝐶. 𝑣𝑎𝑙, 𝐵. 𝑣𝑎𝑙 = 𝐶. 𝑣𝑎𝑙 }
Select the correct option:
i) P1 is L-attributed but P2 is not L-attributed
ii) P1 is S-attributed but P2 is L-attributed
iii) Both P1 and P2 are S-attributed
iv) None of the above
Ans: i. P1 is L-attributed but P2 is not L-attributed
(g) '
( )
Consider the grammar 𝐺 = { 𝑆, 𝑆 , 𝐸 , (𝑖, 𝑡, 𝑒, 𝑎, 𝑏), 𝑆, 𝑃} with the
following production rules:
' '
𝑃 = {𝑆→𝑖𝐸𝑡𝑆𝑆 | 𝑎, 𝑆 →𝑒𝑆 | ε, 𝐸→𝑏}
'
Find the entry 𝑀[𝑆 , 𝑒] in the LL(1) parsing table.
Ans: ' '
The entry 𝑀[𝑆 , 𝑒] in the LL(1) parsing table = {𝑆 →𝑒𝑆, 𝑆 → ε}
'
(h) Differentiate between machine dependent and machine
independent code optimization on the basis of their objective.
Ans: Machine-dependent code optimization is applied to object
code. Machine-independent code optimization is applied to
intermediate code. Machine-dependent optimization involves CPU
registers and absolute memory references. Machine-independent
code optimization does not involve CPU registers or absolute
memory references.
(i) Considering the following dependency graph, write any one
possible order in which we can evaluate the attributes at the
various nodes of a parse tree.
Ans: Some of the possible order in which we can evaluate the attributes
at the various nodes of a parse tree are: ADBC, ABDC, BADC.
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
(j) Write down the optimized version of the three-address code,
assuming the usual associativity and precedence rules:
𝑠 = 𝑎 ^ (𝑏 + 𝑐) ∗ 𝑏 + 𝑐 + 𝑒
Ans: 𝑡1 = 𝑏 + 𝑐
𝑡2 = 𝑎^𝑡1
𝑡1 = 𝑡2∗𝑏
𝑡2 = 𝑡1 + 𝑐
𝑡1 = 𝑡2 + 𝑒
𝑠 = 𝑡1
SECTION-B (Learning levels 2, 3, 4, 5 and 6)
Prof Roshni Pradhan
2. (a) A compiler uses a two-buffering scheme to read the input. Provide [5]
a pseudo code that will scan the input through the buffers in an
efficient way. Make use of sentinels.
Ans:
Refer Text
const BUFFER_SIZE = 1024 // Size of each buffer
const SENTINEL = '\0' // Sentinel character to indicate end of
buffer
function readInput (inputStream):
buffer1 = new char[BUFFER_SIZE + 1]
buffer2 = new char[BUFFER_SIZE + 1]
currentBuffer = buffer1
nextBuffer = buffer2
currentBufferSize = 0
currentIndex = 0
// Fill the first buffer
fillBuffer (currentBuffer, inputStream)
// Main loop for scanning input
while true:
if currentBuffer[currentIndex] == SENTINEL: // Check for
end of buffer
// Swap buffers
temp = currentBuffer
currentBuffer = nextBuffer
nextBuffer = temp
// Reset index
currentIndex = 0
// Fill the next buffer asynchronously
fillBuffer(nextBuffer, inputStream)
// Check if the next buffer is empty, indicating end of input
if currentBuffer[0] == SENTINEL:
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
break
// Process the current character
processCharacter(currentBuffer[currentIndex])
// Move to the next character
currentIndex += 1
function fillBuffer (buffer, inputStream):
// Read from input stream into buffer
bytesRead = read (inputStream, buffer, BUFFER_SIZE)
buffer[bytesRead] = SENTINEL // Add sentinel at the end
of buffer
function processCharacter(character):
// Process the character (e.g., tokenize, parse, etc.)
// Example: Print the character
print(character)
Prof Amiya Kumar Dash
(b) Write the Regular Definition for unsigned numbers (integer or [5]
floating point) in C language. Also construct the State Transition
Diagram from the above definition.
Ans:
Prof Lipika Dewangan
3. (a) Construct the LL(1) parser for the following grammar: [5]
𝑆→ (𝐿) | 𝑎
𝐿→ 𝐿 , 𝑆 | 𝑆
Parse the given input string “(a,a)” using the above specified
grammar.
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
Ans:
1. Eliminate Left Recursion [1 Mark]
S → (L) | a
L → S L'
L' → , S L' | ε
2. Find First and Follow [1 Mark]
FIRST(S) = {( a} FOLLOW(S) = { , ) $}
FIRST(L) = {( a} FOLLOW(L) = { ) }
FIRST(L') = {, ε} FOLLOW(L') = { ) }
//, is a valid symbol, not to treated as separator
3. Construct LL(1) parsing table: [1.5 Marks]
( ) , a $
S S → (L) S→a
L L → S L' L → S L'
L' L' → ε L' → , S L'
4. Parse the given input string (a,a) [1.5 Marks]
Stack Input Output
----------------------------------------
S$ (a,a) $
(L) $ (a,a) $ S → (L)
(S L') $ (a,a) $ L → S L'
(a L') $ (a,a) $ S→a
a L') $ (a,a) $ Matched 'a'
L') $ (a,a) $ Matched ','
S L') $ (a,a) $ S→a
a L') $ (a,a) $ Matched 'a'
L') $ (a,a) $ Matched ','
S L') $ (a,a) $ S→a
a L') $ (a,a) $ Matched 'a'
L') $ (a,a) $ Matched ','
S L') $ (a,a) $ S→a
a L') $ (a,a) $ Matched 'a'
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
L') $ (a,a) $ Matched ','
S L') $ (a,a) $ S→a
a L') $ (a,a) $ Matched 'a'
L') $ (a,a) $ Matched ','
S L') $ (a,a) $ S→a
a L') $ (a,a) $ Matched 'a'
L') $ (a,a) $ Matched ','
$ (a,a) $ Matched ')'
$ a,a) $ Matched '('
$ ,a) $ Matched 'a'
$ a) $ Matched ','
$ )$ Matched 'a'
$ $ Matched ')'
The parsing is successful, and the input string (a,a) is accepted by the parser according
to the grammar.
Prof Lipika Mohanty
(b) Construct the SLR(1) parsing table for the following grammar: [5].
𝑆→ 𝐴𝑆 | 𝑏
𝐴→ 𝑆𝐴 | 𝑎
Check if the given grammar is SLR(1) or not. Give justification to
your answer.
Ans:
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
Prof Bindu Agrawal
4. (a) Construct the LALR(1) parsing table for the following grammar: [5]
𝐸→ 𝐸 + 𝑇 | 𝑇
𝑇→ 𝐹 ↑ 𝑇 | 𝐹
𝐹 → 𝑖𝑑
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
Ans:
Prof Prabhu Prasad Dev
(b) Construct the annotated parse tree and evaluate the value of the [5]
string “-110” using the following SDT:
Production Attribute Rule
𝑛𝑢𝑚𝑏𝑒𝑟 → 𝑠𝑖𝑔𝑛 𝑙𝑖𝑠𝑡 𝑙𝑖𝑠𝑡. 𝑝𝑜𝑠 = 0
𝑖𝑓 𝑠𝑖𝑔𝑛. 𝑛𝑒𝑔:
𝑛𝑢𝑚𝑏𝑒𝑟. 𝑣𝑎𝑙 =− 𝑙𝑖𝑠𝑡. 𝑣𝑎𝑙
𝑒𝑙𝑠𝑒:
𝑛𝑢𝑚𝑏𝑒𝑟. 𝑣𝑎𝑙 = 𝑙𝑖𝑠𝑡. 𝑣𝑎𝑙
𝑠𝑖𝑔𝑛→ + 𝑠𝑖𝑔𝑛. 𝑛𝑒𝑔 = 𝑓𝑎𝑙𝑠𝑒
𝑠𝑖𝑔𝑛→ − 𝑠𝑖𝑔𝑛. 𝑛𝑒𝑔 = 𝑡𝑟𝑢𝑒
𝑙𝑖𝑠𝑡 → 𝑏𝑖𝑡 𝑏𝑖𝑡. 𝑝𝑜𝑠 = 𝑙𝑖𝑠𝑡. 𝑝𝑜𝑠
𝑙𝑖𝑠𝑡. 𝑣𝑎𝑙 = 𝑏𝑖𝑡. 𝑣𝑎𝑙
𝑙𝑖𝑠𝑡0 → 𝑙𝑖𝑠𝑡1 𝑏𝑖𝑡 𝑙𝑖𝑠𝑡1. 𝑝𝑜𝑠 = 𝑙𝑖𝑠𝑡0. 𝑝𝑜𝑠 + 1
𝑏𝑖𝑡. 𝑝𝑜𝑠 = 𝑙𝑖𝑠𝑡0. 𝑝𝑜𝑠
𝑙𝑖𝑠𝑡0. 𝑣𝑎𝑙 = 𝑙𝑖𝑠𝑡1. 𝑣𝑎𝑙 + 𝑏𝑖𝑡. 𝑣𝑎𝑙
𝑏𝑖𝑡→0 𝑏𝑖𝑡. 𝑣𝑎𝑙 = 0
𝑏𝑖𝑡→1 𝑏𝑖𝑡.𝑝𝑜𝑠
𝑏𝑖𝑡. 𝑣𝑎𝑙 = 2
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
Ans:
Prof Tanik Saikh
5. (a) What is a DAG? Why DAG is used in the process of compiler [5]
design? Construct the DAG for the following statement:
((𝑎 + 𝑏) − ((𝑎 + 𝑏)∗ (𝑎 − 𝑏))) + ( (𝑎 + 𝑏)∗ (𝑎 − 𝑏))
Ans: DAG: A directed acyclic graph for an expression identifies the
common subexpression of the expression. Like the syntax tree for
an expression, a DAG has leaves corresponding to atomic
operands and interior nodes corresponding to operators.
DAG provides important clues regarding the generation of
efficient code to evaluate the expressions. It represents the control
flow and data dependencies of a program. These representations
are often used as an intermediate representation that facilitates
program optimization and transformation.
Prof Nayan Subhasis Behera
(b) Translate the intermediate code corresponding to the given [5]
expression “− (𝑎∗𝑏) + (𝑐 + 𝑑) − (𝑎 + 𝑏 + 𝑐 + 𝑑)” in
quadruple, triple and indirect triple.
Ans: Given expression is : -(a * b) + (c + d) – (a + b + c + d)
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
Three Address Code for the given expression is-
(1) T1 = a * b
(2) T2 = uminus T1
(3) T3 = c + d
(4) T4 = T2 + T3
(5) T5 = a + b
(6) T6 = T3 + T5
(7) T7 = T4 – T6
Quadruple Representation
# OP ARG-1 ARG-2 RESULT
(1) * a b T1
(2) uminus T1 T2
(3) + c d T3
(4) + T2 T3 T4
(5) + a b T5
(6) + T3 T5 T6
(7) - T4 T6 T7
Triples Representation
# OP ARG-1 ARG-2
(1) * a b
(2) uminus (1)
(3) + c d
(4) + (2) (3)
(5) + a b
(6) + (3) (5)
(7) - (4) (6)
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
Indirect Triples Representation
# OP ARG- ARG- # STATEMENT
1 2
(14) * a b (1) (14)
(15) uminus (1) (2) (15)
(16) + c d (3) (16)
(17) + (2) (3) (4) (17)
(18) + a b (5) (18)
(19) + (3) (5) (6) (19)
(20) - (4) (6) (7) (20)
Prof Ajay Anand
6. (a) For the following flow graph: [5]
i) Identify the loops of the flow graph.
ii) Statements (1) and (2) in 𝐵1 are both copy statements, in
which a and b are given constant values. For which uses of 𝑎
and 𝑏 can we perform copy propagation and replace these
uses of variables by uses of a constant? Do so, wherever
possible.
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
iii)Identify any global common subexpressions for each loop.
Eliminate these subexpressions if possible.
Ans:
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
Prof Bhaswati Sahoo
(b) Generate the three-address code for the following C code, where 𝑎 [5]
and 𝑏 are arrays of size 20×20, and there are four bytes per word.
𝑎𝑑𝑑 = 0;
𝑖 = 0;
𝑗 = 1;
𝑑𝑜
{
𝑎𝑑𝑑 = 𝑎𝑑𝑑 + 𝑎[𝑖][𝑗]∗𝑏[𝑗][𝑖]
𝑖=𝑖 +1
𝑗=𝑗 +1
}𝑤ℎ𝑖𝑙𝑒 (𝑖 <= 20 && 𝑖 <= 20);
Ans: 1. add = 0
2. i = 0
3. j = 1
4. L1: if i > 20 goto L2
5. if j > 20 goto L2
6. t1 = i * 80 // Assuming each row has 80
bytes (20 * 4)
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
7. t2 = t1 + j * 4 // Assuming each
element is 4 bytes
8. t3 = a[t2] // Assuming a is
a one-dimensional array
9. t4 = j * 80 // Assuming each
row has 80 bytes (20 * 4)
10. t5 = t4 + i * 4 // Assuming each
element is 4 bytes
11. t6 = b[t5] // Assuming b is
a one-dimensional array
12. t7 = t3 * t6
13. add = add + t7
14. i = i + 1
15. j = j + 1
16. goto L1
17. L2: // End of loop
*****
Solution Template Prepared by : Prof Jasaswi Prasad Mohanty
KIIT-DU/2023/SOT/Spring End Semester Examination-2023