SlideShare a Scribd company logo
UNIVERSITY OF AZAD JAMMU & KASHMIR
MUZAFFARABAD
Dawood Faheem Abbasi BSCS-VI-05
FIRST AND FOLLOW SET
Compiler Construction
FIRST & FOLLOW
• The construction of a predictive parser is aided by two functions associated with a
grammar G.
• These functions, FIRST and FOLLOW, allow us to fill in the entries of a predictive
parsing table for G,
• Whenever possible. Sets of tokens yielded by the FOLLOW function can also be used
as synchronizing tokens during panic-mode error recovery.
FIRST SET
• If α is any string of grammar symbols, let FIRST(α) be the set of terminals that
begin the strings derived from a. If a => Ɛ then Ɛ is also in FIRST(α).
• To compute FIRST(X) for all grammar symbols X, apply the following rules until no
more terminals or Ɛ can be added to any FIRST set.
RULES FOR FIRST SET
1. If X is terminal, then FIRST(X) is {X}.
2. If X => Ɛ is a production, then add Ɛ to FIRST(X).
3. If X is nonterminal and X => Y1 Y2 ... Yk. is a production, then place a in FIRST(X) if for
some i, a is in FIRST(Yi), and Ɛ is in all of FIRST(Y1), ... , FIRST(Yi-1); that is, Y1, ... ,Yi-1
=> Ɛ. If Ɛ is in FIRST(Yj) for all j = 1, 2, ... , k, then add Ɛ to FIRST(X).
For example, everything in FIRST(Y1) is surely in FIRST(X). If Y1 does not derive Ɛ,
then we add nothing more to FIRST(X), but if Y1 => Ɛ, then we add FIRST(Y2) and so on.
FOLLOW SET
• Define FOLLOW(A), for nonterminal A, to be the set of terminals a that can appear
immediately to the right of A in some sentential form, that is, the set of terminals a such
that there exists a derivation of the form
• S => α Aaβ for some α and β. Note that there may, at some time during the derivation,
have been symbols between A and a, but if so, they derived Ɛ and disappeared.
• If A can be the rightmost symbol in some sentential form, then $, representing the input
right end marker, is in FOLLOW(A).
RULES FOR FOLLOW SET
1. Place $ in FOLLOW(S), where S is the start symbol and $ is the input right end
marker.
2. If there is a production A => α Bβ, then everything in FIRST(β), except for Ɛ, is
placed in FOLLOW(B).
3. If there is a production A => α B, or a production A => α Bβ where FIRST(β)
contains Ɛ (i.e., β => Ɛ), then everything in FOLLOW(A) is in FOLLOW(B).
EXAMPLES
E => T E’
E’=> + T E’ | Ɛ
T => F T’
T’=> * F T’ | Ɛ
F => ( E ) | id
FIRST(E) = FIRST(T) = FIRST(F) = {( , id}
FIRST(E’) = {+, Ɛ}
FIRST(T’) = {*, Ɛ}
FOLLOW(E) = FOLLOW(E’) = {) , $}
FOLLOW(T) = FOLLOW(T’) = {+, ), $}
FOLLOW(F) = {+, *, ), $}
EXAMPLE
S => A a
A => B D
B => b | Ɛ
D => d | Ɛ
First(S) = {b, d, a}
First(A) = {b, d, Ɛ}
First(B) = {b, Ɛ}
First(D) = {d, Ɛ }
Follow(S) = {$}
Follow(A) = {a}
Follow(B) = {d, a}
Follow(D) = {a}
EXAMPLE
• C => P F class id X Y
• P => public | Ɛ
• F => final | Ɛ
• X => extends id | j
• Y => implements I | Ɛ
• I => id j
• J => , I | Ɛ
1. C => P F class id X Y
2. P => public
3. P => Ɛ
4. F => final
5. F => Ɛ
6. X => extends id
7. X => Ɛ
8. Y => implements I
9. Y => Ɛ
10. I => id J
11. J => , I
12. J => Ɛ
First(C) = {public, final,
class}
First(P) = {public, Ɛ }
First(F) = {final, Ɛ }
First(X) = {extends, Ɛ }
First(Y) = {implements, Ɛ }
First(I) = {id}
First(J) = {‘,', Ɛ }
Follow(C) = {$}
Follow(P) = {final, class}
Follow(F) = {class}
Follow(X) = {implements, $}
Follow(Y) = {$}
Follow(I) = {$}
Follow(J) = {$}
THANK YOU

More Related Content

What's hot

Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
Radhakrishnan Chinnusamy
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
ASHOK KUMAR REDDY
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
swapnac12
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
Tech_MX
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
LakshmiSamivel
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler design
Riazul Islam
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
Dattatray Gandhmal
 
Compiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLRCompiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLR
Riazul Islam
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
Radhakrishnan Chinnusamy
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Ratnakar Mikkili
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
Archana Gopinath
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
Akshaya Arunan
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
Prankit Mishra
 
Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithms
Chankey Pathak
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
Anusuya123
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
Iffat Anjum
 
Top down parsering and bottom up parsering.pptx
Top down parsering and bottom up parsering.pptxTop down parsering and bottom up parsering.pptx
Top down parsering and bottom up parsering.pptx
LaibaFaisal3
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
MAHASREEM
 

What's hot (20)

Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler design
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Compiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLRCompiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLR
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithms
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
 
Top down parsering and bottom up parsering.pptx
Top down parsering and bottom up parsering.pptxTop down parsering and bottom up parsering.pptx
Top down parsering and bottom up parsering.pptx
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 

Similar to First and follow set

Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
Siddhesh Pange
 
Cs419 lec9 constructing parsing table ll1
Cs419 lec9   constructing parsing table ll1Cs419 lec9   constructing parsing table ll1
Cs419 lec9 constructing parsing table ll1
Arab Open University and Cairo University
 
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
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
d72994185
 
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
 
LECTURE-2 W12.pptx
LECTURE-2 W12.pptxLECTURE-2 W12.pptx
LECTURE-2 W12.pptx
MRKUsafzai0607
 
ALF 5 - Parser Top-Down
ALF 5 - Parser Top-DownALF 5 - Parser Top-Down
ALF 5 - Parser Top-Down
Alexandru Radovici
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
FutureTechnologies3
 
LL(1) Parsers
LL(1) ParsersLL(1) Parsers
compiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptcompiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.ppt
SheikhMuhammadSaad3
 
Ch4a
Ch4aCh4a
Circular queues
Circular queuesCircular queues
Circular queues
Ssankett Negi
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
ayyankhanna6480086
 
Presentation1.pdf
Presentation1.pdfPresentation1.pdf
Presentation1.pdf
GauravChaudhary531452
 
5 top-down-parsers
5  top-down-parsers 5  top-down-parsers
5 top-down-parsers
Saeed Parsa
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
Nitesh Dubey
 
Scalaエンジニアのためのモナド入門
Scalaエンジニアのためのモナド入門Scalaエンジニアのためのモナド入門
Scalaエンジニアのためのモナド入門
Takashi Imahiro
 
Lecture8 syntax analysis_4
Lecture8 syntax analysis_4Lecture8 syntax analysis_4
Lecture8 syntax analysis_4
Mahesh Kumar Chelimilla
 
13000120020_A.pptx
13000120020_A.pptx13000120020_A.pptx
13000120020_A.pptx
0020mdfatehcseB
 
Automata theory -RE to NFA-ε
Automata theory -RE to  NFA-εAutomata theory -RE to  NFA-ε
Automata theory -RE to NFA-ε
Akila Krishnamoorthy
 

Similar to First and follow set (20)

Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
Cs419 lec9 constructing parsing table ll1
Cs419 lec9   constructing parsing table ll1Cs419 lec9   constructing parsing table ll1
Cs419 lec9 constructing parsing table ll1
 
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.
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
 
ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)
 
LECTURE-2 W12.pptx
LECTURE-2 W12.pptxLECTURE-2 W12.pptx
LECTURE-2 W12.pptx
 
ALF 5 - Parser Top-Down
ALF 5 - Parser Top-DownALF 5 - Parser Top-Down
ALF 5 - Parser Top-Down
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
 
LL(1) Parsers
LL(1) ParsersLL(1) Parsers
LL(1) Parsers
 
compiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptcompiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.ppt
 
Ch4a
Ch4aCh4a
Ch4a
 
Circular queues
Circular queuesCircular queues
Circular queues
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
Presentation1.pdf
Presentation1.pdfPresentation1.pdf
Presentation1.pdf
 
5 top-down-parsers
5  top-down-parsers 5  top-down-parsers
5 top-down-parsers
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
 
Scalaエンジニアのためのモナド入門
Scalaエンジニアのためのモナド入門Scalaエンジニアのためのモナド入門
Scalaエンジニアのためのモナド入門
 
Lecture8 syntax analysis_4
Lecture8 syntax analysis_4Lecture8 syntax analysis_4
Lecture8 syntax analysis_4
 
13000120020_A.pptx
13000120020_A.pptx13000120020_A.pptx
13000120020_A.pptx
 
Automata theory -RE to NFA-ε
Automata theory -RE to  NFA-εAutomata theory -RE to  NFA-ε
Automata theory -RE to NFA-ε
 

