BottomUp Parsing Exersices Exercise 4 5 1 For
Bottom-Up Parsing Exersices
Exercise 4. 5. 1 • For the grammar • S --> 0 S 1 | 0 1 • indicate the handle in each of the following rightsentential forms: • a) 000111 • b) 00 S 11 • Answer: • a) 01 • b) 0 S 1
Exercise 4. 5. 2 • For the grammar • S --> SS+ | SS* | a • indicate the handle in each of the following right-sentential forms: • a) SSS+a*+ • b) SS+a*a+ • c) aaa*a++ • • Answer: a) SS+ b) SS+ c) a
Exercise 4. 5. 3 • Give bottom-up parses for the following input strings and grammars: • The input 000111 according to the grammar of Exercise 4. 5. 1. • Answer
Exercise 4. 5. 3. b • Give bottom-up parses for the following input strings and grammars: • The input aaa*a++ according to the grammar of Exercise 4. 5. 2. • Answer
Example • Give bottom-up parses for the following input strings and grammars: • The input id+id*id according to the following grammar: E E+T | T T T*F | F F (E) | id • Answer next slide
Example: (Answer)
Exercise 4. 6. 1 • Construct the SLR sets of items for the following grammar. Compute the GOTO function for these sets of items. Show the parsing table for this grammar. a) S --> 0 S 1 | 0 1 b) S --> SS+ | SS* | a c) S -> S (S) S | ε • Answer
Exercise 4. 6. 1. a Answer 0) S' -> S 1) S -> 0 A 2) A -> 0 A 1 3) A -> 1 I 0 = {S' -> S , S -> 0 A} GOTO(I 0, S) = {S' -> S } = I 1 . . . GOTO(I , 0) = {S -> 0. A, A ->. 0 A 1 , A ->. 1 } = I GOTO(I , A) = {S -> 0 A. } = I 0 2 2 3 . . 0 A 1 , A ->. 1 } = I GOTO(I , 1) = {A -> 1. } = I GOTO(I , A) = {A -> 0 A. 1} = I GOTO(I , 0) = {A -> 0. A 1 , A ->. 0 A 1 , A ->. 1 } = I GOTO(I , 1) = {A -> 1. } = I GOTO(I , 1) = {A -> 0 A 1. } = I GOTO(I 2, 0) = {A -> 0 A 1, A -> 2 5 4 6 4 5 7 4
Exercise 4. 6. 1. a Answer(cont. ) Follow(S) = { $ } Follow(A) = {1, $ } I 0 1 2 3 4 5 6 7 0 S 2 ACTION 1 $ GOTO S A 1 accept S 4 S 5 3 R {S -> 0 A } S 4 S 5 R {A -> 1} S 7 R {A -> 0 A 1} 6 R {A -> 1} R {A -> 0 A 1}
Exercise 4. 6. 1. b Answer • • S - > SSA | a A -> + | * 0) 1) 2, 3) 4, 5) S’-> S S -> a. B B-> SAB| ε //B->a. BAB A -> + | * I 0 = { S’ ->. S, S ->. a. B} GOTO(I 0, S) = {S’ -> S. } =I 1 GOTO(I 0, a) = {S -> a. B, B->. a. BAB , B->. } =I 2 GOTO(I 2, B) = {S -> a. B. } =I 3 GOTO(I 2, a) = {B-> a. BAB , B->. } =I 4 GOTO(I 4, B) = {B-> a. B. AB , A ->. + |. *} =I 5 GOTO(I 4, a) = I 4 GOTO(I 5, A) = {B-> a. BA. B, B->. a. BAB , B->. } =I 6 GOTO(I 5, +) = {A -> +. } =I 7 GOTO(I 5, *) = {A -> *. } =I 8 GOTO(I 6, B) = {B-> a. BAB. } =I 9 GOTO(I 6, a) = I 4
Exercise 4. 6. 1. b Answer (cont. ) Follow (S) = {$} Follow (A) = {$, a} Follow(B) = {$, + , *} I 0 1 2 3 4 5 6 7 8 9 a S 2 + ACTION * S 4 R{B-> ε} accept R{B-> ε} S 4 R{B-> ε} S 7 R{B-> ε} S 8 R{B-> ε} R{B->a. BAB} S 4 R{A -> +} R{A -> *} $ GOTO S A 1 R{B-> ε} R{S -> a. B} R{B-> ε} B 3 5 6 R{B-> ε} R{A -> +} R{A -> *} R{B->a. BAB} 9
Exercise 4. 6. 1. c Answer 1) 2) 3, 4) S’ --> S S --> A A --> (S) S A | ε I 0 = { S’ ->. S, S ->. A , A ->. (S) S A , A - >. } GOTO(I 0 , S ) = {S’ --> S. } = I 1 GOTO(I 0, A ) = {S -> A. } = I 2 GOTO(I 0, ( ) = {A -> (. S) S A , S ->. A , A ->. (S) S A , A - >. } = I 3 GOTO(I 3, S ) = {A -> (S. ) S A } = I 4 GOTO(I 3, A ) = I 2 GOTO(I 3, ( ) = I 3 GOTO(I 4, ) ) = {A -> (S). S A, S ->. A , A ->. (S) S A , A - >. } = I 5 GOTO(I 5, S ) = {A -> (S) S. A, A -> (. S) S A , S ->. A , A ->. (S) S A , A - >. } = I 6 GOTO(I 5, A ) = I 2 GOTO(I 5, ( ) = I 3 GOTO(I 6, A ) = {A -> (S) S A. , S -> A. } = I 7 GOTO(I 6, S ) = I 4 GOTO(I 6, ( ) = I 3
Exercise 4. 6. 1. c Answer (cont. ) Follow (A) = Follow (S) = { $, ) } ACTION I 0 1 2 3 4 5 6 7 ( S 3 S 3 ) R {S --> A} R {A --> ε} S 5 R {A --> ε} R {A --> (S) S A} $ accept R {S --> A} R {A --> ε} R {A --> (S) S A} GOTO S A 1 2 4 2 6 2 7
Exercise 4. 6. 3 • Show the actions of your parsing table from Exercise 4. 6. 1. b on the input aa * a+. • Answer
Example 1 • Construct the SLR sets of items for the following grammar. Compute the GOTO function for these sets of items. Show the parsing table for this grammar. • 1) E E+T • 2) E T • 3) T T*F • 4) T F • 5) F (E) • 6) F id • Answer
Example 1 Answer . . . • 0) E’ --> E I 0 ={ E’ E, E E+T, E T, T T*F, T F, F (E), F id } goto(I 0, E) = { E’ E , E E +T } = I 1 goto(I 0, T) = { E T , T T *F } = I 2 goto(I 0, F) = {T F } = I 3 goto(I 0, () = { F ( E), E E+T, E T, T T*F, T F, F (E), F id } = I 4 goto(I 0, id) = { F id } = I 5 goto(I 1, +) = {E E+ T , T T*F, T F, F (E), F id } = I 6 goto(I 2, *) = {T T*. F , F . (E), F . id } = I 7 goto(I 4, E) = {F (E ), E E +T } = I 8 goto(I 4, T) = I 2 goto(I 4, F) = I 3 goto(I 4, id) = I 5 goto(I 6, T) = {E E+ T , T T *F } = I 9 goto(I 6, F) = I 3 goto(I 6, ( ) = I 4 goto(I 6, id) = I 5 goto(I 7, F) = {T T*F } = I 10 goto(I 7, () = I 4 goto(I 7, id) = I 5 goto(I 8, )) = {F (E) } = I 11 goto(I 8, +) = I 6 goto(I 9, +) = I 7 . . .
Example 1 Answer (cont. )
Example 1 Answer (cont. )
Example 2 • Show the actions of your parsing table from Example 1 on the input id * id + id. • Answer
Example 2 Answer
- Slides: 22