Phases of compiler (10 marks)
A compiler is a software tool that converts high-level programming code into machine code that a computer can understand and execute.
The whole compilation process is divided into two parts, front-end and back-end.
Broadly, the compilation process can be divided into two main parts:
1. Analysis Phase: The analysis phase breaks the source program into its basic components and creates an intermediate representation of the
program. It is sometimes referred to as front end.
2. Synthesis Phase: The synthesis phase creates the final target program from the intermediate representation. It is sometimes referred to as
back end.
Phases of a Compiler
1. Lexical Analysis
Lexical analysis is the first phase of a compiler, responsible for converting the raw source code into a
sequence of tokens.
A token is the smallest unit of meaningful data in a programming language. Lexical analysis involves
scanning the source code, recognizing patterns, and categorizing groups of characters into distinct tokens
These tokens can represent keywords, identifiers, constants, operators, or punctuation marks.
By converting the source code into tokens, lexical analysis simplifies the process of understanding and
processing the code in later stages of compilation.
2. Syntax Analysis
Syntax analysis, also known as parsing, is the second phase of a compiler where the structure of the
source code is checked.
This phase ensures that the code follows the correct grammatical rules of the programming language.
It checks whether the code adheres to the language’s rules, such as correct use of operators, keywords,
and parentheses.
To represent the structure of the source code, syntax analysis uses parse trees or syntax trees.
3. Semantic Analysis
Semantic analysis is the phase of the compiler that ensures the source code makes sense logically.
checks whether the program has any semantic errors, such as type mismatches or undeclared variables.
PASS
A pass reads or scans the instructions of the source program or the output produced by the previous pass, which makes necessary transformation
specified by its phases.
A Compiler pass refers to the traversal of a compiler through the entire program
There are generally two types of passes
1. One-pass
2. Two-pass
1. One-Pass – In One-pass all the phases are grouped into one phase. The six phases are
included here in one pass.
known as a single pass compiler.
A one-pass/single-pass compiler is a type of compiler that passes through the part of each compilation unit exactly once.
Single pass compiler is faster and smaller than the multi-pass compiler.
2. Two-Pass – In Two-pass the phases are divided into two parts i.e. Analysis or Front End
part of the compiler and the synthesis part or back end part of the compiler.
First Pass is referred as
Front end
Analytic part
Platform independent
Second Pass is referred as
Back end
Synthesis Part
Platform Dependent
What is Cross Compiler?
Compilers are the tool used to translate high-level programming language to low-level programming language. The simple compiler works in one system
only, but what will happen if we need a compiler that can compile code from another platform, to perform such compilation, the cross compiler is
introduced. In this article, we are going to discuss cross-compiler.
A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. For example, a
cross compiler executes on machine X and produces machine code for machine Y.
A compiler is characterized by three languages:
1. The source language that is compiled.
2. The target language T is generated.
3. The implementation language that is used for writing the compiler.
Bootstrapping is an important technique in compiler design, where a basic compiler is used to create a
more advanced version of itself. This process helps in building compilers for new programming languages and improving the ones already in use. By
starting with a simple compiler, bootstrapping allows gradual improvements and makes the compiler more efficient over time.
Bootstrapping relies on the idea of a self-compiling compiler, where each iteration improves the compiler’s ability to handle more complex code.
It simplifies the development cycle, allowing incremental improvements and faster deployment of more robust compilers.
Many successful programming languages, including C and Java, have used bootstrapping techniques during their development
Cross-Compilation Using Bootstrapping
Using bootstrapping in cross-compilation can help create a compiler that runs on one system (the host) but produces code for another system (the target).