More from Dawood Faheem Abbasi

TCP/IP and UDP protocols
TCP/IP and UDP protocolsTCP/IP and UDP protocols
TCP/IP and UDP protocols
Dawood Faheem Abbasi
 
Report writing
Report writingReport writing
Report writing
Dawood Faheem Abbasi
 
UML constructs
UML constructs UML constructs
UML constructs
Dawood Faheem Abbasi
 
Mathematical induction and divisibility rules
Mathematical induction and divisibility rulesMathematical induction and divisibility rules
Mathematical induction and divisibility rules
Dawood Faheem Abbasi
 
Hashing
HashingHashing
Features of-RC-quad-copter
Features of-RC-quad-copter Features of-RC-quad-copter
Features of-RC-quad-copter
Dawood Faheem Abbasi
 
7 cs of communication
7 cs of communication7 cs of communication
7 cs of communication
Dawood Faheem Abbasi
 
Geography of pakistan
Geography of pakistanGeography of pakistan
Geography of pakistan
Dawood Faheem Abbasi
 
FET (Field Effect Transistors)
FET (Field Effect Transistors)FET (Field Effect Transistors)
FET (Field Effect Transistors)
Dawood Faheem Abbasi
 
Cyber crime.pptx
Cyber crime.pptxCyber crime.pptx
Cyber crime.pptx
Dawood Faheem Abbasi
 
BJT’s (bipolar junction transistor)
BJT’s (bipolar junction transistor)BJT’s (bipolar junction transistor)
BJT’s (bipolar junction transistor)
Dawood Faheem Abbasi
 

More from Dawood Faheem Abbasi (11)

TCP/IP and UDP protocols
TCP/IP and UDP protocolsTCP/IP and UDP protocols
TCP/IP and UDP protocols
 
Report writing
Report writingReport writing
Report writing
 
UML constructs
UML constructs UML constructs
UML constructs
 
Mathematical induction and divisibility rules
Mathematical induction and divisibility rulesMathematical induction and divisibility rules
Mathematical induction and divisibility rules
 
Hashing
HashingHashing
Hashing
 
Features of-RC-quad-copter
Features of-RC-quad-copter Features of-RC-quad-copter
Features of-RC-quad-copter
 
7 cs of communication
7 cs of communication7 cs of communication
7 cs of communication
 
Geography of pakistan
Geography of pakistanGeography of pakistan
Geography of pakistan
 
FET (Field Effect Transistors)
FET (Field Effect Transistors)FET (Field Effect Transistors)
FET (Field Effect Transistors)
 
Cyber crime.pptx
Cyber crime.pptxCyber crime.pptx
Cyber crime.pptx
 
BJT’s (bipolar junction transistor)
BJT’s (bipolar junction transistor)BJT’s (bipolar junction transistor)
BJT’s (bipolar junction transistor)
 

Recently uploaded

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 

Recently uploaded (20)

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 

First and follow set

  • 1. UNIVERSITY OF AZAD JAMMU & KASHMIR MUZAFFARABAD Dawood Faheem Abbasi BSCS-VI-05
  • 2. FIRST AND FOLLOW SET Compiler Construction
  • 3. FIRST & FOLLOW • The construction of a predictive parser is aided by two functions associated with a grammar G. • These functions, FIRST and FOLLOW, allow us to fill in the entries of a predictive parsing table for G, • Whenever possible. Sets of tokens yielded by the FOLLOW function can also be used as synchronizing tokens during panic-mode error recovery.
  • 4. FIRST SET • If α is any string of grammar symbols, let FIRST(α) be the set of terminals that begin the strings derived from a. If a => Ɛ then Ɛ is also in FIRST(α). • To compute FIRST(X) for all grammar symbols X, apply the following rules until no more terminals or Ɛ can be added to any FIRST set.
  • 5. RULES FOR FIRST SET 1. If X is terminal, then FIRST(X) is {X}. 2. If X => Ɛ is a production, then add Ɛ to FIRST(X). 3. If X is nonterminal and X => Y1 Y2 ... Yk. is a production, then place a in FIRST(X) if for some i, a is in FIRST(Yi), and Ɛ is in all of FIRST(Y1), ... , FIRST(Yi-1); that is, Y1, ... ,Yi-1 => Ɛ. If Ɛ is in FIRST(Yj) for all j = 1, 2, ... , k, then add Ɛ to FIRST(X). For example, everything in FIRST(Y1) is surely in FIRST(X). If Y1 does not derive Ɛ, then we add nothing more to FIRST(X), but if Y1 => Ɛ, then we add FIRST(Y2) and so on.
  • 6. FOLLOW SET • Define FOLLOW(A), for nonterminal A, to be the set of terminals a that can appear immediately to the right of A in some sentential form, that is, the set of terminals a such that there exists a derivation of the form • S => α Aaβ for some α and β. Note that there may, at some time during the derivation, have been symbols between A and a, but if so, they derived Ɛ and disappeared. • If A can be the rightmost symbol in some sentential form, then $, representing the input right end marker, is in FOLLOW(A).
  • 7. RULES FOR FOLLOW SET 1. Place $ in FOLLOW(S), where S is the start symbol and $ is the input right end marker. 2. If there is a production A => α Bβ, then everything in FIRST(β), except for Ɛ, is placed in FOLLOW(B). 3. If there is a production A => α B, or a production A => α Bβ where FIRST(β) contains Ɛ (i.e., β => Ɛ), then everything in FOLLOW(A) is in FOLLOW(B).
  • 8. EXAMPLES E => T E’ E’=> + T E’ | Ɛ T => F T’ T’=> * F T’ | Ɛ F => ( E ) | id FIRST(E) = FIRST(T) = FIRST(F) = {( , id} FIRST(E’) = {+, Ɛ} FIRST(T’) = {*, Ɛ} FOLLOW(E) = FOLLOW(E’) = {) , $} FOLLOW(T) = FOLLOW(T’) = {+, ), $} FOLLOW(F) = {+, *, ), $}
  • 9. EXAMPLE S => A a A => B D B => b | Ɛ D => d | Ɛ First(S) = {b, d, a} First(A) = {b, d, Ɛ} First(B) = {b, Ɛ} First(D) = {d, Ɛ } Follow(S) = {$} Follow(A) = {a} Follow(B) = {d, a} Follow(D) = {a}
  • 10. EXAMPLE • C => P F class id X Y • P => public | Ɛ • F => final | Ɛ • X => extends id | j • Y => implements I | Ɛ • I => id j • J => , I | Ɛ
  • 11. 1. C => P F class id X Y 2. P => public 3. P => Ɛ 4. F => final 5. F => Ɛ 6. X => extends id 7. X => Ɛ 8. Y => implements I 9. Y => Ɛ 10. I => id J 11. J => , I 12. J => Ɛ First(C) = {public, final, class} First(P) = {public, Ɛ } First(F) = {final, Ɛ } First(X) = {extends, Ɛ } First(Y) = {implements, Ɛ } First(I) = {id} First(J) = {‘,', Ɛ } Follow(C) = {$} Follow(P) = {final, class} Follow(F) = {class} Follow(X) = {implements, $} Follow(Y) = {$} Follow(I) = {$} Follow(J) = {$}