Presentation is loading. Please wait.

Presentation is loading. Please wait.

Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\}

Similar presentations


Presentation on theme: "Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\}"— Presentation transcript:

1 Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\}

2 One Derivation Tree for ()(())() G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S

3 First Expansion Phase G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS

4 Second Expansion Phase G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS SS()S

5 Third Expansion Phase G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S

6 Fourth Expansion Phase G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S

7 Final Expansion Phase G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S

8 Leftmost/Rightmost Derivations G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost:S ==> SS Leftmost: S ==> SS

9 Leftmost/Rightmost Variables G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS Leftmost: S ==> SS

10 Second Derivation Steps G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS ==> S(S) Leftmost: S ==> SS ==> SSS

11 Leftmost/Rightmost Variables G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS ==> S(S) Leftmost: S ==> SS ==> SSS

12 Third Derivation Steps G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS ==> S(S) ==> S(/\) Leftmost: S ==> SS ==> SSS ==> (S)SS

13 Leftmost/Rightmost Variables G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS ==> S(S) ==> S() Leftmost: S ==> SS ==> SSS ==> (S)SS

14 Fourth Derivation Steps G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS ==> S(S) ==> S() ==> SS() Leftmost: S ==> SS ==> SSS ==> (S)SS ==> (/\)SS

15 Leftmost/Rightmost Variables G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS ==> S(S) ==> S() ==> SS() Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS

16 Fifth Derivation Steps G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS ==> S(S) ==> S() ==> SS() ==> S(S)() Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS ==> ()(S)S

17 Leftmost/Rightmost Variables G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS ==> S(S) ==> S() ==> SS() ==> S(S)() Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS ==> ()(S)S

18 Sixth Derivation Steps G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS ==> S(S) ==> S() ==> SS() ==> S(S)() ==> S((S))() Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS ==> ()(S)S ==> ()((S))S

19 Leftmost/Rightmost Variables G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS ==> S(S) ==> S() ==> SS() ==> S(S)() ==> S((S))() Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS ==> ()(S)S ==> ()((S))S

20 Seventh Derivation Steps G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS ==> S(S) ==> S() ==> SS() ==> S(S)() ==> S((S))() ==> S((/\))() Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS ==> ()(S)S ==> ()((S))S ==> ()((/\))S

21 Leftmost/Rightmost Variables G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS ==> S(S) ==> S() ==> SS() ==> S(S)() ==> S((S))() ==> S(())() Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS ==> ()(S)S ==> ()((S))S ==> ()(())S

22 Eighth Derivation Steps G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS ==> S(S) ==> S() ==> SS() ==> S(S)() ==> S((S))() ==> S(())() ==> (S)(())() Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS ==> ()(S)S ==> ()((S))S ==> ()(())S ==> ()(())(S)

23 Leftmost/Rightmost Variables G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS ==> S(S) ==> S() ==> SS() ==> S(S)() ==> S((S))() ==> S(())() ==> (S)(())() Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS ==> ()(S)S ==> ()((S))S ==> ()(())S ==> ()(())(S)

24 Final Derivation Steps G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S Rightmost: S ==> SS ==> S(S) ==> S() ==> SS() ==> S(S)() ==> S((S))() ==> S(())() ==> (S)(())() ==> (/\)(())() Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS ==> ()(S)S ==> ()((S))S ==> ()(())S ==> ()(())(S) ==> ()(())(/\)

25 Ambiguous Grammar G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S ()S S S ()S S S ()S S ()S ()S We’ll show a second derivation tree for the string ()(())(). This proves the grammar G is ambiguous.

26 Unambiguous Grammar G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\} S’ T () /\() G’ = (V’, , S’, P’) V = {S’,T}  = {(,)} Start variable is S’ P = { S’ --> S’T | /\ T --> (S’)} S’ /\ T ()S’ /\ S’ /\ T ()S’ /\ T


Download ppt "Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\}"

Similar presentations


Ads by Google