Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CD 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

CD-1

Introduction to Compiler Design:

CD-1 1
comparison between compilers and interpreters
Feature Compiler Interpreter

CD-1 2
Translation Translates source code line by
Translates the entire source code
Process line

before execution. during runtime.

Execution Executes compiled code. Executes code directly.

Speed Generally faster execution. May have slower execution.

Often involves a separate


Debugging Provides detailed error messages
debugging

process. during runtime.

Generally consumes less


Memory Usage May take up more memory.
memory.

Portability Results in machine-specific code, Same source code can run on

requires recompilation for any platform with the


different corresponding

platforms. interpreter.

Examples GCC, Visual C++, Clang. Python, Ruby, JavaScript (in

web browsers).

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:


Fig: phases of compiler

CD-1 3
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

CD-1 4
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.

CD-1 5
CD-1 6
The Science Of Building Compiler
A compiler needs to handle any valid program written in a given programming
language, and since the range of possible programs is infinite, they can be very
large, even reaching millions of lines of code. When a compiler translates a
program, any changes it makes must not alter the intended meaning of the original
program. This means compiler writers hold significant influence, not only over the
compilers they design but also over every program that gets processed by their
compilers.

Modelling in compiler design and implementation


The study of compilers is mainly a study of how we design the right mathe-
matical models
and choose the right algorithms.
Some of most fundamental models are finite-state machines and regular
expressions,
These models are useful for describing the lexical units of programs (keywords,
identifiers)
and for describing the algorithms used by the compiler to recognize those units.

context-free grammars are used to describe the syntactic structure of


programming
languages such as the nesting of parentheses or control constructs.
Trees are an important model for representing the structure of programs and their
translation into object code.

The Science of Code Optimization

To produce shorter and faster code.


Graphs, matrices, and linear programs are necessary.
The optimization must be correct, that is, the optimized program must give the
same result as
the original program.

CD-1 7
PROGRAMMING LANGUAGE BASICS IN
COMPILER DESIGN

Environments and States


1. The environment is a mapping from variable names to locations in the store.
int

x; / variable name x refers to a memory location

2. The state is a mapping from locations in store to their values. That is, the state
maps l-values to their corresponding r-values, in the terminology of C.
x=10;

// l-value is x, r-value is 10

CD-1 8
CD-1 9
Call by value
is a parameter-passing mechanism where a function receives a copy of the actual
parameter's value, and changes made inside the function do not affect the original
variable outside it.

Call by reference
is a parameter-passing mechanism where a function receives the memory
address of the actual parameter, allowing it to directly modify the original variable
outside the function.

CD-1 10
LEXICAL ANALYSIS

The Role Of Lexical Analyzer


As the first phase of a compiler, the main task of the lexical analyzer is to read
the input characters of the source program, group them into lexemes, and
produce as
output a sequence of tokens for each lexeme in the source program.

The stream of tokens is sent to the parser for syntax analysis.

When the lexical analyzer discovers a lexeme constituting an identifier, it


needs to enter that lexeme along with its attributes into the symbol table.

CD-1 11
CD-1 12
CD-1 13
CD-1 14
CD-1 15
CD-1 16
CD-1 17
CD-1 18
CD-1 19
CD-1 20
CD-1 21

You might also like