SlideShare a Scribd company logo
1 of 113
1.1 Compilers: 
• A compiler is a program that reads a program 
written in one language –– the source 
language –– and translates it into an 
equivalent program in another language –– 
the target language 
1
1.1 Compilers: 
• As an important part of this translation 
process, the compiler reports to its user the 
presence of errors in the source program. 
2
1.1 Compilers: 
3
1.1 Compilers: 
• At first glance, the variety of compilers may 
appear overwhelming. 
• There are thousands of source languages, 
ranging from traditional programming 
languages such as FORTRAN and Pascal to 
specialized languages. 
4
1.1 Compilers: 
• Target languages are equally as varied; 
• A target language may be another 
programming language, or the machine 
language of any computer. 
5
1.1 Compilers: 
• Compilers are sometimes classified as: 
– single-pass 
– multi-pass 
– load-and-go 
– Debugging 
– optimizing 
6
1.1 Compilers: 
• The basic tasks that any compiler must 
perform are essentially the same. 
• By understanding these tasks, we can 
construct compilers for a wide variety of 
source languages and target machines using 
the same basic techniques. 
7
1.1 Compilers: 
• Throughout the 1950’s, compilers were 
considered notoriously difficult programs to 
write. 
• The first FORTRAN compiler, for example, took 
18 staff-years to implement. 
8
The Analysis-Synthesis Model of 
Compilation: 
• There are two parts of compilation: 
– Analysis 
– Synthesis 
9
The Analysis-Synthesis Model of 
Compilation: 
• The analysis part breaks up the source 
program into constituent pieces 
• creates an intermediate representation of the 
source program. 
10
The Analysis-Synthesis Model of 
Compilation: 
• The synthesis part constructs the desired 
target program from the intermediate 
representation. 
11
The Analysis-Synthesis Model of 
Compilation: 
Front 
End 
Back 
End 
source 
code 
IR machine 
code 
errors 
12
The Analysis-Synthesis Model of 
Compilation: 
• During analysis, the operations implied by the 
source program are determined and recorded 
in a hierarchical structure called a tree. 
• Often, a special kind of tree called a syntax 
tree is used. 
13
The Analysis-Synthesis Model of 
Compilation: 
• In syntax tree each node represents an 
operation and the children of the node 
represent the arguments of the operation. 
• For example, a syntax tree of an assignment 
statement is shown below. 
14
The Analysis-Synthesis Model of 
Compilation: 
15
Analysis of the Source Program: 
• In compiling, analysis consists of three phases: 
– Linear Analysis: 
– Hierarchical Analysis: 
– Semantic Analysis: 
16
Analysis of the Source Program: 
• Linear Analysis: 
– In which the stream of characters making up the 
source program is read from left-to-right and 
grouped into tokens that are sequences of 
characters having a collective meaning. 
17
Scanning or Lexical Analysis (Linear 
Analysis): 
• In a compiler, linear analysis is called lexical 
analysis or scanning. 
• For example, in lexical analysis the characters in 
the assignment statement 
• Position: = initial + rate * 60 
• Would be grouped into the following tokens: 
18
Scanning or Lexical Analysis (Linear 
Analysis): 
• The identifier, position. 
• The assignment symbol := 
• The identifier initial. 
• The plus sign. 
• The identifier rate. 
• The multiplication sign. 
• The number 60. 
19
Scanning or Lexical Analysis (Linear 
Analysis): 
• The blanks separating the characters of these 
tokens would normally be eliminated during 
the lexical analysis. 
20
Analysis of the Source Program: 
• Hierarchical Analysis: 
– In which characters or tokens are grouped 
hierarchically into nested collections with 
collective meaning. 
21
Syntax Analysis or Hierarchical 
Analysis (Parsing): 
• Hierarchical analysis is called parsing or syntax 
analysis. 
• It involves grouping the tokens of the source 
program into grammatical phases that are 
used by the compiler to synthesize output. 
22
Syntax Analysis or Hierarchical 
Analysis (Parsing): 
• The grammatical phrases of the source 
program are represented by a parse tree. 
23
Syntax Analysis or Hierarchical 
Analysis (Parsing): 
Assignment 
Statement 
:= 
Identifier 
Position 
Expression 
+ 
Expression 
Identifier 
Initial 
Expression 
* 
Expression 
Identifier 
Rate 
Expression 
Number 
60 
24
Syntax Analysis or Hierarchical 
Analysis (Parsing): 
• In the expression initial + rate * 60, the phrase 
rate * 60 is a logical unit because the usual 
conventions of arithmetic expressions tell us 
that the multiplication is performed before 
addition. 
• Because the expression initial + rate is 
followed by a *, it is not grouped into a single 
phrase by itself 
25
Syntax Analysis or Hierarchical 
Analysis (Parsing): 
• The hierarchical structure of a program is 
usually expressed by recursive rules. 
• For example, we might have the following 
rules, as part of the definition of expression: 
26
Syntax Analysis or Hierarchical 
Analysis (Parsing): 
• Any identifier is an expression. 
• Any number is an expression 
• If expression1 and expression2 are expressions, 
then so are 
– Expression1 + expression2 
– Expression1 * expression2 
– (Expression1 ) 
27
Analysis of the Source Program: 
• Semantic Analysis: 
– In which certain checks are performed to ensure 
that the components of a program fit together 
meaningfully. 
28
Semantic Analysis: 
• The semantic analysis phase checks the source 
program for semantic errors and gathers type 
information for the subsequent code-generation 
phase. 
29
Semantic Analysis: 
• It uses the hierarchical structure determined 
by the syntax-analysis phase to identify the 
operators and operand of expressions and 
statements. 
30
Semantic Analysis: 
• An important component of semantic analysis 
is type checking. 
• Here are the compiler checks that each 
operator has operands that are permitted by 
the source language specification. 
31
Semantic Analysis: 
• For example, when a binary arithmetic 
operator is applied to an integer and real. In 
this case, the compiler may need to be 
converting the integer to a real. As shown in 
figure given below 
32
Semantic Analysis: 
33
1.3 The Phases of a Compiler: 
• A compiler operates in phases. 
• Each of which transforms the source program 
from one representation to another. 
• A typical decomposition of a compiler is 
shown in fig given below 
34
1.3 The Phases of a Compiler: 
35
1.3 The Phases of a Compiler: 
• Linear Analysis: 
– In which the stream of characters making up the 
source program is read from left-to-right and 
grouped into tokens that are sequences of 
characters having a collective meaning. 
36
1.3 The Phases of a Compiler: 
• In a compiler, linear analysis is called lexical 
analysis or scanning. 
• For example, in lexical analysis the characters in 
the assignment statement 
• Position: = initial + rate * 60 
• Would be grouped into the following tokens: 
37
1.3 The Phases of a Compiler: 
• The identifier, position. 
• The assignment symbol := 
• The identifier initial. 
• The plus sign. 
• The identifier rate. 
• The multiplication sign. 
• The number 60. 
38
1.3 The Phases of a Compiler: 
• The blanks separating the characters of these 
tokens would normally be eliminated during 
the lexical analysis. 
39
1.3 The Phases of a Compiler: 
• Hierarchical Analysis: 
– In which characters or tokens are grouped 
hierarchically into nested collections with 
collective meaning. 
40
1.3 The Phases of a Compiler: 
• Hierarchical analysis is called parsing or syntax 
analysis. 
• It involves grouping the tokens of the source 
program into grammatical phases that are 
used by the compiler to synthesize output. 
41
1.3 The Phases of a Compiler: 
• The grammatical phrases of the source 
program are represented by a parse tree. 
42
1.3 The Phases of a Compiler: 
Assignment 
Statement 
:= 
Identifier 
Position 
Expression 
+ 
Expression 
Identifier 
Initial 
Expression 
* 
Expression 
Identifier 
Rate 
Expression 
Number 
60 
43
1.3 The Phases of a Compiler: 
• In the expression initial + rate * 60, the phrase 
rate * 60 is a logical unit because the usual 
conventions of arithmetic expressions tell us 
that the multiplication is performed before 
addition. 
• Because the expression initial + rate is 
followed by a *, it is not grouped into a single 
phrase by itself 
44
1.3 The Phases of a Compiler: 
• The hierarchical structure of a program is 
usually expressed by recursive rules. 
• For example, we might have the following 
rules, as part of the definition of expression: 
45
1.3 The Phases of a Compiler: 
• Any identifier is an expression. 
• Any number is an expression 
• If expression1 and expression2 are expressions, 
then so are 
– Expression1 + expression2 
– Expression1 * expression2 
– (Expression1 ) 
46
1.3 The Phases of a Compiler: 
• Semantic Analysis: 
– In which certain checks are performed to ensure 
that the components of a program fit together 
meaningfully. 
47
1.3 The Phases of a Compiler: 
• The semantic analysis phase checks the source 
program for semantic errors and gathers type 
information for the subsequent code-generation 
phase. 
48
1.3 The Phases of a Compiler: 
• It uses the hierarchical structure determined 
by the syntax-analysis phase to identify the 
operators and operand of expressions and 
statements. 
49
1.3 The Phases of a Compiler: 
• An important component of semantic analysis 
is type checking. 
• Here are the compiler checks that each 
operator has operands that are permitted by 
the source language specification. 
50
1.3 The Phases of a Compiler: 
• For example, when a binary arithmetic 
operator is applied to an integer and real. In 
this case, the compiler may need to be 
converting the integer to a real. As shown in 
figure given below 
51
1.3 The Phases of a Compiler: 
52
1.3 The Phases of a Compiler: 
• Symbol Table Management: 
– An essential function of a compiler is to record the 
identifiers used in the source program and collect 
information about various attributes of each 
identifier. 
– These attributes may provide information about 
the storage allocated for an identifier, its type, its 
scope. 
53
1.3 The Phases of a Compiler: 
– The symbol table is a data structure containing a 
record for each identifier with fields for the 
attributes of the identifier. 
– When an identifier in the source program is 
detected by the lexical analyzer, the identifier is 
entered into the symbol table 
54
1.3 The Phases of a Compiler: 
– However, the attributes of an identifier cannot 
normally be determined during lexical analysis. 
– For example, in a Pascal declaration like 
– Var position, initial, rate : real; 
– The type real is not known when position, initial 
and rate are seen by the lexical analyzer. 
55
1.3 The Phases of a Compiler: 
– The remaining phases gets information about 
identifiers into the symbol table and then use this 
information in various ways. 
– For example, when doing semantic analysis and 
intermediate code generation, we need to know 
what the types of identifiers are, so we can check 
that the source program uses them in valid ways, 
and so that we can generate the proper 
operations on them. 
56
1.3 The Phases of a Compiler: 
– The code generator typically enters and uses 
detailed information about the storage assigned 
to identifiers. 
57
Error Detection and Reporting: 
• Each phase can encounter errors. 
• However, after detecting an error, a phase 
must somehow deal with that error, so that 
compilation can proceed, allowing further 
errors in the source program to be detected. 
58
Error Detection and Reporting: 
• A compiler that stops when it finds the first 
error is not as helpful as it could be. 
• The syntax and semantic analysis phases 
usually handle a large fraction of the errors 
detectable by the compiler. 
59
Error Detection and Reporting: 
• Errors where the token stream violates the 
structure rules (syntax) of the language are 
determined by the syntax analysis phase. 
• The lexical phase can detect errors where the 
characters remaining in the input do not form 
any token of the language. 
60
Intermediate Code Generation: 
• After Syntax and semantic analysis, some 
compilers generate an explicit intermediate 
representation of the source program. 
• We can think of this intermediate 
representation as a program for an abstract 
machine. 
61
Intermediate Code Generation: 
• This intermediate representation should have 
two important properties; 
– it should be easy to produce, 
– easy to translate into the target program. 
62
Intermediate Code Generation: 
• We consider an intermediate form called 
“three-address code,” 
• which is like the assembly language for a 
machine in which every memory location can 
act like a register. 
63
Intermediate Code Generation: 
• Three-address code consists of a sequence of 
instructions, each of which has at most three 
operands. 
• The source program in (1.1) might appear in 
three-address code as 
64
Intermediate Code Generation: 
(1.3) 
• Temp1 := inttoreal (60) 
• Temp2 := id3 * temp1 
• Temp3 := id2 + temp2 
• id1 := temp3 
65
Code Optimization: 
• The code optimization phase attempts to 
improve the intermediate code, so that faster-running 
machine code will result. 
66
Code Optimization: 
• Some optimizations are trivial. 
• For example, a natural algorithm generates 
the intermediate code (1.3), using an 
instruction for each operator in the tree 
representation after semantic analysis, even 
though there is a better way to perform the 
same calculation, using the two instructions. 
67
Code Optimization: 
(1.4) 
• Temp1 := id3 * 60.0 
• id := id2 + temp1 
• There is nothing wrong with this simple 
algorithm, since the problem can be fixed 
during the code-optimization phase. 
68
Code Optimization: 
• That is, the compiler can deduce that the 
conversion of 60 from integer to real 
representation can be done once and for all at 
compile time, so the inttoreal operation can 
be eliminated. 
69
Code Optimization: 
• Besides, temp3 is used only once, to transmit 
its value to id1. It then becomes safe to 
substitute id1 for temp3, whereupon the last 
statement of 1.3 is not needed and the code 
of 1.4 results. 
70
Code Generation 
• The final phase of the compiler is the 
generation of target code 
• consisting normally of relocatable machine 
code or assembly code. 
71
Code Generation 
• Memory locations are selected for each of the 
variables used by the program. 
• Then, intermediate instructions are each 
translated into a sequence of machine 
instructions that perform the same task. 
• A crucial aspect is the assignment of variables 
to registers. 
72
Code Generation 
• For example, using registers 1 and 2, the 
translation of the code of 1.4 might become 
• MOVF id3, r2 
• MULF #60.0, r2 
• MOVF id2, r1 
• ADDF r2, r1 
• MOVF r1, id1 
73
Code Generation 
• The first and second operands of each 
instruction specify a source and destination, 
respectively. 
• The F in each instruction tells us that 
instructions deal with floating-point numbers. 
74
Code Generation 
• This code moves the contents of the address 
id3 into register 2, and then multiplies it with 
the real-constant 60.0. 
• The # signifies that 60.0 is to be treated as a 
constant. 
75
Code Generation 
• The third instruction moves id2 into register 1 
and adds to it the value previously computed 
in register 2 
• Finally, the value in register 1 is moved into 
the address of id1. 
76
1.4 Cousins of the Compiler: 
• As we saw in given figure, the input to a 
compiler may be produced by one or more 
preprocessors, and further processing of the 
compiler’s output may be needed before 
running machine code is obtained. 
77
LIBRARY, RELOCATABLE 
OBJECT FILES 
Skeletal Source Program 
Preprocessor 
Source program 
Compiler 
Target assembly program 
Assembler 
Relocatable machine code 
Loader/link-editor 
Absolute machine code 
1.3. A LANGUAGE-PROCESSING SYSTEM 78
1.4 Cousins of the Compiler: 
• Preprocessors: 
• preprocessors produce input to compilers. 
They may perform the following functions: 
– Macro Processing: 
– File inclusion: 
– “Rational” Preprocessors: 
– Language extensions: 
79
Preprocessors: 
• Macro Processing: 
– A preprocessor may allow a user to define macros 
that are shorthand’s for longer constructs. 
80
Preprocessors: 
• File inclusion: 
– A preprocessor may include header files into the 
program text. 
– For example, the C preprocessor causes the 
contents of the file <global.h> to replace the 
statement #include <global.h> when it processes a 
file containing this statement. 
81
Preprocessors: 
defs.h 
////// 
////// 
////// 
main.c 
#include “defs.h” 
…---…---…--- 
…---…---…--- 
…---…---…--- 
////// 
////// 
////// 
…---…---…--- 
…---…---…--- 
…---…---…--- 
82
Preprocessors: 
• “Rational” Preprocessors: 
– These processors augment older languages with 
more modern flow-of-control and data-structuring 
facilities. 
83
Preprocessors: 
• Language extensions: 
– These processors attempt to add capabilities to 
the language by what amounts to built-in macros. 
– For example, the language Equal is a database 
query language embedded in C. Statements 
beginning with ## are taken by the preprocessor 
to be database-access statements, unrelated to C, 
and are translated into procedure calls on routines 
that perform the database access. 
84
Assemblers: 
• Some compilers produce assembly code that 
is passed to an assembler for further 
processing. 
• Other compilers perform the job of the 
assembler, producing relocatable machine 
code that can be passed directly to the 
loader/link-editor. 
85
Assemblers: 
• Here we shall review the relationship between 
assembly and machine code. 
86
Assemblers: 
• Assembly code is a mnemonic version of 
machine code. 
• In which names are used instead of binary 
codes for operations, and names are also 
given to memory addresses. 
87
Assemblers: 
• A typical sequence of assembly instructions 
might be 
• MOV a , R1 
• ADD #2 , R1 
• MOV R1 , b 
88
Assemblers: 
• This code moves the contents of the address a 
into register 1, then adds the constant 2 to it, 
reading the contents of register 1 as a fixed-point 
number, and finally stores the result in 
the location named by b. thus, it computes 
b:=a+2. 
89
Two-Pass Compiler: 
• The simplest form of assembler makes two 
passes over the input. 
90
Two-Pass Compiler: 
• in the first pass, all the identifiers that denote 
storage locations are found and stored in a 
symbol table 
• Identifiers are assigned storage locations as 
they are encountered for the first time, so 
after reading 1.6, for example, the symbol 
table might contain the entries shown in given 
below. 
91
Two-Pass Compiler: 
MOV a , R1 
ADD #2 , R1 
MOV R1 , b 
Identifiers Address 
a 0 
b 4 
92
Two-Pass Compiler: 
• In the second pass, the assembler scans the 
input again. 
• This time, it translates each operation code 
into the sequence of bits representing that 
operation in machine language. 
• The output of the 2nd pass is usually 
relocatable machine code. 
93
Loaders and Link-Editors: 
• usually, a program called a loader performs 
the two functions of loading and link-editing. 
94
Loaders and Link-Editors: 
• The process of loading consists of taking 
relocatable machine code, altering the 
relocatable addresses, and placing the altered 
instructions and data in memory at the proper 
location. 
95
Loaders and Link-Editors: 
• The link-editor allows us to make a single 
program from several files of relocatable 
machine code. 
96
1.5 
The Grouping of Phases: 
97
Front and Back Ends: 
• The phases are collected into a front end and 
a back end. 
• The front end consists of those phases that 
depend primarily on the source language and 
are largely independent of the target machine. 
98
Front and Back Ends: 
• These normally include lexical and syntactic 
analysis, the creating of the symbol table, 
semantic analysis, and the generation of 
intermediate code. 
• A certain among of code optimization can be 
done by the front end as well. 
99
Front and Back Ends: 
• The front end also includes the error handling 
that goes along with each of these phases. 
100
Front and Back Ends: 
• The back end includes those portions of the 
compiler that depend on the target machine. 
• And generally, these portions do not depend 
on the source language, depend on just the 
intermediate language. 
101
Front and Back Ends: 
• In the back end, we find aspects of the code 
optimization phase, and we find code 
generation, along with the necessary error 
handling and symbol table operations. 
102
Compiler-Construction Tools: 
• The compiler writer, like any programmer, can 
profitably use tools such as 
– Debuggers, 
– Version managers, 
– Profilers and so on. 
103
Compiler-Construction Tools: 
• In addition to these software-development 
tools, other more specialized tools have been 
developed for helping implement various 
phases of a compiler. 
104
Compiler-Construction Tools: 
• Shortly after the first compilers were written, 
systems to help with the compiler-writing 
process appeared. 
• These systems have often been referred to as 
– Compiler-compilers, 
– Compiler-generators, 
– Or Translator-writing systems. 
105
Compiler-Construction Tools: 
• Some general tools have been created for the 
automatic design of specific compiler 
components. 
• These tools use specialized languages for 
specifying and implementing the component, 
and many use algorithms that are quite 
sophisticated. 
106
Compiler-Construction Tools: 
• The most successful tools are those that hide 
the details of the generation algorithm and 
produce components that can be easily 
integrated into the remainder of a compiler. 
107
Compiler-Construction Tools: 
• The following is a list of some useful compiler-construction 
tools: 
– Parser generators 
– Scanner generators 
– Syntax directed translation engines 
– Automatic code generators 
– Data-flow engines 
108
Compiler-Construction Tools: 
• Parser generators 
– These produce syntax analyzers, normally from 
input that is based on a context-free grammar. 
– In early compilers, syntax analysis consumed not 
only a large fraction of the running time of a 
compiler, but a large fraction of the intellectual 
effort of writing a compiler. 
– This phase is considered one of the easiest to 
implement. 
109
Compiler-Construction Tools: 
• Scanner generators: 
– These tools automatically generate lexical 
analyzers, normally from a specification based on 
regular expressions. 
– The basic organization of the resulting lexical 
analyzer is in effect a finite automaton. 
110
Compiler-Construction Tools: 
• Syntax directed translation engines: 
– These produce collections of routines that walk 
the parse tree, generating intermediate code. 
– The basic idea is that one or more “translations” 
are associated with each node of the parse tree, 
and each translation is defined in terms of 
translations at its neighbor nodes in the tree. 
111
Compiler-Construction Tools: 
• Automatic code generators: 
– Such a tool takes a collection of rules that define 
the translation of each operation of the 
intermediate language into the machine language 
for the target machine. 
112
• Data-flow engines: 
– Much of the information needed to perform good 
code optimization involves “data-flow analysis,” 
the gathering of information how values are 
transmitted from one part of a program to each 
other part. 
113

More Related Content

What's hot

Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code GeneratorDarshan sai Reddy
 
Computer Language Translator
Computer Language TranslatorComputer Language Translator
Computer Language TranslatorRanjeet Kumar
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil Kaushik
 
Language translator
Language translatorLanguage translator
Language translatorasmakh89
 
Language processor
Language processorLanguage processor
Language processorAbha Damani
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languagesVarun Garg
 
basics of compiler design
basics of compiler designbasics of compiler design
basics of compiler designPreeti Katiyar
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generatorsanchi29
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design) Tasif Tanzim
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & LanguagesGaditek
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ LanguageWay2itech
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++Neeru Mittal
 

What's hot (20)

Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code Generator
 
Interpreter
InterpreterInterpreter
Interpreter
 
Computer Language Translator
Computer Language TranslatorComputer Language Translator
Computer Language Translator
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Language translator
Language translatorLanguage translator
Language translator
 
Language processor
Language processorLanguage processor
Language processor
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languages
 
basics of compiler design
basics of compiler designbasics of compiler design
basics of compiler design
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
System Programming Overview
System Programming OverviewSystem Programming Overview
System Programming Overview
 
Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
 
Assembler
AssemblerAssembler
Assembler
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
 

Viewers also liked (20)

sCode optimization
sCode optimizationsCode optimization
sCode optimization
 
States machine
States machineStates machine
States machine
 
Keyword Presentation
Keyword PresentationKeyword Presentation
Keyword Presentation
 
Types and roles
Types and rolesTypes and roles
Types and roles
 
Uml Common Mechanism
Uml Common MechanismUml Common Mechanism
Uml Common Mechanism
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
What is symbol table?
What is symbol table?What is symbol table?
What is symbol table?
 
Single Pass Assembler
Single Pass AssemblerSingle Pass Assembler
Single Pass Assembler
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Compiler Design Tutorial
Compiler Design Tutorial Compiler Design Tutorial
Compiler Design Tutorial
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture Notes
 
Linux programming - Getting self started
Linux programming - Getting self started Linux programming - Getting self started
Linux programming - Getting self started
 
Linux Internals - Interview essentials 4.0
Linux Internals - Interview essentials 4.0Linux Internals - Interview essentials 4.0
Linux Internals - Interview essentials 4.0
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)
 
Operating Systems: Processor Management
Operating Systems: Processor ManagementOperating Systems: Processor Management
Operating Systems: Processor Management
 
Two pass Assembler
Two pass AssemblerTwo pass Assembler
Two pass Assembler
 

Similar to Compilers

Comiler construction Notes
Comiler construction NotesComiler construction Notes
Comiler construction NotesNadeem Khan
 
