SlideShare a Scribd company logo
Rigvendra Kumar Vardhan 
M.TECH ECE 
Pondicherry University 
1
 Interpreters (direct execution) 
 Assemblers 
 Preprocessors 
 Text formatters (non-WYSIWYG) 
 Analysis tools 
CS 540 Spring 2013 GMU 2
 Interpreter 
A program that reads a source program 
and produces the results of executing 
that program 
 Compiler 
A program that translates a program 
from one language (the source) to 
another (the target) 
3
 Correctness 
 Speed (runtime and compile time) 
Degrees of optimization 
Multiple passes 
 Space 
 Feedback to user 
 Debugging 
CS 540 Spring 2013 GMU 4
 Interpreter 
Execution engine 
Program execution interleaved with 
analysis 
running = true; 
while (running) { 
analyze next statement; 
execute that statement; 
} 
May involve repeated analysis of some 
statements (loops, functions) 
5
 Read and analyze entire program 
 Translate to semantically equivalent 
program in another language 
Presumably easier to execute or more 
efficient 
Should “improve” the program in some 
fashion 
 Offline process 
Tradeoff: compile time overhead 
(preprocessing step) vs execution 
performance 
6
 Compilers 
FORTRAN, C, C++, Java, COBOL, etc. 
etc. 
Strong need for optimization, etc. 
 Interpreters 
PERL, Python, awk, sed, sh, csh, 
postscript printer, Java VM 
Effective if interpreter overhead is low 
relative to execution cost of language 
statements 
7
8 
Source code 
Compiler 
Assembly code 
Assembler 
Object code 
(machine code) 
Fully-resolved object 
code (machine code) 
Linker 
Loader 
Executable image
 Series of program representations 
 Intermediate representations optimized 
for program manipulations of various 
kinds (checking, optimization) 
 Become more machine-specific, less 
language-specific as translation proceeds 
9
 First approximation 
Front end: analysis 
 Read source program and understand its structure 
and meaning 
Back end: synthesis 
 Generate equivalent target language program 
Source Front End Back End Target 
10
 Must recognize legal programs (& 
complain about illegal ones) 
 Must generate correct code 
 Must manage storage of all variables 
 Must agree with OS & linker on target 
format 
Source Front End Back End Target 
11
 Need some sort of Intermediate 
Representation (IR) 
 Front end maps source into IR 
 Back end maps IR to target machine code 
Source Front End Back End Target 
12
CS 540 Spring 2013 GMU 13 
Scanner 
(lexical 
analysis) 
Parser 
(syntax 
analysis) 
Code 
Optimizer 
Semantic 
Analysis 
(IC generator) 
Code 
Generator 
Symbol 
Table 
Source 
language 
tokens Syntactic 
structure 
Intermediate 
Language 
Target 
language 
Intermediate 
Language
 Lexical Analysis 
 Syntax Analysis 
 Semantic Analysis 
 Runtime environments 
 Code Generation 
 Code Optimization 
CS 540 Spring 2013 GMU 14
15 
Source code 
(character stream) 
Lexical analysis 
Parsing 
Token stream 
Abstract syntax tree 
Intermediate Code Generation 
Intermediate code 
Optimization 
Code generation 
Intermediate code 
Assembly code 
Front end 
(machine-independent) 
Back end 
(machine-dependent)
Scanner Parser source tokens IR 
 Split into two parts 
Scanner: Responsible for converting 
character stream to token stream 
 Also strips out white space, comments 
Parser: Reads token stream; generates IR 
 Both of these can be generated 
automatically 
Source language specified by a formal 
grammar 
Tools read the grammar and generate 
scanner & parser (either table-driven or hard 
coded) 
16
 Token stream: Each significant lexical 
chunk of the program is represented by a 
token 
Operators & Punctuation: {}[]!+-=*;: … 
Keywords: if while return goto 
Identifiers: id & actual name 
Constants: kind & value; int, floating-point 
character, string, … 
17
 Input text 
