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 PortalCOMP3149Ll1 Grammars Predictiv…
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 13 of 32
    Topic Study Note Illustration
    13

    LL(1) Grammars and Non-recursive Predictive Parsing

    (in simple language with definitions, steps, diagrams, table construction, and key exam points)

    πŸ“˜ LL(1) Grammars and Non-Recursive Predictive Parsing

    (in simple language with definitions, steps, diagrams, table construction, and key exam points)


    🧠 1. LL(1) Grammar

    βœ… Definition

    An LL(1) grammar is a grammar that can be parsed:

    • L β†’ Left to right scanning of input
    • L β†’ Leftmost derivation
    • 1 β†’ One lookahead symbol is enough

    πŸ‘‰ So, LL(1) means:

    β€œPredict the correct production using only 1 input symbol lookahead.”


    ⭐ Key Idea

    LL(1) parser is a top-down predictive parser with no backtracking.


    πŸ“Š 2. Conditions for LL(1) Grammar (Very Important)

    A grammar is LL(1) if:


    πŸ”Ή 1. No Left Recursion

    ❌ Bad:

    E β†’ E + T | T
    

    βœ” Fixed:

    E β†’ T E'
    

    πŸ”Ή 2. Left Factored

    ❌ Bad:

    A β†’ ab | ac
    

    βœ” Fixed:

    A β†’ aA'
    A' β†’ b | c
    

    πŸ”Ή 3. FIRST & FOLLOW Condition (Most Important)

    For:

    A β†’ Ξ± | Ξ²
    

    Grammar is LL(1) if:

    βœ” Condition 1:

    FIRST(Ξ±) ∩ FIRST(Ξ²) = βˆ…
    

    βœ” Condition 2 (if Ξ΅ exists):

    FIRST(Ξ±) ∩ FOLLOW(A) = βˆ…
    

    ⭐ Key Point (Exam)

    πŸ‘‰ No ambiguity + no overlap in FIRST/FOLLOW = LL(1)


    🧠 3. Predictive Parsing

    βœ… Definition

    Predictive parsing is a top-down parsing method that:

    • Uses LL(1) grammar
    • Uses a parsing table
    • Does not use backtracking

    πŸ”§ 4. Non-Recursive Predictive Parsing

    βœ… Definition

    A Non-Recursive Predictive Parser uses:

    • A stack
    • A parsing table
    • Input buffer

    πŸ‘‰ It does NOT use recursion (unlike recursive descent).


    πŸ“Š 5. Structure of Predictive Parser

    Input Buffer β†’ [Stack + Parsing Table] β†’ Output
    

    πŸ”Ή Components

    1. Stack

    • Stores grammar symbols
    • Starts with $S

    2. Input Buffer

    • Contains input string + $

    3. Parsing Table (M)

    • Guides production selection

    πŸ“Œ 6. Algorithm (Very Important)

    Step-by-step:

    1. Initialize stack:

      $ S
      
    2. Read input string with $

    3. Repeat:

      • If top of stack = input symbol β†’ pop & move input
      • If top is non-terminal β†’ use parsing table
      • If mismatch β†’ error

    πŸ§ͺ 7. Example Grammar

    E β†’ T E'
    E' β†’ + T E' | Ξ΅
    T β†’ id
    

    πŸ”΅ FIRST & FOLLOW (Needed)

    FIRST:

    FIRST(E) = {id}
    FIRST(E') = {+, Ξ΅}
    FIRST(T) = {id}
    

    FOLLOW:

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

    πŸ“Š 8. LL(1) Parsing Table

    Non-Terminal id + $
    E E β†’ T E'
    E' E' β†’ + T E' E' β†’ Ξ΅
    T T β†’ id

    πŸ”„ 9. Parsing Example

    Input:

    id + id $
    

    Stack Process (Simplified)

    Stack Input Action
    $E id+id$ E β†’ T E'
    $E'T id+id$ T β†’ id
    $E'T id+id$ match id
    $E' +id$ E' β†’ +T E'
    $E'T+ +id$ match +
    ... ... continues

    βœ” Accept when both reach $


    ⚠️ 10. Errors in Predictive Parsing

    • Missing table entry β†’ error
    • Stack-input mismatch β†’ error
    • Grammar not LL(1) β†’ cannot parse

    πŸ“Œ 11. Advantages

    βœ” No backtracking βœ” Fast parsing βœ” Easy implementation βœ” Deterministic


    ❌ 12. Disadvantages

    ❌ Cannot handle left recursion ❌ Cannot handle ambiguous grammar ❌ Requires grammar transformation


    🎯 13. Important Exam Questions

    πŸ‘‰ Very frequently asked:

    • Define LL(1) grammar
    • Conditions for LL(1) grammar
    • Construct predictive parsing table
    • Explain non-recursive predictive parsing
    • Difference between recursive and non-recursive parsing
    • Steps of predictive parser algorithm

    πŸ“ 14. Short Notes (Quick Revision)

    LL(1)

    • Left to right input
    • Leftmost derivation
    • 1 lookahead symbol

    Predictive Parsing

    • Uses stack + table
    • No backtracking
    • Top-down method

    Non-Recursive Parser

    • Stack-based
    • Table-driven
    • Efficient

    πŸ“Š 15. Final Summary Table

    Feature LL(1) Grammar Predictive Parsing
    Meaning Grammar type Parsing technique
    Lookahead 1 symbol 1 symbol
    Method Rules Stack + table
    Backtracking No No
    Requirement FIRST & FOLLOW Parsing table
    Efficiency High High
    Use Grammar design Syntax analysis

    βœ… Final Conclusion

    • LL(1) grammar is designed for predictive parsing
    • Non-recursive predictive parsing uses a stack + parsing table
    • It is fast, deterministic, and widely used in compilers

    ← Previous topic 12
    First and Follow Sets
    Next topic 14 β†’
    Bottom-Up Parsing: Reductions and Shift-Reduce 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
    629words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction