SlideShare una empresa de Scribd logo
1 de 17
Phases of Compiler
MUHAMMAD SABEEH SAFDAR
2017-ag-7953 BScs 7th
Topics
• Phases of Compiler
• Lexical Analysis
• What is token?
• What is stream of token?
• What is lexeme?
• What is Pattern?
• What is valid token?
• Regular Expression
• Finite Automata
• Context free grammar
• Syntax Analysis
• Derivation
• Left-most Derivation
• Right-most Derivation
Phases of Compiler
Lexical Analysis
• It is also called scanner.
• Lexical analysis is the first phase of a compiler. It takes the modified source code from language
preprocessors that are written in the form of sentences.
• The lexical analyzer breaks these syntaxes into a series of tokens.
• By removing any whitespace or comments in the source code.
What is Token?
• What is a token?
A lexical token is a sequence of characters that can be treated as a unit in the grammar of the
programming languages.
• Example of tokens:
• Type token (id, number, real, . . . )
• Punctuation tokens (IF, void, return, . . . )
• Alphabetic tokens (keywords)
What is Lexeme?
• A lexeme is a sequence of characters in the source program that is matched by the pattern for a
token.
• Lexeme is a specific instance of a token.
• Example:
• x = a + b * 2
What is Pattern?
• Pattern is a rule describing all those lexemes that can represent a particular token in a source
language.
• Example
• Identifier: start with an alphabet or _ followed by any alphanumeric character.
What is Valid Token?
• There are some predefined rules for every lexeme to be identified as a valid token. These rules are
defined by grammar rules, by means of a pattern.
Regular Expression
Regular expression is a formula that describes a possible set of string. Component of regular
expression..
X the characters
• . any character, usually accept a newline [x y z] any of the characters x, y,
z,…..
R? a R or nothing (=optionally as R)
R* zero or more occurrences…..
R+ one or more occurrences……
R1R2 an R1 followed by anR2
R1|R1 either an R1 or anR2.
Finite Automata
• Finite Automata(FA) is the simplest machine to recognize patterns.
• The finite automata or finite state machine is an abstract machine which have five elements or
tuple.
• There are two types
• NFA and DFA
Deterministic Finite Automata (DFA)
• In a DFA, for a particular input character, the machine goes to one state only. A transition function
is defined on every state for every input symbol. Also in DFA null (or ε) move is not allowed, i.e.,
DFA cannot change state without any input character.
• For example, below DFA with Σ = {0, 1} accepts all strings ending with 0.
• One important thing to note is, there can be many possible DFAs for a pattern. A DFA with
minimum number of states is generally preferred
Non-Deterministic Finite Automata (NFA)
• NFA is similar to DFA except following additional features:
• Null (or ε) move is allowed i.e., it can move forward without reading symbols.
• Ability to transmit to any number of states for a particular input.
• As you can see in transition function is for any input including null (or ε), NFA can go to any state
number of states.
• One important thing to note is, in NFA, if any path for an input string leads to a final state, then
the input string accepted.
Syntax Analyzer
• A syntax analyzer or parser takes the input from a lexical analyzer in the form of token streams.
• The parser analyzes the source code (token stream) against the production rules to detect any
errors in the code. The output of this phase is a parse tree.
• Why do you need Syntax Analyzer?
• Check if the code is valid grammatically
• The syntactical analyzer helps you to apply rules to the code
• Helps you to make sure that each opening brace has a corresponding closing balance
• Each declaration has a type and that the type must be exists
Syntax Analyzer
Derivation
• A derivation is basically a sequence of production rules, in order to get the input string. During
parsing, we take two decisions for some sentential form of input:
• Deciding the non-terminal which is to be replaced.
• Deciding the production rule, by which, the non-terminal will be replaced.
• To decide which non-terminal to be replaced with production rule, we can have two options.
• Left-most Derivation
• Right-most Derivation
Left-most Derivation
• In the left most derivation, the input is scanned and replaced with the production rule from left to right. So in
left most derivatives we read the input string from left to right.
Example
Production Rules
S= S+S
S=S-S
S=a|b|c
Input
a-b+c
Left-most Derivation
S= S+S
S=S-S+S
S=a-S+S
S=a-b+S
S=a-b+c
Right-most Derivation
• In the right most derivation, the input is scanned and replaced with the production rule from right to left. So in
right most derivatives we read the input string from right to left.
Example
Production Rules
S= S+S
S=S-S
S=a|b|c
Input
a-b+c
Right-most Derivation
S= S+S
S=S-S+S
S=S-S+c
S=S-b+c
S=a-b+c

Más contenido relacionado

La actualidad más candente

Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch cases
MeoRamos
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in Ruby
Alex Teut
 
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4
Oliver Zeigermann
 

La actualidad más candente (20)

Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingCompiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type Checking
 
A simple approach of lexical analyzers
A simple approach of lexical analyzersA simple approach of lexical analyzers
A simple approach of lexical analyzers
 
Parsing
ParsingParsing
Parsing
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch cases
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
Type Checking
Type CheckingType Checking
Type Checking
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in Ruby
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4
 
2nd presantation
2nd presantation2nd presantation
2nd presantation
 
Gradual Typing for Generics
Gradual Typing for GenericsGradual Typing for Generics
Gradual Typing for Generics
 
C language 2
C language 2C language 2
C language 2
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Javascript
JavascriptJavascript
Javascript
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
 
Lexical analysis
Lexical analysisLexical analysis
Lexical analysis
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C language
 

Similar a phases of compiler

