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 PortalCOMP3149Lr Parsing Lr0
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 15 of 32
    Topic Study Note Illustration
    15

    LR Parsing and LR(0) Parsers

    (in simple language with definitions, working, items, tables, examples, diagrams, and key exam points)

    πŸ“˜ LR Parsing and LR(0) Parsers

    (in simple language with definitions, working, items, tables, examples, diagrams, and key exam points)


    🧠 1. LR Parsing

    βœ… Definition

    LR Parsing is a bottom-up parsing technique that reads input:

    • L β†’ Left to right scanning
    • R β†’ Rightmost derivation in reverse

    πŸ‘‰ So LR means:

    β€œScan input left-to-right and produce rightmost derivation in reverse.”


    ⭐ Key Idea

    LR parser builds the parse tree: πŸ‘‰ from bottom (input) to top (start symbol)


    πŸ“Š 2. Why LR Parsing is Important?

    βœ” Most powerful deterministic parsing method βœ” Used in real compilers βœ” Handles large class of grammars βœ” No backtracking


    πŸ”§ 3. Types of LR Parsers

    Type Meaning
    LR(0) Simplest LR parser
    SLR(1) Simple LR with FOLLOW sets
    CLR(1) Canonical LR (most powerful)
    LALR(1) Look-Ahead LR (used in practice)

    🧠 4. LR(0) Parser

    βœ… Definition

    An LR(0) parser is a bottom-up parser that uses:

    • No lookahead symbol (0 lookahead)
    • LR(0) items
    • State machine (DFA of items)

    πŸ“Œ 5. LR(0) Items

    βœ… Definition

    An LR(0) item is a production with a dot (β€’) showing parsing position.


    πŸ“Œ Example Grammar

    E β†’ E + T
    E β†’ T
    T β†’ id
    

    πŸ”„ LR(0) Items

    For production:

    E β†’ E + T
    

    Items:

    E β†’ β€’ E + T
    E β†’ E β€’ + T
    E β†’ E + β€’ T
    E β†’ E + T β€’
    

    ⭐ Key Point

    πŸ‘‰ Dot shows how much of production is parsed


    🧠 6. Closure Operation

    βœ… Definition

    Closure adds all possible productions for a non-terminal after dot.


    πŸ“Œ Rule

    If:

    A β†’ Ξ± β€’ B Ξ²
    

    Then add:

    • All productions of B with dot at start

    πŸ”§ 7. GOTO Operation

    βœ… Definition

    GOTO moves the dot over a symbol.


    πŸ“Œ Example

    If:

    E β†’ E β€’ + T
    

    After GOTO(+):

    E β†’ E + β€’ T
    

    πŸ“Š 8. LR(0) Automaton (Concept)

    LR(0) parser uses a DFA:

    States (Items) β†’ Transitions β†’ Parsing Table
    

    πŸ“Œ 9. LR(0) Parsing Table

    Two parts:

    Part Meaning
    ACTION shift/reduce/accept
    GOTO next state

    πŸ§ͺ 10. Example Grammar

    E β†’ E + T | T
    T β†’ id
    

    Step 1: Augment Grammar

    E' β†’ E
    

    Step 2: LR(0) Items (Simplified Idea)

    States are built using:

    • Closure
    • GOTO

    Step 3: Parsing Table (Conceptual)

    State id + $ E T
    0 S5 1 2
    1 S6 ACC
    2 R2 R2
    5 R3 R3

    Legend:

    • S = Shift
    • R = Reduce
    • ACC = Accept

    πŸ”„ 11. LR(0) Parsing Process

    Steps:

    1. Start with initial state (0)

    2. Read input left to right

    3. Use ACTION table:

      • shift β†’ push state
      • reduce β†’ apply rule
    4. Use GOTO for transitions

    5. Accept when input is fully parsed


    ⚠️ 12. Limitations of LR(0)

    ❌ Too weak ❌ Cannot handle many grammars ❌ Shift-reduce conflicts common ❌ No lookahead (big issue)


    πŸ“Š 13. LR(0) vs Other Parsers

    Feature LR(0) SLR(1) CLR(1)
    Lookahead None 1 1
    Power Weak Medium Very Strong
    Conflicts Many Fewer Almost none
    Use Theory Some use Compiler design

    🎯 14. Important Exam Concepts

    πŸ‘‰ Frequently asked:

    • Define LR parsing
    • What is LR(0) parser?
    • What is LR(0) item?
    • Closure and GOTO functions
    • Construct LR(0) parsing table
    • Advantages and disadvantages
    • Difference between LR(0), SLR, CLR

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

    πŸ”΅ LR Parsing

    • Bottom-up method
    • Left-to-right scanning
    • Rightmost derivation (reverse)

    πŸ”΅ LR(0) Parser

    • No lookahead
    • Uses item sets
    • ACTION + GOTO tables

    πŸ”΅ LR(0) Item

    • Production with dot

    πŸ“Š 16. Final Summary Table

    Aspect LR Parsing LR(0) Parser
    Definition Parsing technique Simplest LR parser
    Direction Bottom-up Bottom-up
    Lookahead Depends on type None
    Method LR family LR(0) items
    Strength Very powerful Weak
    Components Stack, table Items, DFA
    Use Compiler design Theoretical basis

    βœ… Final Conclusion

    • LR parsing is the most powerful bottom-up parsing technique
    • LR(0) is the simplest version but has limitations
    • It uses items, closure, GOTO, and parsing tables
    • Forms the foundation of SLR, CLR, and LALR parsers

    ← Previous topic 14
    Bottom-Up Parsing: Reductions and Shift-Reduce Parsing
    Next topic 16 β†’
    LR(0) Automaton and Parsing Table

    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
    642words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction