Compiler Design Assignment
Compiler Design Assignment
RESEARCH
Name: Abhijeet Kumar Pandey Subject:
Compiler Design
Branch: Computer Science & Engg.
Year/Sem:2010/VII
Q1. Explain why we should study about compiler. What is Compiler & its
various phases with diagram & by taking a example of a= b*cd.
Ans: Reasons for Studying Compilers:
• An essential programming tool
• Improves software productivity by hiding low-level details
• A tool for designing and evaluating computer architectures
• Inspired RISC, VLIW machines
• Machines’ performance measured on compiled code
• Techniques for developing other programming tools Examples: error
detection tools
• Little languages and program translations can be used to solve
other problems
Phases of Compiler:
Lexical Analyzer: The lexical analysis stage transforms a sequence of
characters to a sequence of lexical elements. These lexical entities
correspond principally to integers, floating point numbers, characters,
strings of characters and identifiers. The message Illegal
character might be generated by this analysis.
Given Expression is
a=b*c
Laxical Analyzer
id1=id2*id3;
Syntax Analyzer
Semantic Analyzer
Temp1=id3; temp2=id2;
Temp3=temp2*temp1; id1=temp3;
code optimizer
Temp1=id2*id3;
Id1=temp1;
Code generator
MOVE ID2,AX
MOVE ID3,BX
MUL AX,BX
MOV AX,ID1
Q2. What is parsing? How many types of techniques used in the parsing
explain with diagram?
Ans: Parsing is the process of analyzing a text, made of a sequence
of tokens (for example, words), to determine its grammatical structure
with respect to a given (more or less) formal grammar. Parsing can
also be used as a linguistic term, especially in reference to how
phrases are divided up in garden path sentences.
The basic connection between a sentence and the grammar it derives
from is the parse
tree, which describes how the grammar was used to produce the
sentence.
There are only two techniques to do parsing.
The first method tries to imitate the original production process by
rederiving the sentence from the start symbol. This method is called top-
down, because the production tree is reconstructed from the top
downwards.
The second methods tries to roll back the production process and to
reduce the
sentence back to the start symbol. Quite naturally this technique is
called bottom-up.
Top-down parsing:
grammar for the language anb nc n
SS -> aSQ
S -> abc
bQc -> bbcc
cQ -> Qc
and suppose the (input) sentence is aabbcc.
Bottom-up parsing:
A bottom up parser is trying to go backwards, performing the following
reverse derivation sequence:
ax → Ax → S
Intuitively, a top-down parser tries to expand nonterminals into right-
hand-sides and a bottom-up parser tries to replace (reduce) right-hand-sides
with nonterminals. The first action of the bottom-up parser would be to
replace a with A yielding Ax. Then it would replace Ax with S. Once it
arrives at a sentential form with exactly S, it has reached the goal and
stops, indicating success.
Q3. What do you understand by error recovery & error handling in LL & LR
parsing also explains its types.
Ans: Error Recovery in Predictive Parsing: An error is detected during
predictive parsing when the terminal on top of the stack does not match
the next input symbol or when nonterminal A is on top of the stack, a is
the next input symbol, and M[A, a] is error (i.e., the parsing-table entry is
empty).
Panic Mode
Panic-mode error recovery is based on the idea of skipping symbols on
the the input until a token in a selected set of synchronizing tokens
appears. Its effectiveness depends on the choice of synchronizing
set. The sets should be chosen so that the parser recovers quickly
from errors that are likely to occur in practice.
Phrase-level Recovery
Phrase-level error recovery is implemented by filling in the blank
entries in the predictive parsing table with pointers to error
routines. These routines may change, insert, or delete symbols on the input
and issue appropriate error messages. They may also pop from the stack.
Alteration of stack symbols or the pushing of new symbols onto the
stack is questionable for several reasons. First, the steps carried out by
the parser might then not correspond to the derivation of any word in the
language at all. Second, we must ensure that there is no possibility of an
infinite loop. Checking that any recovery action eventually results in an
input symbol being consumed (or the stack being shortened if the end
of the input has been reached) is a good way to protect against such loops.
Q6. Translate the expression into Quadruples, Triples and Indirect Triples.
-(a+b)*(c+d)-(a+b+c)
Ans:
Quadruples:
Triples:
Indirect Triples:
Pointers to triples
(0) (14)
(1) (15)
(2) (16)
(3) (17)
(4) (18)
(5) (19)
Q7. What is activation record and what are its contents with diagram.
Ans: Procedure calls and returns are usually managed by a run-time stack
called the control stack. Each live activation has an activation record
(sometimes called a frame) on the control stack, with the root of the
activation tree at the bottom, and the entire sequence of activation
records on the stack corresponding to the path in the activation tree to
the activation where control currently resides. The latter activation has
its record at the top of the stack. The contents of activation records
vary with the language being implemented. Here is a list of the kinds
of data that might appear in an activation record
In above figure (a) represent the simple list and (b) represent self organzing
list in which Id1 is related to Id2
and Id3 is related to Id1.
Hash table:
A hash table, or a hash map, is a data structure that associates keys with
values ‘Open hashing’ is a key that
is applied to hash table. In hashing –open, there is a property that no limit on
number of entries that can be
made in table. Hash table consist an array ‘HESH’ and several buckets
attached to array HESH according to
hash function.
Search Tree:
Another approach to organize symbol table is that we add two link fields i.e.
left and right child, we use these
field as binary search tree. All names are created as child of root node that
always follow the property of binary
tree i.e. name <name ie and Namej <name. These two statements show that
all smaller name than Namei must be
left child of name otherwise right child of namej. For inserting any name it
always follow binary search tree
insert algorithm.
Q10. What is DAG? How basic blocks are represented through DAG.What are
its Advantages and Applications.
Ans: In mathematics and computer science, a directed acyclic
graph (commonly abbreviated to DAG), is a directed graph with
no directed cycles. That is, it is formed by a collection
of vertices and directed edges, each edge connecting one vertex to
another, such that there is no way to start at some vertex v and follow
a sequence of edges that eventually loops back to v again.
The DAG Representation of Basic Blocks
Directed acyclic graphs (DAGs) give a picture of how the value
computed by each statement in the basic block is used in the subsequent
statements of the block.
Definition: a dag for a basic block is a directed acyclic graph with the
following labels on nodes:
t1:= 4*i
t2:= a[t1]
t3:= 4*i
t4:= b[t3]
t5:= t2 * t4
t6:= prod + t5
prod:= t6
t7:= i + 1
i:= t7
if i <= 20 goto 1
Three address code
+
t5
*
prod t4 (1)
[] t2 [] <=
t1, t3
* + t7, i
a b 20
4 i0 1
An object file will contain a symbol table of the identifiers it contains that are
externally visible. During the linking of different object files, a linker will use
these symbol tables to resolve any unresolved references.
A symbol table may only exist during the translation process, or it may be
embedded in the output of that process for later exploitation, for example,
during an interactive debugging session, or as a resource for formatting a
diagnostic report during or after execution of a program.
While reverse engineering an executable a lot of tools refer the symbol table
to check what addresses have been assigned to global variables and known
functions. If the symbol table has been stripped or cleaned out before
converting it into an executable tools will find it hard to find out addresses
and understand anything about the program.