Download presentation
Presentation is loading. Please wait.
Published byKelley Austin Modified over 9 years ago
1
Bottom-up Parsing
2
Task Parsing (of course); but do it this way: Bottom-up Easy and algorithmic Efficiently –Knowing (input) as little as possible –Marking errors as soon as possible Using a stack explicitly
3
Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num Lex. An. array[numptptnum
4
Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple array[numptptnum Lex. An.
5
Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum array[simple] Lex. An. ofchar Reduction r6 r6
6
Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum simple array[simple]ofchar Lex. An. r6
7
Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple array[simple]of simple Lex. An. Reduction r5 r5 r6
8
Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple array[simple]ofsimple type Lex. An. r5 r6
9
Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple array[simple]of type Lex. An. type Reduction r1 r1 r5 r6
10
Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple array[simple]oftype Lex. An. r1 r5 r6
11
Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple type ]ofarray[ type $ (no more) Lex. An. $ type r1 r5 r6 Reduction r3 r3
12
array [ num ptpt num ] of char $$ $ array $ array [ $ array [ num $ array [ num ptpt $ array [ num ptpt num $ array [ simple $ array [ simple ] $ array [ simple ] of $ array [ simple ] of char $ array [ simple ] of simple $ array [ simple ] of type $ type stack [ num ptpt num ] of char $ num ptpt num ] of char $ ptpt num ] of char $ num ] of char $ ] of char $ of char $ char $ $ $ $ $ input shift reduce 6 shift reduce 5 reduce 1 reduce 3 OK action
13
simple numptptnum array [ num ptpt num ] of char Lex. An. array[numptptnum
14
simple numptptnum char simple array [ num ptpt num ] of char array [ simple ] of char Lex. An. array[simple]ofchar
15
simple numptptnum char simple type array [ num ptpt num ] of char array [ simple ] of char array [ simple ] of simple Lex. An. array[simple of]
16
simple numptptnum char simple type array [ num ptpt num ] of char array [ simple ] of char array [ simple ] of simple Lex. An. array[typesimpleof] array [ simple ] of type ]ofarray[ type
17
simple numptptnum char simple type array [ num ptpt num ] of char array [ simple ] of char array [ simple ] of simple Lex. An. array [ simple ] of type ]ofarray[ type
18
simple numptptnum char simple type array [ num ptpt num ] of char array [ simple ] of char array [ simple ] of simple Lex. An. array [ simple ] of type ]ofarray[ type ═> rm ═> rm ═> rm ═> rm terminals
19
Wrong example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num Lex. An. array[numptptnum
20
Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple array[numptptnum Lex. An.
21
Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum array[simple Lex. An.
22
Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum array[simple Lex. An. type
23
Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum array[ type Lex. An. type ]ofchar
24
Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum simple array[type]ofchar Lex. An. type
25
Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple array[type]of simple Lex. An. type
26
Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple array[type]ofsimple type Lex. An. type
27
Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple array[type]of type Lex. An. type $ ?
28
array [ num ptpt num ] of char array [ simple ] of char array [ simple ] of simple array [ simple ] of type simple numptptnum char simple type ]ofarray[ type ═> 6 ═> 5 ═> 1 ═> 3 handle
29
array [ num ptpt num ] of char $$ $ array $ array [ $ array [ num $ array [ num ptpt $ array [ num ptpt num $ array [ simple $ array [ simple ] $ array [ simple ] of $ array [ simple ] of char $ array [ simple ] of simple $ array [ simple ] of type $ type stack [ num ptpt num ] of char $ num ptpt num ] of char $ ptpt num ] of char $ num ] of char $ ] of char $ of char $ char $ $ $ $ $ input shift reduce 6 shift reduce 5 reduce 1 reduce 3 OK action handle
30
What is the handle? Shift or Reduce? If Reduce, which rule? = Knowing read input (and actions done), and (at most) the next input symbol
31
Some tips type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num reduce type → simple Don’t reduce type → simple after [ When there is simple at the top of the stack : after of when the next input symbol is $ or mark error if the next input symbol is not ] nor $ Marking errors as soon as possible Knowing (input) as little as possible
32
How to decide shift-reduce type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num At the beginning (state 0): –Shift on integer, char, num. (state 1) –Shift on array (state2) –otherwise Mark error State1 (integer, char, num on top): –If num on top, shift on ptpt (state 3), mark error otherwise –Else, reduce by 4 or 5,depending on the symbol at the top State 2 (array on top) : –Shift on [ (state 4) –Mark error otherwise State 3 (num ptpt on top) –Shift on num (state 5) –Mark error otherwise … Let Yacc do it
33
S → c S A d | d A → a B | a B → a | b S SAdc da S SAdc da Adc S B a a c d a d c c d a a d a d c S a d c S A d S S → d A → a S → cSAd
34
S SAdc da c d a d $$ $ c $ c d $ c S $ c S a $ c S A $ c S A d $ S stack d a d $ a d $ d $ $ $ input shift reduce 2: S → d shift reduce 4: A → a shift reduce 1: S → cSAd OK action c d a d c S a d c S A d S S → d A → a S → cSAd dc da S SA ═> rm ═> rm ═> rm
35
c d a d $$ $ c $ c d $ c S $ c S a $ c S A $ c S A d $ S stack d a d $ a d $ d $ $ $ input shift reduce 2: S → d shift reduce 4: A → a shift reduce 1: S → cSAd OK action LR (1)LALR(1) ∩ YACC →
36
S SAdc da c d a d $$ 0 $ 0 c 1 $ 0 c 1 d 2 $ 0 c 1 S 4 $ 0 c 1 S 4 a 5 $ 0 c 1 S 4 A 6 $ 0 c 1 S 5 A 6 d 10 $ 0 S 3 stack d a d $ a d $ d $ $ $ input shift 1 shift 2 reduce 2: S → d; goto [1, S] = 4 shift 5 reduce 4: A → a; goto [4, A] = 6 shift 10 reduce 1: S → cSAd; goto [0, S] = 3 OK action dc da S SA 1 0 2 4 5 610 3 ActionGoTo abcd$SAB 0s1s23 1s1s24 2r2 3OK 4s56 5s7s8r49 6s10 7r5 8r6 9r3 10r1
37
S SAdc da c d a d $$ 0 $ 0 c 1 $ 0 c 1 d 2 $ 0 c 1 S 4 $ 0 c 1 S 4 a 5 $ 0 c 1 S 4 A 6 $ 0 c 1 S 5 A 6 d 10 $ 0 S 3 stack d a d $ a d $ d $ $ $ input shift 1 shift 2 reduce 2: S → d; goto [1, S] = 4 shift 5 reduce 4: A → a; goto [4, A] = 6 shift 10 reduce 1: S → cSAd; goto [0, S] = 3 OK action dc da S SA 1 0 2 4 5 610 3 ActionGoTo abcd$SAB 0s1s23 1s1s24 2r2 3OK 4s56 5s7s8r49 6s10 7r5 8r6 9r3 10r1
38
ActionGoTo abcd$SAB 0s1s23 1s1s24 2r2 3OK 4s56 5s7s8r49 6s10 7r5 8r6 9r3 10r1 0: $accept :. S $end (0) 1: S : c. S A d (1) 2: S : d. (2) 4: S : c S. A d (1) 3: $accept : S. $end (0) 5: A : a. B (3) A : a. (4) 6: S : c S A. d (1) 7: B : a. (5) 8: B : b. (6) 9: A : a B. (3) 10: S : c S A d. (1) (Rule number) State number Meaning
39
S SAdc da c d a d $$ 0 $ 0 c 1 $ 0 c 1 d 2 $ 0 c 1 S 4 $ 0 c 1 S 4 a 5 $ 0 c 1 S 4 A 6 $ 0 c 1 S 5 A 6 d 10 $ 0 S 3 stack d a d $ a d $ d $ $ $ input shift 1 shift 2 reduce 2: S → d; goto [1, S] = 4 shift 5 reduce 4: A → a; goto [4, A] = 6 shift 10 reduce 1: S → cSAd; goto [0, S] = 3 OK action dc da S SA 1 0 2 4 5 610 3 ActionGoTo abcd$SAB 0s1s23 1s1s24 2r2 3OK 4s56 5s7s8r49 6s10 7r5 8r6 9r3 10r1 0: $accept :. S $end (0) 1: S : c. S A d (1) 2: S : d. (2)
40
S SAdc da c d a d $ 0 0 1 0 1 2 0 1 4 0 1 4 5 0 1 4 6 0 1 5 6 10 0 3 stack d a d $ a d $ d $ $ $ input shift 1 shift 2 reduce 2: S → d; goto [1, S] = 4 shift 5 reduce 4: A → a; goto [4, A] = 6 shift 10 reduce 1: S → cSAd; goto [0, S] = 3 OK action dc da S SA 1 0 2 4 5 610 3 ActionGoTo abcd$SAB 0s1s23 1s1s24 2r2 3OK 4s56 5s7s8r49 6s10 7r5 8r6 9r3 10r1
42
S → cAba | cbbb | bA A → b S cbaA b S cbbb S bA b S═>cAbacbba═>S cbbbS═>bAbb═> b$bbb $ Lex. An. Conflicts
43
$ S cbaA b S cbbb S bA b S═>cAbacbba═>S cbbbS═>bAbb═> Lex. An. $b $ A S → cAba | cbbb | bA A → b
44
$ Conflicts S cbaA b S cbbb S bA b S═>cAbacbba═>S cbbbS═>bAbb═> Lex. An. $S $ S → cAba | cbbb | bA A → b
45
Conflicts S cbaA b S cbbb S bA b S═>cAbacbba═>S cbbbS═>bAbb═> Lex. An. c$c b b b shift/reduce conflict S → cAba | cbbb | bA A → b
46
Conflicts S cbaA b S cbbb S═>cAbacbba═>S cbbb Lex. An. $c b b shift/reduce conflict 4: shift/reduce conflict (shift 8, reduce 4) on ‘b’ state 4 S :’c’ ‘b’. ’b’ ‘b’ (2) A : ‘b’. (4) S → cAba | cbbb | bA A → b
47
Conflicts S cbaA b S═>cAbacbba═> S → cAba | cBbb | bB A → b B → ε c Lex. An. $ c b shift/reduce conflict S cbbB ε S═>cBbbc bb═>
48
Conflicts S cbaA b S═>cAbacbba═> S → cAba | cBbb | bB A → b B → ε S cbbB ε S═>cBbbc bb═> c Lex. An. $ c b shift/reduce conflict 1: shift/reduce conflict (shift 4, reduce 5) on ‘b’ state 1 S :’c’. A ’b’ ‘a’ (1) S :’c’. B ’b’ ‘b’ (2) B :. (5)
49
Conflicts S cbaA b S═>cAbacbba═> S cbbB b S cBbbcbbb═> S → cAba | cBbb | bB A → b | a B → b c Lex. An. $ c b b b reduce/reduce conflict
50
Conflicts S cbaA b S═>cAbacbba═> S cbbB b S cBbbcbbb═> S → cAba | cBbb | bB A → b | a B → b Lex. An. $ cb b reduce/reduce conflict 4: reduce/reduce conflict (reduce 4, reduce 6) on ‘b’ state 4 A : ‘b’. (4) B : ‘b’. (6)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.