SlideShare a Scribd company logo
1 of 21
Chapter 1
Introduction to Compilers
Compilers and Interpreters
 “Compilation”
◦ Translation of a program written in a
source language into a semantically
equivalent program written in a target
language
◦ Oversimplified view:
2
Compiler
Error messages
Source
Program
Target
Program
Input
Output
Compilers and Interpreters
(cont’d)
 “Interpretation”
◦ Performing the operations implied by the
source program
◦ Oversimplified view:
3
Interpreter
Source
Program
Input
Output
Error messages
Compilers and Interpreters
(cont’d)
 Compiler: a program that translates an
executable program in one language into
an executable program in another
language
 Interpreter: a program that reads an
executable program and produces the
results of running that program
4
The Analysis-Synthesis Model of
Compilation
 There are two parts to compilation:
◦ Analysis
 Breaks up source program into pieces and
imposes a grammatical structure
 Creates intermediate representation of
source program
 Determines the operations and records them in
a tree structure, syntax tree
 Known as front end of compiler
5
The Analysis-Synthesis Model of
Compilation (cont’d)
◦ Synthesis
 Constructs target program from intermediate
representation
 Takes the tree structure and translates the
operations into the target program
 Known as back end of compiler
6
Other Tools that Use the
Analysis-Synthesis Model
 Editors (syntax highlighting)
 Pretty printers (e.g. Doxygen)
 Static checkers (e.g. Lint and Splint)
 Interpreters
 Text formatters (e.g. TeX and LaTeX)
 Silicon compilers (e.g. VHDL)
 Query interpreters/compilers
(Databases)
7
A language-processing
system
8
Preprocessor
Compiler
Assembler
Linker
Skeletal Source Program
Source Program
Target Assembly Program
Relocatable Object Code
Absolute Machine Code
Libraries and
Relocatable Object Files
Try for example:
gcc -v myprog.c
Analysis
 In compiling, analysis has three
phases:
◦ Linear analysis: stream of characters
read from left-to-right and grouped into
tokens; known as lexical analysis or
scanning
◦ Hierarchical analysis: tokens grouped
hierarchically with collective meaning;
known as parsing or syntax analysis
◦ Semantic analysis: check if the program
components fit together meaningfully
9
Lexical analysis
 Characters grouped into tokens.
10
Syntax analysis (Parsing)
 Grouping tokens into grammatical phrases
 Character groups recorded in symbol table
 Represented by a parse tree
11
Syntax analysis (cont’d)
 Hierarchical structure usually
expressed by recursive rules
 Rules for definition of expression:
12
Semantic analysis
 Checks source program for semantic
errors
 Gathers type information for
subsequent code generation (type
checking)
 Identifies operator and operands of
expressions and statements
13
Phases of a compiler
14
Symbol-Table Management
 Symbol table – data structure with a
record for each identifier and its
attributes
 Attributes include storage allocation,
type, scope, etc
 All the compiler phases insert and
modify the symbol table
15
Intermediate code generation
 Program representation for an
abstract machine
 Should have two properties
◦ Easy to produce
◦ Easy to translate into target program
 Three-address code is a commonly
used form – similar to assembly
language
16
Code optimization and generation
 Code Optimization
◦ Improve intermediate code by
producing code that runs faster
 Code Generation
◦ Generate target code, which is machine
code or assembly code
17
The Phases of a Compiler
18
Phase Output Sample
Programmer (source code
producer)
Source string A=B+C;
Scanner (performs lexical
analysis)
Token string ‘A’, ‘=’, ‘B’, ‘+’, ‘C’,
‘;’
And symbol table with names
Parser (performs syntax analysis
based on the grammar of the
programming language)
Parse tree or abstract syntax
tree
;
|
=
/ 
A +
/ 
B C
Semantic analyzer (type checking,
etc)
Annotated parse tree or
abstract syntax tree
Intermediate code generator Three-address code, quads, or
RTL
int2fp B t1
+ t1 C t2
:= t2 A
Optimizer Three-address code, quads, or
RTL
int2fp B t1
+ t1 #2.3 A
Code generator Assembly code MOVF #2.3,r1
ADDF2 r1,r2
The Grouping of Phases
 Compiler front and back ends:
◦ Front end:
 Analysis steps + Intermediate code generation
 Depends primarily on the source language
 Machine independent
◦ Back end:
 Code optimization and generation
 Independent of source language
 Machine dependent
19
The Grouping of Phases
(cont’d)
 Compiler passes:
◦ A collection of phases is done only once (single
pass) or multiple times (multi pass)
 Single pass: reading input, processing, and producing
output by one large compiler program; usually runs faster
 Multi pass: compiler split into smaller programs, each
making a pass over the source; performs better code
optimization
20
Compiler-Construction Tools
 Software development tools are
available to implement one or more
compiler phases
◦ Scanner generators
◦ Parser generators
◦ Syntax-directed translation engines
◦ Automatic code generators
◦ Data-flow engines
21

More Related Content

What's hot

Techniques & applications of Compiler
Techniques & applications of CompilerTechniques & applications of Compiler
Techniques & applications of CompilerPreethi AKNR
 
Language and Processors for Requirements Specification
Language and Processors for Requirements SpecificationLanguage and Processors for Requirements Specification
Language and Processors for Requirements Specificationkirupasuchi1996
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 
System Programing Unit 1
System Programing Unit 1System Programing Unit 1
System Programing Unit 1Manoj Patil
 
Introduction to systems programming
Introduction to systems programmingIntroduction to systems programming
Introduction to systems programmingMukesh Tekwani
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Assemblers: Ch03
Assemblers: Ch03Assemblers: Ch03
Assemblers: Ch03desta_gebre
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignKuppusamy P
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introductionRana Ehtisham Ul Haq
 

What's hot (20)

Techniques & applications of Compiler
Techniques & applications of CompilerTechniques & applications of Compiler
Techniques & applications of Compiler
 
Back patching
Back patchingBack patching
Back patching
 
Language and Processors for Requirements Specification
Language and Processors for Requirements SpecificationLanguage and Processors for Requirements Specification
Language and Processors for Requirements Specification
 
Role-of-lexical-analysis
Role-of-lexical-analysisRole-of-lexical-analysis
Role-of-lexical-analysis
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Intro to assembly language
Intro to assembly languageIntro to assembly language
Intro to assembly language
 
System Programing Unit 1
System Programing Unit 1System Programing Unit 1
System Programing Unit 1
 
Compilers
CompilersCompilers
Compilers
 
Introduction to systems programming
Introduction to systems programmingIntroduction to systems programming
Introduction to systems programming
 
Assembly language
Assembly languageAssembly language
Assembly language
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
Assemblers: Ch03
Assemblers: Ch03Assemblers: Ch03
Assemblers: Ch03
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Code generation
Code generationCode generation
Code generation
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 

Similar to 1 - Introduction to Compilers.ppt

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 design and lexical analyser
Compiler design and lexical analyserCompiler design and lexical analyser
Compiler design and lexical analyserabhishek gupta
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilersvijaya603274
 
Chapter One
Chapter OneChapter One
Chapter Onebolovv
 
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
 
C Language Compiler in Python c language compiler using python
C Language Compiler in Python c language compiler using pythonC Language Compiler in Python c language compiler using python
C Language Compiler in Python c language compiler using pythonAmmarAhmed900673
 
Chapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfChapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfDrIsikoIsaac
 
Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in detailskazi_aihtesham
 
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docxvenkatapranaykumarGa
 
unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfDrIsikoIsaac
 

Similar to 1 - Introduction to Compilers.ppt (20)

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 design
Compiler designCompiler design
Compiler design
 
Cd econtent link1
Cd econtent link1Cd econtent link1
Cd econtent link1
 
Ch1 (1).ppt
Ch1 (1).pptCh1 (1).ppt
Ch1 (1).ppt
 
CD U1-5.pptx
CD U1-5.pptxCD U1-5.pptx
CD U1-5.pptx
 
Chapter#01 cc
Chapter#01 ccChapter#01 cc
Chapter#01 cc
 
Compiler design and lexical analyser
Compiler design and lexical analyserCompiler design and lexical analyser
Compiler design and lexical analyser
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
 
Chapter One
Chapter OneChapter One
Chapter One
 
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
 
C Language Compiler in Python c language compiler using python
C Language Compiler in Python c language compiler using pythonC Language Compiler in Python c language compiler using python
C Language Compiler in Python c language compiler using python
 
Chapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfChapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdf
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
 
Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in details
 
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
 
Compiler
Compiler Compiler
Compiler
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Assignment1
Assignment1Assignment1
Assignment1
 
unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdf
 

Recently uploaded

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
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
 
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
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Recently uploaded (20)

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
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
 
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
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

