SlideShare a Scribd company logo
Lectured by Rebaz Najeeb
Compilers (CPL5316)
Software Engineering
Koya university
2015-2016
e-mail: rebaz.najeeb at koyauniversity dot org
Lecture 1 :
Introduction to language processors
Outline
Overview
 Introduction to compiler
 The Architecture of a Compiler
 Lexical analysis (Scanning)
 Syntax Analysis (Parsing)
 Semantic Analysis
 Intermediate Code Generation
 Code Optimization
 Code Generation
What to know?
The essential knowledge in Computation Theory RE, FSA, and CFG.
The essential skills of a Java developer.
The Dragon book
Why compilers?
• Increase productivity
• Low level programming languages are harder to write , less portable , error-prone area, harder to
maintain.
• Hardware synthesis :
Verilog , VHDL to describe high-level hardware description language.
Register Transfer Language (RTL)  gates  transistors  physical layout.
• Reverse engineering eg. (Executble applications .exe )
runtime (0100101100)  assembly code  high level language.
Understanding binary language is hard
• 01000101 01110110 01100101 01110010 01111001 01101111 01101110
01100101 00100000 01100011 01100001 01101110 00100000 01110000
01100001 01110011 01110011 00101100 00100000 01110101 01101110
01101100 01100101 01110011 01110011 00100000 01010011 00101111
01101000 01100101 00100000 01100100 01101111 01100101 01110011
01101110 00100111 01110100 00100000 01110111 01100001 01101110
01110100 00100000 01110100 01101111 00101110
• What does that mean ?
Programming language classification (Levels)
• Low-level language
Assembly language
Machine language
• High-level language
C, C++, java, Pascal, Prolog, Scheme
• Natural language
English, Kurdish.
* There is also a classification by programming language generations.
Who is that girl? Tell me what is her name?
History of compilers
• Grace Hopper, in 1952 ,A-0 System .
• Alick Glennie in 1952, Autocode.
• John W. Backus
Speedcoding , 1953 .
- More productive , but 10-20 slower in execution.
- took 300 bytes of memory (30% memory)
• Fortran, first complete compiler, in 1954-1957 (18 yrs)
%50 code were in Fortran.
• 1960 Cobol, lisp
• 1970 Pascal , C
• 1980 OOP Ada , smalltalk , C++ .
• 1990 Java , script , Perl
• 2000 language specifications
Compilers
• A compiler translates the code written in one language (HLL) to some other language (LLL) without changing the
meaning of the program.
• It is also expected that a compiler should make the target code efficient and optimized in terms of time and space.
• Compiler design covers basic translation mechanism and error detection & recovery.
• Fortran , Ada, C, C++ , C# , Cobol.
Source program Compiler Output (result)
Error/ Warning
Executable program
Executable program
Input(data)
interpreters
• Interpreter is a type of language processor that directly executes the operations specified in the source program on
inputs supplied by the user.
• Python , Perl , Basic , Ruby, AWK.
Source program Interpreter Output (result)
Error/ Warning
Input(data)
Compilers vs interpreters
•No Compiler Interpreter
1 Compiler Takes Entire program as input Interpreter Takes Single instruction as input .
2 Intermediate Object Code is Generated No Intermediate Object Code isGenerated
3 Conditional Control Statements are Executes faster Conditional Control Statements are Executes slower
4
Memory Requirement : More(Since Object Code is
Generated)
Memory Requirement is Less
5 Program need not be compiled every time
Every time higher level program is converted into
lower level program
6 Errors are displayed after entire program is checked
Errors are displayed for every instruction interpreted
(if any)
7 Example : C Compiler Example : BASIC
http://www.c4learn.com/c-programming/compiler-vs-interpreter/
Java Virtual Machine
• Why Java machine independent ?
• Tools to view and edit bytecodes
- ASM (http://asm.ow2.org)
- Jasmin (http://jasmin.sourceforge.net)
• Show demo by CMD
Java compilers
List of compilers
https://en.wikipedia.org/wiki/List_of_compilers
Language processing phases?
Source Program
Preprocessor
Compiler
Assembler
Linker/Loader
Target machine code
Modified source program
Target assembly language
Relocatable machine code
Library files
Relocatable object
files
Memory
Language processing phases?
Source Program
Preprocessor
Compiler
Assembler
Linker/Loader
Target machine code
Modified source program
Target assembly language
Relocatable machine code
Compiler
Analysis
Synthesis
Library files
Relocatable object
files
Memory
Analysis and Synthesis
Compiler
Analysis
Synthesis
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate code generation
Front-end
Code optimization
Code Generation
Back-end
Analysis VS Synthesis
Analysis part
Breaks up the source program into constituent pieces and create an intermediate representation of the source
program. It is often called the front end of the compiler.
The analysis part can be divided along the following phases:
Lexical analysis , syntax analysis , semantic analysis and intermediate code generation (optional)
Synthesis part
Construct the desired target program from the intermediate representation and the information of the symbol table. It
is often called the back end.
The Synthesis part can be divided along the following phases:
Intermediate code generator, code optimizer, code generator
Compiler phases
Lexical analysis Syntax analysis Semantic analysis
Intermediate code generation
Machine-independent
Code optimization
Code Generator
Character
stream
Token
stream
Syntax
tree
Syntax tree
Intermediate code
representation
Intermediate code
representation
Target machine
code
Machine-independent
Code optimization
Target
machine code
Symbol table
Compiler phases
Target code
1. Lexical analysis
2. Syntax analysis
3. Semantic analysis
4. Code optimization
5. Code Generation
http://www.tutorialspoint.com/compiler_design/i
mages/compiler_phases.jpg
Lexical analysis (Scanning)
• How do we understand English ?
• Break up sentence and recognize words
Iamasmartstudent. I am a smart student.
Separator
Noun
Lexical analysis (Scanning)
lexical analysis is the process of converting a sequence of characters (such as
a computer program or web page) into a sequence of tokens (strings with an
identified "meaning").
Syntax analysis
• How do we understand English?
The Smart students never ever give up.
S Adv V
Sentence
Syntax analysis (Parsing)
A Parser reads a stream of tokens from scanner, and determines if the syntax of the
program is correct according to the context-free grammar (CFG) of the source language.
Then, Tokens are grouped into grammatical phrases represented by a Parse Tree or an
abstract syntax tree, which gives a hierarchical structure to the source program.
Syntax analysis example
Semantic analysis
For example :
A woman without her man is nothing. Vs A woman: without her, man is nothing
Semantic analysis
The Semantic Analysis phase checks the (meaning of) source program for semantic errors
(Type Checking) and gathers type information for the successive phases.
Semantic analysis is the heart of compiler. Also, type checking is the important part in this phase.
Check language requirements like proper declarations.
Semantic analysis catches inconsistencies for instance mismatching datatypes.
Intermediate code generation
After syntax and semantic analysis of the source program , many compilers generate an explicit low-level
or machine-like intermediate code.
In some compilers, a source program is translated into an intermediate code first and then translated into
the target language. In other compilers, translated directly into the target language.
One of the popular intermediate code is three-address code.
Example:
temp1 = int_to_float(60)
temp2 = id3 ∗ temp1
temp3 = id2 + temp2
id1 = temp3
Optimization
This phase attempts to improve the intermediate code, which is produced. So that faster-running
machine code can be achieved in the term of time and space.
The optimized code MUST be CORRECT
Run Faster (time)
Minimize power consumption (Mobile devices)
Use less memory
Shorter is better
Consider network, database access.
Optimize this code
Code Generation
The code generator takes as input an intermediate code representation of
the source program and maps it into the target language.
If the target language is machine code , registers or memory locations are
selected for each of the variables used by the program.
Symbol table
• A symbol table is a data structure containing a record for each variable name, with fields for the
attributes of the name.
• Symbol table should allow find the record for each name quickly and to store and retrieve data from that
record quickly.
• Attributes may provide information about storage allocation, type , scope , number and type of
arguments , method of passing, the type returned.
All phases in one example
Lecture 1  introduction to language processors

More Related Content

What's hot (20)

PPT
Introduction to compiler
Abha Damani
 
PPT
Introduction to Compiler design
Dr. C.V. Suresh Babu
 
PDF
Python algorithm
Prof. Dr. K. Adisesha
 
PPTX
Design of a two pass assembler
Dhananjaysinh Jhala
 
PPTX
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Maulik Borsaniya
 
PPTX
Linkers
Tech_MX
 
PPTX
[OOP - Lec 01] Introduction to OOP
Muhammad Hammad Waseem
 
PPTX
Two pass Assembler
Satyamevjayte Haxor
 
PPTX
The role of the parser and Error recovery strategies ppt in compiler design
Sadia Akter
 
PPTX
Presentation on python
william john
 
PPTX
Functions in Python
Shakti Singh Rathore
 
PPTX
NFA & DFA
Akhil Kaushik
 
PDF
Python basic
Saifuddin Kaijar
 
PPT
Intermediate code generation (Compiler Design)
Tasif Tanzim
 
KEY
Evolution of Programming Languages
Sayanee Basu
 
PPTX
Single pass assembler
Bansari Shah
 
PPTX
Programming paradigm
busyking03
 
PPTX
Compiler design syntax analysis
Richa Sharma
 
PPT
Compiler Design Basics
Akhil Kaushik
 
PPTX
Loaders ( system programming )
Adarsh Patel
 
Introduction to compiler
Abha Damani
 
Introduction to Compiler design
Dr. C.V. Suresh Babu
 
Python algorithm
Prof. Dr. K. Adisesha
 
Design of a two pass assembler
Dhananjaysinh Jhala
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Maulik Borsaniya
 
Linkers
Tech_MX
 
[OOP - Lec 01] Introduction to OOP
Muhammad Hammad Waseem
 
Two pass Assembler
Satyamevjayte Haxor
 
The role of the parser and Error recovery strategies ppt in compiler design
Sadia Akter
 
Presentation on python
william john
 
Functions in Python
Shakti Singh Rathore
 
NFA & DFA
Akhil Kaushik
 
Python basic
Saifuddin Kaijar
 
Intermediate code generation (Compiler Design)
Tasif Tanzim
 
Evolution of Programming Languages
Sayanee Basu
 
Single pass assembler
Bansari Shah
 
Programming paradigm
busyking03
 
Compiler design syntax analysis
Richa Sharma
 
Compiler Design Basics
Akhil Kaushik
 
Loaders ( system programming )
Adarsh Patel
 

Viewers also liked (14)

PPT
Compiler Construction
Sarmad Ali
 
PPTX
Type checking in compiler design
Sudip Singh
 
PPT
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
 
PPT
Compiler Construction
Army Public School and College -Faisal
 
PPTX
Syntax directed-translation
Junaid Lodhi
 
PPTX
Lecture 11 semantic analysis 2
Iffat Anjum
 
PDF
Syntax directed translation
Akshaya Arunan
 
PPTX
History of computers - Modern
Damian T. Gordon
 
PPTX
Phases of Compiler
Tanzeela_Hussain
 
PPT
Compiler Design
Mir Majid
 
PDF
Phases of the Compiler - Systems Programming
Mukesh Tekwani
 
PPT
Analysis of the source program
Huawei Technologies
 
PPTX
Compiler Chapter 1
Huawei Technologies
 
Compiler Construction
Sarmad Ali
 
Type checking in compiler design
Sudip Singh
 
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
 
Syntax directed-translation
Junaid Lodhi
 
Lecture 11 semantic analysis 2
Iffat Anjum
 
Syntax directed translation
Akshaya Arunan
 
History of computers - Modern
Damian T. Gordon
 
Phases of Compiler
Tanzeela_Hussain
 
Compiler Design
Mir Majid
 
Phases of the Compiler - Systems Programming
Mukesh Tekwani
 
Analysis of the source program
Huawei Technologies
 
Compiler Chapter 1
Huawei Technologies
 
Ad

Similar to Lecture 1 introduction to language processors (20)

PDF
Lecture 1 Compiler design , computation
Rebaz Najeeb
 
PDF
Compiler_Lecture1.pdf
AkarTaher
 
PPTX
Compilers.pptx
MohammedMohammed578197
 
PDF
unit1pdf__2021_12_14_12_37_34.pdf
DrIsikoIsaac
 
PDF
Chapter#01 cc
abdulbaki3
 
PPTX
Chapter 2 Program language translation.pptx
dawod yimer
 
PDF
Phases of compiler
ahsaniftikhar19
 
PPT
Introduction to compiler design and phases of compiler
Ranjeet Reddy
 
PDF
Compiler design Introduction
Aman Sharma
 
PPT
A basic introduction to compiler design.ppt
pandaashirbad9
 
PPT
A basic introduction to compiler design.ppt
pandaashirbad9
 
PPTX
The Phases of a Compiler
Radhika Talaviya
 
PDF
Chapter1pdf__2021_11_23_10_53_20.pdf
DrIsikoIsaac
 
PDF
COMPILER DESIGN.pdf
AdiseshaK
 
PPTX
Compiler Design Introduction With Design
rashmishekhar81
 
PPTX
Pros and cons of c as a compiler language
Ashok Raj
 
PPTX
Compiler Design Introduction
Thapar Institute
 
PPT
Chapter One
bolovv
 
PPT
Introduction to compiler design, PPT-1, MODULE 1
ROSHNI PRADHAN
 
PPTX
16 compiler-151129060845-lva1-app6892-converted.pptx
nandan543979
 
Lecture 1 Compiler design , computation
Rebaz Najeeb
 
Compiler_Lecture1.pdf
AkarTaher
 
Compilers.pptx
MohammedMohammed578197
 
unit1pdf__2021_12_14_12_37_34.pdf
DrIsikoIsaac
 
Chapter#01 cc
abdulbaki3
 
Chapter 2 Program language translation.pptx
dawod yimer
 
Phases of compiler
ahsaniftikhar19
 
Introduction to compiler design and phases of compiler
Ranjeet Reddy
 
Compiler design Introduction
Aman Sharma
 
A basic introduction to compiler design.ppt
pandaashirbad9
 
A basic introduction to compiler design.ppt
pandaashirbad9
 
The Phases of a Compiler
Radhika Talaviya
 
Chapter1pdf__2021_11_23_10_53_20.pdf
DrIsikoIsaac
 
COMPILER DESIGN.pdf
AdiseshaK
 
Compiler Design Introduction With Design
rashmishekhar81
 
Pros and cons of c as a compiler language
Ashok Raj
 
Compiler Design Introduction
Thapar Institute
 
Chapter One
bolovv
 
Introduction to compiler design, PPT-1, MODULE 1
ROSHNI PRADHAN
 
16 compiler-151129060845-lva1-app6892-converted.pptx
nandan543979
 
Ad

Recently uploaded (20)

PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 

Lecture 1 introduction to language processors

  • 1. Lectured by Rebaz Najeeb Compilers (CPL5316) Software Engineering Koya university 2015-2016 e-mail: rebaz.najeeb at koyauniversity dot org Lecture 1 : Introduction to language processors
  • 2. Outline Overview  Introduction to compiler  The Architecture of a Compiler  Lexical analysis (Scanning)  Syntax Analysis (Parsing)  Semantic Analysis  Intermediate Code Generation  Code Optimization  Code Generation
  • 3. What to know? The essential knowledge in Computation Theory RE, FSA, and CFG. The essential skills of a Java developer. The Dragon book
  • 4. Why compilers? • Increase productivity • Low level programming languages are harder to write , less portable , error-prone area, harder to maintain. • Hardware synthesis : Verilog , VHDL to describe high-level hardware description language. Register Transfer Language (RTL)  gates  transistors  physical layout. • Reverse engineering eg. (Executble applications .exe ) runtime (0100101100)  assembly code  high level language.
  • 5. Understanding binary language is hard • 01000101 01110110 01100101 01110010 01111001 01101111 01101110 01100101 00100000 01100011 01100001 01101110 00100000 01110000 01100001 01110011 01110011 00101100 00100000 01110101 01101110 01101100 01100101 01110011 01110011 00100000 01010011 00101111 01101000 01100101 00100000 01100100 01101111 01100101 01110011 01101110 00100111 01110100 00100000 01110111 01100001 01101110 01110100 00100000 01110100 01101111 00101110 • What does that mean ?
  • 6. Programming language classification (Levels) • Low-level language Assembly language Machine language • High-level language C, C++, java, Pascal, Prolog, Scheme • Natural language English, Kurdish. * There is also a classification by programming language generations.
  • 7. Who is that girl? Tell me what is her name?
  • 8. History of compilers • Grace Hopper, in 1952 ,A-0 System . • Alick Glennie in 1952, Autocode. • John W. Backus Speedcoding , 1953 . - More productive , but 10-20 slower in execution. - took 300 bytes of memory (30% memory) • Fortran, first complete compiler, in 1954-1957 (18 yrs) %50 code were in Fortran. • 1960 Cobol, lisp • 1970 Pascal , C • 1980 OOP Ada , smalltalk , C++ . • 1990 Java , script , Perl • 2000 language specifications
  • 9. Compilers • A compiler translates the code written in one language (HLL) to some other language (LLL) without changing the meaning of the program. • It is also expected that a compiler should make the target code efficient and optimized in terms of time and space. • Compiler design covers basic translation mechanism and error detection & recovery. • Fortran , Ada, C, C++ , C# , Cobol. Source program Compiler Output (result) Error/ Warning Executable program Executable program Input(data)
  • 10. interpreters • Interpreter is a type of language processor that directly executes the operations specified in the source program on inputs supplied by the user. • Python , Perl , Basic , Ruby, AWK. Source program Interpreter Output (result) Error/ Warning Input(data)
  • 11. Compilers vs interpreters •No Compiler Interpreter 1 Compiler Takes Entire program as input Interpreter Takes Single instruction as input . 2 Intermediate Object Code is Generated No Intermediate Object Code isGenerated 3 Conditional Control Statements are Executes faster Conditional Control Statements are Executes slower 4 Memory Requirement : More(Since Object Code is Generated) Memory Requirement is Less 5 Program need not be compiled every time Every time higher level program is converted into lower level program 6 Errors are displayed after entire program is checked Errors are displayed for every instruction interpreted (if any) 7 Example : C Compiler Example : BASIC http://www.c4learn.com/c-programming/compiler-vs-interpreter/
  • 12. Java Virtual Machine • Why Java machine independent ? • Tools to view and edit bytecodes - ASM (http://asm.ow2.org) - Jasmin (http://jasmin.sourceforge.net) • Show demo by CMD
  • 13. Java compilers List of compilers https://en.wikipedia.org/wiki/List_of_compilers
  • 14. Language processing phases? Source Program Preprocessor Compiler Assembler Linker/Loader Target machine code Modified source program Target assembly language Relocatable machine code Library files Relocatable object files Memory
  • 15. Language processing phases? Source Program Preprocessor Compiler Assembler Linker/Loader Target machine code Modified source program Target assembly language Relocatable machine code Compiler Analysis Synthesis Library files Relocatable object files Memory
  • 16. Analysis and Synthesis Compiler Analysis Synthesis Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Front-end Code optimization Code Generation Back-end
  • 17. Analysis VS Synthesis Analysis part Breaks up the source program into constituent pieces and create an intermediate representation of the source program. It is often called the front end of the compiler. The analysis part can be divided along the following phases: Lexical analysis , syntax analysis , semantic analysis and intermediate code generation (optional) Synthesis part Construct the desired target program from the intermediate representation and the information of the symbol table. It is often called the back end. The Synthesis part can be divided along the following phases: Intermediate code generator, code optimizer, code generator
  • 18. Compiler phases Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Machine-independent Code optimization Code Generator Character stream Token stream Syntax tree Syntax tree Intermediate code representation Intermediate code representation Target machine code Machine-independent Code optimization Target machine code Symbol table
  • 19. Compiler phases Target code 1. Lexical analysis 2. Syntax analysis 3. Semantic analysis 4. Code optimization 5. Code Generation http://www.tutorialspoint.com/compiler_design/i mages/compiler_phases.jpg
  • 20. Lexical analysis (Scanning) • How do we understand English ? • Break up sentence and recognize words Iamasmartstudent. I am a smart student. Separator Noun
  • 21. Lexical analysis (Scanning) lexical analysis is the process of converting a sequence of characters (such as a computer program or web page) into a sequence of tokens (strings with an identified "meaning").
  • 22. Syntax analysis • How do we understand English? The Smart students never ever give up. S Adv V Sentence
  • 23. Syntax analysis (Parsing) A Parser reads a stream of tokens from scanner, and determines if the syntax of the program is correct according to the context-free grammar (CFG) of the source language. Then, Tokens are grouped into grammatical phrases represented by a Parse Tree or an abstract syntax tree, which gives a hierarchical structure to the source program.
  • 25. Semantic analysis For example : A woman without her man is nothing. Vs A woman: without her, man is nothing
  • 26. Semantic analysis The Semantic Analysis phase checks the (meaning of) source program for semantic errors (Type Checking) and gathers type information for the successive phases. Semantic analysis is the heart of compiler. Also, type checking is the important part in this phase. Check language requirements like proper declarations. Semantic analysis catches inconsistencies for instance mismatching datatypes.
  • 27. Intermediate code generation After syntax and semantic analysis of the source program , many compilers generate an explicit low-level or machine-like intermediate code. In some compilers, a source program is translated into an intermediate code first and then translated into the target language. In other compilers, translated directly into the target language. One of the popular intermediate code is three-address code. Example: temp1 = int_to_float(60) temp2 = id3 ∗ temp1 temp3 = id2 + temp2 id1 = temp3
  • 28. Optimization This phase attempts to improve the intermediate code, which is produced. So that faster-running machine code can be achieved in the term of time and space. The optimized code MUST be CORRECT Run Faster (time) Minimize power consumption (Mobile devices) Use less memory Shorter is better Consider network, database access.
  • 30. Code Generation The code generator takes as input an intermediate code representation of the source program and maps it into the target language. If the target language is machine code , registers or memory locations are selected for each of the variables used by the program.
  • 31. Symbol table • A symbol table is a data structure containing a record for each variable name, with fields for the attributes of the name. • Symbol table should allow find the record for each name quickly and to store and retrieve data from that record quickly. • Attributes may provide information about storage allocation, type , scope , number and type of arguments , method of passing, the type returned.
  • 32. All phases in one example