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(1) Parsers: Automaton and Parsing Table
    Compiler ConstructionTopic 19 of 32

    LR(1) Parsers: Automaton and Parsing Table

    4 minread
    663words
    Beginnerlevel

    📘 LR(1) Parsers: Automaton and Parsing Table

    (Exam-ready, simple explanation with steps, items, diagrams, and key points)


    🧠 1. What is LR(1) Parser?

    ✅ Definition

    An LR(1) parser is a bottom-up parsing technique that:

    • Scans input Left to right (L)
    • Produces Rightmost derivation in reverse (R)
    • Uses 1 lookahead symbol (1)

    👉 Full form:

    LR(1) = Left-to-right scan + Rightmost derivation (reverse) + 1 lookahead


    ⭐ Key Idea

    LR(1) parser uses:

    • LR(1) items (with lookahead symbols)
    • More precise decisions than LR(0) and SLR(1)
    • Very powerful parsing method

    🧠 2. LR(1) Item (Very Important)

    ✅ Definition

    An LR(1) item is written as:

    [A → α • β, a]
    

    Where:

    • A → αβ is production
    • • shows parsing position
    • a is lookahead symbol

    📌 Meaning

    👉 “We expect to reduce A → αβ only if next input is a”


    🔄 3. Closure Operation (LR(1))

    Rule:

    If:

    [A → α • B β, a]
    

    Then for every production:

    B → γ
    

    Add:

    [B → • γ, b]
    

    Where:

    • b ∈ FIRST(βa)

    ⭐ Key Point

    👉 Lookahead makes LR(1) very precise


    🔧 4. GOTO Operation (LR(1))

    If:

    [A → α • X β, a]
    

    Then:

    GOTO(I, X) = [A → α X • β, a]
    

    📊 5. LR(1) Automaton

    ✅ Definition

    An LR(1) automaton is a DFA of LR(1) item sets.

    👉 It contains:

    • States = LR(1) item sets
    • Transitions = GOTO function

    🔹 Construction Steps

    Step 1: Augment Grammar

    S' → S
    

    Step 2: Start State

    I0 = CLOSURE([S' → • S, $])
    

    Step 3: Build States

    • Apply GOTO for all symbols
    • Repeat until no new states

    📌 Concept Diagram

    I0 --S--> I1
    I0 --C--> I2
    I1 --+--> I3
    

    🧪 6. Example Grammar

    S → CC
    C → cC | d
    

    Step 1: Augment Grammar

    S' → S
    

    Step 2: Start Item

    [S' → • S, $]
    

    Step 3: LR(1) Item Example

    [S → • CC, $]
    [C → • cC, c/d/$]
    [C → • d, c/d/$]
    

    📊 7. LR(1) Parsing Table

    ✅ Structure

    State ACTION (terminals) GOTO (non-terminals)
    c d $ S C

    🔧 8. Table Construction Rules


    🔹 1. SHIFT

    If:

    [A → α • a β, b]
    

    Then:

    ACTION[i, a] = SHIFT j
    

    🔹 2. REDUCE (Important LR(1) rule)

    If:

    [A → α •, a]
    

    Then:

    ACTION[i, a] = REDUCE A → α
    

    👉 Only for that specific lookahead a


    🔹 3. ACCEPT

    If:

    [S' → S •, $]
    

    Then:

    ACTION[i, $] = ACCEPT
    

    🔹 4. GOTO

    GOTO[i, A] = j
    

    📊 9. Why LR(1) is Powerful

    ✔ Uses exact lookahead symbol ✔ Avoids unnecessary reductions ✔ Removes most conflicts ✔ Handles complex grammars


    ⚠️ 10. Advantages

    ✔ Very powerful parser ✔ No ambiguity in decisions ✔ No shift/reduce conflicts (in most cases) ✔ Used as theoretical base for LALR


    ❌ 11. Disadvantages

    ❌ Large number of states ❌ Complex parsing table ❌ Memory expensive ❌ Hard to construct manually


    ⚖️ 12. Comparison

    Feature LR(0) SLR(1) LR(1)
    Lookahead None FOLLOW Specific symbol
    Power Low Medium Very High
    States Few Medium Many
    Conflicts Many Some Very few
    Complexity Low Medium High

    🎯 13. Important Exam Questions

    👉 Very frequently asked:

    • Define LR(1) parser
    • What is LR(1) item?
    • Construct LR(1) automaton
    • Explain LR(1) parsing table
    • Difference between LR(0), SLR, LR(1)
    • Why LR(1) is more powerful
    • Closure and GOTO in LR(1)

    📝 14. Short Notes (Quick Revision)

    🔵 LR(1)

    • Bottom-up parser
    • Uses 1 lookahead symbol
    • Very precise

    🔵 LR(1) Item

    • [A → α • β, a]
    • Includes lookahead

    🔵 Automaton

    • DFA of LR(1) item sets

    🔵 Parsing Table

    • ACTION + GOTO
    • Uses lookahead for decisions

    📊 15. Final Summary Table

    Aspect LR(1) Parser
    Type Bottom-up
    Lookahead 1 (exact symbol)
    Basis LR(1) items
    Power Very high
    Table ACTION + GOTO
    Automaton DFA of item sets
    Conflicts Minimal
    Complexity High

    ✅ Final Conclusion

    • LR(1) parser is the most powerful classical parsing method
    • It uses LR(1) items with lookahead symbols
    • Its automaton is a DFA of item sets
    • It produces very accurate parsing decisions but large tables

    Previous topic 18
    SLR(1) Parsers: Automaton and Parsing Table
    Next topic 20
    LALR Parsing: 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 count663
      Code examples0
      DifficultyBeginner