Homework 3 LR Parsing Solutions
Homework 3 LR Parsing Solutions
accept
I𝟑 I𝟒
a I𝟏 a +
S→SS.+ S→SS+.
S’→S.
I𝟎 a a S→SS.*
S→S.S+ I𝟐
S’→.S S
S→S.S+ I𝟓
S→S.S* S→a. *
S→.SS+
S S→S.S* S→SS*.
S→.SS+
S→.SS+
S→.SS+
S→.SS*
S→.a
S→.SS*
S-→a
S-→a S
Finding follow of the grammar’s symbols (you may need to find First to find the Follow):
FIRST(S)={a}
FOLLOW(S) = {$.+,*,a}
King Saud University
College of Computer and Information Sciences
Computer Science Department
ACTION GOTO
STATE + * a $ S
0 S2 1
1 S2 acc 3
* in SLR place Reduce action in the follow of the lhs of S->a 2 R3 R3 R3 R3
3 S4 S5 S2 3
*in SLR, place Reduce action in the follow of the lhs of S->SS+ 4 R1 R1 R1 R1
* in SLR place Reduce action in the follow of the lhs of S->SS* 5 R2 R2 R2 R2
SLR Parsing Table T
No conflicts in SLR parsing table, therefore, the grammar is SLR.
E→E+T|T
T→T*F|F
F→ (E)|id
A- LR(0) :
King Saud University
College of Computer and Information Sciences
Computer Science Department
(9) 01 $E $ accept
S-> CC
C-> cC | d
Solution [1][2]:
First(S)={c,d}
Follow(S)={$}
First(C) ={c,d}
Follow(C)={$}
For Augmented Production (initial production) put $ as a look Ahead.
In case we want to do the closure, to find the lookAhead: we find the first of whatever
remaining.
When we change the DOT, when we transfer, we do Not change the LookAhead.
While applying the closure, LookAhead might change.
First of any terminal = the terminal itself
o First($)={$}
o First(C$)=First(C)={c,d}
o We use the notation [C→.cC, c/d] as a shorthand for the two items
:[C→.cC,c]and[C→.cC,d]
o First(c)={c}
o First(d)={d}
In the table, the only difference in LR(1) is where to put the reduce move. Place the
reduce move only in the LookAhead symbols.
King Saud University
College of Computer and Information Sciences
Computer Science Department
References:
[1] “Compilers Principles, Techniques, & Tools” Second Edition, Alfred V. Aho, Monica S.
Lam, Ravi Sethi, Jeffrey D. Ullman
[2] Compiler Design Lecture 14 -- CLR(1) and LALR(1) Parsers: