SlideShare a Scribd company logo
1 of 34
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)

Variables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaVariables & Data Types In Python | Edureka
Variables & Data Types In Python | Edureka
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
 
Strings in C
Strings in CStrings in C
Strings in C
 
java token
java tokenjava token
java token
 
Function in C program
Function in C programFunction in C program
Function in C program
 
C Tokens
C TokensC Tokens
C Tokens
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Strings
StringsStrings
Strings
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Operator.ppt
Operator.pptOperator.ppt
Operator.ppt
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
Compiler design
Compiler designCompiler design
Compiler design
 
Debugging
DebuggingDebugging
Debugging
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
How to download and install Python - lesson 2
How to download and install Python - lesson 2How to download and install Python - lesson 2
How to download and install Python - lesson 2
 

Viewers also liked

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 EMtejas2019
 
Compiler design tutorial
Compiler design tutorialCompiler design tutorial
Compiler design tutorialVarsha Shukla
 
Computational Spectroscopy in G03
Computational Spectroscopy in G03Computational Spectroscopy in G03
Computational Spectroscopy in G03Inon 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 GirlsGeorge Hutton
 
Data Locality
Data LocalityData Locality
Data LocalitySyam Lal
 
COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1Vishal Patil
 
Steps for Developing a 'C' program
 Steps for Developing a 'C' program Steps for Developing a 'C' program
Steps for Developing a 'C' programSahithi Naraparaju
 
Introduction to molecular spectroscopy
Introduction to molecular spectroscopyIntroduction to molecular spectroscopy
Introduction to molecular spectroscopyNeel Kamal Kalita
 
Interpretation of IR
Interpretation of IRInterpretation of IR
Interpretation of IRLokesh Patil
 
Compiler Optimization Presentation
Compiler Optimization PresentationCompiler Optimization Presentation
Compiler Optimization Presentation19magnet
 
INFRARED SPECTROSCOPY
INFRARED SPECTROSCOPYINFRARED SPECTROSCOPY
INFRARED SPECTROSCOPYNur Fatihah
 

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
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Properties EM
Properties EMProperties EM
Properties EM
 
Infrared spectoscopy
Infrared spectoscopyInfrared spectoscopy
Infrared spectoscopy
 
Compiler design tutorial
Compiler design tutorialCompiler design tutorial
Compiler design tutorial
 
Computational Spectroscopy in G03
Computational Spectroscopy in G03Computational Spectroscopy in G03
Computational Spectroscopy in G03
 
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
 
Data Locality
Data LocalityData Locality
Data Locality
 
Compiler design
Compiler designCompiler design
Compiler design
 
Infrared spectroscopy
Infrared spectroscopyInfrared spectroscopy
Infrared spectroscopy
 
COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1
 
Lex
LexLex
Lex
 
Steps for Developing a 'C' program
 Steps for Developing a 'C' program Steps for Developing a 'C' program
Steps for Developing a 'C' program
 
Unit 1 cd
Unit 1 cdUnit 1 cd
Unit 1 cd
 
Introduction to molecular spectroscopy
Introduction to molecular spectroscopyIntroduction to molecular spectroscopy
Introduction to molecular spectroscopy
 
Interpretation of IR
Interpretation of IRInterpretation of IR
Interpretation of IR
 
Compiler Optimization Presentation
Compiler Optimization PresentationCompiler Optimization Presentation
Compiler Optimization Presentation
 
INFRARED SPECTROSCOPY
INFRARED SPECTROSCOPYINFRARED SPECTROSCOPY
INFRARED SPECTROSCOPY
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 

Similar to C program compiler presentation

Chapter Eight(1)
Chapter Eight(1)Chapter Eight(1)
Chapter Eight(1)bolovv
 
1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.pptRakesh Kumar
 
Code Analysis-run time error prediction
Code Analysis-run time error predictionCode Analysis-run time error prediction
Code Analysis-run time error predictionNIKHIL NAWATHE
 
Chapter One
Chapter OneChapter One
Chapter Onebolovv
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to ProgrammingChaffey College
 
Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)omercomail
 
Compiler design and lexical analyser
Compiler design and lexical analyserCompiler design and lexical analyser
Compiler design and lexical analyserabhishek gupta
 
Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT
Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDTEclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT
Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDTElena Laskavaia
 
1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdfSemsemSameer1
 

Similar to C program compiler presentation (20)

Introduction to Compilers | Phases & Structure
Introduction to Compilers | Phases & StructureIntroduction to Compilers | Phases & Structure
Introduction to Compilers | Phases & Structure
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Chapter Eight(1)
Chapter Eight(1)Chapter Eight(1)
Chapter Eight(1)
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
 
7068458.ppt
7068458.ppt7068458.ppt
7068458.ppt
 
Ss ui lecture 2
Ss ui lecture 2Ss ui lecture 2
Ss ui lecture 2
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
Presentation compiler design
Presentation compiler designPresentation compiler design
Presentation compiler design
 
1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt
 
01. introduction
01. introduction01. introduction
01. introduction
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
Code Analysis-run time error prediction
Code Analysis-run time error predictionCode Analysis-run time error prediction
Code Analysis-run time error prediction
 
Chapter One
Chapter OneChapter One
Chapter One
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
 
Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)
 
Compiler design and lexical analyser
Compiler design and lexical analyserCompiler design and lexical analyser
Compiler design and lexical analyser
 
Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT
Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDTEclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT
Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT
 
1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf
 

Recently uploaded

Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixingviprabot1
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 

Recently uploaded (20)

Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixing
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 

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

  1. Au02: from Cooper’s slides
  2. Au02: from Cooper’s slides
  3. Lex, yacc examples
  4. Au02: plan on drawing something here
  5. Au02: plan on drawing something here
  6. Au02: taken from one of Cooper’s slides
  7. This is similar to what is used in the Algol Report