balanced parentheses g = (v, , s, p) v = {s} = {(,)} start variable is s p = { s --> (s) | ss |...

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

Post on 21-Dec-2015

226 views

Category:

Documents


3 download

TRANSCRIPT

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

Balanced ParenthesesG = (V, , S, P)

V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}

Page 2: 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 SP = { S --> (S) | SS | /\}

S

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

First Expansion PhaseG = (V, , S, P)

V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}

S

S S

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

Second Expansion PhaseG = (V, , S, P)

V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}

S

S S

S S ( )S

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

Third Expansion PhaseG = (V, , S, P)

V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}

S

S S

( )S

/\

S

( )S

S

( )S

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

Fourth Expansion PhaseG = (V, , S, P)

V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}

S

S S

( )S

/\

S

( )S

S

( )S

( )S/\

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

Final Expansion PhaseG = (V, , S, P)

V = {S} = {(,)}Start variable is SP = { S --> (S) | SS | /\}

S

S S

( )S

/\

S

( )S

S

( )S

( )S/\

/\

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

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

Page 9: Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (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

Page 10: Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (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

Page 11: Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (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)

Leftmost: S ==> SS ==> SSS

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

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

Page 13: Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (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

Page 14: Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (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

Page 15: Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (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() ==> SS()

Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS

Page 16: Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | 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

Page 17: Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (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() ==> SS() ==> S(S)()

Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS ==> ()(S)S

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

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

Page 19: Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (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() ==> SS() ==> S(S)() ==> S((S))()

Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS ==> ()(S)S ==> ()((S))S

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

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

Page 21: Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (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() ==> SS() ==> S(S)() ==> S((S))() ==> S(())()

Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS ==> ()(S)S ==> ()((S))S ==> ()(())S

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

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)

Page 23: Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (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() ==> SS() ==> S(S)() ==> S((S))() ==> S(())() ==> (S)(())()

Leftmost: S ==> SS ==> SSS ==> (S)SS ==> ()SS ==> ()(S)S ==> ()((S))S ==> ()(())S ==> ()(())(S)

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

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) ==> ()(())(/\)

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

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.

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

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