SlideShare a Scribd company logo
INTRODUCTION TO COMPILERS
UNIT 1
Syllabus:
Translator – Compilation and Interpretation, language processors; The phase of compiler – Errors
Encountered in Different Phases, The Grouping of phases; Compiler Construction Tools; Programming
Language Basics.
SYLLABUS
TRANSLATOR
 Translator is a program that takes as input a program written in one
programming language (the source language) and produces as output a program
in another language (the object or target language).
COMPILER
 A compiler is a program that can read a program in one language, the
source language, and translate it into an equivalent program in another
language - the target language
If the target program is an executable machine-
language program, it can then be called by the
user to process inputs and produce outputs;
Note:An important role of the
compiler is to report any
errors in the source program
that it detects during the
translation process.
INTERPRETER
 An interpreter is another common kind of language processor.
 Instead of producing a target program as a translation, an interpreter
appears to directly execute the operations specified in the source
program on inputs supplied by the user.
 The machine-language target program produced by a compiler is usually
much faster than an interpreter at mapping inputs to outputs .
 An interpreter, however, can usually give better error diagnostics than a
compiler, because it executes the source program statement by statement
• Java language processors combine compilation and interpretation.
• A Java source program may first be compiled into an intermediate form called
bytecodes interpreted by virtual machine.
compiled on one machine can be
interpreted on another machine
across a network
Achieve faster processing of inputs to outputs:
Java compilers, called just-in-time compilers, translate
the bytecodes into machine language immediately before
they run the intermediate program to process the input
Introduction to Compiler Design Structutre, Phases
LANGUAGE PROCESSORS
• A source program may be divided into modules stored in separate files.
• The task of collecting the source program is sometimes entrusted to a
separate program, called a preprocessor.
• The preprocessor may also expand shorthand's, called macros, into
source language statements.
• The modified source program is then fed to a compiler.
• The compiler may produce an assembly-language program as its output,
because assembly language is easier to produce as output and is easier to
debug.
• The assembly language is then processed by a program called an assembler that
produces relocatable machine code as its output.
• Large programs are often compiled in pieces, so the relocatable machine code may
have to be linked together with other relocatable object files and library files into
the code that actually runs on the machine.
• The linker resolves external memory addresses, where the code in one file may refer
to a location in another file.
• The loader then puts together all of the executable object files into memory for
execution.
THE STRUCTURE OF A COMPILER
• A compiler is treated as a single box that maps a source program into a semantically
equivalent target program.
• There are two parts to this mapping: analysis part and synthesis part
• The analysis part is often called the front end of the compiler; the synthesis part is the
back end of the compiler.
ANALYSIS
 It breaks up the source program into constituent pieces and imposes a grammatical structure
on them.
 It detects that the source program is either syntactically ill formed or semantically unsound, then it must
provide informative messages, so the user can take corrective action
 This structure to create an intermediate representation of the source program
 It also collects information about the source program and stores it in a data structure called a
symbol table, which is passed along with the intermediate representation to the synthesis part
SYNTHESIS
• In this part, it constructs the desired target program from the
intermediate representation and the information in the symbol table
THE PHASES OF A COMPILER
The symbol table, which
stores information about
the entire source
program, is used by all
phases of the compiler
The purpose of this optimization phase
is to perform transformations on the
intermediate representation, the
backend can produce a better target
program
HOW DOES ITWORKING
19
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
20
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
21
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
22
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
23
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
24
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
25
THE STRUCTURE OF A COMPILER (8)
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 Optimizer
Target machine code
Ad

More Related Content

Similar to Introduction to Compiler Design Structutre, Phases (20)

Compiler design
Compiler designCompiler design
Compiler design
sanchi29
 
Chapter 1.pptx compiler design lecture note
Chapter 1.pptx compiler design lecture noteChapter 1.pptx compiler design lecture note
Chapter 1.pptx compiler design lecture note
adugnanegero
 
COMPILER DESIGN OPTIONS
COMPILER DESIGN OPTIONSCOMPILER DESIGN OPTIONS
COMPILER DESIGN OPTIONS
sonalikharade3
 
Cd unit i
Cd unit iCd unit i
Cd unit i
Abhimanyu Mishra
 
Compiler Construction from very basic start
Compiler Construction from very basic startCompiler Construction from very basic start
Compiler Construction from very basic start
voyoc79528
 
