Introdution To Compilers
Introdution To Compilers
Introdution To Compilers
Reference:
Compilers : Principles, Techniques and Tools
Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman
What is a Compiler?
A compiler is a program that can read a program in one
language - the source language - and translate it into an
equivalent program in another language - the target
language
Error Messages
What is a Compiler?
Compilers are classified into
1. Single pass
2. Multipass
3. Just-in-time
4. Debugging
5. Optimizing
A language-processing system
What is a Compiler?
Compilation has 2 parts
1. Analysis
2. Synthesis
Analysis Phase
Synthesis Phase
Phases of a Compiler
1. Lexical Analysis (scanning)
Token are :
Phases of a Compiler
1. Lexical Analysis (scanning)
int a[20];
A[2.4] = 12 is a semantic error
Phases of a Compiler
3. Semantic Analysis
Intermediate Code
An example of
3 –address code
Phases of a Compiler
5. Code Optimization
Optimized code
Phases of a Compiler
6. Code Generation
The code generator takes as input an intermediate
representation of the source program and maps it into
the target language.
Target code may be relocatable machine code or
assembly code
Phases of a Compiler
Symbol-Table Management
A symbol table is a data structure containing a record
for each variable name, with fields for the attributes
of the name
A compiler is assisted by
1. Preprocessors
2. Assemblers
3. linkers/loaders
Cousins of the compilers
A language-processing system
Cousins of the compilers
1. Preprocessors
Processors produce input to compilers. Its tasks are
1. Macro processing
• e..g In C #define PI 3.14
2. File Inclusions
• E.g. In C #include <global.h>
3. Rational Preprocessors
4. Language extensions
Cousins of the compilers
Preprocessors
Rational Preprocessors
- Augment older languages with more modern
flow-of-control and data-structuring facilities.
Language extensions
- These processors attempt to add capabilities to
the language by what amounts to built-in macros
The Grouping of Phases into Passes
Phases of a compiler are collected into front end and back end
Front end
Back end
The Grouping of Phases into Passes
Example
One Pass
The Grouping of Phases into Passes
optional
1. Scanner generators
2. Parser generators
3. Syntax directed translation engines
4. Automatic code generators
5. Data flow engines
Compiler-Construction Tools
1.Scanner generators
• produce lexical analyzers from a regular-expression
description of the tokens of a language
E.g. : LEX
Compiler-Construction Tools
2. Parser generators
• Automatically produce syntax analyzers from a
grammatical description of a programming language
E.g. : YACC.
Compiler-Construction Tools
3. Syntax directed translation engines
• produce collections of routines for walking a parse
tree and generating intermediate code.