// this statement does very little 
if (x >= y) y = 42; 
 Token Stream 
IF LPAREN ID(x) GEQ ID(y) 
RPAREN ID(y) BECOMES INT(42) SCOLON 
Note: tokens are atomic items, not character 
strings 
18
 Many different forms 
(Engineering tradeoffs) 
 Common output from a parser is an 
abstract syntax tree 
Essential meaning of the program 
without the syntactic noise 
19
 Token Stream Input  Abstract Syntax Tree 
20 
IF LPAREN ID(x) 
GEQ ID(y) RPAREN 
ID(y) BECOMES 
INT(42) SCOLON 
ifStmt 
>= 
ID(x) ID(y) 
assign 
ID(y) INT(42)
 During or (more common) after parsing 
Type checking 
Check for language requirements like 
“declare before use”, type compatibility 
Preliminary resource allocation 
Collect other information needed by 
back end analysis and code generation 
21
 Responsibilities 
Translate IR into target machine code 
Should produce fast, compact code 
Should use machine resources 
effectively 
Registers 
Instructions 
Memory hierarchy 
22
 Typically split into two major parts with 
sub phases 
“Optimization” – code improvements 
May well translate parser IR into 
another IR 
Code generation 
Instruction selection & scheduling 
Register allocation 
23
 Input 
if (x >= y) 
y = 42; 
 Output 
mov eax,[ebp+16] 
cmp eax,[ebp-8] 
jl L17 
mov [ebp-8],42 
L17: 
24
lda $30,-32($30) 
stq $26,0($30) 
stq $15,8($30) 
bis $30,$30,$15 
bis $16,$16,$1 
stl $1,16($15) 
lds $f1,16($15) 
sts $f1,24($15) 
ldl $5,24($15) 
bis $5,$5,$2 
s4addq $2,0,$3 
ldl $4,16($15) 
mull $4,$3,$2 
ldl $3,16($15) 
addq $3,1,$4 
mull $2,$4,$2 
ldl $3,16($15) 
addq $3,1,$4 
mull $2,$4,$2 
stl $2,20($15) 
ldl $0,20($15) 
br $31,$33 
$33: 
bis $15,$15,$30 
ldq $26,0($30) 
ldq $15,8($30) 
addq $30,32,$30 
ret $31,($26),1 
Optimized Code 
25 
s4addq $16,0,$0 
mull $16,$0,$0 
addq $16,1,$16 
mull $0,$16,$0 
mull $0,$16,$0 
ret $31,($26),1 
Unoptimized Code
26 
Source code 
(character stream) 
Lexical analysis 
Parsing 
Token stream 
Abstract syntax tree 
(AST) 
Semantic Analysis 
if (b == 0) a = b; 
if ( b == 0 ) a = b ; 
if 
== 
b 0 
= 
a b 
if 
== 
int b int 0 
= 
int a 
lvalue 
int b 
boolean 
Decorated AST 
; 
int ;
Intermediate Code Generation 
Optimization 
Code generation 
27 
if 
boolean == 
int ; 
int b int 0 
= 
int a 
lvalue 
int b 
CJUMP == 
MEM 
+ 
fp 8 
CONST MOVE 
0 MEM MEM 
fp 4 fp 8 
NOP 
+ + 
CJUMP == 
CONST MOVE 
CX NOP 
0 DX CX 
CMP CX, 0 
CMOVZ DX,CX
 Compiler techniques are everywhere 
Parsing (little languages, interpreters) 
Database engines 
AI: domain-specific languages 
Text processing 
Tex/LaTex -> dvi -> Postscript -> pdf 
Hardware: VHDL; model-checking tools 
Mathematics (Mathematica, Matlab) 
28
 Fascinating blend of theory and 