1 - Introduction to Compilers.ppt

  • 2. Compilers and Interpreters  “Compilation” ◦ Translation of a program written in a source language into a semantically equivalent program written in a target language ◦ Oversimplified view: 2 Compiler Error messages Source Program Target Program Input Output
  • 3. Compilers and Interpreters (cont’d)  “Interpretation” ◦ Performing the operations implied by the source program ◦ Oversimplified view: 3 Interpreter Source Program Input Output Error messages
  • 4. Compilers and Interpreters (cont’d)  Compiler: a program that translates an executable program in one language into an executable program in another language  Interpreter: a program that reads an executable program and produces the results of running that program 4
  • 5. The Analysis-Synthesis Model of Compilation  There are two parts to compilation: ◦ Analysis  Breaks up source program into pieces and imposes a grammatical structure  Creates intermediate representation of source program  Determines the operations and records them in a tree structure, syntax tree  Known as front end of compiler 5
  • 6. The Analysis-Synthesis Model of Compilation (cont’d) ◦ Synthesis  Constructs target program from intermediate representation  Takes the tree structure and translates the operations into the target program  Known as back end of compiler 6
  • 7. Other Tools that Use the Analysis-Synthesis Model  Editors (syntax highlighting)  Pretty printers (e.g. Doxygen)  Static checkers (e.g. Lint and Splint)  Interpreters  Text formatters (e.g. TeX and LaTeX)  Silicon compilers (e.g. VHDL)  Query interpreters/compilers (Databases) 7
  • 8. A language-processing system 8 Preprocessor Compiler Assembler Linker Skeletal Source Program Source Program Target Assembly Program Relocatable Object Code Absolute Machine Code Libraries and Relocatable Object Files Try for example: gcc -v myprog.c
  • 9. Analysis  In compiling, analysis has three phases: ◦ Linear analysis: stream of characters read from left-to-right and grouped into tokens; known as lexical analysis or scanning ◦ Hierarchical analysis: tokens grouped hierarchically with collective meaning; known as parsing or syntax analysis ◦ Semantic analysis: check if the program components fit together meaningfully 9
  • 10. Lexical analysis  Characters grouped into tokens. 10
  • 11. Syntax analysis (Parsing)  Grouping tokens into grammatical phrases  Character groups recorded in symbol table  Represented by a parse tree 11
  • 12. Syntax analysis (cont’d)  Hierarchical structure usually expressed by recursive rules  Rules for definition of expression: 12
  • 13. Semantic analysis  Checks source program for semantic errors  Gathers type information for subsequent code generation (type checking)  Identifies operator and operands of expressions and statements 13
  • 14. Phases of a compiler 14
  • 15. Symbol-Table Management  Symbol table – data structure with a record for each identifier and its attributes  Attributes include storage allocation, type, scope, etc  All the compiler phases insert and modify the symbol table 15
  • 16. Intermediate code generation  Program representation for an abstract machine  Should have two properties ◦ Easy to produce ◦ Easy to translate into target program  Three-address code is a commonly used form – similar to assembly language 16
  • 17. Code optimization and generation  Code Optimization ◦ Improve intermediate code by producing code that runs faster  Code Generation ◦ Generate target code, which is machine code or assembly code 17
  • 18. The Phases of a Compiler 18 Phase Output Sample Programmer (source code producer) Source string A=B+C; Scanner (performs lexical analysis) Token string ‘A’, ‘=’, ‘B’, ‘+’, ‘C’, ‘;’ And symbol table with names Parser (performs syntax analysis based on the grammar of the programming language) Parse tree or abstract syntax tree ; | = / A + / B C Semantic analyzer (type checking, etc) Annotated parse tree or abstract syntax tree Intermediate code generator Three-address code, quads, or RTL int2fp B t1 + t1 C t2 := t2 A Optimizer Three-address code, quads, or RTL int2fp B t1 + t1 #2.3 A Code generator Assembly code MOVF #2.3,r1 ADDF2 r1,r2
  • 19. The Grouping of Phases  Compiler front and back ends: ◦ Front end:  Analysis steps + Intermediate code generation  Depends primarily on the source language  Machine independent ◦ Back end:  Code optimization and generation  Independent of source language  Machine dependent 19
  • 20. The Grouping of Phases (cont’d)  Compiler passes: ◦ A collection of phases is done only once (single pass) or multiple times (multi pass)  Single pass: reading input, processing, and producing output by one large compiler program; usually runs faster  Multi pass: compiler split into smaller programs, each making a pass over the source; performs better code optimization 20
  • 21. Compiler-Construction Tools  Software development tools are available to implement one or more compiler phases ◦ Scanner generators ◦ Parser generators ◦ Syntax-directed translation engines ◦ Automatic code generators ◦ Data-flow engines 21