0% found this document useful (0 votes)
133 views

ASSIGNMENT # 1update

This document contains 14 questions regarding compilers and compiler design. The questions cover topics such as: - Drawing parse trees and syntax trees for code snippets - Describing the steps to create compilers using T-diagrams - Defining compiler phases like the front-end and back-end - Determining if optimizations like constant folding are performed - Describing the roles of programs like pre-processors and interpreters - Translating grammars to EBNF and drawing syntax diagrams

Uploaded by

zain ghuman zain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

ASSIGNMENT # 1update

This document contains 14 questions regarding compilers and compiler design. The questions cover topics such as: - Drawing parse trees and syntax trees for code snippets - Describing the steps to create compilers using T-diagrams - Defining compiler phases like the front-end and back-end - Determining if optimizations like constant folding are performed - Describing the roles of programs like pre-processors and interpreters - Translating grammars to EBNF and drawing syntax diagrams

Uploaded by

zain ghuman zain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment # 01.

BCS-7 (CLO-1) (10 points)


Date of Submission: 28/09 for 7A & 29/09 for 7B.
----------------------------------------------------------------------------------------------
Q.1
Given the C assignment:
A [ i + 1 ] = a[i] + 2
Draw a parse tree and a syntax tree for the above expression.
Q.2
Suppose you have a working C compiler on machine A with the help of language M. Use T-
diagrams to describe the steps you would take to create a working compiler for the language C
on another machine B while the host language is T.

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

b) For the following Tiny Program:


read x;
x := x+1;
write x
a. Draw the TINY parse tree.
b. Draw the TINY syntax tree.

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.


b. Draw syntax diagrams for the EBNF of part(a).
-----------------------------------------------------------------
Q. 13 Draw the syntax tree for the following code segment.
{ sample program that computes gcd of two numbers}
read u;
read v; {input two integers}
if v = 0 then v := 0 {do nothing }
else
repeat
temp := v;
v := u – u /v*v;
u := temp
until v= 0
end;
write u { output gcd of original u & v }
---------------------------------------------------------------------------
Q.14.
Consider the following grammar representing simplified arithmetic
expressions:
exp  exp addop term | term
addop  + | -
term  term mulop factor | factor
mulop  *
factor  (exp) | number

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 ---------------------------------------------------

You might also like