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 PortalCOMP3149Lr0 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 16 of 32
    Topic Study Note Illustration
    16

    LR(0) Automaton and Parsing Table

    (in simple language with definitions, construction steps, diagrams, and examples)

    πŸ“˜ LR(0) Automaton and Parsing Table

    (in simple language with definitions, construction steps, diagrams, and examples)


    🧠 1. LR(0) Automaton

    βœ… Definition

    An LR(0) automaton is a finite state machine (DFA) constructed from LR(0) items of a grammar. It is used to guide bottom-up (shift-reduce) parsing.

    πŸ‘‰ It shows:

    • States (sets of items)
    • Transitions between states

    πŸ“Œ 2. LR(0) Item (Revision)

    A production with a dot (β€’):

    A β†’ Ξ± β€’ Ξ²
    

    πŸ‘‰ Dot shows how much of the production is already seen.


    πŸ”§ 3. Two Important Functions

    πŸ”Ή 1. Closure

    If:

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

    Then include:

    • All productions of B with dot at start

    πŸ”Ή 2. GOTO

    Moves dot over a symbol:

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

    πŸ”„ 4. Construction of LR(0) Automaton

    πŸ“Œ Step-by-step Procedure


    πŸ”Ή Step 1: Augment Grammar

    Add new start symbol:

    S' β†’ S
    

    πŸ”Ή Step 2: Start State (Closure of S')

    I0 = CLOSURE(S' β†’ β€’ S)
    

    πŸ”Ή Step 3: Compute GOTO

    For each state and symbol:

    GOTO(I, X)
    

    πŸ”Ή Step 4: Repeat

    Continue until no new states are generated.


    πŸ“Š 5. Example Grammar

    S β†’ CC
    C β†’ cC | d
    

    πŸ”Ή Augmented Grammar

    S' β†’ S
    S β†’ CC
    C β†’ cC | d
    

    🌳 6. LR(0) Automaton (Conceptual Diagram)

    States:

    I0 β†’ I1 β†’ I2 β†’ ...
    

    Transitions:

    I0 --S--> I1
    I0 --C--> I2
    I0 --c--> I3
    I0 --d--> I4
    

    πŸ‘‰ Each state = set of LR(0) items


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

    βœ… Definition

    An LR(0) parsing table is a table that tells the parser:

    • When to shift
    • When to reduce
    • When to accept
    • Where to go next

    πŸ“Š Structure

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

    πŸ”§ 8. Table Entries

    πŸ”Ή 1. Shift

    If:

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

    Then:

    ACTION[i, a] = Shift j
    

    πŸ”Ή 2. Reduce

    If:

    A β†’ Ξ± β€’
    

    Then:

    ACTION[i, a] = Reduce A β†’ Ξ±
    (for all terminals)
    

    πŸ”Ή 3. Accept

    If:

    S' β†’ S β€’
    

    Then:

    ACTION[i, $] = ACCEPT
    

    πŸ”Ή 4. GOTO

    If:

    GOTO(I, A) = j
    

    Then:

    GOTO[i, A] = j
    

    πŸ§ͺ 9. Example (Simple Table Idea)

    Grammar:

    S β†’ CC
    C β†’ cC | d
    

    πŸ”Ή ACTION/GOTO Table (Simplified)

    State c d $ S C
    0 S3 S4 1 2
    1 ACC
    2 S3 S4 5
    3 S3 S4 6
    4 R C→d R C→d R
    5 R S→CC

    Legend:

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

    πŸ”„ 10. How LR(0) Parsing Works

    Steps:

    1. Push initial state (0)

    2. Read input symbol

    3. Check ACTION table:

      • Shift β†’ push state
      • Reduce β†’ pop symbols & push non-terminal
    4. Use GOTO for next state

    5. Accept at final state


    ⚠️ 11. Problems in LR(0)

    ❌ 1. Shift/Reduce Conflict

    • Cannot decide shift or reduce

    ❌ 2. Reduce/Reduce Conflict

    • Multiple reductions possible

    πŸ‘‰ That’s why LR(0) is weak


    πŸ“Š 12. Advantages

    βœ” Simple concept βœ” Foundation of LR parsing βœ” Easy automaton construction


    ❌ 13. Disadvantages

    ❌ Too many conflicts ❌ Not suitable for real compilers ❌ No lookahead symbol


    βš–οΈ 14. LR(0) vs SLR vs CLR

    Feature LR(0) SLR(1) CLR(1)
    Lookahead None FOLLOW set Full lookahead
    Conflicts Many Fewer Almost none
    Power Weak Medium Very strong
    Use Theory Some use Real compilers

    🎯 15. Important Exam Questions

    πŸ‘‰ Frequently asked:

    • What is LR(0) automaton?
    • Construct LR(0) items and automaton
    • Define closure and GOTO
    • Draw LR(0) parsing table
    • Explain shift-reduce parsing using LR(0)
    • Why LR(0) is not used in practice

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

    πŸ”΅ LR(0) Automaton

    • DFA of LR(0) items
    • Uses closure + GOTO

    πŸ”΅ LR(0) Parsing Table

    • ACTION + GOTO table
    • Guides parsing decisions

    πŸ“Š 17. Final Summary Table

    Aspect LR(0) Automaton LR(0) Parsing Table
    Definition DFA of items Parsing decision table
    Components States + transitions ACTION + GOTO
    Purpose Show grammar states Drive parsing
    Construction Closure + GOTO From automaton
    Output State diagram Table format
    Use Basis of LR parsing Shift-reduce parsing

    βœ… Final Conclusion

    • LR(0) automaton represents grammar as a DFA of item sets
    • LR(0) parsing table converts this DFA into shift-reduce decisions
    • It is simple but has many conflicts, so it is mainly used as a foundation for SLR, CLR, and LALR parsing

    ← Previous topic 15
    LR Parsing and LR(0) Parsers
    Next topic 17 β†’
    Shift-Reduce Conflicts

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

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction