Professional Documents
Culture Documents
Ch4b Modified PDF
Ch4b Modified PDF
Syntax Analysis
Part II
Chapter 4
Bottom-Up Parsing
• LR (k) methods (Left-to-right, Rightmost
derivation, k is lookahead symbol, default is
1)
– SLR, Canonical LR, LALR
• Other special cases:
– Shift-reduce parsing
– Operator-precedence parsing
3
Shift-Reduce Parsing
Grammar: Reducing a sentence: Shift-reduce corresponds
SaABe abbcde to a rightmost derivation:
AAbc|b aAbcde S rm a A B e
Bd aAde rm a A d e
aABe rm a A b c d e
These match S rm a b b c d e
production’s
right-hand sides
S
A A A
A A A B A B
a b b c d e a b b c d e a b b c d e a b b c d e
4
Handles
A handle is a substring of grammar symbols in a
right-sentential form that matches a right-hand side
of a production
Grammar: abbcde
SaABe aAbcde
AAbc|b aAde Handle
Bd aABe
S
abbcde
aAbcde NOT a handle, because
aAAe further reductions will fail
…? (result is not a sentential form)
5
Stack Implementation of
Shift-Reduce Parsing
Stack Input Action
$ id+id*id$ shift
$id +id*id$ reduce E id How to
Grammar: $E +id*id$ shift
$E+ id*id$ shift
resolve
EE+E conflicts?
$E+id *id$ reduce E id
EE*E $E+E *id$ shift (or reduce?)
E(E) $E+E* id$ shift
E id $E+E*id $ reduce E id
$E+E*E $ reduce E E * E
$E+E $ reduce E E + E
Found handles $E $ accept
to reduce
6
Conflicts
• Shift-reduce and reduce-reduce conflicts are
caused by
– The limitations of the LR parsing method (even
when the grammar is unambiguous)
– Ambiguity of the grammar
7
Model of an LR Parser
input a1 a2 … ai … an $
stack
LR Parsing Program
sm (driver) output
Xm
sm-1
Xm-1 action goto Constructed with
… LR(0) method,
shift DFA SLR method,
s0 reduce LR(1) method, or
accept LALR(1) method
error
8
LR (0) Parser
LR(0) parser don't do any lookahead (that is, they look ahead zero
symbols) before deciding which reduction to perform.
9
Example ((a),b)
13
LR(0) Limitations
14
Shift-Reduce Parsing:
Shift-Reduce Conflicts
Stack Input Action
$… …$ …
$…if E then S else…$ shift or reduce?
Ambiguous grammar:
S if E then S
| if E then S else S
| other
Resolve in favor
of shift, so else
matches closest if
15
Shift-Reduce Parsing:
Reduce-Reduce Conflicts
Stack Input Action
$ aa$ shift
$a a$ reduce A a or B a ?
Grammar:
CAB
Aa
Ba
Resolve in favor
of reducing A a,
otherwise we’re stuck!
16
SLR Parsing
Shift-Reduce Conflict
• The simple improvement that SLR(1)
makes on the basic LR(0) parser is to
reduce only if the next input token is a
member of the follow set of the nonterminal
being reduced.
• Shift-Reduce conflict can be reduced using
SLR parsing, because Follow is used for
reduce operation in SLR parsing.
37
I8 L *R , =/$
R L, =/$ I7
Example LR(1) Parsing Table
id * = $ S L R
0 s3 s5 1 2 4
1 acc
Grammar:
2 s6 r6
1. S’ S
2. S L = R 3 r5 r5
3. S R 4 r3 r3
4. L * R 5 s3 s5 7 8
5. L id 6 s3' s5' 7’ 9
6. R L 7 r6 r6
8 r4 r4
9 r2
3’ r5
5’ s3' s5' 7’ 8’
7’ r6
8’ r4
47
LR(1) Limitations
• A LR(1) grammar is one where the construction
of an LR(1) parse table does not require two
action (shift-reduce or reduce-reduce) in any
one cell.
• Many conflicts in SLR(1) parse tables are
avoided if the LR(1) parse approach is used,
because the latter approach is more restrictive
on where it allows reduce operations. An
SLR(1) parse table may allow reduces where
the next input token should not allow such.
LALR(1) parsing
• This is the result of an effort to reduce the
number of states in an LR(1) parser.
• We notice that some states in our LR(1)
automaton have the same core items and differ
only in the possible lookahead information.
Furthermore, their transitions are similar.
– States I3 and I3', I5 and I5', I7 and I7', I8 and I8'
• We shrink our parser by merging such states.
• SLR : 10 states, LR(1): 14 states, LALR(1) : 10 states
LALR(1) parsing
I1 I9
S
I6 S L= R, $ R SL=R, $
I0 S' S, $ S' S , $
S L=R, $ R L, $
S R, $ L L *R, $ id I
S L =R, $ = L id, $
3
L *R, =/$
I2 R L , $
L id, =/$ * L
R L, $ *
L *R, =/$
id R I5 R L, =/$
L id, =/$
I3 L id , =/$ R L, =/$ I7
id L *R, =/$ L
* R
I4 S R, =/$
I8 L *R , =/$
Example LR(1) Parsing Table
id * = $ S L R
0 s3 s5 1 2 4
1 acc
Grammar:
2 s6 r6
1. S’ S
3
2. S L = R r5 r5
3. S R 4 r3 r3
4. L * R 5 s3 s5 7 8
5. L id 6 s3 s5 7 9
6. R L 7 r6 r6
8 r4 r4
9 r2
Conflicts in LALR(1) parsing
• Note that the conflict that had vanished
when we created the LR(1) parser has not
reappeared.
• Can LALR(1) parsers introduce conflicts
that did not exist in the LR(1) parser?
• Unfortunately YES.
• BUT, only reduce/reduce conflicts.
Conflicts in LALR(1) parsing
• LALR(1) parsers cannot introduce shift/reduce conflicts.
– Such conflicts are caused when a lookahead is the
same as a token on which we can shift. They depend
on the core of the item. But we only merge states that
had the same core to begin with. The only way for an
LALR(1) parser to have a shift/reduce conflict is if
one existed already in the LR(1) parser.
• LALR(1) parsers can introduce reduce/reduce conflicts.
– Here's a situation when this might happen:
A B , x A B,y A B , x/y
merges with to give:
A C , y A C , x A C , x/y
53
Example
54
55
56
57
Example The grammar G
S’ → S
S →CC
C →cC | d
• Let I = { (S’ → •S, $) }
• I0 = closure(I) = {
S’ → •S, $
S → • C C, $
C → •c C, c/d
C → •d, c/d
}
• goto(I0, S) = closure( {S’ → S •, $ } )
= {S’ → S •, $ } = I1
Exercise
The grammar G
S’ → S
S →CC
• Let I = { (S → C •C, $) } C →cC | d
• I2 = closure(I) = ?
• I3 = goto(I2, c) = ?
LR(1) Automation
Example
ACTION GOTO The grammar G
state
c d $ S C S’ → S
0 s3 s4 1 2 S →CC
1 acc C →cC | d
2 s6 s7 5
3 s3 s4 8
4 r3 r3
5 r1
6 s6 s7 9
7 r3
8 r2 r2
9 r2
LL, SLR, LR, LALR Summary
• LL parse tables computed using FIRST/FOLLOW
– Nonterminals terminals productions
– Computed using FIRST/FOLLOW
• LR parsing tables computed using closure/goto
– LR states terminals shift/reduce actions
– LR states nonterminals goto state transitions
• A grammar is
– LL(1) if its LL(1) parse table has no conflicts
– SLR if its SLR parse table has no conflicts
– LR(1) if its LR(1) parse table has no conflicts
– LALR(1) if its LALR(1) parse table has no conflicts
63
Classification of Grammars
YACC
yacc
specification Yacc or Bison y.tab.c
yacc.y compiler
y.tab.c C a.out
compiler
input output
stream a.out stream