Chapter-1.pptx compiler Design Course Material
Chapter-1.pptx compiler Design Course MaterialChapter-1.pptx compiler Design Course Material
Chapter-1.pptx compiler Design Course MaterialgadisaAdamu
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler ConstructionSarmad Ali
 
what is compiler and five phases of compiler
what is compiler and five phases of compilerwhat is compiler and five phases of compiler
what is compiler and five phases of compileradilmehmood93
 
System software module 4 presentation file
System software module 4 presentation fileSystem software module 4 presentation file
System software module 4 presentation filejithujithin657
 
1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.pptRakesh Kumar
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.pptsivaganesh293
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.pptsivaganesh293
 
phases of compiler-analysis phase
phases of compiler-analysis phasephases of compiler-analysis phase
phases of compiler-analysis phaseSuyash Srivastava
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overviewamudha arul
 
COMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxCOMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxRossy719186
 
Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in detailskazi_aihtesham
 

Similar to Compilers (20)

Comiler construction Notes
Comiler construction NotesComiler construction Notes
Comiler construction Notes
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
Chapter-1.pptx compiler Design Course Material
Chapter-1.pptx compiler Design Course MaterialChapter-1.pptx compiler Design Course Material
Chapter-1.pptx compiler Design Course Material
 
Compiler unit 1
Compiler unit 1Compiler unit 1
Compiler unit 1
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
what is compiler and five phases of compiler
what is compiler and five phases of compilerwhat is compiler and five phases of compiler
what is compiler and five phases of compiler
 