Ch 1.pptx
Ch 1.pptxCh 1.pptx
Ch 1.pptx
woldu2
 
design intoduction of_COMPILER_DESIGN.pdf
design intoduction of_COMPILER_DESIGN.pdfdesign intoduction of_COMPILER_DESIGN.pdf
design intoduction of_COMPILER_DESIGN.pdf
advRajatSharma
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
Rana Ehtisham Ul Haq
 
Language translators
Language translatorsLanguage translators
Language translators
Aditya Sharat
 
COMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdf
COMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdfCOMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdf
COMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdf
Abolarinwa
 
Unit2_CD.pptx more about compilation of the day
Unit2_CD.pptx more about compilation of the dayUnit2_CD.pptx more about compilation of the day
Unit2_CD.pptx more about compilation of the day
k12196987
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
NesredinTeshome1
 
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
DrIsikoIsaac
 
La 5 Pl Translator
La 5   Pl TranslatorLa 5   Pl Translator
La 5 Pl Translator
Cma Mohd
 
Chapter#01 cc
Chapter#01 ccChapter#01 cc
Chapter#01 cc
abdulbaki3
 
Chapter1.pdf
Chapter1.pdfChapter1.pdf
Chapter1.pdf
tharwatabdulhmed
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
Dr. C.V. Suresh Babu
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
ssuser3b4934
 
Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in details
kazi_aihtesham
 
Compiler_Lecture1.pdf
Compiler_Lecture1.pdfCompiler_Lecture1.pdf
Compiler_Lecture1.pdf
AkarTaher
 
Compiler design
Compiler designCompiler design
Compiler design
sanchi29
 
Chapter 1.pptx compiler design lecture note
Chapter 1.pptx compiler design lecture noteChapter 1.pptx compiler design lecture note
Chapter 1.pptx compiler design lecture note
adugnanegero
 
COMPILER DESIGN OPTIONS
COMPILER DESIGN OPTIONSCOMPILER DESIGN OPTIONS
COMPILER DESIGN OPTIONS
sonalikharade3
 
Compiler Construction from very basic start
Compiler Construction from very basic startCompiler Construction from very basic start
Compiler Construction from very basic start
voyoc79528
 
Ch 1.pptx
Ch 1.pptxCh 1.pptx
Ch 1.pptx
woldu2
 
design intoduction of_COMPILER_DESIGN.pdf
design intoduction of_COMPILER_DESIGN.pdfdesign intoduction of_COMPILER_DESIGN.pdf
design intoduction of_COMPILER_DESIGN.pdf
advRajatSharma
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
Rana Ehtisham Ul Haq
 
Language translators
Language translatorsLanguage translators
Language translators
Aditya Sharat
 
COMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdf
COMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdfCOMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdf
COMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdf
Abolarinwa
 
Unit2_CD.pptx more about compilation of the day
Unit2_CD.pptx more about compilation of the dayUnit2_CD.pptx more about compilation of the day
Unit2_CD.pptx more about compilation of the day
k12196987
 
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
DrIsikoIsaac
 
La 5 Pl Translator
La 5   Pl TranslatorLa 5   Pl Translator
La 5 Pl Translator
Cma Mohd
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
ssuser3b4934
 
Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in details
kazi_aihtesham
 
Compiler_Lecture1.pdf
Compiler_Lecture1.pdfCompiler_Lecture1.pdf
Compiler_Lecture1.pdf
AkarTaher
 

Recently uploaded (20)

INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024
INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024
INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024
Quiz Club of PSG College of Arts & Science
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-14-2025 .pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-14-2025  .pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-14-2025  .pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-14-2025 .pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Module_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptxModule_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptx
drroxannekemp
 
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFAMCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptxUnit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Mayuri Chavan
 
libbys peer assesment.docx..............
libbys peer assesment.docx..............libbys peer assesment.docx..............
libbys peer assesment.docx..............
19lburrell
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
The History of Kashmir Lohar Dynasty NEP.ppt
The History of Kashmir Lohar Dynasty NEP.pptThe History of Kashmir Lohar Dynasty NEP.ppt
The History of Kashmir Lohar Dynasty NEP.ppt
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
ArkaDas54
 
How to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo SlidesHow to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo Slides
Celine George
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
How to Manage Manual Reordering Rule in Odoo 18 Inventory
How to Manage Manual Reordering Rule in Odoo 18 InventoryHow to Manage Manual Reordering Rule in Odoo 18 Inventory
How to Manage Manual Reordering Rule in Odoo 18 Inventory
Celine George
 
How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18
Celine George
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Module_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptxModule_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptx
drroxannekemp
 
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFAMCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptxUnit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Mayuri Chavan
 
libbys peer assesment.docx..............
libbys peer assesment.docx..............libbys peer assesment.docx..............
libbys peer assesment.docx..............
19lburrell
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
ArkaDas54
 
How to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo SlidesHow to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo Slides
Celine George
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
How to Manage Manual Reordering Rule in Odoo 18 Inventory
How to Manage Manual Reordering Rule in Odoo 18 InventoryHow to Manage Manual Reordering Rule in Odoo 18 Inventory
How to Manage Manual Reordering Rule in Odoo 18 Inventory
Celine George
 
How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18
Celine George
 
Ad

Introduction to Compiler Design Structutre, Phases

  • 2. Syllabus: Translator – Compilation and Interpretation, language processors; The phase of compiler – Errors Encountered in Different Phases, The Grouping of phases; Compiler Construction Tools; Programming Language Basics. SYLLABUS
  • 3. TRANSLATOR  Translator is a program that takes as input a program written in one programming language (the source language) and produces as output a program in another language (the object or target language).
  • 4. COMPILER  A compiler is a program that can read a program in one language, the source language, and translate it into an equivalent program in another language - the target language
  • 5. If the target program is an executable machine- language program, it can then be called by the user to process inputs and produce outputs; Note:An important role of the compiler is to report any errors in the source program that it detects during the translation process.
  • 6. INTERPRETER  An interpreter is another common kind of language processor.  Instead of producing a target program as a translation, an interpreter appears to directly execute the operations specified in the source program on inputs supplied by the user.
  • 7.  The machine-language target program produced by a compiler is usually much faster than an interpreter at mapping inputs to outputs .  An interpreter, however, can usually give better error diagnostics than a compiler, because it executes the source program statement by statement
  • 8. • Java language processors combine compilation and interpretation. • A Java source program may first be compiled into an intermediate form called bytecodes interpreted by virtual machine. compiled on one machine can be interpreted on another machine across a network Achieve faster processing of inputs to outputs: Java compilers, called just-in-time compilers, translate the bytecodes into machine language immediately before they run the intermediate program to process the input
  • 11. • A source program may be divided into modules stored in separate files. • The task of collecting the source program is sometimes entrusted to a separate program, called a preprocessor. • The preprocessor may also expand shorthand's, called macros, into source language statements.
  • 12. • The modified source program is then fed to a compiler. • The compiler may produce an assembly-language program as its output, because assembly language is easier to produce as output and is easier to debug.
  • 13. • The assembly language is then processed by a program called an assembler that produces relocatable machine code as its output. • Large programs are often compiled in pieces, so the relocatable machine code may have to be linked together with other relocatable object files and library files into the code that actually runs on the machine.
  • 14. • The linker resolves external memory addresses, where the code in one file may refer to a location in another file. • The loader then puts together all of the executable object files into memory for execution.
  • 15. THE STRUCTURE OF A COMPILER • A compiler is treated as a single box that maps a source program into a semantically equivalent target program. • There are two parts to this mapping: analysis part and synthesis part • The analysis part is often called the front end of the compiler; the synthesis part is the back end of the compiler.
  • 16. ANALYSIS  It breaks up the source program into constituent pieces and imposes a grammatical structure on them.  It detects that the source program is either syntactically ill formed or semantically unsound, then it must provide informative messages, so the user can take corrective action  This structure to create an intermediate representation of the source program  It also collects information about the source program and stores it in a data structure called a symbol table, which is passed along with the intermediate representation to the synthesis part
  • 17. SYNTHESIS • In this part, it constructs the desired target program from the intermediate representation and the information in the symbol table
  • 18. THE PHASES OF A COMPILER The symbol table, which stores information about the entire source program, is used by all phases of the compiler The purpose of this optimization phase is to perform transformations on the intermediate representation, the backend can produce a better target program
  • 19. HOW DOES ITWORKING 19 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
  • 20. 20 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
  • 21. 21 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
  • 22. 22 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
  • 23. 23 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
  • 24. 24 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
  • 25. 25 THE STRUCTURE OF A COMPILER (8) 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 Optimizer Target machine code
  翻译: