ASSIGNMENT # 1update
ASSIGNMENT # 1update
Q. 3
Define the following terms:
a) Front-end and Back-end of the compiler.
b) Analysis and synthesis phases of the compiler
Q. 4
This question assumes that you have a compiler that has an option to produce assembly output.
a. Determine if your compiler performs constant folding optimizations.
b. A related but more advanced optimization is that of constant propagation: a variable that
currently has a constant value is replaced by that value in expressions. For example, the code(in
C syntax)
x= 4;
y= x+2;
would, using constant propagation (and constant folding), be replaced by the code
x = 4;
y = 6;
determine if your compiler performs constant propagation.
c. Give as many reasons as you can why constant propagation is more difficult than constant
folding.
d. A situation related to constant propagation and constant folding is the use of named constant
in a program. Using a named constant X instead of a variable, we can translate the above
example as the following C code:
const int x=4;
………………………
……………………..
y= x+2;
……………
…………..
Determine if your compiler performs propagation/folding under these circumstances. How is this
different from part (b)?
Q. 5
Describe the tasks performed by the following programs. And explain how these programs
resemble or are related to compilers.
a. A Language Pre-processor.
b. Interpreter.
c. Assembler.
Q. 6
Suppose you have a Pascal to-C translator written in C and a working C compiler. Use T-
diagrams to describe the steps you would take to create a working Pascal compiler.
Q. 7
The process of porting a compiler can be considered as two distinct operations: Retargeting
(modifying the compiler to produce target code for a new machine) and Rehosting ( modifying
the compiler to run on a new machine). Draw the distinctness of these two operations in terms
of T-diagrams.
Q.8
Suppose you want to create a cross compiler for the source language S that generates target
code in language T and the implementation language of this compiler is A. Further suppose
you have a compiler written for language A with both target and implementation language as
M.
Use T-diagrams to make a compiler that compiles a source program written in language S and
generates the target code in T that runs on machine M.
Q. 9
a) Draw the syntax tree for the following code segment.
{ sample program that computes factorial}
read x; {input an integer}
if 0 < x then { do not compute if x <= 0 }
fact := 1;
repeat
fact := fact * x;
x ;= x-1
until x = 0;
write fact { output factorial of x}
endx
Q. 10 Show the actions taken by each phase of compiler for the following source instructions.
a) T = n1 + n2 * 7
b) a[index]= 4+2
c) S = a * b - c + 4/2
Q. 11
What is scanning or lexical analysis? What does LEX tool do? Describe the difference between
Lexemes and tokens?
------------------------------------------------------------------------------------------
Q. 12
Consider the following grammar representing simplified expressions:
lexp atom | list
atom number | identifier
list (lexp-seq)
lexp-seq lexp-seq lexp | lexp
a. Translate the above grammar into EBNF and then modify the grammar.
b. Draw syntax diagrams for the EBNF rules of part(a).
----------------------------------------- THE END ---------------------------------------------------