Professional Documents
Culture Documents
Q1.Explain Compiler and Its Phases: Single-Pass Compilers Multi-Pass Compilers
Q1.Explain Compiler and Its Phases: Single-Pass Compilers Multi-Pass Compilers
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.
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.