section c
section c
compiler working
Compiler Phases
The compilation process contains the sequence of various phases. Each
phase takes source program in one representation and produces output in
another representation. Each phase takes input from its previous stage.
Lexical Analysis:
Lexical analyzer phase is the first phase of compilation process. It takes
source code as input. It reads the source program one character at a time
and converts it into meaningful lexemes. Lexical analyzer represents these
lexemes in the form of tokens.
Syntax Analysis
Syntax analysis is the second phase of compilation process. It takes tokens
as input and generates a parse tree as output. In syntax analysis phase, the
parser checks that the expression made by the tokens is syntactically correct
or not.
Semantic Analysis
Semantic analysis is the third phase of compilation process. It checks
whether the parse tree follows the rules of language. Semantic analyzer
keeps track of identifiers, their types and expressions. The output of
semantic analysis phase is the annotated tree syntax.
Code Optimization
Code optimization is an optional phase. It is used to improve the
intermediate code so that the output of the program could run faster and
take less space. It removes the unnecessary lines of the code and arranges
the sequence of statements in order to speed up the program execution.
Code Generation
Code generation is the final stage of the compilation process. It takes the
optimized intermediate code as input and maps it to the target machine
language. Code generator translates the intermediate code into the machine
code of the specified computer.
Lexical Analysis:
A lexical analyzer is also called a "Scanner". Given the code's statement/
input string, it reads the statement from left to right character-wise. The
input to a lexical analyzer is the pure high-level code from the preprocessor.
It identifies valid lexemes from the program and returns tokens to the syntax
analyzer, one after the other, corresponding to
the getNextToken command from the syntax analyzer.
**Advantages:**
Parser
Parser is a compiler that is used to break the data into smaller elements
coming from lexical analysis phase.
A parser takes input in the form of sequence of tokens and produces output
in the form of parse tree.
4. **Caching:** The compiler may cache intermediate results, such as object files
or compiled code, to reuse them in subsequent compilations. This reduces
redundant work and speeds up the compilation process, especially for frequently
used libraries or modules.
Advantages:
Disadvantages:
Cross Compiler
**Advantages:**
**Disadvantages:**