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

Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Codesanchi29
 
1.10. pumping lemma for regular sets
1.10. pumping lemma for regular sets1.10. pumping lemma for regular sets
1.10. pumping lemma for regular setsSampath Kumar S
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design) Tasif Tanzim
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compilerSudhaa Ravi
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)swapnac12
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya Arunan
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler DesignKuppusamy P
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computationBipul Roy Bpl
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
Algorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsMohamed Loey
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra pptGirdharRatne
 

What's hot (20)

Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Code
 
Parse Tree
Parse TreeParse Tree
Parse Tree
 
1.10. pumping lemma for regular sets
1.10. pumping lemma for regular sets1.10. pumping lemma for regular sets
1.10. pumping lemma for regular sets
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compiler
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
Automata theory -RE to NFA-ε
Automata theory -RE to  NFA-εAutomata theory -RE to  NFA-ε
Automata theory -RE to NFA-ε
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Algorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph Algorithms
 
Linked list
Linked listLinked list
Linked list
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
 

Similar to First and follow set

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
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
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
 
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 in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
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
 

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

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
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
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 

Recently uploaded (20)

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
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
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 

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) = {$}