2. Recursive-descent:
By: Fereshteh Jadidi 2
proc A
If lookahead = a then lookahead=nexttoken() else
{sw=1; error;}
call B;
return;
proc B
If lookahead = b then lookahead=nexttoken() else
{sw=1; error;}
return;
1. S cAd
2. S fBAc
3. A aB
4. B b
9. Closure:
A XYZ 悋惘惘擯
:
x awz
x FM
y d
z f
F (E)
F id
4/6/2022
By: Fereshteh Jadidi 9
Closure of grammar:
A .XYZ
x .awz
x .FM
F .(E)
F .id
悋慍 惡 愀 悋擯惘
terminal
惡 悋擯惘 悋悋 惘惆 愆惆 悴悋擯悵悋惘 惡悋愆惆
悋慍
non_terminal
悋 悴悋擯悵悋惘 惆惡悋 擯惘惆 悋惺惆 惆惘 惡悋愆惆
10. 愆惠
parser
惘愆 惡
LR
4/6/2022
By: Fereshteh Jadidi 10
.1
擧愆惆
State diagram
.2
拆悋惘愕 悴惆 惘愕
)
惘 悋慍 悴惆 擧惘惆 拆惘
(SD
.3
悴惆 惡 惠悴 惡悋 拆悋惘愕惘 惡惘悋 愆惠
0. E E
1. E E+T
2. E T
3. T T*F
4. T F
5. F (E)
6. F id
11. E .E
E .E+T
E .T
E .E+T
E .T
T .T*F
T .F
T .T*F
T .F
F .(E)
F .id
0
SLR STATE DIAGRAM:
0. E E
1. E E+T
2. E T
3. T T*F
4. T F
5. F (E)
6. F id
12. E .E
E .E+T
E .T
E .E+T
E .T
T .T*F
T .F
T .T*F
T .F
F .(E)
F .id
F id.
E E.
E E.+T
E T.
T T.*F
T F.
F (.E)
E .E+T
E .T
E .E+T
E .T
T .T*F
T .F
T .T*F
T .F
F .(E)
F .id
0
5
4
3
2
1
F
(
id
T
E
SLR STATE DIAGRAM:
12
0. E E
1. E E+T
2. E T
3. T T*F
4. T F
5. F (E)
6. F id
悋惶
13. E .E
E .E+T
E .T
E .E+T
E .T
T .T*F
T .F
T .T*F
T .F
F .(E)
F .id
F id.
E E.
E E.+T
E T.
T T.*F
T F.
F (.E)
E .E+T
E .T
E .E+T
E .T
T .T*F
T .F
T .T*F
T .F
F .(E)
F .id
E E+.T
T .T*F
T .F
T .T*F
T .F
F .(E)
F .id
T T*.F
F .(E)
F .id
F (E.)
E E.+T
F (E).
T T*F.
E E+T.
T T.*F
0
5
4
3
2
1
6 9
7
8 11
10
F
(
id
T
E
5
T
E
*
+
F
(
id
(
(
id
T +
id
F
)
2
5
4
5
4
3
6
4
3
7
*
SLR STATE DIAGRAM:
13
0. E E
1. E E+T
2. E T
3. T T*F
4. T F
5. F (E)
6. F id
F
15. i n p u t
s t a c k
( i d + i d ) * i d $
0
$
i d + i d ) * i d $
0 ( 4
$
+ i d ) * i d $
0 ( 4 i d 5
$
+ i d ) * i d $
0 ( 4 F 3
$
+ i d ) * i d $
0 ( 4 T 2
$
+ i d ) * i d $
0 ( 4 E 8
$
i d ) * i d $
0 ( 4 E 8 + 6
$
) * i d $
0 ( 4 E 8 + 6 i d 5
$
) * i d $
0 ( 4 E 8 + 6 F 3
$
) * i d $
0 ( 4 E 8 + 6 T 9
$
) * i d $
0 ( 4 E 8
$
* i d $
0 ( 4 E 8 ) 1 1
$
* i d $
0 F 3
$
* i d $
0 T 2
$
i d $
0 T 2 * 7
$
$
0 T 2 * 7 i d 5
$
$
0 T 2 * 7 F 1 0
$
$
0 T 2
$
$
0 E 1
$
SLR:
惆惘 惘擯悋
stack
0E1
惡擯惘惆 惘悋惘
惘惆 惆惘
$
惠悴 惡 惺 惡悋愆惆
悋
悋 惘愕惆 愀惡
.
0. E E
1. E E+T
2. E T
3. T T*F
4. T F
5. F (E)
6. F id
16. 4/6/2022 16
悴惆惆 惘愆惠
-
愆悋 惠惘悋 悛慍悋惆 惆悋愆擯悋 惺 悧惠 惺惷
BUTTOM-UP PARSER: SLR
悴惆
SLR
擧惆 惘愕 慍惘 擯惘悋惘
:
0
S .S
S .CC
C .cC
C .d
(0)S S
(1)S CC
(2)C cC
(3)C d
17. 4/6/2022 17
悴惆惆 惘愆惠
-
愆悋 惠惘悋 悛慍悋惆 惆悋愆擯悋 惺 悧惠 惺惷
BUTTOM-UP PARSER: SLR
悴惆
SLR
擧惆 惘愕 慍惘 擯惘悋惘
:
0
6
5
4
3
2
1
d
C
C
d
c
C
d
c
S .S
S .CC
C .cC
C .d
S S.
S C.C
C .cC
C .d
C c.C
C .cC
C .d
S
S CC.
3
4
C cC.
c
3
4
(0)S S
(1)S CC
(2)C cC
(3)C d
C d.
18. 4/6/2022
18
悴惆惆 惘愆惠
-
愆悋 惠惘悋 悛慍悋惆 惆悋愆擯悋 惺 悧惠 惺惷
BUTTOM-UP PARSER: SLR
悴惆
SLR
擧惆 惘愕 慍惘 擯惘悋惘
:
0
6
5
4
3
2
1
d
C
C
d
c
C
d
c
S .S
S .CC
C .cC
C .d
S S.
S C.C
C .cC
C .d
C c.C
C .cC
C .d
S
S CC.
3
4
C cC.
c 3
4
(0)S S
(1)S CC
(2)C cC
(3)C d
C d.
State c d $ S C
0
1
2
3
4
5
6
19. 4/6/2022
19
悴惆惆 惘愆惠
-
愆悋 惠惘悋 悛慍悋惆 惆悋愆擯悋 惺 悧惠 惺惷
BUTTOM-UP PARSER: SLR
悴惆
SLR
擧惆 惘愕 慍惘 擯惘悋惘
:
1
(0)S S
(1)S CC
(2)C cC
(3)C d
State c d $ S C
0 s3 s4 1 2
1 acc
2 s3 s4 5
3 s3 s4 6
4 r3 r3 r3
5 r1
6 r2 r2 r2
20. 4/6/2022 20
悴惆惆 惘愆惠
-
愆悋 惠惘悋 悛慍悋惆 惆悋愆擯悋 惺 悧惠 惺惷
0. S S
1. S CC
2. C cC
3. C d
BUTTOM-UP PARSER: CLR
21. S .S {$}
S .CC {$}
C .cC {c,d}
C .d {c,d}
C d.{c,d}
S S. {$}
S C.C {$}
C.cC {$}
C .d {$}
C c.C {c,d}
C .cC {c,d}
C .d {c,d} CcC. {c,d}
C c.C {$}
C .cC {$}
C .d {$}
C d. {$}
S CC.{$}
C cC.{$}
0
4
3
2
1 C
6 9
c
8
c
d
C
S
7
5
d
c
c
d
d
C
3
7
6
C
CLR STATE DIAGRAM
4
By: Fereshteh Jadidi
0. S S
1. S CC
2. C cC
3. C d
22. c d $ S C
0 s3 s4 1 2
1 End
2 s6 s7 5
3 s3 s4 r4 8
4 r3 r3
5 r1
6 s6 s7 9
7 r3
8 r2 r2
9 r1
CLR PARS TABLE:
By: Fereshteh Jadidi 22
23. action
input
s t a c k
s 4
d c c d $
0
$
r 3
c c d $
d 4 0 ( 4
$
s 6
c c d $
$ 0 C 2
s 6
c d $
$ 0 C 2 c 6
s 7
d $
$ 0 C 2 c 6 c 6
r 3
$
$ 0 C 2 c 6 c 6 d 7
r 2
$
$ 0 C 2 c 6 c 6 C 9
r 2
$
$ 0 C 2 c 6 C 9
r 1
$
$ 0 C 2 C 5
A C C
$
$ 0 S 1
4/6/2022
By: Fereshteh Jadidi 23
惆惘 惘擯悋
stack
0S1
惆惘 惡擯惘惆 惘悋惘
惘惆
$
惡 惺 惡悋愆惆
愀惡 悋 惠悴
悋 惘愕惆
.
擧 拆悋惘愕 惡 惶忰 拆悋惘愕 悴惆 惠愕愀 惘悋 慍惘 惘愆惠
惆
.