SlideShare a Scribd company logo
1 of 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

More Related Content

What's hot

Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingCompiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingEelco Visser
 
A simple approach of lexical analyzers
A simple approach of lexical analyzersA simple approach of lexical analyzers
A simple approach of lexical analyzersArchana Gopinath
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch casesMeoRamos
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysisRicha Sharma
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in RubyAlex Teut
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 SlidesRakesh Roshan
 
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 ANTLR4Oliver Zeigermann
 
Gradual Typing for Generics
Gradual Typing for GenericsGradual Typing for Generics
Gradual Typing for GenericsLintaro Ina
 
Functional programming
Functional programmingFunctional programming
Functional programmingKibru Demeke
 
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 languageRakesh Roshan
 

What's hot (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 to phases of compiler

Ch 2.pptx
Ch 2.pptxCh 2.pptx
Ch 2.pptxwoldu2
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilersvijaya603274
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.pptsivaganesh293
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.pptsivaganesh293
 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdfTANZINTANZINA
 
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 designSadia Akter
 
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. Pptovidlivi91
 
Lecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.pptLecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.pptNderituGichuki1
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite AutomataArchana Gopinath
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01riddhi viradiya
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysisIffat Anjum
 
Compiler Designs
Compiler DesignsCompiler Designs
Compiler Designswasim liam
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 
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.pptxvenkatapranaykumarGa
 

Similar to 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
 

Recently uploaded

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 

Recently uploaded (20)

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 

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