Balanced Parentheses - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Balanced Parentheses

Description:

Balanced Parentheses G = (V, S, S, P) V = {S} S = {(,)} Start variable is S P = { S -- (S) | SS | /\} – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 27
Provided by: facul329
Learn more at: http://www.cse.msu.edu
Category:

less

Transcript and Presenter's Notes

Title: Balanced Parentheses


1
Balanced Parentheses
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
2
One Derivation Tree for ()(())()
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
3
First Expansion Phase
G (V, S, S, P) V S S (,) Start
variable is S P S --gt (S) SS /\
S
S
S
4
Second 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
5
Third 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
6
Fourth 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
/\
7
Final 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
/\
/\
8
Leftmost/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
9
Leftmost/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
10
Second 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)
11
Leftmost/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)
12
Third 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(/\)
13
Leftmost/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()
14
Fourth 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()
15
Leftmost/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()
16
Fifth 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)()
17
Leftmost/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)()
18
Sixth 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))()
19
Leftmost/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))()
20
Seventh 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((/\))()
21
Leftmost/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(())()
22
Eighth 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)(())()
23
Leftmost/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)(())()
24
Final 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
(/\)(())()
25
Ambiguous 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
26
Unambiguous 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
/\
/\
Write a Comment
User Comments (0)
About PowerShow.com