engineering 
Direct applications of theory to practice 
Parsing, scanning, static analysis 
Some very difficult problems (NP-hard or 
worse) 
Resource allocation, “optimization”, 
etc. 
Need to come up with good-enough 
solutions 
29
 Ideas from many parts of CSE 
AI: Greedy algorithms, heuristic search 
Algorithms: graph algorithms, dynamic 
programming, approximation 
algorithms 
Theory: Grammars DFAs and PDAs, 
pattern matching, fixed-point 
algorithms 
Systems: Allocation & naming, 
synchronization, locality 
Architecture: pipelines & hierarchy 
management, instruction set use 
30
 program ::= statement | program 
statement 
 statement ::= assignStmt | ifStmt 
 assignStmt ::= id = expr ; 
 ifStmt ::= if ( expr ) stmt 
 expr ::= id | int | expr + expr 
 Id ::= a | b | c | i | j | k | n | x | y | z 
 int ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 
31
 There are several syntax notations for 
productions in common use; all mean the 
same thing 
ifStmt ::= if ( expr ) stmt 
ifStmt if ( expr ) stmt 
<ifStmt> ::= if ( <expr> ) <stmt> 
32
program ::= statement | program statement 
statement ::= assignStmt | ifStmt 
assignStmt ::= id = expr ; 
ifStmt ::= if ( expr ) stmt 
expr ::= id | int | expr + expr 
id ::= a | b | c | i | j | k | n | x | y | z 
int ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 
a = 1 ; 
if ( a + 1 ) 
b = 2 ; 
33 
program 
program 
ID(a) expr 
stmt 
stmt 
assign 
int (1) 
ifStmt 
expr stmt 
expr expr 
assign 
ID(a) int (1) ID(b) expr 
int (2)
34

More Related Content

What's hot (20)

Assembly Language
Assembly LanguageAssembly Language
Assembly Language
Ibrahimcommunication Al Ani
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
David Livingston J
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
Sathish Narayanan
 
Data types in C
Data types in CData types in C
Data types in C
Tarun Sharma
 
Shell programming
Shell programmingShell programming
Shell programming
Moayad Moawiah
 
ARM Processor
ARM ProcessorARM Processor
ARM Processor
Aniket Thakur
 
Structure in C
Structure in CStructure in C
Structure in C
Kamal Acharya
 
Addressing sequencing
Addressing sequencingAddressing sequencing
Addressing sequencing
rajshreemuthiah
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
Mahantesh Devoor
 
MACRO PROCESSOR
MACRO PROCESSORMACRO PROCESSOR
MACRO PROCESSOR
Bhavik Vashi
 
Computer organization
Computer organizationComputer organization
Computer organization
ishapadhy
 
Problem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to CProblem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to C
Prabu U
 
Types of Programming Errors
Types of Programming ErrorsTypes of Programming Errors
Types of Programming Errors
Neha Sharma
 
C language ppt
C language pptC language ppt
C language ppt
Ğäùråv Júñêjå
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
lavanya marichamy
 
GE3171-PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
GE3171-PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORYGE3171-PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
GE3171-PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
ANJALAI AMMAL MAHALINGAM ENGINEERING COLLEGE
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
Embedded c lab and keil c manual
Embedded  c  lab  and keil c  manualEmbedded  c  lab  and keil c  manual
Embedded c lab and keil c manual
Hari K
 
Modular programming
Modular programmingModular programming
Modular programming
Mohanlal Sukhadia University (MLSU)
 
Embedded System Tools ppt
Embedded System Tools  pptEmbedded System Tools  ppt
Embedded System Tools ppt
Halai Hansika
 

Viewers also liked (20)

How c program execute in c program
How c program execute in c program How c program execute in c program
How c program execute in c program
Rumman Ansari
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
Royalzig Luxury Furniture
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
BBDITM LUCKNOW
 
Properties EM
Properties EMProperties EM
Properties EM
tejas2019
 
