Shift-reduce parsing in Compiler Design - Time & Space Complexity
When we analyze shift-reduce parsing, we want to understand how the time it takes grows as the input gets longer.
We ask: How does the parser's work increase when the input size increases?
Analyze the time complexity of the following simplified shift-reduce parsing steps.
while (input not empty) {
if (can reduce top of stack) {
reduce();
} else {
shift();
}
}
while (stack can be reduced) {
reduce();
}
This code shows the parser shifting input symbols onto a stack or reducing them by grammar rules until the input is fully processed.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Shifting input symbols and reducing stack elements repeatedly.
- How many times: Each input symbol is shifted once, and reductions happen as needed, but total steps relate to input length.
As input size grows, the parser processes each symbol mostly once, with some extra steps for reductions.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20-30 steps |
| 100 | About 200-300 steps |
| 1000 | About 2000-3000 steps |
Pattern observation: The total steps grow roughly in proportion to the input size, maybe a few times more due to reductions.
Time Complexity: O(n)
This means the parsing time grows linearly with the length of the input; doubling input roughly doubles the work.
[X] Wrong: "Shift-reduce parsing always takes quadratic time because of repeated reductions."
[OK] Correct: Each input symbol is shifted once, and reductions are limited by grammar rules, so total steps grow linearly, not quadratically.
Understanding how shift-reduce parsing scales helps you explain parser efficiency clearly and shows you grasp practical compiler design concepts.
"What if the grammar allowed ambiguous reductions causing backtracking? How would the time complexity change?"
Practice
shift in shift-reduce parsing?Solution
Step 1: Understand the role of shift
In shift-reduce parsing, shifting means taking the next input symbol and placing it on the stack.Step 2: Differentiate shift from reduce
Reduce means replacing symbols on the stack using grammar rules, while shift just moves input symbols onto the stack.Final Answer:
To move the next input symbol onto the stack -> Option AQuick Check:
Shift = move input symbol to stack [OK]
- Confusing shift with reduce
- Thinking shift removes symbols
- Assuming shift checks grammar
Solution
Step 1: Recall the parsing process order
Shift-reduce parsing first shifts symbols onto the stack, then reduces them using grammar rules, and finally accepts if input is valid.Step 2: Match the correct sequence
The correct order is Shift, then Reduce, then Accept.Final Answer:
Shift, Reduce, Accept -> Option CQuick Check:
Shift -> Reduce -> Accept [OK]
- Mixing up reduce and shift order
- Thinking accept happens before reduce
- Assuming reduce happens before any shift
S -> aAA -> bAnd input string
ab, what is the correct sequence of stack contents during shift-reduce parsing?Solution
Step 1: Shift input symbols onto stack
First, shift 'a' onto the stack: stack = [a]. Then shift 'b': stack = [a, b].Step 2: Apply reductions using grammar rules
Reduce 'b' to 'A' using A -> b, then reduce 'aA' to 'S' using S -> aA. Final stack is [S].Final Answer:
[ ] -> [a] -> [a, b] -> [S] -> Option DQuick Check:
Shift a, shift b, reduce to S [OK]
- Reducing too early before all symbols shifted
- Mixing order of symbols on stack
- Skipping reduction steps
Solution
Step 1: Understand reduction failure
If no grammar rule matches the symbols on the stack, the parser cannot reduce, indicating a problem with the input.Step 2: Interpret the failure meaning
This usually means the input string does not follow the grammar rules and is invalid or contains errors.Final Answer:
The input string is invalid or contains errors -> Option BQuick Check:
Failed reduce = invalid input [OK]
- Assuming parser is always correct
- Thinking reduce failure means grammar is simple
- Confusing shift errors with reduce errors
E -> E + T | TT -> intFor the input string
int + int + int, which sequence of actions correctly shows the shift-reduce parsing steps to fully reduce the input?Solution
Step 1: Shift and reduce first int
Shift 'int', reduce to T, then reduce T to E (since E -> T).Step 2: Process first plus and second int
Shift '+', shift 'int', reduce 'int' to T, then reduce 'E + T' to E.Step 3: Process second plus and third int
Shift '+', shift 'int', reduce 'int' to T, then reduce 'E + T' to E again, fully reducing input.Final Answer:
Shift int, reduce T, reduce E, shift +, shift int, reduce T, reduce E, shift +, shift int, reduce T, reduce E -> Option AQuick Check:
Shift and reduce each int, then reduce E + T repeatedly [OK]
- Reducing E too early or too late
- Skipping shifts of '+' symbols
- Confusing order of reduce steps
Ready to prove it?
Pass the test and this pattern moves unknown
