balanced parentheses g = (v, , s, p) v = {s} = {(,)} start variable is s p = { s --> (s) | ss |...
Post on 21-Dec-2015
226 views
TRANSCRIPT
Balanced ParenthesesG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
One Derivation Tree for ()(())()G = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
First Expansion PhaseG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
Second Expansion PhaseG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
S S ( )S
Third Expansion PhaseG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )S
/\
S
( )S
S
( )S
Fourth Expansion PhaseG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )S
/\
S
( )S
S
( )S
( )S/\
Final Expansion PhaseG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )S
/\
S
( )S
S
( )S
( )S/\
/\
Leftmost/Rightmost DerivationsG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )S
/\
S
( )S
S
( )S
( )S/\
/\
Rightmost:S ==> SSLeftmost: S ==> SS
Leftmost/Rightmost VariablesG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )S
/\
S
( )S
S
( )S
( )S/\
/\
Rightmost: S ==> SSLeftmost: S ==> SS
Second Derivation StepsG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )S
/\
S
( )S
S
( )S
( )S/\
/\
Rightmost: S ==> SS ==> S(S)
Leftmost: S ==> SS ==> SSS
Leftmost/Rightmost VariablesG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )S
/\
S
( )S
S
( )S
( )S/\
/\
Rightmost: S ==> SS ==> S(S)
Leftmost: S ==> SS ==> SSS
Third Derivation StepsG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )S
/\
S
( )S
S
( )S
( )S/\
/\
Rightmost: S ==> SS ==> S(S) ==> S(/\)
Leftmost: S ==> SS ==> SSS ==> (S)SS
Leftmost/Rightmost VariablesG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )S
/\
S
( )S
S
( )S
( )S/\
/\
Rightmost: S ==> SS ==> S(S) ==> S()
Leftmost: S ==> SS ==> SSS ==> (S)SS
Fourth Derivation StepsG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )S
/\
S
( )S
S
( )S
( )S/\
/\
Rightmost: S ==> SS ==> S(S) ==> S() ==> SS()
Leftmost: S ==> SS ==> SSS ==> (S)SS ==> (/\)SS
Leftmost/Rightmost VariablesG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )S
/\
S
( )S
S
( )S
( )S/\
/\
Rightmost: S ==> SS ==> S(S) ==> S() ==> SS()
Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS
Fifth Derivation StepsG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )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 VariablesG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )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 StepsG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )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 VariablesG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )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 StepsG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )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 VariablesG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )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 StepsG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )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 VariablesG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )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 StepsG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )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 GrammarG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S
S S
( )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 GrammarG = (V, , S, P)
V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}
S’
S’ T
( )S’
/\( )
G’ = (V’, , S’, P’)
V = {S’,T} = {(,)}Start variable is S’P = { S’ --> S’T | /\ T --> (S’)}
S’
S’
/\
T
( )S’
/\
S’
S’
/\
T
( )S’
/\
T