Lexical analysis, syntax analysis, semantic analysis. Ppt
Lexical analysis, syntax analysis, semantic analysis. PptLexical analysis, syntax analysis, semantic analysis. Ppt
Lexical analysis, syntax analysis, semantic analysis. Ppt
ovidlivi91
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01
riddhi viradiya
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
venkatapranaykumarGa
 

Similar a phases of compiler (20)

Ch 2.pptx
Ch 2.pptxCh 2.pptx
Ch 2.pptx
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf
 
Pcd question bank
Pcd question bank Pcd question bank
Pcd question bank
 
The role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designThe role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler design
 
Lexical analysis, syntax analysis, semantic analysis. Ppt
Lexical analysis, syntax analysis, semantic analysis. PptLexical analysis, syntax analysis, semantic analysis. Ppt
Lexical analysis, syntax analysis, semantic analysis. Ppt
 
Lecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.pptLecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.ppt
 
Module 11
Module 11Module 11
Module 11
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01
 
Compiler lecture 06
Compiler lecture 06Compiler lecture 06
Compiler lecture 06
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysis
 
Compiler Designs
Compiler DesignsCompiler Designs
Compiler Designs
 
Parsing
ParsingParsing
Parsing
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
 

Último

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 

phases of compiler

  • 1. Phases of Compiler MUHAMMAD SABEEH SAFDAR 2017-ag-7953 BScs 7th
  • 2. Topics • Phases of Compiler • Lexical Analysis • What is token? • What is stream of token? • What is lexeme? • What is Pattern? • What is valid token? • Regular Expression • Finite Automata • Context free grammar • Syntax Analysis • Derivation • Left-most Derivation • Right-most Derivation
  • 4. Lexical Analysis • It is also called scanner. • Lexical analysis is the first phase of a compiler. It takes the modified source code from language preprocessors that are written in the form of sentences. • The lexical analyzer breaks these syntaxes into a series of tokens. • By removing any whitespace or comments in the source code.
  • 5. What is Token? • What is a token? A lexical token is a sequence of characters that can be treated as a unit in the grammar of the programming languages. • Example of tokens: • Type token (id, number, real, . . . ) • Punctuation tokens (IF, void, return, . . . ) • Alphabetic tokens (keywords)
  • 6. What is Lexeme? • A lexeme is a sequence of characters in the source program that is matched by the pattern for a token. • Lexeme is a specific instance of a token. • Example: • x = a + b * 2
  • 7. What is Pattern? • Pattern is a rule describing all those lexemes that can represent a particular token in a source language. • Example • Identifier: start with an alphabet or _ followed by any alphanumeric character.
  • 8. What is Valid Token? • There are some predefined rules for every lexeme to be identified as a valid token. These rules are defined by grammar rules, by means of a pattern.
  • 9. Regular Expression Regular expression is a formula that describes a possible set of string. Component of regular expression.. X the characters • . any character, usually accept a newline [x y z] any of the characters x, y, z,….. R? a R or nothing (=optionally as R) R* zero or more occurrences….. R+ one or more occurrences…… R1R2 an R1 followed by anR2 R1|R1 either an R1 or anR2.
  • 10. Finite Automata • Finite Automata(FA) is the simplest machine to recognize patterns. • The finite automata or finite state machine is an abstract machine which have five elements or tuple. • There are two types • NFA and DFA
  • 11. Deterministic Finite Automata (DFA) • In a DFA, for a particular input character, the machine goes to one state only. A transition function is defined on every state for every input symbol. Also in DFA null (or ε) move is not allowed, i.e., DFA cannot change state without any input character. • For example, below DFA with Σ = {0, 1} accepts all strings ending with 0. • One important thing to note is, there can be many possible DFAs for a pattern. A DFA with minimum number of states is generally preferred
  • 12. Non-Deterministic Finite Automata (NFA) • NFA is similar to DFA except following additional features: • Null (or ε) move is allowed i.e., it can move forward without reading symbols. • Ability to transmit to any number of states for a particular input. • As you can see in transition function is for any input including null (or ε), NFA can go to any state number of states. • One important thing to note is, in NFA, if any path for an input string leads to a final state, then the input string accepted.
  • 13. Syntax Analyzer • A syntax analyzer or parser takes the input from a lexical analyzer in the form of token streams. • The parser analyzes the source code (token stream) against the production rules to detect any errors in the code. The output of this phase is a parse tree. • Why do you need Syntax Analyzer? • Check if the code is valid grammatically • The syntactical analyzer helps you to apply rules to the code • Helps you to make sure that each opening brace has a corresponding closing balance • Each declaration has a type and that the type must be exists
  • 15. Derivation • A derivation is basically a sequence of production rules, in order to get the input string. During parsing, we take two decisions for some sentential form of input: • Deciding the non-terminal which is to be replaced. • Deciding the production rule, by which, the non-terminal will be replaced. • To decide which non-terminal to be replaced with production rule, we can have two options. • Left-most Derivation • Right-most Derivation
  • 16. Left-most Derivation • In the left most derivation, the input is scanned and replaced with the production rule from left to right. So in left most derivatives we read the input string from left to right. Example Production Rules S= S+S S=S-S S=a|b|c Input a-b+c Left-most Derivation S= S+S S=S-S+S S=a-S+S S=a-b+S S=a-b+c
  • 17. Right-most Derivation • In the right most derivation, the input is scanned and replaced with the production rule from right to left. So in right most derivatives we read the input string from right to left. Example Production Rules S= S+S S=S-S S=a|b|c Input a-b+c Right-most Derivation S= S+S S=S-S+S S=S-S+c S=S-b+c S=a-b+c