SlideShare a Scribd company logo
COMPILER INTRODUCTION
4TH STAGE
Lecturer:
Hanan Kamal
International University of Erbil
College of Science
Computer Science Department
Outlines
• Overview and History
• What Do Compilers Do?
• The Structure of a Compiler
• The Syntax and Semantics of Programming Languages
2
Overview and History (1)
• Cause
• Software for early computers was written in assembly language
• The benefits of reusing software on different CPUs started to
become significantly greater than the cost of writing a compiler
• The first real compiler
• FORTRAN compilers of the late 1950s
3
Overview and History (2)
• Compiler technology
• is more broadly applicable and has been employed in
rather unexpected areas.
• Text-formatting languages
• Command languages of OS
• Query languages of Database systems
4
What Do Compilers Do (1)
• A compiler acts as a translator,
transforming human-oriented programming languages
into computer-oriented machine languages.
Programming
Language
(Source)
Compiler
Machine
Language
(Target)
5
Compilers
• “Compilation”
• Translation of a program written in a source language into a
semantically equivalent program written in a target language
Compiler
Error messages
Source
Program
Target
Program
Input
Output
6
Interpreters
• “Interpretation”
• Performing the operations implied by the source program
Interpreter
Source
Program
Input
Output
Error messages
7
What Do Compilers Do (2)
• Compilers may generate three types of code:
• Pure Machine Code
• Machine instruction set without assuming the existence of any operating
system or library.
• Mostly being OS or embedded applications.
• Augmented Machine Code
• Code with OS routines and runtime support routines.
• More often
• Virtual Machine Code
• Virtual instructions, can be run on any architecture with a virtual
machine interpreter or a just-in-time compiler
• Ex. Java
8
What Do Compilers Do (3)
• Another way that compilers
differ from one another is in the format of the target
machine code they generate:
• Assembly or other source format
• Relocatable binary
• Relative address
• A linkage step is required
• Absolute binary
• Absolute address
• Can be executed directly
9
• Any compiler must perform two major tasks
• Analysis of the source program
• Synthesis of a machine-language program
The Structure of a Compiler (1)
Compiler
Analysis Synthesis
10
The Structure of a Compiler (2)
Scanner Parser
Semantic
Routines
Code
Generator
Optimizer
Source
Program Tokens Syntactic
Structure
Symbol and
Attribute
Tables
(Used by all Phases of The Compiler)
(Character
Stream)
Intermediate
Representation
Target machine code
11
The Structure of a Compiler (3)
Scanner Parser
Semantic
Routines
Code
Generator
Optimizer
Source
Program Tokens Syntactic
Structure
Symbol and
Attribute
Tables
(Used by all
Phases of
The Compiler)
Scanner
 The scanner begins the analysis of the source program by
reading the input, character by character, and grouping
characters into individual words and symbols (tokens)
 RE ( Regular expression )
 NFA ( Non-deterministic Finite Automata )
 DFA ( Deterministic Finite Automata )
 LEX
(Character
Stream)
Intermediate
Representation
Target machine code
12
The Structure of a Compiler (4)
Scanner Parser
Semantic
Routines
Code
Generator
Optimizer
Source
Program Tokens Syntactic
Structure
Symbol and
Attribute
Tables
(Used by all
Phases of
The Compiler)
Parser
 Given a formal syntax specification (typically as a context-
free grammar [CFG] ), the parse reads tokens and groups
them into units as specified by the productions of the CFG
being used.
 As syntactic structure is recognized, the parser either calls
corresponding semantic routines directly or builds a
syntax tree.
 CFG ( Context-Free Grammar )
 BNF ( Backus-Naur Form )
 GAA ( Grammar Analysis Algorithms )
 LL, LR, SLR, LALR Parsers
 YACC
(Character
Stream)
Intermediate
Representation
Target machine code
13
The Structure of a Compiler (5)
Scanner Parser
Semantic
Routines
Code
Generator
Optimizer
Source
Program
(Character
Stream)
Tokens Syntactic
Structure
Intermediate
Representation
Symbol and
Attribute
Tables
(Used by all
Phases of
The Compiler)
Semantic Routines
 Perform two functions
 Check the static semantics of each construct
 Do the actual translation
 The heart of a compiler
 Syntax Directed Translation
 Semantic Processing Techniques
 IR (Intermediate Representation)
Target machine code
14
The Structure of a Compiler (6)
Scanner Parser
Semantic
Routines
Code
Generator
Optimizer
Source
Program Tokens Syntactic
Structure
Symbol and
Attribute
Tables
(Used by all
Phases of
The Compiler)
Optimizer
 The IR code generated by the semantic routines is
analyzed and transformed into functionally equivalent but
improved IR code
 This phase can be very complex and slow
 Peephole optimization
 loop optimization, register allocation, code scheduling
 Register and Temporary Management
 Peephole Optimization
(Character
Stream)
Intermediate
Representation
Target machine code
15
The Structure of a Compiler (7)
Source
Program
(Character
Stream)
Scanner
Tokens
Parser
Syntactic
Structure
Semantic
Routines
Intermediate
Representation
Optimizer
Code
Generator
Code Generator
 Interpretive Code Generation
 Generating Code from Tree/Dag
 Grammar-Based Code Generator
Target machine code
16
Preprocessors, Compilers, Assemblers, and
Linkers
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 -S myprog.c
javap Class
17
Analysis of Source Programs
lexical analyzer
syntax analyzer
semantic analyzer
source program
tokens
parse trees
parse trees
18
Lexical Analysis
tokens
19
Syntax Analysis
parse tree
20
Semantic Analysis
type checking
type conversion
21
Symbol Table
• There is a record for each identifier
• The attributes include name, type, location,
etc.
22
Synthesis of Object Code
intermediate code generator
code optimizer
code generator
parse tree & symbol table
intermediate code
optimized intermediate code
target program
23
Intermediate Code Generation
24
Code Optimization
25
Code Generation
26
Compiler Steps
27
The Syntax and Semantics of
Programming Language
• A programming language must include the
specification of syntax (structure) and semantics
(meaning).
• Syntax typically means the context-free syntax
because of the almost universal use of context-
free-grammar (CFGs)
• Ex.
• a = b + c is syntactically legal
• b + c = a is illegal
28
The Syntax and Semantics of
Programming Language
• The semantics of a programming language are
commonly divided into two classes:
• Static semantics
• Semantics rules that can be checked at compiled time.
• Ex. The type and number of a function’s arguments
• Runtime semantics
• Semantics rules that can be checked only at run time
29
Compiler
30
Thanks for your attention
Questions???
31

More Related Content

What's hot (20)

PDF
COMPILER DESIGN- Introduction & Lexical Analysis:
Jyothishmathi Institute of Technology and Science Karimnagar
 
PPSX
Compilers
Jayanga V. Liyanage
 
PPT
Compiler
IGZ Software house
 
PPT
Introduction to course
nikit meshram
 
PPT
Introduction to Compiler design
Dr. C.V. Suresh Babu
 
PPT
Classification of Compilers
Sarmad Ali
 
PDF
Compiler Design Lecture Notes
FellowBuddy.com
 
PPTX
Presentation compiler design
Md. Touhidur Rahman
 
PDF
Compiler unit 1
BBDITM LUCKNOW
 
PPT
Introduction to compiler
Abha Damani
 
PDF
Compiler Construction | Lecture 1 | What is a compiler?
Eelco Visser
 
PPT
Introduction to Compiler
Radhakrishnan Chinnusamy
 
PPT
Passes of compilers
Vairavel C
 
PDF
Lecture1 introduction compilers
Mahesh Kumar Chelimilla
 
PPTX
Syntax directed-translation
Junaid Lodhi
 
PDF
Lecture 1 Compiler design , computation
Rebaz Najeeb
 
PPT
Cd econtent link1
suganyasanjai
 
PPT
what is compiler and five phases of compiler
adilmehmood93
 
PPTX
Pros and cons of c as a compiler language
Ashok Raj
 
PDF
Different phases of a compiler
Sumit Sinha
 
COMPILER DESIGN- Introduction & Lexical Analysis:
Jyothishmathi Institute of Technology and Science Karimnagar
 
Introduction to course
nikit meshram
 
Introduction to Compiler design
Dr. C.V. Suresh Babu
 
Classification of Compilers
Sarmad Ali
 
Compiler Design Lecture Notes
FellowBuddy.com
 
Presentation compiler design
Md. Touhidur Rahman
 
Compiler unit 1
BBDITM LUCKNOW
 
Introduction to compiler
Abha Damani
 
Compiler Construction | Lecture 1 | What is a compiler?
Eelco Visser
 
Introduction to Compiler
Radhakrishnan Chinnusamy
 
Passes of compilers
Vairavel C
 
Lecture1 introduction compilers
Mahesh Kumar Chelimilla
 
Syntax directed-translation
Junaid Lodhi
 
Lecture 1 Compiler design , computation
Rebaz Najeeb
 
Cd econtent link1
suganyasanjai
 
what is compiler and five phases of compiler
adilmehmood93
 
Pros and cons of c as a compiler language
Ashok Raj
 
Different phases of a compiler
Sumit Sinha
 

Similar to 1 introduction to compiler (20)

PPT
Cd unit i
Abhimanyu Mishra
 
PPT
cd-greeshma (1).ppt compiler design unit 1
vrawat4
 
PPT
cd-greeshma.ppt online downloaded for study
yashashreeg2307
 
PPTX
Introduction to Compiler Design Structutre, Phases
Vanitha Alagesan
 
PPTX
Compiler Design Slides for Third Year Computer Science and Engineering
DrRajurkarArchanaMil
 
PDF
Structure-Compiler-phases information about basics of compiler. Pdfpdf
ovidlivi91
 
PPT
Compiler Design Basics
Akhil Kaushik
 
PPTX
CD U1-5.pptx
Himajanaidu2
 
PPTX
Chapter-1.pptx compiler Design Course Material
gadisaAdamu
 
PPT
Chap01-Intro.ppt
AhmadAbubaker16
 
PDF
Chapter#01 cc
abdulbaki3
 
PPT
Compiler Design Basics
Akhil Kaushik
 
PPT
Chapter One
bolovv
 
PPT
Introduction to compiler design and phases of compiler
Ranjeet Reddy
 
PPT
A basic introduction to compiler design.ppt
pandaashirbad9
 
PPT
A basic introduction to compiler design.ppt
pandaashirbad9
 
PPTX
COMPILER DESIGN PPTS.pptx
MUSHAMHARIKIRAN6737
 
PPTX
Lecture 1 introduction to language processors
Rebaz Najeeb
 
PDF
Lecture 01 introduction to compiler
Iffat Anjum
 
PPTX
Ss ui lecture 2
Avinash Kapse
 
Cd unit i
Abhimanyu Mishra
 
cd-greeshma (1).ppt compiler design unit 1
vrawat4
 
cd-greeshma.ppt online downloaded for study
yashashreeg2307
 
Introduction to Compiler Design Structutre, Phases
Vanitha Alagesan
 
Compiler Design Slides for Third Year Computer Science and Engineering
DrRajurkarArchanaMil
 
Structure-Compiler-phases information about basics of compiler. Pdfpdf
ovidlivi91
 
Compiler Design Basics
Akhil Kaushik
 
CD U1-5.pptx
Himajanaidu2
 
Chapter-1.pptx compiler Design Course Material
gadisaAdamu
 
Chap01-Intro.ppt
AhmadAbubaker16
 
Chapter#01 cc
abdulbaki3
 
Compiler Design Basics
Akhil Kaushik
 
Chapter One
bolovv
 
Introduction to compiler design and phases of compiler
Ranjeet Reddy
 
A basic introduction to compiler design.ppt
pandaashirbad9
 
A basic introduction to compiler design.ppt
pandaashirbad9
 
COMPILER DESIGN PPTS.pptx
MUSHAMHARIKIRAN6737
 
Lecture 1 introduction to language processors
Rebaz Najeeb
 
Lecture 01 introduction to compiler
Iffat Anjum
 
Ss ui lecture 2
Avinash Kapse
 
Ad

Recently uploaded (20)

PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Python basic programing language for automation
DanialHabibi2
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Python basic programing language for automation
DanialHabibi2
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Ad

1 introduction to compiler

  • 1. COMPILER INTRODUCTION 4TH STAGE Lecturer: Hanan Kamal International University of Erbil College of Science Computer Science Department
  • 2. Outlines • Overview and History • What Do Compilers Do? • The Structure of a Compiler • The Syntax and Semantics of Programming Languages 2
  • 3. Overview and History (1) • Cause • Software for early computers was written in assembly language • The benefits of reusing software on different CPUs started to become significantly greater than the cost of writing a compiler • The first real compiler • FORTRAN compilers of the late 1950s 3
  • 4. Overview and History (2) • Compiler technology • is more broadly applicable and has been employed in rather unexpected areas. • Text-formatting languages • Command languages of OS • Query languages of Database systems 4
  • 5. What Do Compilers Do (1) • A compiler acts as a translator, transforming human-oriented programming languages into computer-oriented machine languages. Programming Language (Source) Compiler Machine Language (Target) 5
  • 6. Compilers • “Compilation” • Translation of a program written in a source language into a semantically equivalent program written in a target language Compiler Error messages Source Program Target Program Input Output 6
  • 7. Interpreters • “Interpretation” • Performing the operations implied by the source program Interpreter Source Program Input Output Error messages 7
  • 8. What Do Compilers Do (2) • Compilers may generate three types of code: • Pure Machine Code • Machine instruction set without assuming the existence of any operating system or library. • Mostly being OS or embedded applications. • Augmented Machine Code • Code with OS routines and runtime support routines. • More often • Virtual Machine Code • Virtual instructions, can be run on any architecture with a virtual machine interpreter or a just-in-time compiler • Ex. Java 8
  • 9. What Do Compilers Do (3) • Another way that compilers differ from one another is in the format of the target machine code they generate: • Assembly or other source format • Relocatable binary • Relative address • A linkage step is required • Absolute binary • Absolute address • Can be executed directly 9
  • 10. • Any compiler must perform two major tasks • Analysis of the source program • Synthesis of a machine-language program The Structure of a Compiler (1) Compiler Analysis Synthesis 10
  • 11. The Structure of a Compiler (2) Scanner Parser Semantic Routines Code Generator Optimizer Source Program Tokens Syntactic Structure Symbol and Attribute Tables (Used by all Phases of The Compiler) (Character Stream) Intermediate Representation Target machine code 11
  • 12. The Structure of a Compiler (3) Scanner Parser Semantic Routines Code Generator Optimizer Source Program Tokens Syntactic Structure Symbol and Attribute Tables (Used by all Phases of The Compiler) Scanner  The scanner begins the analysis of the source program by reading the input, character by character, and grouping characters into individual words and symbols (tokens)  RE ( Regular expression )  NFA ( Non-deterministic Finite Automata )  DFA ( Deterministic Finite Automata )  LEX (Character Stream) Intermediate Representation Target machine code 12
  • 13. The Structure of a Compiler (4) Scanner Parser Semantic Routines Code Generator Optimizer Source Program Tokens Syntactic Structure Symbol and Attribute Tables (Used by all Phases of The Compiler) Parser  Given a formal syntax specification (typically as a context- free grammar [CFG] ), the parse reads tokens and groups them into units as specified by the productions of the CFG being used.  As syntactic structure is recognized, the parser either calls corresponding semantic routines directly or builds a syntax tree.  CFG ( Context-Free Grammar )  BNF ( Backus-Naur Form )  GAA ( Grammar Analysis Algorithms )  LL, LR, SLR, LALR Parsers  YACC (Character Stream) Intermediate Representation Target machine code 13
  • 14. The Structure of a Compiler (5) Scanner Parser Semantic Routines Code Generator Optimizer Source Program (Character Stream) Tokens Syntactic Structure Intermediate Representation Symbol and Attribute Tables (Used by all Phases of The Compiler) Semantic Routines  Perform two functions  Check the static semantics of each construct  Do the actual translation  The heart of a compiler  Syntax Directed Translation  Semantic Processing Techniques  IR (Intermediate Representation) Target machine code 14
  • 15. The Structure of a Compiler (6) Scanner Parser Semantic Routines Code Generator Optimizer Source Program Tokens Syntactic Structure Symbol and Attribute Tables (Used by all Phases of The Compiler) Optimizer  The IR code generated by the semantic routines is analyzed and transformed into functionally equivalent but improved IR code  This phase can be very complex and slow  Peephole optimization  loop optimization, register allocation, code scheduling  Register and Temporary Management  Peephole Optimization (Character Stream) Intermediate Representation Target machine code 15
  • 16. The Structure of a Compiler (7) Source Program (Character Stream) Scanner Tokens Parser Syntactic Structure Semantic Routines Intermediate Representation Optimizer Code Generator Code Generator  Interpretive Code Generation  Generating Code from Tree/Dag  Grammar-Based Code Generator Target machine code 16
  • 17. Preprocessors, Compilers, Assemblers, and Linkers 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 -S myprog.c javap Class 17
  • 18. Analysis of Source Programs lexical analyzer syntax analyzer semantic analyzer source program tokens parse trees parse trees 18
  • 22. Symbol Table • There is a record for each identifier • The attributes include name, type, location, etc. 22
  • 23. Synthesis of Object Code intermediate code generator code optimizer code generator parse tree & symbol table intermediate code optimized intermediate code target program 23
  • 28. The Syntax and Semantics of Programming Language • A programming language must include the specification of syntax (structure) and semantics (meaning). • Syntax typically means the context-free syntax because of the almost universal use of context- free-grammar (CFGs) • Ex. • a = b + c is syntactically legal • b + c = a is illegal 28
  • 29. The Syntax and Semantics of Programming Language • The semantics of a programming language are commonly divided into two classes: • Static semantics • Semantics rules that can be checked at compiled time. • Ex. The type and number of a function’s arguments • Runtime semantics • Semantics rules that can be checked only at run time 29
  • 31. Thanks for your attention Questions??? 31