Professional Documents
Culture Documents
Kashif Sharif School of Computer Science & Technology
Kashif Sharif School of Computer Science & Technology
S A | B A aA | b | Bc
S •A S A•
S •B S B•
A •aA A a•A A aA•
A •b S b•
A•
B •c S c•
G(S): S→A|B A→aA|b|ε B→c
S→·A S→A·
S→·B S→B·
A→·aA A→a·A A→aA·
A→·b A→b·
A→·
B→·c B→c·
Simplified Example:
S’→A A→aA | b
2 a 3 A 7
A→·aA A→a·A A→aA·
1 A 6
S → ·A S →A ·
4 b 5
A→ ·b A→b ·
NFA
NFA to DFA
a
A
2 5
a
1 A 3 b
b
4
Example
Input: a b $
1 $
Input: a b $
2 a
1 $
Input: a b $
3 b
2 a
1 $
Input: a b $
A
2 a
1 $
Input: a b $
4 A
2 a
1 $
Input: a b $
A
1 $
Input: a b $
5 A
1 $
Input: a b $
1 $
Directly build the DFA (using Closure & GOTO)
Things to remember, so far
Canonical Collection
C={Closure({[E’.E]})}
DFA
States are Sets of items from
canonical collection
Transitions are GOTO(I,X)
Note: Each state is
associated to a specific
symbol.
id * id
Table construction
Productions are numbered
si means shift and stack had state i.
rj means reduce using the production number j.
acc is accept.
Blank means error.
Given a grammar G, augment G to produce G’. i.e. S’E
For G’, construct C the canonical collection and GOTO functions.
Find Follow(A) for each non-terminal A in G’.
Items in I0
F.(E)
F.id
EE.+T
Items in I2
ET. Follow(E)={ }
TT.*F
Remember:
LR(0)
The simplest LR parser
Covers more grammars than
LL, but still has issues.
Shift/Reduce Conflict
Reduce/Reduce Conflict
Shift/Reduce Conflict
A shift-reduce conflict occurs in a state that
requests both a shift action and a reduce action.
Shift/Reduce Conflict - Solutions
Fix the grammar