Title: Balanced Parentheses
1Balanced Parentheses
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
2One Derivation Tree for ()(())()
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
3First Expansion Phase
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
4Second Expansion Phase
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
S
S
(
)
S
5Third Expansion Phase
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
6Fourth Expansion Phase
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
7Final Expansion Phase
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
8Leftmost/Rightmost Derivations
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS
RightmostS gt SS
9Leftmost/Rightmost Variables
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS
Rightmost S gt SS
10Second Derivation Steps
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS gt SSS
Rightmost S gt SS gt S(S)
11Leftmost/Rightmost Variables
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS gt SSS
Rightmost S gt SS gt S(S)
12Third Derivation Steps
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS gt SSS gt (S)SS
Rightmost S gt SS gt S(S) gt S(/\)
13Leftmost/Rightmost Variables
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS gt SSS gt (S)SS
Rightmost S gt SS gt S(S) gt S()
14Fourth Derivation Steps
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS gt SSS gt (S)SS gt (/\)SS
Rightmost S gt SS gt S(S) gt S() gt SS()
15Leftmost/Rightmost Variables
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS gt SSS gt (S)SS gt ()SS
Rightmost S gt SS gt S(S) gt S() gt SS()
16Fifth Derivation Steps
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS gt SSS gt (S)SS gt ()SS
gt ()(S)S
Rightmost S gt SS gt S(S) gt S() gt SS() gt
S(S)()
17Leftmost/Rightmost Variables
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS gt SSS gt (S)SS gt ()SS
gt ()(S)S
Rightmost S gt SS gt S(S) gt S() gt SS() gt
S(S)()
18Sixth Derivation Steps
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS gt SSS gt (S)SS gt ()SS
gt ()(S)S gt ()((S))S
Rightmost S gt SS gt S(S) gt S() gt SS() gt
S(S)() gt S((S))()
19Leftmost/Rightmost Variables
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS gt SSS gt (S)SS gt ()SS
gt ()(S)S gt ()((S))S
Rightmost S gt SS gt S(S) gt S() gt SS() gt
S(S)() gt S((S))()
20Seventh Derivation Steps
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS gt SSS gt (S)SS gt ()SS
gt ()(S)S gt ()((S))S gt ()((/\))S
Rightmost S gt SS gt S(S) gt S() gt SS() gt
S(S)() gt S((S))() gt S((/\))()
21Leftmost/Rightmost Variables
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS gt SSS gt (S)SS gt ()SS
gt ()(S)S gt ()((S))S gt ()(())S
Rightmost S gt SS gt S(S) gt S() gt SS() gt
S(S)() gt S((S))() gt S(())()
22Eighth Derivation Steps
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS gt SSS gt (S)SS gt ()SS
gt ()(S)S gt ()((S))S gt ()(())S gt ()(())(S)
Rightmost S gt SS gt S(S) gt S() gt SS() gt
S(S)() gt S((S))() gt S(())() gt (S)(())()
23Leftmost/Rightmost Variables
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS gt SSS gt (S)SS gt ()SS
gt ()(S)S gt ()((S))S gt ()(())S gt ()(())(S)
Rightmost S gt SS gt S(S) gt S() gt SS() gt
S(S)() gt S((S))() gt S(())() gt (S)(())()
24Final Derivation Steps
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
(
)
S
S
/\
(
)
S
/\
/\
Leftmost S gt SS gt SSS gt (S)SS gt ()SS
gt ()(S)S gt ()((S))S gt ()(())S gt ()(())(S)
gt ()(())(/\)
Rightmost S gt SS gt S(S) gt S() gt SS() gt
S(S)() gt S((S))() gt S(())() gt (S)(())() gt
(/\)(())()
25Ambiguous Grammar
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
Well show a second derivation tree for the
string ()(())(). This proves the grammar G is
ambiguous.
S
S
26Unambiguous Grammar
G (V, S, S, P) V S,T S (,) Start
variable is S P S --gt ST /\ T
--gt (S)
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
T
(
)
S
S
T
/\
(
)
S
S
S
/\
/\