SlideShare a Scribd company logo
7
Most read
8
Most read
9
Most read
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

PPTX
Top down parsing
PPT
Top down parsing
PDF
Operator precedence
PPTX
Lexical analyzer generator lex
PPTX
The role of the parser and Error recovery strategies ppt in compiler design
PPT
Bottom - Up Parsing
PPTX
Top down and botttom up Parsing
PPTX
Input-Buffering
Top down parsing
Top down parsing
Operator precedence
Lexical analyzer generator lex
The role of the parser and Error recovery strategies ppt in compiler design
Bottom - Up Parsing
Top down and botttom up Parsing
Input-Buffering

What's hot (20)

PPTX
Context free grammar
PPTX
Lexical analysis - Compiler Design
PPTX
LL(1) parsing
PPTX
Ambiguous & Unambiguous Grammar
PPT
Symbol table management and error handling in compiler design
PPT
Ll(1) Parser in Compilers
PPTX
Specification-of-tokens
PDF
NFA to DFA
PPTX
stack & queue
PPT
NFA or Non deterministic finite automata
PPTX
8 queens problem using back tracking
PPTX
RECURSIVE DESCENT PARSING
PPTX
Syntax Analysis in Compiler Design
PPT
1.1 binary tree
PDF
Code optimization in compiler design
PPTX
Two-way Deterministic Finite Automata
PPTX
Graph coloring using backtracking
PDF
Symbol table in compiler Design
PPTX
Two pass Assembler
PPT
Finite automata examples
Context free grammar
Lexical analysis - Compiler Design
LL(1) parsing
Ambiguous & Unambiguous Grammar
Symbol table management and error handling in compiler design
Ll(1) Parser in Compilers
Specification-of-tokens
NFA to DFA
stack & queue
NFA or Non deterministic finite automata
8 queens problem using back tracking
RECURSIVE DESCENT PARSING
Syntax Analysis in Compiler Design
1.1 binary tree
Code optimization in compiler design
Two-way Deterministic Finite Automata
Graph coloring using backtracking
Symbol table in compiler Design
Two pass Assembler
Finite automata examples
Ad

Similar to First and follow set (20)

PPTX
Top down parsing(sid) (1)
PDF
Lecture8 syntax analysis_4
PPTX
Top down and botttom up 2 LATEST.
PDF
201506 CSE340 Lecture 15
PPTX
5 top-down-parsers
PPT
compiler-lecture-6nn-14112022-110738am.ppt
PPTX
Parsing in Compiler Design
PDF
ACD-U2-TopDown..pdf it hhepls inall the the
PDF
CS17604_TOP Parser Compiler Design Techniques
PPT
Integrated Fundamental and Technical Analysis of Select Public Sector Oil Com...
PPTX
LECTURE-2 W12.pptx
PPTX
Cs419 lec9 constructing parsing table ll1
PPTX
Compiler Design_Syntax Analyzer_Top Down Parsers.pptx
PDF
Syntax Analysis PPTs for Third Year Computer Sc. and Engineering
PPT
51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt
PPT
51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt
PPT
predatory parser left factorial 4-CFGs.ppt
Top down parsing(sid) (1)
Lecture8 syntax analysis_4
Top down and botttom up 2 LATEST.
201506 CSE340 Lecture 15
5 top-down-parsers
compiler-lecture-6nn-14112022-110738am.ppt
Parsing in Compiler Design
ACD-U2-TopDown..pdf it hhepls inall the the
CS17604_TOP Parser Compiler Design Techniques
Integrated Fundamental and Technical Analysis of Select Public Sector Oil Com...
LECTURE-2 W12.pptx
Cs419 lec9 constructing parsing table ll1
Compiler Design_Syntax Analyzer_Top Down Parsers.pptx
Syntax Analysis PPTs for Third Year Computer Sc. and Engineering
51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt
51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt
predatory parser left factorial 4-CFGs.ppt
Ad

More from Dawood Faheem Abbasi (11)

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

Recently uploaded (20)

PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
master seminar digital applications in india
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Lesson notes of climatology university.
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Structure & Organelles in detailed.
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Complications of Minimal Access Surgery at WLH
Abdominal Access Techniques with Prof. Dr. R K Mishra
Microbial disease of the cardiovascular and lymphatic systems
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pre independence Education in Inndia.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Cell Types and Its function , kingdom of life
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
TR - Agricultural Crops Production NC III.pdf
master seminar digital applications in india
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Lesson notes of climatology university.
VCE English Exam - Section C Student Revision Booklet
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Cell Structure & Organelles in detailed.
PPH.pptx obstetrics and gynecology in nursing
102 student loan defaulters named and shamed – Is someone you know on the list?
Complications of Minimal Access Surgery at WLH

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