0% found this document useful (0 votes)
57 views20 pages

Compiler Construction Chapter1

The document discusses various aspects of compiler construction including: - A compiler converts code written in a high-level language into machine language that can be understood by a processor. It performs tasks like error checking and code optimization without changing the program's meaning. - Compilation involves two main phases - analysis and synthesis. Analysis checks for errors while synthesis generates the target code. Compilers can have multiple passes where it traverses the source code. - The compilation process consists of phases like lexical analysis, syntax analysis, semantic analysis, code generation, and optimization. Multi-pass compilers make multiple passes while one-pass compilers traverse the code only once.

Uploaded by

Rabia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views20 pages

Compiler Construction Chapter1

The document discusses various aspects of compiler construction including: - A compiler converts code written in a high-level language into machine language that can be understood by a processor. It performs tasks like error checking and code optimization without changing the program's meaning. - Compilation involves two main phases - analysis and synthesis. Analysis checks for errors while synthesis generates the target code. Compilers can have multiple passes where it traverses the source code. - The compilation process consists of phases like lexical analysis, syntax analysis, semantic analysis, code generation, and optimization. Multi-pass compilers make multiple passes while one-pass compilers traverse the code only once.

Uploaded by

Rabia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Compiler Construction

• Compiler is a translator that converts the high-level


language into the machine language.
• High-level language is written by a developer and
machine language can be understood by the processor.
• Compiler is used to show errors to the programmer.
• The main purpose of compiler is to change the code
written in one language without changing the meaning
of the program.
• When you execute a program which is written in HLL
programming language then it executes into two parts.
• In the first part, the source program compiled and
translated into the object program (low level language).
• In the second part, object program translated into the
target program through the assembler.
Execution process of source program in Compiler
Language Processing System:
• computer system is made of hardware and
software.
• The hardware understands a language, which
humans cannot understand.
• So we write programs in high-level language,
which is easier for us to understand and
remember.
• These programs are then fed into a series of tools
and OS components to get the desired code that
can be used by the machine. This is known as
Language Processing System.
Let us first understand how a program, using C compiler, is executed on a
host machine.
• User writes a program in C language (high-level language).
• The C compiler, compiles the program and translates it to assembly
program (low-level language).
• An assembler then translates the assembly program into machine code
(object).
• A linker tool is used to link all the parts of the program together for
execution (executable machine code).
• A loader loads all of them into memory and then the program is executed.
A compiler can broadly be divided into two
phases based on the way they compile.

Analysis Phase
• Known as the front-end of the compiler,
the analysis phase of the compiler reads the source
program, divides it into core parts and then checks for
lexical, grammar and syntax errors.
• The analysis phase generates an intermediate
representation of the source program and symbol table,
which should be fed to the Synthesis phase as input.
Synthesis Phase:
• Known as the back-end of the compiler,
the synthesis phase generates the target program
with the help of intermediate source code
representation and symbol table.

A compiler can have many phases and passes.


• Pass : A pass refers to the traversal of a compiler
through the entire program.
• Phase : A phase of a compiler is a distinguishable
stage, which takes input from the previous stage,
processes and yields output that can be used as
input for the next stage. A pass can have more
than one phase.
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.
• There are the various phases of compiler:
Y=2*x;
Y=x+x;
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.
Intermediate Code Generation
• In the intermediate code generation, compiler generates the source
code into the intermediate code. Intermediate code is generated
between the high-level language and the machine language. The
intermediate code should be generated in such a way that you can
easily translate it into the target machine code.
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.
Example:
Compiler Passes
Pass is a complete traversal of the source program. Compiler has two
passes to traverse the source program.
Multi-pass Compiler
• Multi pass compiler is used to process the source code of a program
several times.
• In the first pass, compiler can read the source program, scan it,
extract the tokens and store the result in an output file.
• In the second pass, compiler can read the output file produced by
first pass, build the syntactic tree and perform the syntactical
analysis. The output of this phase is a file that contains the
syntactical tree.
• In the third pass, compiler can read the output file produced by
second pass and check that the tree follows the rules of language or
not. The output of semantic analysis phase is the annotated tree
syntax.
• This pass is going on, until the target output is produced.
One-pass Compiler
• One-pass compiler is used to traverse the
program only once. The one-pass compiler passes
only once through the parts of each compilation
unit. It translates each part into its final machine
code.
• In the one pass compiler, when the line source is
processed, it is scanned and the token is
extracted.
• Then the syntax of each line is analyzed and the
tree structure is build. After the semantic part,
the code is generated.
• The same process is repeated for each line of
code until the entire program is compiled.
Bootstrapping
• Bootstrapping is widely used in the compilation
development.
• Bootstrapping is used to produce a self-hosting
compiler. Self-hosting compiler is a type of compiler
that can compile its own source code.
• Bootstrap compiler is used to compile the compiler and
then you can use this compiled compiler to compile
everything else as well as future versions of itself.
• A compiler can be characterized by three languages:
• Source Language
• Target Language
• Implementation Language
The T- diagram shows a compiler SCIT for
Source S, Target T, implemented in I.

Follow some steps to produce a new language L for machine A:

1. Create a compiler SCAA for subset, S of the desired language, L using language "A"
and that compiler runs on machine A.

2. Create a compiler LCSA for language L written in a subset of L.


• 3. Compile LCSA using the compiler SCAA to
obtain LCAA. LCAA is a compiler for language L,
which runs on machine A and produces code
for machine A.

The process described by the T-diagrams is called bootstrapping.

You might also like