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 PortalCOMP3149Lr1 Automaton Parsing โ€ฆ
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 19 of 32
    Topic Study Note Illustration
    19

    LR(1) Parsers: Automaton and Parsing Table

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

    ๐Ÿ“˜ 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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    663words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction