SLR(1) stands for Simple LR parsing with 1 lookahead symbol. It is a type of bottom-up parser used in compiler design.
Click to reveal answer
beginner
What is the main purpose of SLR(1) parsing?
The main purpose of SLR(1) parsing is to analyze the syntax of a programming language by reading input from left to right and producing a rightmost derivation in reverse, using a single lookahead symbol to decide parsing actions.
Click to reveal answer
intermediate
How does SLR(1) parsing decide when to shift or reduce?
SLR(1) parsing uses a parsing table built from the grammar's LR(0) items and FOLLOW sets. It shifts when the next input symbol matches a transition and reduces when the lookahead symbol is in the FOLLOW set of the production's left side nonterminal.
Click to reveal answer
intermediate
What is a conflict in SLR(1) parsing and why does it occur?
A conflict occurs when the parser cannot decide between shifting or reducing (shift-reduce conflict) or between two reductions (reduce-reduce conflict). It happens because the grammar is not SLR(1) compatible, meaning the parsing table has ambiguous entries.
Click to reveal answer
advanced
What is the difference between SLR(1) and LR(1) parsing?
SLR(1) uses FOLLOW sets for lookahead decisions, which is simpler but less powerful. LR(1) parsing uses specific lookahead sets for each item, making it more precise and able to handle more grammars without conflicts.
Click to reveal answer
What does the '1' in SLR(1) parsing represent?
AOne lookahead symbol
BOne production rule
COne parsing state
DOne grammar symbol
✗ Incorrect
The '1' means the parser looks ahead one symbol to decide parsing actions.
Which set is used in SLR(1) parsing to decide reductions?
AFIRST set
BLR(0) items
CFOLLOW set
DNullable set
✗ Incorrect
SLR(1) uses FOLLOW sets to decide when to reduce.
What type of parsing method is SLR(1)?
ATop-down parsing
BBottom-up parsing
CRecursive descent parsing
DPredictive parsing
✗ Incorrect
SLR(1) is a bottom-up parsing technique.
What causes a shift-reduce conflict in SLR(1) parsing?
AMissing production
BToo many lookaheads
CIncorrect FOLLOW set
DAmbiguous grammar
✗ Incorrect
Ambiguous grammars can cause shift-reduce conflicts.
Compared to SLR(1), LR(1) parsing is:
AMore powerful
BSlower but less accurate
CThe same
DLess powerful
✗ Incorrect
LR(1) parsing is more powerful because it uses specific lookahead sets.
Explain how SLR(1) parsing uses FOLLOW sets to decide when to reduce.
Think about how the parser checks the next input symbol against FOLLOW sets before reducing.
You got /4 concepts.
Describe what a shift-reduce conflict is and why it might happen in SLR(1) parsing.
Consider when the parser cannot decide between shifting or reducing.
You got /4 concepts.
Practice
(1/5)
1. What does the 1 in SLR(1) parsing represent?
easy
A. One grammar rule applied
B. One step shift operation
C. One reduction per state
D. One symbol lookahead during parsing
Solution
Step 1: Understand the meaning of lookahead in parsing
Lookahead means the parser looks at upcoming input symbols to decide the next action.
Step 2: Identify what the number 1 specifies in SLR(1)
The number 1 indicates the parser looks ahead by exactly one symbol to make decisions.
Final Answer:
One symbol lookahead during parsing -> Option D
Quick Check:
SLR(1) means 1 symbol lookahead [OK]
Hint: Remember: number in SLR(n) = lookahead symbols [OK]
Common Mistakes:
Confusing lookahead with number of grammar rules
Thinking it means one shift or reduction only
Assuming it refers to parser states
2. Which of the following is a correct component of the SLR(1) parsing table?
easy
A. ACTION and GOTO tables
B. PREDICT and FOLLOW tables
C. SHIFT and REDUCE stacks
D. FIRST and LAST tables
Solution
Step 1: Recall the structure of SLR(1) parsing tables
SLR(1) parsing uses two main tables: ACTION for parser actions and GOTO for state transitions.
Step 2: Identify the correct table names
Only ACTION and GOTO tables are part of the SLR(1) parsing table, others are unrelated or parts of grammar analysis.
Final Answer:
ACTION and GOTO tables -> Option A
Quick Check:
SLR(1) uses ACTION and GOTO tables [OK]
Hint: ACTION table decides moves; GOTO table changes states [OK]
Common Mistakes:
Mixing parsing tables with grammar sets like FIRST or FOLLOW
Confusing stacks with tables
Assuming PREDICT table exists in SLR(1)
3. Given the grammar:
S -> CC C -> cC | d
What is the correct ACTION for state 2 on input c in SLR(1) parsing?
medium
A. Reduce by C -> d
B. Shift to state 3
C. Accept input
D. Error
Solution
Step 1: Identify state 2's items and possible moves
State 2 likely has an item expecting c to continue parsing C -> cC, so it should shift on c.
Step 2: Determine the action for input c
Since c can start a new C, the parser shifts to state 3 to parse C -> cC.
Final Answer:
Shift to state 3 -> Option B
Quick Check:
Input 'c' leads to shift in state 2 [OK]
Hint: Shift on terminals that start productions [OK]
Common Mistakes:
Choosing reduce too early without checking lookahead
Confusing accept with shift
Assuming error when shift is valid
4. In an SLR(1) parser, a conflict occurs when: state 4 has both shift and reduce actions on the same input symbol. What is the likely cause?
medium
A. Grammar is not SLR(1) compatible
B. Parser table was built incorrectly
C. Lookahead symbol is missing
D. Input string is invalid
Solution
Step 1: Understand shift-reduce conflicts in SLR(1)
Shift-reduce conflicts happen when the parser cannot decide whether to shift or reduce on the same input symbol.
Step 2: Identify the cause of such conflicts
This usually means the grammar is not suitable for SLR(1) parsing, i.e., it is not SLR(1) compatible.
Final Answer:
Grammar is not SLR(1) compatible -> Option A
Quick Check:
Shift-reduce conflict = grammar not SLR(1) [OK]
Hint: Conflicts mean grammar is not SLR(1) compatible [OK]
Common Mistakes:
Blaming parser table construction errors
Thinking input string causes conflicts
Assuming missing lookahead causes conflict
5. You have the grammar:
E -> E + T | T T -> T * F | F F -> ( E ) | id
Why might SLR(1) parsing fail for this grammar, and what is a common solution?
hard
A. Grammar is left-factored; remove left recursion
B. Grammar lacks terminals; add more tokens
C. Grammar is ambiguous; use LALR(1) or LR(1) parsing
D. Grammar is too simple; add more productions
Solution
Step 1: Recognize the grammar type
This grammar is classic expression grammar with ambiguity due to operator precedence and associativity.
Step 2: Understand SLR(1) limitations
SLR(1) parsing often fails on ambiguous grammars because it cannot resolve conflicts with only one symbol lookahead.
Step 3: Identify common solutions
More powerful parsers like LALR(1) or LR(1) handle such grammars by using more precise lookahead and states.
Final Answer:
Grammar is ambiguous; use LALR(1) or LR(1) parsing -> Option C
Quick Check:
Ambiguous grammar needs stronger parser than SLR(1) [OK]
Hint: Ambiguous grammar needs LR(1) or LALR(1) [OK]
Common Mistakes:
Thinking adding tokens fixes ambiguity
Confusing left recursion removal with ambiguity
Assuming grammar simplicity causes failure
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.