SlideShare uma empresa Scribd logo
1 de 38
Akhil Kaushik
Asstt. Prof., CE Deptt.,
TIT Bhiwani
Parsing Introduction
What is Parsing?
• In the syntax analysis phase, a compiler verifies
whether or not the tokens generated by the lexical
analyzer are grouped according to the syntactic
rules of the language.
• This is done by a parser.
• It detects and reports any syntax errors and
produces a parse tree from which intermediate code
can be generated.
What is Parsing?
Context-Free Grammars
• Terminals - Basic symbols from which strings are
formed.
• Non-terminals - Syntactic variables that denote sets
of strings.
• Start Symbol - Denotes the nonterminal that
generates strings of the languages
• Productions - A = X … X
- Head/left side (A) is a nonterminal
- Body/right side (X … X) zero or more terminals and non-
terminals
Context-Free Grammars
• Non-terminals, terminals
can be derived from
productions.
• First production defines
start symbol.
CFG Notation
• A, B, C: non-terminals
• l: terminals
• a, b, c: strings of non-terminals and terminals
• (alpha, beta, gamma in math)
• w, v: strings of terminal symbols
Derivations
• Derivation - derives in zero or
more steps
• E =>* "-" "(" ID "+" ID ")"
• Derivation step: replace symbol
by RHS of production.
• Repeatedly apply derivations
Derivations
• Left-most derivation:
Expand left-most non-
terminal at each step.
• Right-most derivation:
Expand right-most
non-terminal at each
step
Types of Parser
• The process of deriving the string from the given
grammar is known as derivation (parsing).
• Depending upon how derivation is done we have
two kinds of parsers :-
• Top Down Parser
• Bottom Up Parser
Top Down Parser
• Top down parsing
attempts to build the
parse tree from root to
leave.
• Top down parser will start
from start symbol and
proceeds to string.
• It follows leftmost
derivation.
Bottom Up Parser
• Bottom-up parsing starts from
the leaf nodes of a tree and
works in upward direction till it
reaches the root node.
• Here, we start from a
sentence and then apply
production rules in reverse
manner in order to reach the
start symbol.
Left Factoring
Left Factoring - Examples
Consider the following grammar:-
• S → iEtS / iEtSeS / a
• E → b
Solution:-
• S → iEtSS’ / a
• S’ → eS / ∈
• E → b
Left Factoring - Examples
Consider the following grammar:-
• A → aAB / aBc / aAc
Solution:-
• A → aA’
• A’ → AB / Bc / Ac // This has to done again
• A → aA’
• A’ → AD / Bc
• D → B / c
Left Factoring - Examples
Consider the following grammar:-
• S → aAd / aB
• A → a / ab
• B → ccd / ddc
Solution:-
• S → aS’
• S’ → Ad / B
• A → aA’
• A’ → b / ∈
Eliminating Left Recursion
Left Recursion - Examples
Consider the following grammar -
• A → ABd / Aa / a
• B → Be / b
The grammar after eliminating left recursion is-
• A → aA’
• A’ → BdA’ / aA’ / ∈
• B → bB’
• B’ → eB’ / ∈
Left Recursion - Examples
Consider the following grammar and eliminate left
recursion-
• E → E + E / E x E / a
The grammar after eliminating left recursion is-
• E → aA
• A → +EA / xEA / ∈
Left Recursion - Examples
• Consider the following grammar and eliminate left
recursion-
• E → E + T / T
• T → T x F / F
• F → id
• E → E + T
• E → T
• T → T x F
• T → F
• F → id
OR
Left Recursion - Examples
The grammar after eliminating left recursion is-
• E → TE’
• E’ → +TE’ / ∈
• T → FT’
• T’ → xFT’ / ∈
• F → id
First & Follow
• FIRST(X) for a grammar symbol X is the set of
terminals that begin the strings derivable from X.
• Follow(X) to be the set of terminals that can appear
immediately to the right of Non-Terminal X in some
sentential form.
• Why calculate FIRST?
– It is useful to calculate FOLLOW.
– Both are useful in parsing (both Top-Down and Bottom-
Up)
First & Follow Rules
Rules to compute FIRST set:-
• If x is a terminal, then FIRST(x) = { ‘x’ }
• If x-> Є, is a production rule, then add Є to
FIRST(x).
• If X->Y1 Y2 Y3….Yn is a production, then FIRST(X) =
FIRST(Y1)
• If FIRST(Y1) contains Є then FIRST(X) = {
FIRST(Y1) – Є } U { FIRST(Y2) }
• If FIRST (Yi) contains Є for all i = 1 to n, then add Є
to FIRST(X).
First - Examples
Calculate the first functions for the given grammar-
• S → aBDh
• B → cC
• C → bC / ∈
• D → EF
• E → g / ∈
• F → f / ∈
First - Examples
• First(S) = { a }
• First(B) = { c }
• First(C) = { b , ∈ }
• First(D) = { First(E) – ∈ } ∪ First(F) = { g , f , ∈}
• First(E) = { g , ∈ }
• First(F) = { f , ∈ }
First - Examples
Calculate the first functions for the given grammar-
• S → A
• A → aB / Ad
• B → b
• C → g
First - Examples
• First(S) = First(A) = { a }
• First(A) = { a }
• First(A’) = { d , ∈ }
• First(B) = { b }
• First(C) = { g }
First & Follow Rules
Rules to compute FOLLOW set:-
• Follow(S) = {$} where S is the start symbol.
• If A->pBq is a production where p, B, q any
grammar symbols then everything in FIRST(q)
except Є is in FOLLOW(B).
• If A->pB is a production or a production A->pBq
where FIRST(q) contains Є then everything in
FOLLOW(A) is in FOLLOW(B).
Follow - Examples
Calculate the follow functions for the given grammar-
• S → aBDh
• B → cC
• C → bC / ∈
• D → EF
• E → g / ∈
• F → f / ∈
Follow - Examples
• Follow(S) = { $ }
• Follow(B) = { First(D) – ∈ } ∪ First(h) = { g , f , h }
• Follow(C) = Follow(B) = { g , f , h }
• Follow(D) = First(h) = { h }
• Follow(E) = { First(F) – ∈ } ∪ Follow(D) = { f , h }
• Follow(F) = Follow(D) = { h }
Follow - Examples
Calculate the follow functions for the given grammar-
• S → A
• A → aB / Ad
• B → b
• C → g
Follow - Examples
• Follow(S) = { $ }
• Follow(A) = Follow(S) = { $ }
• Follow(A’) = Follow(A) = { $ }
• Follow(B) = { First(A’) – ∈ } ∪ Follow(A) = { d , $ }
• Follow(C) = NA
First & Follow - Examples
• Calculate the first and follow functions for the
given grammar-
• S → AaAb / BbBa
• A → ∈
• B → ∈
First & Follow - Examples
• First(S) = { First(A) – ∈ } ∪ First(a) ∪ { First(B) – ∈
} ∪ First(b) = { a , b }
• First(A) = { ∈ }
• First(B) = { ∈ }
• Follow(S) = { $ }
• Follow(A) = First(a) ∪ First(b) = { a , b }
• Follow(B) = First(b) ∪ First(a) = { a , b }
First & Follow - Examples
Calculate the first and follow functions for the given
grammar-
• E → E + T / T
• T → T x F / F
• F → (E) / id
First & Follow - Examples
• The given grammar is left recursive.
After eliminating left recursion, we get -
• E → TE’
• E’ → + TE’ / ∈
• T → FT’
• T’ → x FT’ / ∈
• F → (E) / id
First & Follow - Examples
• First(E) = First(T) = First(F) = { ( , id }
• First(E’) = { + , ∈ }
• First(T) = First(F) = { ( , id }
• First(T’) = { x , ∈ }
• First(F) = { ( , id }
First & Follow - Examples
• Follow(E) = { $ , ) }
• Follow(E’) = Follow(E) = { $ , ) }
• Follow(T) = { First(E’) – ∈ } ∪ Follow(E) ∪
Follow(E’) = { + , $ , ) }
• Follow(T’) = Follow(T) = { + , $ , ) }
• Follow(F) = { First(T’) – ∈ } ∪ Follow(T) ∪
Follow(T’) = { x , + , $ , ) }
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
CONTACT ME AT:
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
THANK YOU !!!

Mais conteúdo relacionado

Mais procurados (20)

Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Run time storage
Run time storageRun time storage
Run time storage
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
And or graph
And or graphAnd or graph
And or graph
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical Analyzer
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 
Role-of-lexical-analysis
Role-of-lexical-analysisRole-of-lexical-analysis
Role-of-lexical-analysis
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
 
Back patching
Back patchingBack patching
Back patching
 
Code generation
Code generationCode generation
Code generation
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 

Semelhante a Parsing 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.pptxvenkatapranaykumarGa
 
04 Syntax Analysis.pdf
04 Syntax Analysis.pdf04 Syntax Analysis.pdf
04 Syntax Analysis.pdfmovamag594
 
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...movocode
 
compiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptcompiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptSheikhMuhammadSaad3
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTFutureTechnologies3
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniquesd72994185
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 
ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)Alexandru Radovici
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up ParsingGerwin Ocsena
 
Chapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxChapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxDrIsikoIsaac
 
Top down and botttom up 2 LATEST.
Top down     and botttom up 2 LATEST.Top down     and botttom up 2 LATEST.
Top down and botttom up 2 LATEST.Gerwin Ocsena
 
lec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdflec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdfwigewej294
 

Semelhante a Parsing in Compiler Design (20)

3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
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
 
