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›LR Parsing and LR(0) Parsers
    Compiler ConstructionTopic 15 of 32

    LR Parsing and LR(0) Parsers

    4 minread
    642words
    Beginnerlevel

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

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