ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Compiler Construction
    COMP3149
    Progress0 / 32 topics
    Topics
    1. Introduction to interpreter and compiler2. Structure of a Compiler and its Phases3. Lexical Analyzer and Input Buffering4. Specifications and Recognitions of Tokens5. Regular Expressions and Finite Automata6. Transition Table and Transition Graph7. Definitions of Grammars, Derivations, and Parse Trees8. Ambiguity, Associativity, and Precedence of Operators9. Syntax Analysis and Role of the Parser10. Eliminating Ambiguity, Left Recursion, and Left Factoring11. Top-Down Parsing and Recursive-Descent Parsing12. First and Follow Sets13. LL(1) Grammars and Non-recursive Predictive Parsing14. Bottom-Up Parsing: Reductions and Shift-Reduce Parsing15. LR Parsing and LR(0) Parsers16. LR(0) Automaton and Parsing Table17. Shift-Reduce Conflicts18. SLR(1) Parsers: Automaton and Parsing Table19. LR(1) Parsers: Automaton and Parsing Table20. LALR Parsing: Automaton and Parsing Table21. Semantic Analysis and Intermediate Code Generation22. Three Address Code23. Tasks of Semantic Analyzer and Types of Errors24. Type Checking and Environments25. Type Conversions: Implicit vs Explicit26. Back Patching and Switch Statements27. Storage Organization and Stack Allocation of Space28. Heap Management and Optimization29. Code Generation: Design of a Code Generator30. Target Language and Addresses in Target Code31. Basic Blocks and Flow Graphs32. Optimization of Basic Blocks
    COMP3149›First and Follow Sets
    Compiler ConstructionTopic 12 of 32

    First and Follow Sets

    4 minread
    644words
    Beginnerlevel

    📘 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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count644
      Code examples0
      DifficultyBeginner