Compiler Design
Compiler Design
i. Structure editors
iii. Static
iv. checkers
v. Interpreters.
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.
Union - L U M ={s | s is in L or s is in M}
5.Define handle.
A handle of a string is a substring that matches the right side of a production, and whose
reduction to the nonterminal on the left side of the production represents one step along the
reverse of a rightmost derivation.
A handle of a right – sentential form γ is a production A→β and a position of γ where the string
β may be found and replaced by A to produce the previous right-sentential form in a rightmost
derivation of γ. That is , if S =>αAw =>αβw,then A→β in the position following α is a handle of
αβw.
A basic block is a sequence of consecutive statements in which flow of Control enters at the
beginning and leaves at the end without halt or possibility Of branching except at the end.
BASIS FOR
COMPILER INTERPRETER
COMPARISON
code. code.
time.
Pertaining C, C++, C#, Scala, typescript PHP, Perl, Python, Ruby uses
languages
Preprocessors
Assemblers
11.What are the two main parts of compilation? What are they performing?
The analysis part breaks up the source program into constituent pieces and creates an
intermediate representation of the source program.
The synthesis part constructs the desired target program from the intermediate representation.
Character classes ([abc] where a,b,c are alphabet symbols denotes the regular
expressions a | b | c.)
Algebraic simplification
Parser generators
Scanner generators
Data-flow engines
T is a set of terminals
S is a start symbol
P is a set of production rules
A flow graph is a directed graph in which the flow control information is added to the
basic blocks.
The block whose leader is the first statement is called initial block.
There is a directed edge from block B1 to block B2 if B2 immediately follows B1 in the given
sequence. We can say that B1 is a predecessor of B2
Symbol table is a data structure used by the compiler to keep track of semantics of the
variables. It stores information about scope and binding information about names.
Static allocation
Stack allocation
Heap allocation
If w is a sentence of the grammar at hand, then w = γn, where γn is the nth right-
sentential form of some as yet unknown rightmost derivation
S = γ0 => γ1…=> γn-1 => γn = w
A→.XYZ
A→X.YZ
A→XY.Z
A→XYZ.
Disadvantages
1. The operator like minus has two different precedence(unary and binary).Hence it is hard to
handle tokens like minus sign.
| other
The cost of an instruction can be computed as one plus cost associated with the source
and destination addressing modes given by added cost.
MOV R0,R1 1
MOV R1,M 2
SUB 5(R0),*10(R1) 3
The three address statements can be implemented using the following types
Indirect triples : the listing of triples has been done and listing pointers are
used instead of using statements.
Call by value
Call by reference
Copy-restore
Call by name
28.Mention the general phases of a compiler
Lexical Analyzer
Syntax Analyzer
Semantic Analyzer
Intermediate code generator
Code optimizer
Code generator
The role of the semantic analysis is the phase in which certain checks are performed to ensure
that the components of a program fit together meaningfully.
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-toright and grouped into
tokens that are sequences of characters having a collective meaning.
Semantic analysis is one in which certain checks are performed to ensure that components of a
program fit together meaningfully. Mainly performs type checking
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.
33.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.
Preprocessor
Source program
i) Macro processing
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 speeding-up the lexical analyzer
37. What is a regular expression? State the rules,which define regular expression?
Rules:
1) ε-is a regular expression that denotes {ε} that is the set containing the empty string
3) Suppose r and s are regular expressions denoting the languages L(r ) and L(s) Then,
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.
i .Linear analysis
Static allocation is defined as lays out for all data objects at compile time. Names are bound to
storage as a program is compiled, so there is no need for a run time support package.
Stack allocation is defined as process in which manages the run time as a Stack. It is based on
the idea of a control stack; storage is organized as a stack, and activation records are pushed
and popped as activations begin and end.
It is the final phase in compiler model and it takes as an input an intermediate representation of
the source program and output produces as equivalent target programs. Then intermediate
instructions are each translated into a sequence of machine instructions that perform the same
task.
It can be used to change the set of expressions computed by a basic blocks into A algebraically
equivalent sets. The useful ones are those that simplify the Expressions place expensive
operations by cheaper ones.
X = X+ 0
X=X*1
43.What is meant by code optimization?
45.Define Loader
It loads the executable code into memory; program and data stack are created, register
gets initialized.
47.What is interpreter?
The activation record is a block of memory used for managing the information needed by a
single execution of a procedure
Target programs
Memory management
Instruction selection
Register allocation
The leftmost derivation is the one in which you always expand the leftmost non-
terminal.
It is the analysis of flow of data in control flow graph, i.e., the analysis that determines
the information regarding the definition and use of data in program. With the help of this
analysis optimization can be done. In general, its process in which values are computed
using data flow analysis.The data flow property represents information which can be
used for optimization.
Inherited Attribute: An attribute that gets its values from the attributes attached to the
parent (or siblings) of its non-terminal. As you can see from the given
grammar(productions), inh is an inherited attribute.
56.Define Debugger?
As its name suggests the debugger is used to test and debug programs. The debugger
allows a user to test a program step by step, so that the problem points or steps can be
identified and rectified. It allows the user to inspect the registers and
the memory locations after a program has executed.
57.Write a short note on LEX.
These are valid code the compiler understands, but they do not what you, the
programmer, intended.
These may be using the wrong variable, the wrong operation, or operations in the wrong
order. There is no way for the compiler to detect them.
int i;
1. Numerical Representation. Encode true with ’1’ and false with ’0’ and
Code that is unreachable or that does not affect the program (e.g. dead
stores) can be eliminated.
Example:
In the example below, the value assigned to i is never used, and the dead
store can be eliminated. The first assignment to global is dead, and the third
assignment to global is unreachable; both can be eliminated.
int global;
void f ()
{
int i;
i = 1; /* dead store */
global = 1; /* dead store */
global = 2;
return;
global = 3; /* unreachable */
}
Example:
1. S = aSb | SS
2. S = ∈
For the string aabb, the above grammar generates two parse trees:
This representation doesn’t make use of extra temporary variable to represent a single
operation instead when a reference to another triple’s value is needed, a pointer to that
triple is used. So, it consist of only three fields namely op, arg1 and arg2.
It is structure with consist of 4 fields namely op, arg1, arg2 and result. op denotes the
operator and arg1 and arg2 denotes the two operands and result is used to store the
result of the expression.
When a called procedure is executed, it returns the control back to the caller. This type
of control flow makes it easier to represent a series of activations in the form of a tree,
known as the activation tree.
LR parser components: input, stack (strings of grammar symbols and states), driver
routine, parsing tables.
Law Description
r|s=s|r | is commutative
r | (s | t) = (r | s ) | t | is associative
r** = r* * is idempotent