Bird
Raised Fist0
Compiler Designknowledge~20 mins

LR(0) items and parsing in Compiler Design - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
LR(0) Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the Dot Position in LR(0) Items

In LR(0) items, the dot (•) indicates the position of the parser in a production rule. Consider the production rule A → αβ. What does the LR(0) item A → α • β represent?

AThe parser has not recognized any part of the production yet.
BThe parser has recognized β and expects to see α next.
CThe parser has recognized α and expects to see β next.
DThe parser has completed recognizing the entire production.
Attempts:
2 left
💡 Hint

Think about what the dot means in terms of what has been read and what is expected next.

📋 Factual
intermediate
1:30remaining
Number of LR(0) Items for a Production

Given a production rule S → XYZ, how many distinct LR(0) items can be formed from this production?

A4
B5
C3
D6
Attempts:
2 left
💡 Hint

Count the positions where the dot can be placed, including before the first symbol and after the last symbol.

🔍 Analysis
advanced
2:30remaining
Closure Operation in LR(0) Parsing

Consider the LR(0) item set containing A → α • B β. What is the purpose of the closure operation applied to this item set?

ATo finalize the parsing table entries for the item set.
BTo move the dot past B in the item <code>A → α • B β</code>.
CTo remove items that do not contain the non-terminal B.
DTo add all items for productions of B with the dot at the start to the set.
Attempts:
2 left
💡 Hint

Think about what happens when the dot is before a non-terminal symbol.

❓ Reasoning
advanced
2:30remaining
Shift-Reduce Conflict in LR(0) Parsing

Why do LR(0) parsers often encounter shift-reduce conflicts in grammars that are not LR(0)?

ABecause LR(0) parsers use too many lookahead symbols, confusing the parser.
BBecause LR(0) items do not consider lookahead symbols, causing ambiguity in action decisions.
CBecause LR(0) parsers cannot handle any non-terminal symbols in productions.
DBecause LR(0) parsers always reduce before shifting, causing conflicts.
Attempts:
2 left
💡 Hint

Consider what information LR(0) items lack compared to more powerful parsers.

🚀 Application
expert
3:00remaining
Determining the Next State in LR(0) Parsing

Given the LR(0) item set containing S' → • S and the grammar with productions S → a S b and S → ε, what is the next item set after reading the symbol a?

A{ S → a • S b, S → • a S b, S → • ε }
B{ S' → S •, S → a • S b, S → • a S b, S → • ε }
C{ S → a S • b }
D{ S → a S b • }
Attempts:
2 left
💡 Hint

Consider the GOTO operation on symbol a and the closure of the resulting items.

Practice

(1/5)
1. What does an LR(0) item represent in compiler design?
easy
A. A syntax error in the source code
B. A token recognized by the lexer
C. A grammar rule with a dot showing how much has been read
D. A semantic action to be performed

Solution

  1. Step 1: Understand LR(0) item definition

    An LR(0) item is a grammar production with a dot indicating the position up to which input has been processed.
  2. Step 2: Differentiate from other concepts

    Tokens are lexer outputs, errors are problems, and semantic actions are separate; only the dot notation fits LR(0) items.
  3. Final Answer:

    A grammar rule with a dot showing how much has been read -> Option C
  4. Quick Check:

    LR(0) item = grammar rule + dot position [OK]
Hint: LR(0) items always show a dot in grammar rules [OK]
Common Mistakes:
  • Confusing LR(0) items with tokens
  • Thinking LR(0) items are errors
  • Mixing semantic actions with LR(0) items
2. Which of the following is the correct way to write an LR(0) item for the production A -> αβ where the dot is before β?
easy
A. A -> α · β
B. A -> · αβ
C. A -> αβ ·
D. A -> β · α

Solution

  1. Step 1: Understand dot placement in LR(0) items

    The dot shows how much of the production has been seen. Before β means after α, so dot goes after α.
  2. Step 2: Match the correct notation

    A -> α · β places the dot correctly as A -> α · β. Others place dot incorrectly.
  3. Final Answer:

    A -> α · β -> Option A
  4. Quick Check:

    Dot before β means after α [OK]
Hint: Dot shows position; before β means after α [OK]
Common Mistakes:
  • Placing dot at start when it should be after α
  • Putting dot at end incorrectly
  • Swapping α and β positions
3. Given the grammar rule S -> a A b, what is the LR(0) item after reading a?
medium
A. S -> · a A b
B. S -> a · A b
C. S -> a A · b
D. S -> a A b ·

Solution

  1. Step 1: Identify the position of the dot after reading 'a'

    Reading 'a' means the dot moves past 'a' in the production.
  2. Step 2: Place the dot correctly

    So the item becomes S -> a · A b, showing 'a' is read and next is 'A'.
  3. Final Answer:

    S -> a · A b -> Option B
  4. Quick Check:

    Dot moves past read symbols [OK]
Hint: Dot moves right after reading a symbol [OK]
Common Mistakes:
  • Keeping dot before 'a' after reading it
  • Moving dot too far past unread symbols
  • Confusing order of symbols
4. Identify the error in this LR(0) item: B -> α β · γ
medium
A. Dot placement is correct
B. Dot must be at the start or end only
C. Dot should be before γ, not after β
D. Dot cannot be placed between two symbols

Solution

  1. Step 1: Understand valid dot positions in LR(0) items

    The dot can appear before any symbol or at the end of the production.
  2. Step 2: Check the given item

    In B -> α β · γ, the dot is before γ, which is valid.
  3. Final Answer:

    Dot placement is correct -> Option A
  4. Quick Check:

    Dot can be anywhere before or after symbols [OK]
Hint: Dot can be before any symbol or at end [OK]
Common Mistakes:
  • Thinking dot can't be between symbols
  • Believing dot must be only at start or end
  • Misreading dot position
5. In LR(0) parsing, what action should be taken when the parser encounters an LR(0) item with the dot at the end of a production, like A -> α ·?
hard
A. Report a syntax error
B. Shift the next input symbol
C. Accept the input string
D. Reduce by the production A -> α

Solution

  1. Step 1: Understand dot at end meaning

    When the dot is at the end, it means the entire production has been recognized.
  2. Step 2: Determine parser action

    The parser should reduce the recognized symbols to the non-terminal on the left side of the production.
  3. Final Answer:

    Reduce by the production A -> α -> Option D
  4. Quick Check:

    Dot at end means reduce [OK]
Hint: Dot at end means reduce by that production [OK]
Common Mistakes:
  • Trying to shift instead of reduce
  • Confusing accept with reduce
  • Reporting error incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard