Balanced Parentheses G = (V, , S, P) V = {S} = {(,)} Start variable is S P = { S --> (S) | SS | /\}
One Derivation Tree for ()(())() G = (V, , S, P) V = {S} = {(,)} Start variable is S P = { S --> (S) | SS | /\} S
First Expansion Phase G = (V, , S, P) V = {S} = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS
Second Expansion Phase G = (V, , S, P) V = {S} = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS SS()S
Third Expansion Phase G = (V, , S, P) V = {S} = {(,)} Start variable is S P = { S --> (S) | SS | /\} S SS ()S /\ S ()S S ()S
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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)
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)
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) ==> ()(())(/\)
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.
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