We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 6
University of Sargodha
BS 7" Term Exam 2019
Subject: Computer Science Paper: Compiler Construction (CS: 4141)
‘Time Allowed: 2:30 Hours Maximum Marks: 80
Objective Part Compulsory
Q.No.1: Attempt all parts and each require answer 2 ~ 3 lines (16*2=32)
1. What are the benefits of generating intermediate code?
‘The benefits of using machine independent intermediate code are:
‘+ Because of the machine independent intermediate code, portability will be enhanced. For ex, suppose,
if a compiler translates the source language to its target machine language without having the option
for generating intermediate code, then for each new machine, a full nativey€ompilén, is required.
Because, obviously, there were some modifications in the compiler itself aécording to the, machine
specifications.
‘+ Retargeting is facilitated
‘+ _Itis easier to apply source code modification to improve the performance of souifée code by optimising
the intermediate code.
2. What is the difference between syntax tree and parse tree?
A parse tree is a record of the rules (and tokens) used to match some input text whereas @ syntax tree records the
‘structure of the input and is insensitive to the grammar that produted it.
3. What is meant by dead code elimination?
In compiler theory, déad code elimination (also knownyas DCE)dead code removal dead code stripping, or dead
code strip) is a compiler optimization to remove code which does notiaffect the program results.
4. Consider the following grammar:
SOA
ADA+A|BH
Boy
Draw the parse tree for the input z+ - +4"
z
There is minus in the question, which cannot be produced from the given grammar. So, either the question is wrong.
or typing mistake. Z++ + Z++ can be parsed.
5. Consider the following grammar:
X > YaYb | ZbZa
Ye
Lee
Using the defi
ion of LL (1), expla
why the grammar is or not LL (1).
Visit tShahab. blogspot.com for more.NT | First | Follow
*
=
a,b
a b Ss
x x>Y
X2Z
Y [x3y [xoy
X9Z |x>z
Z |[x>y [xoy
X>z |x>z
‘As from the table show the grammar is not LL (1), because there are some cells of the table which have more than
‘one entry, that shows conflict.
6. Differentiate between compiler and interpreter.
Compiler transforms code written in a high-level programming language intolthe machine cod@)at oncé, before
program runs, whereas an Interpreter covert each high-level program statement, one by one, into the machine code,
during program run. Compiled code runs faster while interpreted code ruins slower. Compiler displaySall errors after
‘compilation, on the other hand, the Interpreter displays errors of eachuline one by one. Compiler fs based on translation
linking-loading model, whereas Interpreter is based on Interpretation) Method. Compiler takes an entire program
whereas the Interpreter takes a single line of code.
7. What is cros&compiler?
‘Across compiler is a compiler capable of creating executable code)for a platform other than the one on which
the compiler is running. For ékarhplé)a compiler thatruns on a Windows'7.PC but generates code that runs on Android
smartphone is a cross compiler.
8. What is the difference between scanner and parser?
Scanner simply tyfns an input String (say 2 file) into a list of tokens. These tokens represent things like identifiers,
parentheses, operators ete. A parser converts this list of tokens into a Tree-like object to represent how the tokens fit
together to form a cohesive whole (sometimes referred to as.a sentence).
9, Perform the lexical analysis of the follo
If (idx = = 0) idx = 505;
Lexical analysis is the firstyphase of a compiler. It takes the modified source code from language preprocessors that
{fe written in the formof sentences) The lexical analyzer breaks these syntaxes into a series of tokens, by removing
‘any whitespace or comments in the source code.
g statement
(keyword, if), (separator, (), (identifier, idx), (operator,
(literal, 50), (separator, ; )
), (literal, 0), (separator, ) ), (literal, idx),
(operator, =]
10. What is the working of Linker?
Linker are also called link editors. Linking is process of collecting and maintaining piece of code and data into a single
file. Linker also link a particular module into system library. It takes object modules from assembler as input and forms
an executable file as output for loader.
11. Why we need Pre-processor?
A preprocessor is a program that processes its input data to produce output that is used as input to another program,
The output is said to be a preprocessed form of the input data, which is often used by some subsequent programs
like compilers. The amount and kind of processing done depends on the nature of the preprocessor; some
preprocessors are only capable of performing relatively simple textual substitutions and macro expansions, while
others have the power of full-fledged programming languages.
Visit tShahab. blogspot.com for more.12, What is performed in Syntax analysis layer?
‘Syntax analysis is the second phase of compilation process. It takes tokens as input and generates a parse tree as
‘output. In syntax analysis phase, the parser checks that the expression made by the tokens is syntactically correct or
not.
13. Regular Expression or Finite Automata are used in which phase of compiler.
‘The first phase of compiler, Lexical Analysis, use Regular expressions and Finite automata,
14, Context Free Grammar is used in which phase of compiler.
‘Syntax analysis phase uses CFG to check syntax of the input according to the source code language.
15. Differentiate between machine independent and machine dependent optimization?
Machine Dependent code optimization: These techniques involve program transformations that improve the target
code without taking into consideration any properties of the target machine. These techniques are applied on object
code.
Machine Independent code o
properties of the target machine like registers and special machine instruction sequences @vailable ete. these
techniques are applied on target code generated. The optimization which is applied\on an intermediate code called as
machine independent code optimization
16, What is meant by ambiguous grammar?
‘An ambiguous grammar is a context-free grammar for which there exists a string that can have more than one leftmost
derivation or parse tree, while an unambiguous grammar is a(éontext-free grammar for which every valid string has a
unique leftmost derivation or parse tree.
Subjective Part (3*16)
Q2. a) What is front-end and back-end of compiler? What is the significance of
into front-end and back-end?
In compilers, the front end translates a computer programming source code into an intermediate representation, and
the back end works with the intermediate representation to produce code in a computer output language. The back
‘end usually optimizes to produce code that runs faster.
1g com
‘The phases of a compiler are collected into front end and back end, The front end consists of those phases that depend
primarilyoonthe source program. These normally include Lexical and Syntactic analysis, Semantic analysis ,and the
‘generation of intermediate code. A certain amount of code optimization can be done by front end as well. The back
‘end includes the code optimization phase and final code generation phase, along with the necessary error handling
and symbol table operations.
The front end analyzes the source program and produces intermediate code while the back end synthesizes the target
program from the intermediate code,
The advantage is that if you want a compiler for a different language, to run on the same architecture, you only have
to rewrite the front end, Alternatively, if you want a compiler for the same language, but to run on a different
architecture, you only have to rewrite the back end.
By decoupling the front end and back end of the compiler, you need only write one front end (which deals with the
high-level source language you want to compile) and then one back end for each target architecture. This approach to
‘organizing the compiler cuts down significantly on the amount of work required to target another architecture. On the
other side of the coin, if you already have several back ends that support a variety of target architectures, and you
‘want to support a new high-level language, you can write a front end for that high-level language, and you'll
automatically support all the target architectures supported by your back ends.
FRR RRR RII RII
Visit tShahab. blogspot.com for more.Build the parse tree for the arithmetic expression 4+ 3*2 using
EDE+T|E-T|T
TOT*FIF
F>al(E) where a represents an operand of some type
¢ following grammar
Note: The best way to write an answer is to do by some parsing techniques of top-down or bottom-up: (Writing
complete procedure e.g. creating parsing table and stack implementation and then building parse tre from them)
FR RRR RRR RIN RRR RRR KK
Q3. Opt
ize the following code using optimization rules,
Algebraic optimization
‘The statement a = x‘2 can be written asa =x * x
Copy propagation
‘As c=xwe can directly put x in place of cin statement
=c*cthenit will be
Constant folding
Then we can also directly put value of bin statement e = b * 2 then
3*2 will be e=6
Common subexpression elimination
‘As from algebraic optimization, we have a = x*x, and we also have d=x*x from copy propagation, we can place d = a
Copy propagation (round 2)
‘As d=a, we can place a in place of dinf=a+d,so,f=ata
‘And we already have value of e= 6 from constant folding, then g=e * f will beg=6*f
Dead code elimination
‘As we have final code till now is.
Visit tShahab. blogspot.com for more.e=6
feata
e=6tf
as we can see we have dead code, which is not accessible or used in code, by removing the dead code we have:
asxtx
feata
e=6'f
This isthe final code after optimization.
FE RNIN INR NINN NII RIN OE
Q4. a) Make (1) parsing table for the following grammar.
S$ Aa/bAc/Be/ bBa
Add
Bod
In addition to the rules given above, one extra rule S' > S as the initial item. Following the procedures for. constructing
the LR(1) parser, here is the resulting state diagram:
|S _e(7:S'>S..5
Ip: (0) S'S,
1)S—.Aa,
(3 > mags} > CS Ans CES)
@ $ x oo iy: S > bAG GirS> bac.) = bAc.,
5) A> d, a Ss BBR S|
ont RS Bae
Boda
Is: A> da
Bod,c
Based on the state diagram, we derive the LR(1) parsing table as follows:
‘Action’ Goto
igen ee 9 | SEI
3 4 12 4
9 7 8
slo
6
sil
5S
3B
2
Since there are no multiple actions in any entry, the given grammar is LR(1). However, when obtaining the LALR(1)
parsing table by merging states, we will merge states Is and Io, and the resulting state will be as follows:
Visit tShahab. blogspot.com for more.Ig,9: A> d., ale
Bo dale
Itis basically a reduce-reduce conflict. So, the given grammar is not LALR(1).
FRR RRR RRR IRIN HK
b) Parse the strings bde and daa with LR (1) parsing algorithm from above grammar.
Since we have already construct parsing table in previous part, now we parse the input string bde with LR(1) parsing
algorithm, as we already have LR(1) parsing table.
Stack | Input Action
$0 bdc$_| Shift b > $3
$0b3 de$ | Shift d > 59
$ob3d9__ [cS __| Reduce 5: A>
$ob3a7__|[c$__| shift > si1
$0b3A7c11 | $ Reduce r2: § > bAc
$0s1 $ Accept
The string bdc is accepted by the grammar. Now we parse daa.
Stack Input | Action’
$0 das Shiftid> $4
$004 aas
Now we cannot do any further action because there is nolaction written of a in 4 state in parsing table. And also, in
above table there is also left.aa§ iinput column and is not accepted, Therefore, daa is not the string of the above
grammar language.
FRI RINIRRINnRRUnnnrnrunninninninninnn
QS. Why first/first is not a con!
reason or examples,
First/first conflict only occurs in UL(1) parsing whichis a prédictive parser of Top down parsing. That’s why it is not a
conflict of bottom-up parsing.
in bottom parsing? Support your answer with specific
(write some example. Short question 5 is also the example of a LL(1) grammar.)
Je A ronsrunsnunsmununsrunsrunununaninin unsrunrunmnmnnnurrunrunnunnnninnmnnnnn kt
Q6. Given the following CFG.
Exp> Exp+Term
Exp > Exp +-Term
Exp > Term
Exp > Exp + Factor
Exp > Term / Factor
Exp > Factor
Exp > (Exp)
Exp > id
Write an attribute grammar that calculates the value of any arithmetic expression with the following
restriction: Arithmetic operation can only be allowed when both operands of the operator have same
data type otherwise an error should be generated.
FOR RINNE
Visit tShahab. blogspot.com for more.