ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3149First Follow Sets
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3149

    Compiler Construction

    Progress0 / 32 done
    Topics32
    1Introduction to interpreter and compiler2Structure of a Compiler and its Phases3Lexical Analyzer and Input Buffering4Specifications and Recognitions of Tokens5Regular Expressions and Finite Automata6Transition Table and Transition Graph7Definitions of Grammars, Derivations, and Parse Trees8Ambiguity, Associativity, and Precedence of Operators9Syntax Analysis and Role of the Parser10Eliminating Ambiguity, Left Recursion, and Left Factoring11Top-Down Parsing and Recursive-Descent Parsing12First and Follow Sets13LL(1) Grammars and Non-recursive Predictive Parsing
    14Reductions and Shift-Reduce Parsing15LR Parsing and LR(0) Parsers16LR(0) Automaton and Parsing Table17Shift-Reduce Conflicts
    18Automaton and Parsing Table
    19Automaton and Parsing Table
    20Automaton and Parsing Table21Semantic Analysis and Intermediate Code Generation22Three Address Code23Tasks of Semantic Analyzer and Types of Errors24Type Checking and Environments
    25Implicit vs Explicit26Back Patching and Switch Statements27Storage Organization and Stack Allocation of Space28Heap Management and Optimization
    29Design of a Code Generator30Target Language and Addresses in Target Code31Basic Blocks and Flow Graphs32Optimization of Basic Blocks
    4 min read
    Topic 12 of 32
    Topic Study Note Illustration
    12

    First and Follow Sets

    in simple language with definitions, rules, examples, steps, and exam tips .

    ๐Ÿ“˜ FIRST and FOLLOW Sets (Compiler Construction)

    in simple language with definitions, rules, examples, steps, and exam tips.


    ๐Ÿง  1. Introduction

    In syntax analysis (LL(1) parsing), we need to decide which grammar rule to use.

    ๐Ÿ‘‰ For that, we compute:

    • FIRST set
    • FOLLOW set

    These help in building predictive parsing tables.


    ๐Ÿ”ต 2. FIRST Set

    โœ… Definition

    The FIRST set of a symbol is the set of all terminals that can appear as the first symbol in some string derived from it.


    ๐Ÿ“Œ Notation

    FIRST(X)
    

    ๐Ÿ”ง Rules to Find FIRST Set


    ๐Ÿ”น Rule 1: Terminal

    If X is a terminal:

    FIRST(X) = {X}
    

    ๐Ÿ”น Rule 2: Non-terminal Production

    If:

    A โ†’ ฮฑ
    

    Then:

    • Add FIRST(ฮฑ) to FIRST(A)

    ๐Ÿ”น Rule 3: If ฮต (epsilon) exists

    If:

    A โ†’ ฮต
    

    Then:

    ฮต โˆˆ FIRST(A)
    

    ๐Ÿ”น Rule 4: Multiple Symbols

    If:

    A โ†’ X Y Z
    

    Then:

    • Add FIRST(X)
    • If X โ†’ ฮต, then check Y, and so on

    ๐Ÿ“Œ Example

    Grammar:

    E โ†’ T E'
    E' โ†’ + T E' | ฮต
    T โ†’ id
    

    ๐Ÿ”ต FIRST Sets

    FIRST(id) = {id}
    FIRST(T) = {id}
    FIRST(E') = {+, ฮต}
    FIRST(E) = {id}
    

    โญ Key Points (Exam)

    • FIRST = starting terminals
    • Includes ฮต if empty string possible
    • Used in predictive parsing

    ๐ŸŸข 3. FOLLOW Set

    โœ… Definition

    The FOLLOW set of a non-terminal is the set of terminals that can appear immediately after it in a derivation.


    ๐Ÿ“Œ Notation

    FOLLOW(A)
    

    ๐Ÿ”ง Rules to Find FOLLOW Set


    ๐Ÿ”น Rule 1: Start Symbol

    For start symbol S:

    $ โˆˆ FOLLOW(S)
    

    ($ = end of input marker)


    ๐Ÿ”น Rule 2: If A โ†’ ฮฑBฮฒ

    Then:

    FIRST(ฮฒ) - ฮต โІ FOLLOW(B)
    

    ๐Ÿ”น Rule 3: If A โ†’ ฮฑB

    Then:

    FOLLOW(A) โІ FOLLOW(B)
    

    ๐Ÿ”น Rule 4: If ฮฒ โ†’ ฮต

    Then also:

    FOLLOW(A) โІ FOLLOW(B)
    

    ๐Ÿ“Œ Example

    Grammar:

    E โ†’ T E'
    E' โ†’ + T E' | ฮต
    T โ†’ id
    

    ๐Ÿ”ต FOLLOW Sets

    Step-by-step:

    FOLLOW(E) = {$}
    FOLLOW(E') = {$}
    FOLLOW(T) = {+, $}
    

    โญ Key Points (Exam)

    • FOLLOW = what comes after a variable
    • Start symbol always has $
    • Used in parsing table construction

    ๐Ÿ”„ 4. Difference Between FIRST and FOLLOW

    Feature FIRST Set FOLLOW Set
    Meaning First terminal in derivation What follows a non-terminal
    Applies to Terminal & Non-terminal Only Non-terminal
    Includes ฮต Yes No
    Used for Starting rule selection Parsing decision making
    Input direction Left side Right side

    ๐Ÿงช 5. Combined Example (Important Exam Type)

    Grammar:

    E โ†’ T E'
    E' โ†’ + T E' | ฮต
    T โ†’ id
    

    ๐Ÿ”ต FIRST

    FIRST(E) = {id}
    FIRST(E') = {+, ฮต}
    FIRST(T) = {id}
    

    ๐ŸŸข FOLLOW

    FOLLOW(E) = {$}
    FOLLOW(E') = {$}
    FOLLOW(T) = {+, $}
    

    ๐Ÿง  6. Why FIRST and FOLLOW are Important

    They are used to build:

    ๐Ÿ‘‰ LL(1) Parsing Table

    • FIRST decides which rule to choose
    • FOLLOW helps when ฮต appears

    โš ๏ธ 7. Common Exam Mistakes

    • Forgetting $ in FOLLOW of start symbol
    • Ignoring ฮต in FIRST
    • Mixing FIRST and FOLLOW rules
    • Not checking all productions

    ๐ŸŽฏ 8. Important Exam Questions

    ๐Ÿ‘‰ Frequently asked:

    • Define FIRST set with example
    • Define FOLLOW set with example
    • Difference between FIRST and FOLLOW
    • Compute FIRST and FOLLOW for given grammar
    • Use FIRST and FOLLOW in LL(1) parsing
    • Importance of ฮต in FIRST

    ๐Ÿ“ 9. Short Notes (Quick Revision)

    ๐Ÿ”ต FIRST

    • First terminal of derivation
    • Includes ฮต if needed

    ๐ŸŸข FOLLOW

    • Symbols that follow a non-terminal
    • Start symbol has $

    ๐Ÿ“Š 10. Final Summary Table

    Aspect FIRST Set FOLLOW Set
    Definition First terminal in derivation Next possible terminals
    Symbol Type Terminal / Non-terminal Non-terminal only
    ฮต Handling Included Not included
    Start Symbol Not special Contains $
    Use Parsing decisions Parsing table creation
    Importance Very High Very High

    โœ… Final Conclusion

    • FIRST set tells what can appear at the beginning
    • FOLLOW set tells what can appear after a symbol
    • Both are fundamental for LL(1) parsing and compiler design

    โ† Previous topic 11
    Top-Down Parsing and Recursive-Descent Parsing
    Next topic 13 โ†’
    LL(1) Grammars and Non-recursive Predictive Parsing

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    644words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction