際際滷

際際滷Share a Scribd company logo
Bottom up parser
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
Recursive-descent:
惓悋
:
1. S  cAd
2. A  aB
3. a
4. B  b
4/6/2022
By: Fereshteh Jadidi 3
Buttom_up parser
Buttom-up parser
4/6/2022
By: Fereshteh Jadidi 5
SLR
LALR
CLR
Simple
Left to right scanning input
Right most deviration in revese
Buttom_up parser
S aABe
A  Abc
b
B  d
By: Fereshteh Jadidi
6

惡悋愆惆 悋惺惆 悋慍  惺悋惆  悋 惘愆惠 慍惘 惡
handle
擯惆
惓悋
:
a b b c d e
a A b c d e
a A d e
a A B e
S
Buttom_up parser:
)Shift_reduce Parser(
4/6/2022
By: Fereshteh Jadidi 7
stack
Input Buffer
惡惘悋
parser
Pars table
$
State diagram

Reduce
:
惺惶惘 惆 悋   惠
stack
惘悋
pop
悛 惆惘 惺惶惘   惘惆
push
 

Shift
:
push
悋愕惠 惆惘 惘悋 惺惶惘 惘惆
buttom_up
惘悋
shift
擯惆
4/6/2022
By: Fereshteh Jadidi 8
悋 惺 愕惠 悋惡惠惆悋 惆惘 愀
慍
悋愕惠 悽悋惆 慍 惘惆
惆惆  愆悋 惘悋 拆惘惆悋慍愆 愀
惺 惡惘愕惆 悋惠悋 惡 愀 悋擯惘

 悋愕惠 愆惆 惠悋 拆惘惆悋慍愆
惡惘惘愕 悋 愀惘 惡 惘惆
悋愕惠 愆惆
.
LR(0) item:
A xyz
A .xyz
A x.yz
A xy.z
A xyz.
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
 悋 悴悋擯悵悋惘 惆惡悋 擯惘惆  悋惺惆 惆惘 惡悋愆惆
愆惠
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
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
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
悋惶
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
Id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 End
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
goto
action
悴惆
SLR
:
14
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
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
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.
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
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
4/6/2022 20
悴惆惆 惘愆惠
-
愆悋 惠惘悋 悛慍悋惆 惆悋愆擯悋 惺 悧惠 惺惷
0. S  S
1. S CC
2. C cC
3. C d
BUTTOM-UP PARSER: CLR
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
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
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
惆惘  惡擯惘惆 惘悋惘
惘惆
$
惡 惺 惡悋愆惆

愀惡 悋 惠悴
悋 惘愕惆
.
擧 拆悋惘愕 惡 惶忰 拆悋惘愕 悴惆 惠愕愀 惘悋 慍惘 惘愆惠
惆
.
LALR PARSER:

惡惘愆 拆悋惘愕 悴惆 惡 惓悋 擯惘悋惘 惡惘悋 悽悋
LALR
擧 惘愕

拆悋惘愕 悴惆 惡悋惆 悋惡惠惆悋
CLR
擧 惘愕 悛惘悋
.

愕拆愕
kernel
惡悋惡 惘悋 愕悋 悋
:
36 47 89

拆悋惘愕 悴惆 悋 惆惘 愕拆愕
CLR
愆惆 惘愕
state
悋
惡悋
kernel
擧 悋惆愃悋  惆惘 惘悋 愕悋
)
惺悋
擧 悋惆愃悋 惡悋悋惠惘 惘惆悋 惆惘 惘悋 拆悋惠惘 惘惆悋

(

惡惘愆 拆悋惘愕 悴惆 忰悋惶 悴惆
LALR
惆悋惘惆 悋
.
4/6/2022 24
悴惆惆 惘愆惠
-
愆悋 惠惘悋 悛慍悋惆 惆悋愆擯悋 惺 悧惠 惺惷

More Related Content

Bottom up parser

  • 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
  • 3. Recursive-descent: 惓悋 : 1. S cAd 2. A aB 3. a 4. B b 4/6/2022 By: Fereshteh Jadidi 3
  • 5. Buttom-up parser 4/6/2022 By: Fereshteh Jadidi 5 SLR LALR CLR Simple Left to right scanning input Right most deviration in revese
  • 6. Buttom_up parser S aABe A Abc b B d By: Fereshteh Jadidi 6 惡悋愆惆 悋惺惆 悋慍 惺悋惆 悋 惘愆惠 慍惘 惡 handle 擯惆 惓悋 : a b b c d e a A b c d e a A d e a A B e S
  • 7. Buttom_up parser: )Shift_reduce Parser( 4/6/2022 By: Fereshteh Jadidi 7 stack Input Buffer 惡惘悋 parser Pars table $ State diagram
  • 8. Reduce : 惺惶惘 惆 悋 惠 stack 惘悋 pop 悛 惆惘 惺惶惘 惘惆 push Shift : push 悋愕惠 惆惘 惘悋 惺惶惘 惘惆 buttom_up 惘悋 shift 擯惆 4/6/2022 By: Fereshteh Jadidi 8 悋 惺 愕惠 悋惡惠惆悋 惆惘 愀 慍 悋愕惠 悽悋惆 慍 惘惆 惆惆 愆悋 惘悋 拆惘惆悋慍愆 愀 惺 惡惘愕惆 悋惠悋 惡 愀 悋擯惘 悋愕惠 愆惆 惠悋 拆惘惆悋慍愆 惡惘惘愕 悋 愀惘 惡 惘惆 悋愕惠 愆惆 . LR(0) item: A xyz A .xyz A x.yz A xy.z A xyz.
  • 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
  • 14. Id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 End 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 goto action 悴惆 SLR : 14
  • 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 惆惘 惡擯惘惆 惘悋惘 惘惆 $ 惡 惺 惡悋愆惆 愀惡 悋 惠悴 悋 惘愕惆 . 擧 拆悋惘愕 惡 惶忰 拆悋惘愕 悴惆 惠愕愀 惘悋 慍惘 惘愆惠 惆 .
  • 24. LALR PARSER: 惡惘愆 拆悋惘愕 悴惆 惡 惓悋 擯惘悋惘 惡惘悋 悽悋 LALR 擧 惘愕 拆悋惘愕 悴惆 惡悋惆 悋惡惠惆悋 CLR 擧 惘愕 悛惘悋 . 愕拆愕 kernel 惡悋惡 惘悋 愕悋 悋 : 36 47 89 拆悋惘愕 悴惆 悋 惆惘 愕拆愕 CLR 愆惆 惘愕 state 悋 惡悋 kernel 擧 悋惆愃悋 惆惘 惘悋 愕悋 ) 惺悋 擧 悋惆愃悋 惡悋悋惠惘 惘惆悋 惆惘 惘悋 拆悋惠惘 惘惆悋 ( 惡惘愆 拆悋惘愕 悴惆 忰悋惶 悴惆 LALR 惆悋惘惆 悋 . 4/6/2022 24 悴惆惆 惘愆惠 - 愆悋 惠惘悋 悛慍悋惆 惆悋愆擯悋 惺 悧惠 惺惷