Compiler Design Two Marks
Compiler Design Two Marks
INTRODUCTION TO COMPILER
1. What is a Complier?
A Complier is a program that reads a program written in one
language-the source language-and translates it in to an equivalent program
in another language-the target language. As an important part of this
translation process, the compiler reports to its user the presence of errors in
the source program.
2. State some software tools that manipulate source program?
i. Structure editors
ii. Pretty printers
iii. Static
iv. Checkers
v. Interpreters.
3. What are the cousins of compiler? April/May 2004, April/May 2005
The following are the cousins of
i. Preprocessors
ii. Assemblers
iii. Loaders
iv. Link editors.
4. What are the main two parts of compilation? What are they
performing?
The two main parts are
Analysis part breaks up the source program into constituent
pieces and create an intermediate representation of the source program.
Synthesis part constructs the desired target program from the
intermediate representation
5. What is a Structure editor?
A structure editor takes as input a sequence of commands to build a
source program. The structure editor not only performs the text creation and
modification functions of an ordinary text editor but it also analyzes the
program text putting an appropriate hierarchical structure on the source
program.
6. What are a Pretty Printer and Static Checker?
A Pretty printer analyses a program and prints it in such a way that
the structure of the program becomes clearly visible.
A static checker reads a program, analyses it and attempts to
discover potential bugs without running the program.
7. How many phases does analysis consists?
Analysis consists of three phases
1 | Page
CAPE/CSE/R2013
i .Linear analysis
ii. Hierarchical analysis
iii. Semantic analysis
8. What happens in linear analysis?
This is the phase in which the stream of characters making up the
source program is read from left to right and grouped in to tokens that are
sequences of characters having collective meaning.
9. What happens in Hierarchical analysis?
This is the phase in which characters or tokens are grouped
hierarchically in to nested collections with collective meaning.
10. What happens in Semantic analysis?
This is the phase in which certain checks are performed to ensure that
the components of a program fit together meaningfully.
11. State some compiler construction tools? April /May 2008
i. Parse generator
ii. Scanner generators
iii. Syntax-directed translation engines
iv. Automatic code generator
v. Data flow engines.
12. What is a Loader? What does the loading process do?
A Loader is a program that performs the two functions
i. Loading
ii .Link editing
The process of loading consists of taking relocatable machine code,
altering the relocatable address and placing the altered instructions and data
in memory at the proper locations.
13. What does the Link Editing does?
Link editing: This allows us to make a single program from several
files of relocatable machine code. These files may have been the result of
several compilations, and one or more may be library files of routines
provided by the system and available to any program that needs them.
14. What is a preprocessor?
A preprocessor is one, which produces input to compilers. A source
program may be divided into modules stored in separate files. The task of
collecting the source program is sometimes entrusted to a distinct program
called a preprocessor.
The preprocessor may also expand macros into source language
statements.
15. State some functions of Preprocessors
i) Macro processing
ii) File inclusion
2 | Page
CAPE/CSE/R2013
Rules:
1) -is a regular expression that denotes {} that is the set containing
the empty string
2) If a is a symbol in , then a is a regular expression that denotes {a}
3) Suppose r and s are regular expressions denoting the languages
L(r ) and L(s) Then,
a) (r )/(s) is a regular expression denoting L(r) U L(s).
b) (r )(s) is a regular expression denoting L(r )L(s)
c) (r )* is a regular expression denoting L(r)*.
d) (r) is a regular expression denoting L(r ).
24. What are the Error-recovery actions in a lexical analyzer?
1. Deleting an extraneous character
2. Inserting a missing character
3. Replacing an incorrect character by a correct character
4. Transposing two adjacent characters
25. Construct Regular expression for the language
L= {w {a,b}/w ends in abb}
Ans: {a/b}*abb.
26. What is recognizer?
Recognizers are machines. These are the machines which accept the
strings belonging to certain language. If the valid strings of such language
are accepted by the machine then it is said that the corresponding language
is accepted by that machine, otherwise it is rejected.
4 | Page
CAPE/CSE/R2013
UNIT-II
LEXICAL ANALYSIS
1.What is Lexical Analysis?
The first phase of compiler is Lexical Analysis. This is also known as
linear analysis in which the stream of characters making up the source
program is read from left-to-right and grouped into tokens that are
sequences of characters having a collective meaning.
2.What is a lexeme? Define a regular set.
A Lexeme is a sequence of characters in the source program
that is matched by
the pattern for a token.
A language denoted by a regular expression is said to be a
regular set
3.What is a sentinel? What is its usage?
A Sentinel is a special character that cannot be part of the source
program. Normally we use eof as the sentinel. This is used for speedingup the lexical analyzer.
4.What is a regular expression? State the rules, which define regular
expression?
Regular expression is a method to describe regular language.
1) If a is a symbol in ,then a is a regular expression that denotes
{a}
2) Suppose r and s are regular expressions denoting the
languages L(r ) and
L(s)
Then,
a) (r )/(s) is a regular expression denoting L(r)U L(s).
b) (r )(s) is a regular expression denoting L(r )L(s)
c) (r )* is a regular expression denoting L(r)*.
d) (r) is a regular expression denoting L(r ).
5. What are the Error-recovery actions in a lexical analyzer?
1. Deleting an extraneous character
2. Inserting a missing character
3. Replacing an incorrect character by a correct character
4. Transposing two adjacent characters
6. Construct Regular expression for the language
L= {w {a,b}/w ends in abb}
Ans: {a/b}*abb.
7. What is recognizer?
5 | Page
CAPE/CSE/R2013
UNIT III
SYNTAX ANALYSIS
1. Define parser.
Hierarchical analysis is one in which the tokens are grouped
hierarchically into nested collections with collective meaning also termed as
Parsing.
2. Mention the basic issues in parsing.
There are two important issues in parsing.
Specification of syntax.
Representation of input after parsing.
3. Why lexical and syntax analyzers are separated out?
Reasons for separating the analysis phase into lexical and syntax
analyzers:
Simpler design.
Compiler efficiency is improved.
Compiler portability is enhanced.
4. Define a context free grammar.
A context free grammar G is a collection of the following
V is a set of non terminals
T is a set of terminals
S is a start symbol
P is a set of production rules
G can be represented as G = (V,T,S,P)
Production rules are given in the following form
Non terminal (V U T)*
7 | Page
CAPE/CSE/R2013
UNIT IV
SYNTAX DIRECTED TRANSLATION & RUN TIME
ENVIRONMENT
1. List the different storage allocation strategies.
The strategies are:
Static allocation
Stack allocation
Heap allocation
2. What are the contents of activation record?
The activation record is a block of memory used for managing the
information needed by a single execution of a procedure. Various fields f
activation record are:
Temporary variables
Local variables
Saved machine registers
10 | P a g e
CAPE/CSE/R2013
Control link
Access link
Actual parameters
Return values
UNIT V
13 | P a g e
CAPE/CSE/R2013
14 | P a g e
CAPE/CSE/R2013
If the current values of y and/or z have no next uses, are not live on
exit from the block, and are in register, alter the register descriptor.
15. Write the labels on nodes in DAG.
A DAG for a basic block is a directed acyclic graph with the following
Labels on nodes:
Leaves are labeled by unique identifiers, either variable names or
constants.
Interior nodes are labeled by an operator symbol.
Nodes are also optionally given a sequence of identifiers for labels.
16. Give the applications of DAG.
Automatically detect the common sub expressions
Determine which identifiers have their values used in the
block.
Determine which statements compute values that could be
used outside the blocks.
17. Define Peephole optimization.
A Statement by statement code generation strategy often produces
target code that contains redundant instructions and suboptimal constructs.
Optimizing is misleading because there is no guarantee that the resulting
code is optimal. It is a method for trying to improve the performance of the
target program by examining the short sequence of target instructions and
replacing this instructions by shorter or faster sequence.
18. Write the characteristics of peephole optimization?
Redundant-instruction elimination
Flow-of-control optimizations.
Algebraic simplifications
Use of machine idioms
19. What are the structure preserving transformations on basic
blocks?
Common sub-expression elimination
Dead-code elimination
Renaming of temporary variables
Interchange of two independent adjacent statement
21. Define Common sub-expression elimination with ex.
It is defined as the process in which eliminate the statements which
has the same expressions. Hence this basic block may be transformed into
the equivalent Block.
Ex:
a : =b + c b
:=a - d
c :=b + c
After elimination:
a : =b + c b
:=a - d
16 | P a g e
CAPE/CSE/R2013
c :=a
22. Define Dead-code elimination with ex.
It is defined as the process in which the statement x=y+z appear in a
basic block, where x is a dead that is never subsequently used. Then this
statement maybe safely removed without changing the value of basic blocks.
23. Define Renaming of temporary variables with ex.
We have the statement u:=b + c ,where u is a new temporary variable,
and change all uses of this instance of t to u, then the value of the basic
block is not changed.
24. Define reduction in strength with ex.
Reduction in strength replaces expensive operations by equivalent
cheaper ones on the target machines. Certain machine instructions are
cheaper than others and can often be used as special cases of more
expensive operators.
Ex:
X^2 is invariably cheaper to implement as x*x than as a call to an
exponentiation routine.
25. Define use of machine idioms.
The target machine may have harder instructions to implement certain
specific operations efficiently. Detecting situations that permit the use of
these instructions can reduce execution time significantly.
26. Define code optimization and optimizing compiler
The term code-optimization refers to techniques a compiler can
employ in an attempt to produce a better object language program than the
most obvious for a given source program. Compilers that apply codeimproving transformations are called Optimizing-compilers.
17 | P a g e
CAPE/CSE/R2013