04 Syntax Analysis.pdf
04 Syntax Analysis.pdf04 Syntax Analysis.pdf
04 Syntax Analysis.pdf
 
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
 
compiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptcompiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.ppt
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
 
LL(1) Parsers
LL(1) ParsersLL(1) Parsers
LL(1) Parsers
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Lecture8 syntax analysis_4
Lecture8 syntax analysis_4Lecture8 syntax analysis_4
Lecture8 syntax analysis_4
 
Left factor put
Left factor putLeft factor put
Left factor put
 
ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
Chapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxChapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptx
 
ALF 5 - Parser Top-Down
ALF 5 - Parser Top-DownALF 5 - Parser Top-Down
ALF 5 - Parser Top-Down
 
Top down and botttom up 2 LATEST.
Top down     and botttom up 2 LATEST.Top down     and botttom up 2 LATEST.
Top down and botttom up 2 LATEST.
 
Compiler Design Unit 2
Compiler Design Unit 2Compiler Design Unit 2
Compiler Design Unit 2
 
lec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdflec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdf
 

Mais de Akhil Kaushik

Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationAkhil Kaushik
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free GrammarAkhil Kaushik
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & RecoveryAkhil Kaushik
 
Lexical Analyzer Implementation
Lexical Analyzer ImplementationLexical Analyzer Implementation
Lexical Analyzer ImplementationAkhil Kaushik
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignAkhil Kaushik
 
File Handling Python
File Handling PythonFile Handling Python
File Handling PythonAkhil Kaushik
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity CalculationAkhil Kaushik
 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsAkhil Kaushik
 
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & LoopsAkhil Kaushik
 
Basic programs in Python
Basic programs in PythonBasic programs in Python
Basic programs in PythonAkhil Kaushik
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python ProgrammingAkhil Kaushik
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design BasicsAkhil Kaushik
 
Bootstrapping in Compiler
Bootstrapping in CompilerBootstrapping in Compiler
Bootstrapping in CompilerAkhil Kaushik
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction toolsAkhil Kaushik
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to CompilersAkhil Kaushik
 

Mais de Akhil Kaushik (20)

Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
 
Symbol Table
Symbol TableSymbol Table
Symbol Table
 
Lexical Analyzer Implementation
Lexical Analyzer ImplementationLexical Analyzer Implementation
Lexical Analyzer Implementation
 
NFA & DFA
NFA & DFANFA & DFA
NFA & DFA
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & Algorithms
 
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & Loops
 
Basic programs in Python
Basic programs in PythonBasic programs in Python
Basic programs in Python
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
Bootstrapping in Compiler
Bootstrapping in CompilerBootstrapping in Compiler
Bootstrapping in Compiler
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
 

Último

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.pptxAreebaZafar22
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
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 POSCeline George
 
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.pptxheathfieldcps1
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
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
 
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.pdfAdmir Softic
 
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-701bronxfugly43
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
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.MaryamAhmad92
 
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Ữ Â...Nguyen Thanh Tu Collection
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
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 ConsultingTechSoup
 

Último (20)

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
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
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
 
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
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
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...
 
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
 
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
 
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 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.
 
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Ữ Â...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
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
 

Parsing in Compiler Design

  • 1. Akhil Kaushik Asstt. Prof., CE Deptt., TIT Bhiwani Parsing Introduction
  • 2. What is Parsing? • In the syntax analysis phase, a compiler verifies whether or not the tokens generated by the lexical analyzer are grouped according to the syntactic rules of the language. • This is done by a parser. • It detects and reports any syntax errors and produces a parse tree from which intermediate code can be generated.
  • 4. Context-Free Grammars • Terminals - Basic symbols from which strings are formed. • Non-terminals - Syntactic variables that denote sets of strings. • Start Symbol - Denotes the nonterminal that generates strings of the languages • Productions - A = X … X - Head/left side (A) is a nonterminal - Body/right side (X … X) zero or more terminals and non- terminals
  • 5. Context-Free Grammars • Non-terminals, terminals can be derived from productions. • First production defines start symbol.
  • 6. CFG Notation • A, B, C: non-terminals • l: terminals • a, b, c: strings of non-terminals and terminals • (alpha, beta, gamma in math) • w, v: strings of terminal symbols
  • 7. Derivations • Derivation - derives in zero or more steps • E =>* "-" "(" ID "+" ID ")" • Derivation step: replace symbol by RHS of production. • Repeatedly apply derivations
  • 8. Derivations • Left-most derivation: Expand left-most non- terminal at each step. • Right-most derivation: Expand right-most non-terminal at each step
  • 9. Types of Parser • The process of deriving the string from the given grammar is known as derivation (parsing). • Depending upon how derivation is done we have two kinds of parsers :- • Top Down Parser • Bottom Up Parser
  • 10. Top Down Parser • Top down parsing attempts to build the parse tree from root to leave. • Top down parser will start from start symbol and proceeds to string. • It follows leftmost derivation.
  • 11. Bottom Up Parser • Bottom-up parsing starts from the leaf nodes of a tree and works in upward direction till it reaches the root node. • Here, we start from a sentence and then apply production rules in reverse manner in order to reach the start symbol.
  • 13. Left Factoring - Examples Consider the following grammar:- • S → iEtS / iEtSeS / a • E → b Solution:- • S → iEtSS’ / a • S’ → eS / ∈ • E → b
  • 14. Left Factoring - Examples Consider the following grammar:- • A → aAB / aBc / aAc Solution:- • A → aA’ • A’ → AB / Bc / Ac // This has to done again • A → aA’ • A’ → AD / Bc • D → B / c
  • 15. Left Factoring - Examples Consider the following grammar:- • S → aAd / aB • A → a / ab • B → ccd / ddc Solution:- • S → aS’ • S’ → Ad / B • A → aA’ • A’ → b / ∈
  • 17. Left Recursion - Examples Consider the following grammar - • A → ABd / Aa / a • B → Be / b The grammar after eliminating left recursion is- • A → aA’ • A’ → BdA’ / aA’ / ∈ • B → bB’ • B’ → eB’ / ∈
  • 18. Left Recursion - Examples Consider the following grammar and eliminate left recursion- • E → E + E / E x E / a The grammar after eliminating left recursion is- • E → aA • A → +EA / xEA / ∈
  • 19. Left Recursion - Examples • Consider the following grammar and eliminate left recursion- • E → E + T / T • T → T x F / F • F → id • E → E + T • E → T • T → T x F • T → F • F → id OR
  • 20. Left Recursion - Examples The grammar after eliminating left recursion is- • E → TE’ • E’ → +TE’ / ∈ • T → FT’ • T’ → xFT’ / ∈ • F → id
  • 21. First & Follow • FIRST(X) for a grammar symbol X is the set of terminals that begin the strings derivable from X. • Follow(X) to be the set of terminals that can appear immediately to the right of Non-Terminal X in some sentential form. • Why calculate FIRST? – It is useful to calculate FOLLOW. – Both are useful in parsing (both Top-Down and Bottom- Up)
  • 22. First & Follow Rules Rules to compute FIRST set:- • If x is a terminal, then FIRST(x) = { ‘x’ } • If x-> Є, is a production rule, then add Є to FIRST(x). • If X->Y1 Y2 Y3….Yn is a production, then FIRST(X) = FIRST(Y1) • If FIRST(Y1) contains Є then FIRST(X) = { FIRST(Y1) – Є } U { FIRST(Y2) } • If FIRST (Yi) contains Є for all i = 1 to n, then add Є to FIRST(X).
  • 23. First - Examples Calculate the first functions for the given grammar- • S → aBDh • B → cC • C → bC / ∈ • D → EF • E → g / ∈ • F → f / ∈
  • 24. First - Examples • First(S) = { a } • First(B) = { c } • First(C) = { b , ∈ } • First(D) = { First(E) – ∈ } ∪ First(F) = { g , f , ∈} • First(E) = { g , ∈ } • First(F) = { f , ∈ }
  • 25. First - Examples Calculate the first functions for the given grammar- • S → A • A → aB / Ad • B → b • C → g
  • 26. First - Examples • First(S) = First(A) = { a } • First(A) = { a } • First(A’) = { d , ∈ } • First(B) = { b } • First(C) = { g }
  • 27. First & Follow Rules Rules to compute FOLLOW set:- • Follow(S) = {$} where S is the start symbol. • If A->pBq is a production where p, B, q any grammar symbols then everything in FIRST(q) except Є is in FOLLOW(B). • If A->pB is a production or a production A->pBq where FIRST(q) contains Є then everything in FOLLOW(A) is in FOLLOW(B).
  • 28. Follow - Examples Calculate the follow functions for the given grammar- • S → aBDh • B → cC • C → bC / ∈ • D → EF • E → g / ∈ • F → f / ∈
  • 29. Follow - Examples • Follow(S) = { $ } • Follow(B) = { First(D) – ∈ } ∪ First(h) = { g , f , h } • Follow(C) = Follow(B) = { g , f , h } • Follow(D) = First(h) = { h } • Follow(E) = { First(F) – ∈ } ∪ Follow(D) = { f , h } • Follow(F) = Follow(D) = { h }
  • 30. Follow - Examples Calculate the follow functions for the given grammar- • S → A • A → aB / Ad • B → b • C → g
  • 31. Follow - Examples • Follow(S) = { $ } • Follow(A) = Follow(S) = { $ } • Follow(A’) = Follow(A) = { $ } • Follow(B) = { First(A’) – ∈ } ∪ Follow(A) = { d , $ } • Follow(C) = NA
  • 32. First & Follow - Examples • Calculate the first and follow functions for the given grammar- • S → AaAb / BbBa • A → ∈ • B → ∈
  • 33. First & Follow - Examples • First(S) = { First(A) – ∈ } ∪ First(a) ∪ { First(B) – ∈ } ∪ First(b) = { a , b } • First(A) = { ∈ } • First(B) = { ∈ } • Follow(S) = { $ } • Follow(A) = First(a) ∪ First(b) = { a , b } • Follow(B) = First(b) ∪ First(a) = { a , b }
  • 34. First & Follow - Examples Calculate the first and follow functions for the given grammar- • E → E + T / T • T → T x F / F • F → (E) / id
  • 35. First & Follow - Examples • The given grammar is left recursive. After eliminating left recursion, we get - • E → TE’ • E’ → + TE’ / ∈ • T → FT’ • T’ → x FT’ / ∈ • F → (E) / id
  • 36. First & Follow - Examples • First(E) = First(T) = First(F) = { ( , id } • First(E’) = { + , ∈ } • First(T) = First(F) = { ( , id } • First(T’) = { x , ∈ } • First(F) = { ( , id }
  • 37. First & Follow - Examples • Follow(E) = { $ , ) } • Follow(E’) = Follow(E) = { $ , ) } • Follow(T) = { First(E’) – ∈ } ∪ Follow(E) ∪ Follow(E’) = { + , $ , ) } • Follow(T’) = Follow(T) = { + , $ , ) } • Follow(F) = { First(T’) – ∈ } ∪ Follow(T) ∪ Follow(T’) = { x , + , $ , ) }
  • 38. Akhil Kaushik akhilkaushik05@gmail.com 9416910303 CONTACT ME AT: Akhil Kaushik akhilkaushik05@gmail.com 9416910303 THANK YOU !!!