SlideShare a Scribd company logo
Chapter 1. Introduction
Compiler
Dr. Mohammed Khader
ASU
1
2
Outline
• 1.1 History of Compilation
• 1.2 What Compilers Do
• 1.3 Interpreters
• 1.4 Syntax and Semantics
• 1.5 Organization of a Compiler
3
Language Processors
• Act as Translators
– Transforming human-oriented programming
languages into computer-oriented machine
languages
4
Overview and History
• Compilers are fundamental to modern
computing.
• They act as translators, transforming
human-oriented programming languages
into computer-oriented machine languages.
Programming
Language
(Source)
Compiler
Machine
Language
(Target)
5
Overview and History (Cont’d.)
• The first real compiler
– FORTRAN compilers of the late 1950s
• Today, we can build a simple compiler in
a few month.
• Creating an efficient and reliable compiler
is still challenging.
6
Overview and History (Cont’d.)
• Compiler technology is largely applicable
and has been employed in rather
unexpected areas.
– Text-formatting languages, like LaTeX
– Silicon compiler for the creation of VLSI circuits
– Command languages of OS
– Query languages of Database systems
7
8
Front-end and Back-end
• Front-end performs the analysis of the
source language code to build the internal
representation, called
intermediate representation (IR).
• Back-end performs the target language
synthesis to generate the target code
(assembly code).
1.2 What Compilers Do
• Compilers may be distinguished in two
ways:
1. By the kind of machine code they generate
2. By the format of the target code they generate
9
10
Machine Code Generated by
Compilers(kind of Machine code)
 Pure machine code
– Compiler may generate code for a particular
machine’s instruction set without assuming the
existence of any operating system or library
routines.
– Pure machine code is used in compilers for
system implementation languages, which are
for implementing operating systems or
embedded applications.
11
Machine Code Generated by
Compilers (Cont.)
 Augmented machine code
– Compilers generate code for a machine
architecture that is augmented with:
1) operating system routines and
2) runtime language support routines.
12
Machine Code Generated by
Compilers (Cont.)
 Virtual machine code
