CD 1
CD 1
CD 1
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
platforms. interpreter.
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.
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.
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.
CD-1 7
PROGRAMMING LANGUAGE BASICS IN
COMPILER DESIGN
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
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