Infrared spectoscopy
Infrared spectoscopyInfrared spectoscopy
Infrared spectoscopy
Rawat DA Greatt
 
Compiler design tutorial
Compiler design tutorialCompiler design tutorial
Compiler design tutorial
Varsha Shukla
 
Computational Spectroscopy in G03
Computational Spectroscopy in G03Computational Spectroscopy in G03
Computational Spectroscopy in G03
Inon Sharony
 
How To Start And Keep Conversations Going With Girls
How To Start And Keep Conversations Going With GirlsHow To Start And Keep Conversations Going With Girls
How To Start And Keep Conversations Going With Girls
George Hutton
 
Data Locality
Data LocalityData Locality
Data Locality
Syam Lal
 
Compiler design
Compiler designCompiler design
Compiler design
Ashraf Hossain
 
Infrared spectroscopy
Infrared spectroscopyInfrared spectroscopy
Infrared spectroscopy
Rawat DA Greatt
 
COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1
Vishal Patil
 
Lex
LexLex
Lex
BBDITM LUCKNOW
 
Steps for Developing a 'C' program
 Steps for Developing a 'C' program Steps for Developing a 'C' program
Steps for Developing a 'C' program
Sahithi Naraparaju
 
Unit 1 cd
Unit 1 cdUnit 1 cd
Unit 1 cd
codereplugd
 
Introduction to molecular spectroscopy
Introduction to molecular spectroscopyIntroduction to molecular spectroscopy
Introduction to molecular spectroscopy
Neel Kamal Kalita
 
Interpretation of IR
Interpretation of IRInterpretation of IR
Interpretation of IR
Lokesh Patil
 
Compiler Optimization Presentation
Compiler Optimization PresentationCompiler Optimization Presentation
Compiler Optimization Presentation
19magnet
 
INFRARED SPECTROSCOPY
INFRARED SPECTROSCOPYINFRARED SPECTROSCOPY
INFRARED SPECTROSCOPY
Nur Fatihah
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
How c program execute in c program
How c program execute in c program How c program execute in c program
How c program execute in c program
Rumman Ansari
 
Properties EM
Properties EMProperties EM
Properties EM
tejas2019
 
Compiler design tutorial
Compiler design tutorialCompiler design tutorial
Compiler design tutorial
Varsha Shukla
 
Computational Spectroscopy in G03
Computational Spectroscopy in G03Computational Spectroscopy in G03
Computational Spectroscopy in G03
Inon Sharony
 
How To Start And Keep Conversations Going With Girls
How To Start And Keep Conversations Going With GirlsHow To Start And Keep Conversations Going With Girls
How To Start And Keep Conversations Going With Girls
George Hutton
 
Data Locality
Data LocalityData Locality
Data Locality
Syam Lal
 
COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1
Vishal Patil
 
Steps for Developing a 'C' program
 Steps for Developing a 'C' program Steps for Developing a 'C' program
Steps for Developing a 'C' program
Sahithi Naraparaju
 
Introduction to molecular spectroscopy
Introduction to molecular spectroscopyIntroduction to molecular spectroscopy
Introduction to molecular spectroscopy
Neel Kamal Kalita
 
Interpretation of IR
Interpretation of IRInterpretation of IR
Interpretation of IR
Lokesh Patil
 
Compiler Optimization Presentation
Compiler Optimization PresentationCompiler Optimization Presentation
Compiler Optimization Presentation
19magnet
 
INFRARED SPECTROSCOPY
INFRARED SPECTROSCOPYINFRARED SPECTROSCOPY
INFRARED SPECTROSCOPY
Nur Fatihah
 
Ad

Similar to C program compiler presentation (20)

A-overview.pptbb cpp introduction how cpp
A-overview.pptbb cpp introduction  how cppA-overview.pptbb cpp introduction  how cpp
A-overview.pptbb cpp introduction how cpp
compengwaelalahmar
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
guest5de1a5
 
01. Introduction.ppt
01. Introduction.ppt01. Introduction.ppt
01. Introduction.ppt
ReshmaR57
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
Rana Ehtisham Ul Haq
 
Compilers
CompilersCompilers
Compilers
Bense Tony
 
01. introduction
01. introduction01. introduction
01. introduction
babaaasingh123
 
Compiler Design Introduction
Compiler Design IntroductionCompiler Design Introduction
Compiler Design Introduction
Richa Sharma
 
Presentation1
Presentation1Presentation1
Presentation1
Zarin Tasnim
 
Presentation1
Presentation1Presentation1
Presentation1
Zarin Tasnim
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
Sarmad Ali
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Structure-Compiler-phases information about basics of compiler. Pdfpdf
Structure-Compiler-phases information  about basics of compiler. PdfpdfStructure-Compiler-phases information  about basics of compiler. Pdfpdf
Structure-Compiler-phases information about basics of compiler. Pdfpdf
ovidlivi91
 
Cs419 Compiler lec1&2 introduction
Cs419 Compiler lec1&2  introductionCs419 Compiler lec1&2  introduction
Cs419 Compiler lec1&2 introduction
Arab Open University and Cairo University
 
The Phases of a Compiler
The Phases of a CompilerThe Phases of a Compiler
The Phases of a Compiler
Radhika Talaviya
 
lec00-Introduction.pdf
lec00-Introduction.pdflec00-Introduction.pdf
lec00-Introduction.pdf
wigewej294
 
Lecture 1 introduction to language processors
Lecture 1  introduction to language processorsLecture 1  introduction to language processors
Lecture 1 introduction to language processors
Rebaz Najeeb
 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02
Tirumala Rao
 
Compiler design computer science engineering.ppt
Compiler design computer science engineering.pptCompiler design computer science engineering.ppt
Compiler design computer science engineering.ppt
khandareshobhit17
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
Dr. C.V. Suresh Babu
 
phases of a compiler
 phases of a compiler phases of a compiler
phases of a compiler
Ms.SHANTHI.S CSE
 
A-overview.pptbb cpp introduction how cpp
A-overview.pptbb cpp introduction  how cppA-overview.pptbb cpp introduction  how cpp
A-overview.pptbb cpp introduction how cpp
compengwaelalahmar
 
01. Introduction.ppt
01. Introduction.ppt01. Introduction.ppt
01. Introduction.ppt
ReshmaR57
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
Rana Ehtisham Ul Haq
 
Compiler Design Introduction
Compiler Design IntroductionCompiler Design Introduction
Compiler Design Introduction
Richa Sharma
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
Sarmad Ali
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Structure-Compiler-phases information about basics of compiler. Pdfpdf
Structure-Compiler-phases information  about basics of compiler. PdfpdfStructure-Compiler-phases information  about basics of compiler. Pdfpdf
Structure-Compiler-phases information about basics of compiler. Pdfpdf
ovidlivi91
 
lec00-Introduction.pdf
lec00-Introduction.pdflec00-Introduction.pdf
lec00-Introduction.pdf
wigewej294
 
Lecture 1 introduction to language processors
Lecture 1  introduction to language processorsLecture 1  introduction to language processors
Lecture 1 introduction to language processors
Rebaz Najeeb
 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02
Tirumala Rao
 
Compiler design computer science engineering.ppt
Compiler design computer science engineering.pptCompiler design computer science engineering.ppt
Compiler design computer science engineering.ppt
khandareshobhit17
 
Ad

Recently uploaded (20)

Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...
Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...
Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...
ManiMaran230751
 
Webinar On Steel Melting IIF of steel for rdso
Webinar  On Steel  Melting IIF of steel for rdsoWebinar  On Steel  Melting IIF of steel for rdso
Webinar On Steel Melting IIF of steel for rdso
KapilParyani3
 
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
gerogepatton
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
Structural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptxStructural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptx
gunjalsachin
 
Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...
Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...
Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...
ManiMaran230751
 
ISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdf
ISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdfISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdf
ISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)
[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)
[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)
하이플럭스 / HIFLUX Co., Ltd.
 
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
BeHappy728244
 
Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)
elelijjournal653
 
9aeb2aae-3b85-47a5-9776-154883bbae57.pdf
9aeb2aae-3b85-47a5-9776-154883bbae57.pdf9aeb2aae-3b85-47a5-9776-154883bbae57.pdf
9aeb2aae-3b85-47a5-9776-154883bbae57.pdf
RishabhGupta578788
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
UNIT-5-PPT Computer Control Power of Power System
UNIT-5-PPT Computer Control Power of Power SystemUNIT-5-PPT Computer Control Power of Power System
UNIT-5-PPT Computer Control Power of Power System
Sridhar191373
 
world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos12
 
HVAC Air Filter Equipment-Catalouge-Final.pdf
HVAC Air Filter Equipment-Catalouge-Final.pdfHVAC Air Filter Equipment-Catalouge-Final.pdf
HVAC Air Filter Equipment-Catalouge-Final.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
ENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdfENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdf
TAMILISAI R
 
Highway Engineering - Pavement materials
Highway Engineering - Pavement materialsHighway Engineering - Pavement materials
Highway Engineering - Pavement materials
AmrutaBhosale9
 
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning ModelEnhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
IRJET Journal
 
ISO 5011 Air Filter Catalogues .pdf
ISO 5011 Air Filter Catalogues      .pdfISO 5011 Air Filter Catalogues      .pdf
ISO 5011 Air Filter Catalogues .pdf
FILTRATION ENGINEERING & CUNSULTANT
 
Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...
Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...
Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...
ManiMaran230751
 
Webinar On Steel Melting IIF of steel for rdso
Webinar  On Steel  Melting IIF of steel for rdsoWebinar  On Steel  Melting IIF of steel for rdso
Webinar On Steel Melting IIF of steel for rdso
KapilParyani3
 
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
gerogepatton
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
Structural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptxStructural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptx
gunjalsachin
 
Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...
Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...
Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...
ManiMaran230751
 
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
BeHappy728244
 
Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)
elelijjournal653
 
9aeb2aae-3b85-47a5-9776-154883bbae57.pdf
9aeb2aae-3b85-47a5-9776-154883bbae57.pdf9aeb2aae-3b85-47a5-9776-154883bbae57.pdf
9aeb2aae-3b85-47a5-9776-154883bbae57.pdf
RishabhGupta578788
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
UNIT-5-PPT Computer Control Power of Power System
UNIT-5-PPT Computer Control Power of Power SystemUNIT-5-PPT Computer Control Power of Power System
UNIT-5-PPT Computer Control Power of Power System
Sridhar191373
 
world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos12
 
ENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdfENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdf
TAMILISAI R
 
Highway Engineering - Pavement materials
Highway Engineering - Pavement materialsHighway Engineering - Pavement materials
Highway Engineering - Pavement materials
AmrutaBhosale9
 
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning ModelEnhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
IRJET Journal
 

C program compiler presentation

  • 1. Rigvendra Kumar Vardhan M.TECH ECE Pondicherry University 1
  • 2.  Interpreters (direct execution)  Assemblers  Preprocessors  Text formatters (non-WYSIWYG)  Analysis tools CS 540 Spring 2013 GMU 2
  • 3.  Interpreter A program that reads a source program and produces the results of executing that program  Compiler A program that translates a program from one language (the source) to another (the target) 3
  • 4.  Correctness  Speed (runtime and compile time) Degrees of optimization Multiple passes  Space  Feedback to user  Debugging CS 540 Spring 2013 GMU 4
  • 5.  Interpreter Execution engine Program execution interleaved with analysis running = true; while (running) { analyze next statement; execute that statement; } May involve repeated analysis of some statements (loops, functions) 5
  • 6.  Read and analyze entire program  Translate to semantically equivalent program in another language Presumably easier to execute or more efficient Should “improve” the program in some fashion  Offline process Tradeoff: compile time overhead (preprocessing step) vs execution performance 6
  • 7.  Compilers FORTRAN, C, C++, Java, COBOL, etc. etc. Strong need for optimization, etc.  Interpreters PERL, Python, awk, sed, sh, csh, postscript printer, Java VM Effective if interpreter overhead is low relative to execution cost of language statements 7
  • 8. 8 Source code Compiler Assembly code Assembler Object code (machine code) Fully-resolved object code (machine code) Linker Loader Executable image
  • 9.  Series of program representations  Intermediate representations optimized for program manipulations of various kinds (checking, optimization)  Become more machine-specific, less language-specific as translation proceeds 9
  • 10.  First approximation Front end: analysis  Read source program and understand its structure and meaning Back end: synthesis  Generate equivalent target language program Source Front End Back End Target 10
  • 11.  Must recognize legal programs (& complain about illegal ones)  Must generate correct code  Must manage storage of all variables  Must agree with OS & linker on target format Source Front End Back End Target 11
  • 12.  Need some sort of Intermediate Representation (IR)  Front end maps source into IR  Back end maps IR to target machine code Source Front End Back End Target 12
  • 13. CS 540 Spring 2013 GMU 13 Scanner (lexical analysis) Parser (syntax analysis) Code Optimizer Semantic Analysis (IC generator) Code Generator Symbol Table Source language tokens Syntactic structure Intermediate Language Target language Intermediate Language
  • 14.  Lexical Analysis  Syntax Analysis  Semantic Analysis  Runtime environments  Code Generation  Code Optimization CS 540 Spring 2013 GMU 14
  • 15. 15 Source code (character stream) Lexical analysis Parsing Token stream Abstract syntax tree Intermediate Code Generation Intermediate code Optimization Code generation Intermediate code Assembly code Front end (machine-independent) Back end (machine-dependent)
  • 16. Scanner Parser source tokens IR  Split into two parts Scanner: Responsible for converting character stream to token stream  Also strips out white space, comments Parser: Reads token stream; generates IR  Both of these can be generated automatically Source language specified by a formal grammar Tools read the grammar and generate scanner & parser (either table-driven or hard coded) 16
  • 17.  Token stream: Each significant lexical chunk of the program is represented by a token Operators & Punctuation: {}[]!+-=*;: … Keywords: if while return goto Identifiers: id & actual name Constants: kind & value; int, floating-point character, string, … 17
  • 18.  Input text // this statement does very little if (x >= y) y = 42;  Token Stream IF LPAREN ID(x) GEQ ID(y) RPAREN ID(y) BECOMES INT(42) SCOLON Note: tokens are atomic items, not character strings 18
  • 19.  Many different forms (Engineering tradeoffs)  Common output from a parser is an abstract syntax tree Essential meaning of the program without the syntactic noise 19
  • 20.  Token Stream Input  Abstract Syntax Tree 20 IF LPAREN ID(x) GEQ ID(y) RPAREN ID(y) BECOMES INT(42) SCOLON ifStmt >= ID(x) ID(y) assign ID(y) INT(42)
  • 21.  During or (more common) after parsing Type checking Check for language requirements like “declare before use”, type compatibility Preliminary resource allocation Collect other information needed by back end analysis and code generation 21
  • 22.  Responsibilities Translate IR into target machine code Should produce fast, compact code Should use machine resources effectively Registers Instructions Memory hierarchy 22
  • 23.  Typically split into two major parts with sub phases “Optimization” – code improvements May well translate parser IR into another IR Code generation Instruction selection & scheduling Register allocation 23
  • 24.  Input if (x >= y) y = 42;  Output mov eax,[ebp+16] cmp eax,[ebp-8] jl L17 mov [ebp-8],42 L17: 24
  • 25. lda $30,-32($30) stq $26,0($30) stq $15,8($30) bis $30,$30,$15 bis $16,$16,$1 stl $1,16($15) lds $f1,16($15) sts $f1,24($15) ldl $5,24($15) bis $5,$5,$2 s4addq $2,0,$3 ldl $4,16($15) mull $4,$3,$2 ldl $3,16($15) addq $3,1,$4 mull $2,$4,$2 ldl $3,16($15) addq $3,1,$4 mull $2,$4,$2 stl $2,20($15) ldl $0,20($15) br $31,$33 $33: bis $15,$15,$30 ldq $26,0($30) ldq $15,8($30) addq $30,32,$30 ret $31,($26),1 Optimized Code 25 s4addq $16,0,$0 mull $16,$0,$0 addq $16,1,$16 mull $0,$16,$0 mull $0,$16,$0 ret $31,($26),1 Unoptimized Code
  • 26. 26 Source code (character stream) Lexical analysis Parsing Token stream Abstract syntax tree (AST) Semantic Analysis if (b == 0) a = b; if ( b == 0 ) a = b ; if == b 0 = a b if == int b int 0 = int a lvalue int b boolean Decorated AST ; int ;
  • 27. Intermediate Code Generation Optimization Code generation 27 if boolean == int ; int b int 0 = int a lvalue int b CJUMP == MEM + fp 8 CONST MOVE 0 MEM MEM fp 4 fp 8 NOP + + CJUMP == CONST MOVE CX NOP 0 DX CX CMP CX, 0 CMOVZ DX,CX
  • 28.  Compiler techniques are everywhere Parsing (little languages, interpreters) Database engines AI: domain-specific languages Text processing Tex/LaTex -> dvi -> Postscript -> pdf Hardware: VHDL; model-checking tools Mathematics (Mathematica, Matlab) 28
  • 29.  Fascinating blend of theory and engineering Direct applications of theory to practice Parsing, scanning, static analysis Some very difficult problems (NP-hard or worse) Resource allocation, “optimization”, etc. Need to come up with good-enough solutions 29
  • 30.  Ideas from many parts of CSE AI: Greedy algorithms, heuristic search Algorithms: graph algorithms, dynamic programming, approximation algorithms Theory: Grammars DFAs and PDAs, pattern matching, fixed-point algorithms Systems: Allocation & naming, synchronization, locality Architecture: pipelines & hierarchy management, instruction set use 30
  • 31.  program ::= statement | program statement  statement ::= assignStmt | ifStmt  assignStmt ::= id = expr ;  ifStmt ::= if ( expr ) stmt  expr ::= id | int | expr + expr  Id ::= a | b | c | i | j | k | n | x | y | z  int ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 31
  • 32.  There are several syntax notations for productions in common use; all mean the same thing ifStmt ::= if ( expr ) stmt ifStmt if ( expr ) stmt <ifStmt> ::= if ( <expr> ) <stmt> 32
  • 33. program ::= statement | program statement statement ::= assignStmt | ifStmt assignStmt ::= id = expr ; ifStmt ::= if ( expr ) stmt expr ::= id | int | expr + expr id ::= a | b | c | i | j | k | n | x | y | z int ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 a = 1 ; if ( a + 1 ) b = 2 ; 33 program program ID(a) expr stmt stmt assign int (1) ifStmt expr stmt expr expr assign ID(a) int (1) ID(b) expr int (2)
  • 34. 34

Editor's Notes

  • #12: Au02: from Cooper’s slides
  • #13: Au02: from Cooper’s slides
  • #17: Lex, yacc examples
  • #20: Au02: plan on drawing something here
  • #22: Au02: plan on drawing something here
  • #31: Au02: taken from one of Cooper’s slides
  • #32: This is similar to what is used in the Algol Report