– Compilers generate virtual machine code that is
composed entirely of virtual machine instructions.
– That code can run on any architecture for which a VM
interpreter is available.
• For example, the VM for Java, Java virtual machine (JVM), has a
JVM interpreter.
– Portability is achieved by writing just one virtual
machine (VM) interpreter for all the target architectures.
1.2.2 Target Code Formats
Another way that compilers differ from one another is in the
format of the target code they generate as follows:
1. Assembly Language (Source) Format
• Advantages:
– Simplifies translation:
• A number of code generation decisions (e.g. how to compute
addresses) can be left for the assembler.
– Cross-compilation:
• The compiler executes on one computer but generates code that
executes on another.
• Symbolic form is produced that is easily transferred between different
computers.
– Support experimental programming language designs
• Makes it easier to check the correctness of a compiler, since its output
can be observed.
• Disadvantage:
– Need an additional pass (Assembler)
13
1.2.2 Target Code Formats
2. Relocatable binary
– External references, local instruction addresses, and data addresses
are not yet bound.
– Addresses are assigned relative either to the beginning of the
module or to symbolically named locations.
• Advantages:
– More efficient and more control over the translation process: because
it eliminates one step of calculating absolute addresses.
– Allow modular compilation: breaking up of a large program into
separately compiled pieces.
– Allow cross-language references: calls of assembler routines or
subprograms in other high-level languages
– Support subroutine libraries for example, I/O, storage allocation,
and math routines
• Disadvantage:
– A linkage step is required to support libraries and to produce
absolute binary format that is executable. 14
1.2.2 Target Code Formats
3. Absolute binary
• Advantages:
– Program can be directly executed when the compiler is finished
– No need for intermediate step of linking.
– Faster: allows a program to be prepared and executed in a single
step.
– Guarantee the use of only the most current library routines
– Good for debugging (frequent changes)
• Disadvantages:
– Limited ability to interface with other code
– The program must be recompiled for each execution
15
16
1.3 Interpreters
Compiler vs. Interpreter
Compiler has compilation and execution phases:
1) The compilation phase generates target
program from source program.
2) The execution phase executes the target
program.
(Fig. 1.1).
Interpreter directly interprets (executes) the source
program that reads inputs and writes outputs
(Fig. 1.3).
17
Compiler versus Interpreter (Cont.)
1.4 Syntax and Semantics
• Syntax: structure
– E.g. context-free grammars (CFGs)
•a=b+c is legal,
•b+c=a is not legal
• Semantics: meaning
– E.g. a = b + c
•is illegal if any of the variables are
undeclared or if b or c is of type Boolean
– Static semantics
– Runtime semantics
18
Static Semantics
• A set of rules that specify which
syntactically legal programs are actually
valid
– E.g.: Identifier declaration, type-compatibility
of operators and operands, proper number of
parameters in procedure calls
• Can be specified either formally or
informally
– E.g.: attribute grammars
19
An Example of Attribute
Grammars
• Production rule:
– E  E+T
• Augmented production rule:
– Eresult  Ev1 + Tv2
•if v1.type = numeric and v2.type = numeric
then result.type  numeric
else call ERROR( )
20
Runtime Semantics
•Runtime, or execution, semantics are
used to specify what a program
computes.
A. Informal These semantics are often specified
very informally in a language manual or report;
B. Formal approaches to defining the runtime
semantics of programming languages.
21
The Structure of a Compiler
• Modern compilers are syntax-directed
– Compilation is driven by the syntactic
structure of programs;
– i.e., actions are associated with the structures.
22
Tasks performed by compilers
Analysis of the source program such as
scanning and parsing.
•Syntax analysis
•Semantic analysis
Synthesis of a target program such as code
generation, that when executed, will
correctly perform the computations
described by the source program.
23
1.5 Organization of a Compiler
(Used by all Phases
of The Compiler)
24
Front-end vs. Back-end
25
The Scanner
• The scanner begins the analysis of the source
program by reading the input text (character
by character) and grouping individual
characters into tokens such as :
– Identifiers
– Integers
– Reserved words
– Delimiters
• It eliminate unneeded information like comments
• It process compiler control directives.
• It enters preliminary information into symbol table.
26
The Parser
• Reading tokens and grouping them into phrases
according to the syntax specification such as
context free grammar CFG
• It usually builds an Abstract Syntax Tree (AST)
as a concise representation of program structure
– (Chap. 2 & 7)
27
The Type Checker
(Semantic Analysis)
• Checking the static semantics of each AST
node
– the type checker decorates the AST node by
adding type information to it
– It checks type correctness, reachability and
termination, and exception handling.
– Otherwise, a suitable error message is issued
28
Translator (Program Synthesis)
• Translating AST nodes into Intermediate
Representation code (IR) (such as byte code
in Java) that correctly implements the
meaning of the program
• In simple, nonoptimizing compilers, the
translator may generate target code
directly
29
The Optimizer
• Optimizer improves IR code’s performance.
• Analyzing and transforming the IR code
generated by the translator into functionally
equivalent but improved code
– Complex
• Optimization can also be done after code
generation
30
The Code Generator
• Mapping the IR code generated by the translator
into target machine code (assembly code)
– Machine-dependent, complex
• Register allocation
• Code scheduling
• Automatic construction of code generators has
been actively studied
– Matching a low-level IR to target-instruction
templates
– gcc has machine description files for more than ten
popular computer.
31
328
Organization of a Compiler (Cont.)
Symbol Tables
Scannerr Parser Type Checker
Translator
Optimizer
Code
Generator
Source
Program
Tokens AST
Decorated
AST
Intermediate
Representation
Intermediate
Representation
Target Code
Revised Figure 1.4: A syntax-directed compiler. AST denotes the Abstract
Syntax Tree.
Symbol Tables
• A mechanism that allows information to
be associated with identifiers and shared
among compiler phases
– Identifier declaration
– Identifier use
– Type checking
33
The Structure of a Compiler
34
Scanner
[Lexical Analyzer]
Parser
[Syntax Analyzer]
Semantic Process
[Semantic analyzer]
Code Generator
[Intermediate Code Generator]
Code Optimizer
Tokens
Parse tree
Abstract Syntax Tree w/ Attributes
Non-optimized Intermediate Code
Optimized Intermediate Code
Code Generator
Target machine code
Phases of a Compiler
[Aho, Lam, Sethi, Ullman]
Syntax Analyzer
character stream
target machine code
Lexical Analyzer
Intermediate Code Generator
Code Generator
token stream
syntax tree
intermediate representation
Symbol
Table
Semantic Analyzer
syntax tree
Machine-Independent
Code Optimization
Machine-Dependent
Code Optimization
(optional)
(optional)
35
Compiler Writing Tools
• Compiler generators (compiler compilers)
– Scanner generator
– Parser generator
– Symbol table manager
– Attribute grammar evaluator
– Code-generation tools
• Much of the effort in crafting a compiler lies in
writing and debugging the semantic phases
– Usually hand-coded
36
Compiler-writing tools
Regular expressions
Scanner generators
Lex (The input to lex consists of a definition of
each token as a regular expression )
Parser generators:
Yacc
Code generator generators
http://catalog.compilertools.net/
 37
