Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Q1.

Explain compiler and its phases


Ans: A compiler is software that translates the code written in one programming language to
another. Ex, g++ from GNU family of compilers, PowerBASIC, etc. There are 6 phases in the compiler,
namely, lexical analysis, syntax analysis, semantic analysis, intermediate code generator, code
optimization and target code generator.

Lexical Analysis: The first phase of a compiler is lexical analysis, also known as scanning. This phase
reads the source code and breaks it into a stream of tokens, which are the basic units of the
programming language. The tokens are then passed on to the next phase for further processing.

Syntax Analysis: The second phase of a compiler is syntax analysis, also known as parsing. This phase
takes the stream of tokens generated by the lexical analysis phase and checks whether they conform
to the grammar of the programming language. The output of this phase is usually an Abstract Syntax
Tree (AST).

Semantic Analysis: The third phase of a compiler is semantic analysis. This phase checks whether the
code is semantically correct, i.e., whether it conforms to the language’s type system and other
semantic rules.

Intermediate Code Generation: The fourth phase of a compiler is intermediate code generation. This
phase generates an intermediate representation of the source code that can be easily translated into
machine code.

Optimization: The fifth phase of a compiler is optimization. This phase applies various optimization
techniques to the intermediate code to improve the performance of the generated machine code.

Code Generation: The final phase of a compiler is code generation. This phase takes the optimized
intermediate code and generates the actual machine code that can be executed by the target
hardware.

Q.2 What is phases and pass of a compiler?


Ans: phases are the steps in the compilation process

A pass refers to the number of times the compiler goes through the source code. There
are single-pass compilers and multi-pass compilers. Single-pass compiler goes through the
program only once. In other words, the single pass compiler allows the source code to pass
through each compilation unit only once. It immediately translates each code section into its
final machine code.
Multi-pass compiler goes through the source code several times. In other words, it allows
the source code to pass through each compilation unit several times. Each pass takes the
result of the previous pass as input and creates intermediate outputs. Therefore, the code
improves in each pass. The final code is generated after the final pass. Multi-pass compilers
perform additional tasks such as intermediate code generation, machine dependent code
optimization, and machine independent code optimization.
Q.3 Define L attribute and S attribute
Ans: L attribute:
The idea is that between attributes associated with a production body, the edges of
a dependency graph can go from right to left but not the other way round(left to
right), hence the name 'L-attributed'
In other words, each attribute must either be;

1. Synthesized, or,

2. Inherited but with limited rules, i.e Suppose there is a production A → X1X2...Xn and
an inherited attribute Xi.a computed by a rule associated with this production, then
the rule only uses;
** inherited attributes that are associated with head A.
** Either inherited attribute or synthesized attributes associated with the
occurrences of symbols X1,X2, ..., Xi-1 located to the left of Xi.
** Inherited or synthesized attributes that are associated with such an occurrence
of Xi itself, only in such a way that no cycles exist in the dependency graph formed
by Xi attributes.

S-attribute:
An SDD is S-attributed if every attribute is synthesized.
If an SDD is S-attributed, we evaluate its attributes in any bottom-up ordering of the
parse tree nodes.
It is simpler to perform a post-order tree traversal and evaluate the attributes at a
node N when the traversal leaves N for the last time.

Q.4 Explain various data structures in symbol table


Ans: Symbol table operations include inserting, searching, and deleting symbols. Data
structures used for symbol tables include hash tables, binary search trees, and linked lists.
The choice of data structure depends on the requirements of the application and the
characteristics of the symbols being stored.
Data Structures used for the implementation of symbol tables are-
1.Binary Search Tree
2.Hash Tables
1. Hash Table – 
In hashing scheme, two tables are maintained – a hash table and symbol table and are the
most commonly used method to implement symbol tables.
A hash table is an array with an index range: 0 to table size – 1. These entries are pointers
pointing to the names of the symbol table.
To search for a name we use a hash function that will result in an integer between 0 to table
size – 1.
Insertion and lookup can be made very fast – O(1).
The advantage is quick to search is possible and the disadvantage is that hashing is
complicated to implement.
Binary Search Tree – 
Another approach to implementing a symbol table is to use a binary search tree i.e. we add
two link fields i.e. left and right child.
All names are created as child of the root node that always follows the property of the
binary search tree.
Insertion and lookup are O(log2 n) on average.

Q5. Explain Directed acyclic graph


Ans: The Directed Acyclic Graph (DAG) is used to represent the structure of basic blocks, to
visualize the flow of values between basic blocks, and to provide optimization techniques in
the basic block. To apply an optimization technique to a basic block, a DAG is a three-address
code that is generated as the result of an intermediate code generation.
.Directed acyclic graphs are a type of data structure and they are used to apply
transformations to basic blocks.
.The Directed Acyclic Graph (DAG) facilitates the transformation of basic blocks.
.DAG is an efficient method for identifying common sub-expressions.
.It demonstrates how the statement’s computed value is used in subsequent statements.
Application of Directed Acyclic Graph:
Directed acyclic graph determines the subexpressions that are commonly used.
Directed acyclic graph determines the names used within the block as well as the names
computed outside the block.
Determines which statements in the block may have their computed value outside the block.

You might also like