System software module 4 presentation file
System software module 4 presentation fileSystem software module 4 presentation file
System software module 4 presentation file
 
The Phases of a Compiler
The Phases of a CompilerThe Phases of a Compiler
The Phases of a Compiler
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
 
Analysis of the source program
Analysis of the source programAnalysis of the source program
Analysis of the source program
 
1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt
 
Plc part 2
Plc  part 2Plc  part 2
Plc part 2
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
phases of compiler-analysis phase
phases of compiler-analysis phasephases of compiler-analysis phase
phases of compiler-analysis phase
 
1._Introduction_.pptx
1._Introduction_.pptx1._Introduction_.pptx
1._Introduction_.pptx
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
COMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxCOMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptx
 
Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in details
 

More from Satyamevjayte Haxor (9)

Patterns
PatternsPatterns
Patterns
 
Uml class Diagram
Uml class DiagramUml class Diagram
Uml class Diagram
 
Lexical
LexicalLexical
Lexical
 
Linker
LinkerLinker
Linker
 
Nested micro
Nested microNested micro
Nested micro
 
Multiplier control unit
Multiplier control unitMultiplier control unit
Multiplier control unit
 
Control unit design
Control unit designControl unit design
Control unit design
 
Direct linking loaders
Direct linking loadersDirect linking loaders
Direct linking loaders
 
Linking in MS-Dos System
Linking in MS-Dos SystemLinking in MS-Dos System
Linking in MS-Dos System
 

Recently uploaded

WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 

Recently uploaded (20)

WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 