Ad

More Related Content

Similar to Chap01-Intro.ppt (20)

Cd unit i
Cd unit iCd unit i
Cd unit i
Abhimanyu Mishra
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
Akhil Kaushik
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
Akhil Kaushik
 
Week 08_Basics of Compiler Construction.pdf
Week 08_Basics of Compiler Construction.pdfWeek 08_Basics of Compiler Construction.pdf
Week 08_Basics of Compiler Construction.pdf
AnonymousQ3EMYoWNS
 
Compiler Construction Lecture One .pptx
Compiler Construction Lecture One  .pptxCompiler Construction Lecture One  .pptx
Compiler Construction Lecture One .pptx
انشال عارف
 
Pros and cons of c as a compiler language
  Pros and cons of c as a compiler language  Pros and cons of c as a compiler language
Pros and cons of c as a compiler language
Ashok Raj
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
Abha Damani
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
Akhil Kaushik
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
Army Public School and College -Faisal
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
Sarmad Ali
 
Ch 1.pptx
Ch 1.pptxCh 1.pptx
Ch 1.pptx
woldu2
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
Tanzeela_Hussain
 
Ss ui lecture 1
Ss ui lecture 1Ss ui lecture 1
Ss ui lecture 1
Avinash Kapse
 
SS UI Lecture 1
SS UI Lecture 1SS UI Lecture 1
SS UI Lecture 1
Avinash Kapse
 
Compiler construction lecture 01 .pptx.pdf
Compiler construction  lecture 01 .pptx.pdfCompiler construction  lecture 01 .pptx.pdf
Compiler construction lecture 01 .pptx.pdf
zamanjktiktok
 
COMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptxCOMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptx
MUSHAMHARIKIRAN6737
 
Unit 1 part1 Introduction of Compiler Design.pptx
Unit 1 part1 Introduction of Compiler Design.pptxUnit 1 part1 Introduction of Compiler Design.pptx
Unit 1 part1 Introduction of Compiler Design.pptx
Neelkaranbind
 
Chapter 1 Introduction.pptxhjgjghjghjhjhjhgjmjkhgk
Chapter 1 Introduction.pptxhjgjghjghjhjhjhgjmjkhgkChapter 1 Introduction.pptxhjgjghjghjhjhjhgjmjkhgk
Chapter 1 Introduction.pptxhjgjghjghjhjhjhgjmjkhgk
Shemse Shukre
 
Compiler Design Introduction With Design
Compiler Design Introduction With DesignCompiler Design Introduction With Design
Compiler Design Introduction With Design
rashmishekhar81
 
CSC 204 PASSES IN COMPILER CONSTURCTION.pptx
CSC 204 PASSES IN COMPILER CONSTURCTION.pptxCSC 204 PASSES IN COMPILER CONSTURCTION.pptx
CSC 204 PASSES IN COMPILER CONSTURCTION.pptx
ZulukhaniniTijani
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
Akhil Kaushik
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
Akhil Kaushik
 
Week 08_Basics of Compiler Construction.pdf
Week 08_Basics of Compiler Construction.pdfWeek 08_Basics of Compiler Construction.pdf
Week 08_Basics of Compiler Construction.pdf
AnonymousQ3EMYoWNS
 
Compiler Construction Lecture One .pptx
Compiler Construction Lecture One  .pptxCompiler Construction Lecture One  .pptx
Compiler Construction Lecture One .pptx
انشال عارف
 
Pros and cons of c as a compiler language
  Pros and cons of c as a compiler language  Pros and cons of c as a compiler language
Pros and cons of c as a compiler language
Ashok Raj
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
Abha Damani
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
Akhil Kaushik
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
Sarmad Ali
 
Ch 1.pptx
Ch 1.pptxCh 1.pptx
Ch 1.pptx
woldu2
 
Compiler construction lecture 01 .pptx.pdf
Compiler construction  lecture 01 .pptx.pdfCompiler construction  lecture 01 .pptx.pdf
Compiler construction lecture 01 .pptx.pdf
zamanjktiktok
 
Unit 1 part1 Introduction of Compiler Design.pptx
Unit 1 part1 Introduction of Compiler Design.pptxUnit 1 part1 Introduction of Compiler Design.pptx
Unit 1 part1 Introduction of Compiler Design.pptx
Neelkaranbind
 
Chapter 1 Introduction.pptxhjgjghjghjhjhjhgjmjkhgk
Chapter 1 Introduction.pptxhjgjghjghjhjhjhgjmjkhgkChapter 1 Introduction.pptxhjgjghjghjhjhjhgjmjkhgk
Chapter 1 Introduction.pptxhjgjghjghjhjhjhgjmjkhgk
Shemse Shukre
 
Compiler Design Introduction With Design
Compiler Design Introduction With DesignCompiler Design Introduction With Design
Compiler Design Introduction With Design
rashmishekhar81
 
CSC 204 PASSES IN COMPILER CONSTURCTION.pptx
CSC 204 PASSES IN COMPILER CONSTURCTION.pptxCSC 204 PASSES IN COMPILER CONSTURCTION.pptx
CSC 204 PASSES IN COMPILER CONSTURCTION.pptx
ZulukhaniniTijani
 

Recently uploaded (20)

Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
ssuserb14185
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
Top 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docxTop 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docx
Portli
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
Kubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptxKubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptx
CloudScouts
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
ssuserb14185
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
Top 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docxTop 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docx
Portli
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
Kubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptxKubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptx
CloudScouts
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Ad

Chap01-Intro.ppt

  • 1. Chapter 1. Introduction Compiler Dr. Mohammed Khader ASU 1
  • 2. 2
  • 3. Outline • 1.1 History of Compilation • 1.2 What Compilers Do • 1.3 Interpreters • 1.4 Syntax and Semantics • 1.5 Organization of a Compiler 3
  • 4. Language Processors • Act as Translators – Transforming human-oriented programming languages into computer-oriented machine languages 4
  • 5. Overview and History • Compilers are fundamental to modern computing. • They act as translators, transforming human-oriented programming languages into computer-oriented machine languages. Programming Language (Source) Compiler Machine Language (Target) 5
  • 6. Overview and History (Cont’d.) • The first real compiler – FORTRAN compilers of the late 1950s • Today, we can build a simple compiler in a few month. • Creating an efficient and reliable compiler is still challenging. 6
  • 7. Overview and History (Cont’d.) • Compiler technology is largely applicable and has been employed in rather unexpected areas. – Text-formatting languages, like LaTeX – Silicon compiler for the creation of VLSI circuits – Command languages of OS – Query languages of Database systems 7
  • 8. 8 Front-end and Back-end • Front-end performs the analysis of the source language code to build the internal representation, called intermediate representation (IR). • Back-end performs the target language synthesis to generate the target code (assembly code).
  • 9. 1.2 What Compilers Do • Compilers may be distinguished in two ways: 1. By the kind of machine code they generate 2. By the format of the target code they generate 9
  • 10. 10 Machine Code Generated by Compilers(kind of Machine code)  Pure machine code – Compiler may generate code for a particular machine’s instruction set without assuming the existence of any operating system or library routines. – Pure machine code is used in compilers for system implementation languages, which are for implementing operating systems or embedded applications.
  • 11. 11 Machine Code Generated by Compilers (Cont.)  Augmented machine code – Compilers generate code for a machine architecture that is augmented with: 1) operating system routines and 2) runtime language support routines.
  • 12. 12 Machine Code Generated by Compilers (Cont.)  Virtual machine code – Compilers generate virtual machine code that is composed entirely of virtual machine instructions. – That code can run on any architecture for which a VM interpreter is available. • For example, the VM for Java, Java virtual machine (JVM), has a JVM interpreter. – Portability is achieved by writing just one virtual machine (VM) interpreter for all the target architectures.
  • 13. 1.2.2 Target Code Formats Another way that compilers differ from one another is in the format of the target code they generate as follows: 1. Assembly Language (Source) Format • Advantages: – Simplifies translation: • A number of code generation decisions (e.g. how to compute addresses) can be left for the assembler. – Cross-compilation: • The compiler executes on one computer but generates code that executes on another. • Symbolic form is produced that is easily transferred between different computers. – Support experimental programming language designs • Makes it easier to check the correctness of a compiler, since its output can be observed. • Disadvantage: – Need an additional pass (Assembler) 13
  • 14. 1.2.2 Target Code Formats 2. Relocatable binary – External references, local instruction addresses, and data addresses are not yet bound. – Addresses are assigned relative either to the beginning of the module or to symbolically named locations. • Advantages: – More efficient and more control over the translation process: because it eliminates one step of calculating absolute addresses. – Allow modular compilation: breaking up of a large program into separately compiled pieces. – Allow cross-language references: calls of assembler routines or subprograms in other high-level languages – Support subroutine libraries for example, I/O, storage allocation, and math routines • Disadvantage: – A linkage step is required to support libraries and to produce absolute binary format that is executable. 14
  • 15. 1.2.2 Target Code Formats 3. Absolute binary • Advantages: – Program can be directly executed when the compiler is finished – No need for intermediate step of linking. – Faster: allows a program to be prepared and executed in a single step. – Guarantee the use of only the most current library routines – Good for debugging (frequent changes) • Disadvantages: – Limited ability to interface with other code – The program must be recompiled for each execution 15
  • 16. 16 1.3 Interpreters Compiler vs. Interpreter Compiler has compilation and execution phases: 1) The compilation phase generates target program from source program. 2) The execution phase executes the target program. (Fig. 1.1). Interpreter directly interprets (executes) the source program that reads inputs and writes outputs (Fig. 1.3).
  • 18. 1.4 Syntax and Semantics • Syntax: structure – E.g. context-free grammars (CFGs) •a=b+c is legal, •b+c=a is not legal • Semantics: meaning – E.g. a = b + c •is illegal if any of the variables are undeclared or if b or c is of type Boolean – Static semantics – Runtime semantics 18
  • 19. Static Semantics • A set of rules that specify which syntactically legal programs are actually valid – E.g.: Identifier declaration, type-compatibility of operators and operands, proper number of parameters in procedure calls • Can be specified either formally or informally – E.g.: attribute grammars 19
  • 20. An Example of Attribute Grammars • Production rule: – E  E+T • Augmented production rule: – Eresult  Ev1 + Tv2 •if v1.type = numeric and v2.type = numeric then result.type  numeric else call ERROR( ) 20
  • 21. Runtime Semantics •Runtime, or execution, semantics are used to specify what a program computes. A. Informal These semantics are often specified very informally in a language manual or report; B. Formal approaches to defining the runtime semantics of programming languages. 21
  • 22. The Structure of a Compiler • Modern compilers are syntax-directed – Compilation is driven by the syntactic structure of programs; – i.e., actions are associated with the structures. 22
  • 23. Tasks performed by compilers Analysis of the source program such as scanning and parsing. •Syntax analysis •Semantic analysis Synthesis of a target program such as code generation, that when executed, will correctly perform the computations described by the source program. 23
  • 24. 1.5 Organization of a Compiler (Used by all Phases of The Compiler) 24
  • 26. The Scanner • The scanner begins the analysis of the source program by reading the input text (character by character) and grouping individual characters into tokens such as : – Identifiers – Integers – Reserved words – Delimiters • It eliminate unneeded information like comments • It process compiler control directives. • It enters preliminary information into symbol table. 26
  • 27. The Parser • Reading tokens and grouping them into phrases according to the syntax specification such as context free grammar CFG • It usually builds an Abstract Syntax Tree (AST) as a concise representation of program structure – (Chap. 2 & 7) 27
  • 28. The Type Checker (Semantic Analysis) • Checking the static semantics of each AST node – the type checker decorates the AST node by adding type information to it – It checks type correctness, reachability and termination, and exception handling. – Otherwise, a suitable error message is issued 28
  • 29. Translator (Program Synthesis) • Translating AST nodes into Intermediate Representation code (IR) (such as byte code in Java) that correctly implements the meaning of the program • In simple, nonoptimizing compilers, the translator may generate target code directly 29
  • 30. The Optimizer • Optimizer improves IR code’s performance. • Analyzing and transforming the IR code generated by the translator into functionally equivalent but improved code – Complex • Optimization can also be done after code generation 30
  • 31. The Code Generator • Mapping the IR code generated by the translator into target machine code (assembly code) – Machine-dependent, complex • Register allocation • Code scheduling • Automatic construction of code generators has been actively studied – Matching a low-level IR to target-instruction templates – gcc has machine description files for more than ten popular computer. 31
  • 32. 328 Organization of a Compiler (Cont.) Symbol Tables Scannerr Parser Type Checker Translator Optimizer Code Generator Source Program Tokens AST Decorated AST Intermediate Representation Intermediate Representation Target Code Revised Figure 1.4: A syntax-directed compiler. AST denotes the Abstract Syntax Tree.
  • 33. Symbol Tables • A mechanism that allows information to be associated with identifiers and shared among compiler phases – Identifier declaration – Identifier use – Type checking 33
  • 34. The Structure of a Compiler 34 Scanner [Lexical Analyzer] Parser [Syntax Analyzer] Semantic Process [Semantic analyzer] Code Generator [Intermediate Code Generator] Code Optimizer Tokens Parse tree Abstract Syntax Tree w/ Attributes Non-optimized Intermediate Code Optimized Intermediate Code Code Generator Target machine code
  • 35. Phases of a Compiler [Aho, Lam, Sethi, Ullman] Syntax Analyzer character stream target machine code Lexical Analyzer Intermediate Code Generator Code Generator token stream syntax tree intermediate representation Symbol Table Semantic Analyzer syntax tree Machine-Independent Code Optimization Machine-Dependent Code Optimization (optional) (optional) 35
  • 36. Compiler Writing Tools • Compiler generators (compiler compilers) – Scanner generator – Parser generator – Symbol table manager – Attribute grammar evaluator – Code-generation tools • Much of the effort in crafting a compiler lies in writing and debugging the semantic phases – Usually hand-coded 36
  • 37. Compiler-writing tools Regular expressions Scanner generators Lex (The input to lex consists of a definition of each token as a regular expression ) Parser generators: Yacc Code generator generators http://catalog.compilertools.net/  37