Compilers

  • 1. 1.1 Compilers: • A compiler is a program that reads a program written in one language –– the source language –– and translates it into an equivalent program in another language –– the target language 1
  • 2. 1.1 Compilers: • As an important part of this translation process, the compiler reports to its user the presence of errors in the source program. 2
  • 4. 1.1 Compilers: • At first glance, the variety of compilers may appear overwhelming. • There are thousands of source languages, ranging from traditional programming languages such as FORTRAN and Pascal to specialized languages. 4
  • 5. 1.1 Compilers: • Target languages are equally as varied; • A target language may be another programming language, or the machine language of any computer. 5
  • 6. 1.1 Compilers: • Compilers are sometimes classified as: – single-pass – multi-pass – load-and-go – Debugging – optimizing 6
  • 7. 1.1 Compilers: • The basic tasks that any compiler must perform are essentially the same. • By understanding these tasks, we can construct compilers for a wide variety of source languages and target machines using the same basic techniques. 7
  • 8. 1.1 Compilers: • Throughout the 1950’s, compilers were considered notoriously difficult programs to write. • The first FORTRAN compiler, for example, took 18 staff-years to implement. 8
  • 9. The Analysis-Synthesis Model of Compilation: • There are two parts of compilation: – Analysis – Synthesis 9
  • 10. The Analysis-Synthesis Model of Compilation: • The analysis part breaks up the source program into constituent pieces • creates an intermediate representation of the source program. 10
  • 11. The Analysis-Synthesis Model of Compilation: • The synthesis part constructs the desired target program from the intermediate representation. 11
  • 12. The Analysis-Synthesis Model of Compilation: Front End Back End source code IR machine code errors 12
  • 13. The Analysis-Synthesis Model of Compilation: • During analysis, the operations implied by the source program are determined and recorded in a hierarchical structure called a tree. • Often, a special kind of tree called a syntax tree is used. 13
  • 14. The Analysis-Synthesis Model of Compilation: • In syntax tree each node represents an operation and the children of the node represent the arguments of the operation. • For example, a syntax tree of an assignment statement is shown below. 14
  • 15. The Analysis-Synthesis Model of Compilation: 15
  • 16. Analysis of the Source Program: • In compiling, analysis consists of three phases: – Linear Analysis: – Hierarchical Analysis: – Semantic Analysis: 16
  • 17. Analysis of the Source Program: • Linear Analysis: – In which the stream of characters making up the source program is read from left-to-right and grouped into tokens that are sequences of characters having a collective meaning. 17
  • 18. Scanning or Lexical Analysis (Linear Analysis): • In a compiler, linear analysis is called lexical analysis or scanning. • For example, in lexical analysis the characters in the assignment statement • Position: = initial + rate * 60 • Would be grouped into the following tokens: 18
  • 19. Scanning or Lexical Analysis (Linear Analysis): • The identifier, position. • The assignment symbol := • The identifier initial. • The plus sign. • The identifier rate. • The multiplication sign. • The number 60. 19
  • 20. Scanning or Lexical Analysis (Linear Analysis): • The blanks separating the characters of these tokens would normally be eliminated during the lexical analysis. 20
  • 21. Analysis of the Source Program: • Hierarchical Analysis: – In which characters or tokens are grouped hierarchically into nested collections with collective meaning. 21
  • 22. Syntax Analysis or Hierarchical Analysis (Parsing): • Hierarchical analysis is called parsing or syntax analysis. • It involves grouping the tokens of the source program into grammatical phases that are used by the compiler to synthesize output. 22
  • 23. Syntax Analysis or Hierarchical Analysis (Parsing): • The grammatical phrases of the source program are represented by a parse tree. 23
  • 24. Syntax Analysis or Hierarchical Analysis (Parsing): Assignment Statement := Identifier Position Expression + Expression Identifier Initial Expression * Expression Identifier Rate Expression Number 60 24
  • 25. Syntax Analysis or Hierarchical Analysis (Parsing): • In the expression initial + rate * 60, the phrase rate * 60 is a logical unit because the usual conventions of arithmetic expressions tell us that the multiplication is performed before addition. • Because the expression initial + rate is followed by a *, it is not grouped into a single phrase by itself 25
  • 26. Syntax Analysis or Hierarchical Analysis (Parsing): • The hierarchical structure of a program is usually expressed by recursive rules. • For example, we might have the following rules, as part of the definition of expression: 26
  • 27. Syntax Analysis or Hierarchical Analysis (Parsing): • Any identifier is an expression. • Any number is an expression • If expression1 and expression2 are expressions, then so are – Expression1 + expression2 – Expression1 * expression2 – (Expression1 ) 27
  • 28. Analysis of the Source Program: • Semantic Analysis: – In which certain checks are performed to ensure that the components of a program fit together meaningfully. 28
  • 29. Semantic Analysis: • The semantic analysis phase checks the source program for semantic errors and gathers type information for the subsequent code-generation phase. 29
  • 30. Semantic Analysis: • It uses the hierarchical structure determined by the syntax-analysis phase to identify the operators and operand of expressions and statements. 30
  • 31. Semantic Analysis: • An important component of semantic analysis is type checking. • Here are the compiler checks that each operator has operands that are permitted by the source language specification. 31
  • 32. Semantic Analysis: • For example, when a binary arithmetic operator is applied to an integer and real. In this case, the compiler may need to be converting the integer to a real. As shown in figure given below 32
  • 34. 1.3 The Phases of a Compiler: • A compiler operates in phases. • Each of which transforms the source program from one representation to another. • A typical decomposition of a compiler is shown in fig given below 34
  • 35. 1.3 The Phases of a Compiler: 35
  • 36. 1.3 The Phases of a Compiler: • Linear Analysis: – In which the stream of characters making up the source program is read from left-to-right and grouped into tokens that are sequences of characters having a collective meaning. 36
  • 37. 1.3 The Phases of a Compiler: • In a compiler, linear analysis is called lexical analysis or scanning. • For example, in lexical analysis the characters in the assignment statement • Position: = initial + rate * 60 • Would be grouped into the following tokens: 37
  • 38. 1.3 The Phases of a Compiler: • The identifier, position. • The assignment symbol := • The identifier initial. • The plus sign. • The identifier rate. • The multiplication sign. • The number 60. 38
  • 39. 1.3 The Phases of a Compiler: • The blanks separating the characters of these tokens would normally be eliminated during the lexical analysis. 39
  • 40. 1.3 The Phases of a Compiler: • Hierarchical Analysis: – In which characters or tokens are grouped hierarchically into nested collections with collective meaning. 40
  • 41. 1.3 The Phases of a Compiler: • Hierarchical analysis is called parsing or syntax analysis. • It involves grouping the tokens of the source program into grammatical phases that are used by the compiler to synthesize output. 41
  • 42. 1.3 The Phases of a Compiler: • The grammatical phrases of the source program are represented by a parse tree. 42
  • 43. 1.3 The Phases of a Compiler: Assignment Statement := Identifier Position Expression + Expression Identifier Initial Expression * Expression Identifier Rate Expression Number 60 43
  • 44. 1.3 The Phases of a Compiler: • In the expression initial + rate * 60, the phrase rate * 60 is a logical unit because the usual conventions of arithmetic expressions tell us that the multiplication is performed before addition. • Because the expression initial + rate is followed by a *, it is not grouped into a single phrase by itself 44
  • 45. 1.3 The Phases of a Compiler: • The hierarchical structure of a program is usually expressed by recursive rules. • For example, we might have the following rules, as part of the definition of expression: 45
  • 46. 1.3 The Phases of a Compiler: • Any identifier is an expression. • Any number is an expression • If expression1 and expression2 are expressions, then so are – Expression1 + expression2 – Expression1 * expression2 – (Expression1 ) 46
  • 47. 1.3 The Phases of a Compiler: • Semantic Analysis: – In which certain checks are performed to ensure that the components of a program fit together meaningfully. 47
  • 48. 1.3 The Phases of a Compiler: • The semantic analysis phase checks the source program for semantic errors and gathers type information for the subsequent code-generation phase. 48
  • 49. 1.3 The Phases of a Compiler: • It uses the hierarchical structure determined by the syntax-analysis phase to identify the operators and operand of expressions and statements. 49
  • 50. 1.3 The Phases of a Compiler: • An important component of semantic analysis is type checking. • Here are the compiler checks that each operator has operands that are permitted by the source language specification. 50
  • 51. 1.3 The Phases of a Compiler: • For example, when a binary arithmetic operator is applied to an integer and real. In this case, the compiler may need to be converting the integer to a real. As shown in figure given below 51
  • 52. 1.3 The Phases of a Compiler: 52
  • 53. 1.3 The Phases of a Compiler: • Symbol Table Management: – An essential function of a compiler is to record the identifiers used in the source program and collect information about various attributes of each identifier. – These attributes may provide information about the storage allocated for an identifier, its type, its scope. 53
  • 54. 1.3 The Phases of a Compiler: – The symbol table is a data structure containing a record for each identifier with fields for the attributes of the identifier. – When an identifier in the source program is detected by the lexical analyzer, the identifier is entered into the symbol table 54
  • 55. 1.3 The Phases of a Compiler: – However, the attributes of an identifier cannot normally be determined during lexical analysis. – For example, in a Pascal declaration like – Var position, initial, rate : real; – The type real is not known when position, initial and rate are seen by the lexical analyzer. 55
  • 56. 1.3 The Phases of a Compiler: – The remaining phases gets information about identifiers into the symbol table and then use this information in various ways. – For example, when doing semantic analysis and intermediate code generation, we need to know what the types of identifiers are, so we can check that the source program uses them in valid ways, and so that we can generate the proper operations on them. 56
  • 57. 1.3 The Phases of a Compiler: – The code generator typically enters and uses detailed information about the storage assigned to identifiers. 57
  • 58. Error Detection and Reporting: • Each phase can encounter errors. • However, after detecting an error, a phase must somehow deal with that error, so that compilation can proceed, allowing further errors in the source program to be detected. 58
  • 59. Error Detection and Reporting: • A compiler that stops when it finds the first error is not as helpful as it could be. • The syntax and semantic analysis phases usually handle a large fraction of the errors detectable by the compiler. 59
  • 60. Error Detection and Reporting: • Errors where the token stream violates the structure rules (syntax) of the language are determined by the syntax analysis phase. • The lexical phase can detect errors where the characters remaining in the input do not form any token of the language. 60
  • 61. Intermediate Code Generation: • After Syntax and semantic analysis, some compilers generate an explicit intermediate representation of the source program. • We can think of this intermediate representation as a program for an abstract machine. 61
  • 62. Intermediate Code Generation: • This intermediate representation should have two important properties; – it should be easy to produce, – easy to translate into the target program. 62
  • 63. Intermediate Code Generation: • We consider an intermediate form called “three-address code,” • which is like the assembly language for a machine in which every memory location can act like a register. 63
  • 64. Intermediate Code Generation: • Three-address code consists of a sequence of instructions, each of which has at most three operands. • The source program in (1.1) might appear in three-address code as 64
  • 65. Intermediate Code Generation: (1.3) • Temp1 := inttoreal (60) • Temp2 := id3 * temp1 • Temp3 := id2 + temp2 • id1 := temp3 65
  • 66. Code Optimization: • The code optimization phase attempts to improve the intermediate code, so that faster-running machine code will result. 66
  • 67. Code Optimization: • Some optimizations are trivial. • For example, a natural algorithm generates the intermediate code (1.3), using an instruction for each operator in the tree representation after semantic analysis, even though there is a better way to perform the same calculation, using the two instructions. 67
  • 68. Code Optimization: (1.4) • Temp1 := id3 * 60.0 • id := id2 + temp1 • There is nothing wrong with this simple algorithm, since the problem can be fixed during the code-optimization phase. 68
  • 69. Code Optimization: • That is, the compiler can deduce that the conversion of 60 from integer to real representation can be done once and for all at compile time, so the inttoreal operation can be eliminated. 69
  • 70. Code Optimization: • Besides, temp3 is used only once, to transmit its value to id1. It then becomes safe to substitute id1 for temp3, whereupon the last statement of 1.3 is not needed and the code of 1.4 results. 70
  • 71. Code Generation • The final phase of the compiler is the generation of target code • consisting normally of relocatable machine code or assembly code. 71
  • 72. Code Generation • Memory locations are selected for each of the variables used by the program. • Then, intermediate instructions are each translated into a sequence of machine instructions that perform the same task. • A crucial aspect is the assignment of variables to registers. 72
  • 73. Code Generation • For example, using registers 1 and 2, the translation of the code of 1.4 might become • MOVF id3, r2 • MULF #60.0, r2 • MOVF id2, r1 • ADDF r2, r1 • MOVF r1, id1 73
  • 74. Code Generation • The first and second operands of each instruction specify a source and destination, respectively. • The F in each instruction tells us that instructions deal with floating-point numbers. 74
  • 75. Code Generation • This code moves the contents of the address id3 into register 2, and then multiplies it with the real-constant 60.0. • The # signifies that 60.0 is to be treated as a constant. 75
  • 76. Code Generation • The third instruction moves id2 into register 1 and adds to it the value previously computed in register 2 • Finally, the value in register 1 is moved into the address of id1. 76
  • 77. 1.4 Cousins of the Compiler: • As we saw in given figure, the input to a compiler may be produced by one or more preprocessors, and further processing of the compiler’s output may be needed before running machine code is obtained. 77
  • 78. LIBRARY, RELOCATABLE OBJECT FILES Skeletal Source Program Preprocessor Source program Compiler Target assembly program Assembler Relocatable machine code Loader/link-editor Absolute machine code 1.3. A LANGUAGE-PROCESSING SYSTEM 78
  • 79. 1.4 Cousins of the Compiler: • Preprocessors: • preprocessors produce input to compilers. They may perform the following functions: – Macro Processing: – File inclusion: – “Rational” Preprocessors: – Language extensions: 79
  • 80. Preprocessors: • Macro Processing: – A preprocessor may allow a user to define macros that are shorthand’s for longer constructs. 80
  • 81. Preprocessors: • File inclusion: – A preprocessor may include header files into the program text. – For example, the C preprocessor causes the contents of the file <global.h> to replace the statement #include <global.h> when it processes a file containing this statement. 81
  • 82. Preprocessors: defs.h ////// ////// ////// main.c #include “defs.h” …---…---…--- …---…---…--- …---…---…--- ////// ////// ////// …---…---…--- …---…---…--- …---…---…--- 82
  • 83. Preprocessors: • “Rational” Preprocessors: – These processors augment older languages with more modern flow-of-control and data-structuring facilities. 83
  • 84. Preprocessors: • Language extensions: – These processors attempt to add capabilities to the language by what amounts to built-in macros. – For example, the language Equal is a database query language embedded in C. Statements beginning with ## are taken by the preprocessor to be database-access statements, unrelated to C, and are translated into procedure calls on routines that perform the database access. 84
  • 85. Assemblers: • Some compilers produce assembly code that is passed to an assembler for further processing. • Other compilers perform the job of the assembler, producing relocatable machine code that can be passed directly to the loader/link-editor. 85
  • 86. Assemblers: • Here we shall review the relationship between assembly and machine code. 86
  • 87. Assemblers: • Assembly code is a mnemonic version of machine code. • In which names are used instead of binary codes for operations, and names are also given to memory addresses. 87
  • 88. Assemblers: • A typical sequence of assembly instructions might be • MOV a , R1 • ADD #2 , R1 • MOV R1 , b 88
  • 89. Assemblers: • This code moves the contents of the address a into register 1, then adds the constant 2 to it, reading the contents of register 1 as a fixed-point number, and finally stores the result in the location named by b. thus, it computes b:=a+2. 89
  • 90. Two-Pass Compiler: • The simplest form of assembler makes two passes over the input. 90
  • 91. Two-Pass Compiler: • in the first pass, all the identifiers that denote storage locations are found and stored in a symbol table • Identifiers are assigned storage locations as they are encountered for the first time, so after reading 1.6, for example, the symbol table might contain the entries shown in given below. 91
  • 92. Two-Pass Compiler: MOV a , R1 ADD #2 , R1 MOV R1 , b Identifiers Address a 0 b 4 92
  • 93. Two-Pass Compiler: • In the second pass, the assembler scans the input again. • This time, it translates each operation code into the sequence of bits representing that operation in machine language. • The output of the 2nd pass is usually relocatable machine code. 93
  • 94. Loaders and Link-Editors: • usually, a program called a loader performs the two functions of loading and link-editing. 94
  • 95. Loaders and Link-Editors: • The process of loading consists of taking relocatable machine code, altering the relocatable addresses, and placing the altered instructions and data in memory at the proper location. 95
  • 96. Loaders and Link-Editors: • The link-editor allows us to make a single program from several files of relocatable machine code. 96
  • 97. 1.5 The Grouping of Phases: 97
  • 98. Front and Back Ends: • The phases are collected into a front end and a back end. • The front end consists of those phases that depend primarily on the source language and are largely independent of the target machine. 98
  • 99. Front and Back Ends: • These normally include lexical and syntactic analysis, the creating of the symbol table, semantic analysis, and the generation of intermediate code. • A certain among of code optimization can be done by the front end as well. 99
  • 100. Front and Back Ends: • The front end also includes the error handling that goes along with each of these phases. 100
  • 101. Front and Back Ends: • The back end includes those portions of the compiler that depend on the target machine. • And generally, these portions do not depend on the source language, depend on just the intermediate language. 101
  • 102. Front and Back Ends: • In the back end, we find aspects of the code optimization phase, and we find code generation, along with the necessary error handling and symbol table operations. 102
  • 103. Compiler-Construction Tools: • The compiler writer, like any programmer, can profitably use tools such as – Debuggers, – Version managers, – Profilers and so on. 103
  • 104. Compiler-Construction Tools: • In addition to these software-development tools, other more specialized tools have been developed for helping implement various phases of a compiler. 104
  • 105. Compiler-Construction Tools: • Shortly after the first compilers were written, systems to help with the compiler-writing process appeared. • These systems have often been referred to as – Compiler-compilers, – Compiler-generators, – Or Translator-writing systems. 105
  • 106. Compiler-Construction Tools: • Some general tools have been created for the automatic design of specific compiler components. • These tools use specialized languages for specifying and implementing the component, and many use algorithms that are quite sophisticated. 106
  • 107. Compiler-Construction Tools: • The most successful tools are those that hide the details of the generation algorithm and produce components that can be easily integrated into the remainder of a compiler. 107
  • 108. Compiler-Construction Tools: • The following is a list of some useful compiler-construction tools: – Parser generators – Scanner generators – Syntax directed translation engines – Automatic code generators – Data-flow engines 108
  • 109. Compiler-Construction Tools: • Parser generators – These produce syntax analyzers, normally from input that is based on a context-free grammar. – In early compilers, syntax analysis consumed not only a large fraction of the running time of a compiler, but a large fraction of the intellectual effort of writing a compiler. – This phase is considered one of the easiest to implement. 109
  • 110. Compiler-Construction Tools: • Scanner generators: – These tools automatically generate lexical analyzers, normally from a specification based on regular expressions. – The basic organization of the resulting lexical analyzer is in effect a finite automaton. 110
  • 111. Compiler-Construction Tools: • Syntax directed translation engines: – These produce collections of routines that walk the parse tree, generating intermediate code. – The basic idea is that one or more “translations” are associated with each node of the parse tree, and each translation is defined in terms of translations at its neighbor nodes in the tree. 111
  • 112. Compiler-Construction Tools: • Automatic code generators: – Such a tool takes a collection of rules that define the translation of each operation of the intermediate language into the machine language for the target machine. 112
  • 113. • Data-flow engines: – Much of the information needed to perform good code optimization involves “data-flow analysis,” the gathering of information how values are transmitted from one part of a program to each other part. 113