Module 30 • EQUAL language – Designing a CFG – Thinking recursively

advertisement
Module 30
• EQUAL language
– Designing a CFG
– Thinking recursively
1
EQUAL language
Designing a CFG
2
EQUAL
• EQUAL is the set of strings over {a,b} with an equal
number of a’s and b’s
• Strings in EQUAL include
– aabbab
– bbbaaa
– abba
• Strings in {a,b}* not in EQUAL include
–
–
–
–
aaa
bbb
aab
ababa
3
Designing a CFG for EQUAL
• Think recursively
• Base Case
– What is the shortest possible string in EQUAL?
– Production Rule:
4
Recursive Case
• Recursive Case
– Now consider a longer string x in EQUAL
– Since x has length > 0, x must have a first character
• This must be a or b
– Two possibilities for what x looks like
• x = ay
– What must be true about relative number of a’s and b’s in y?
• x = bz
– What must be true about relative number of a’s and b’s in z?
5
Case 1: x=ay
• x = ay where y has one extra b
– What must y look like?
• Some examples
–
–
–
–
b
babba
aabbbab
aaabbbb
• Is there a general pattern that applies to all of the above
examples?
• More specifically, show how we can decompose all of the
above strings y into 3 pieces, two of which belong to EQUAL.
– Some of these pieces might be the empty string λ
6
Decomposing y
• y has one extra b
– Possible examples
• b, babba, aabbbab, aaabbbb
– Decomposition
• y = ubv where
– u and v both have an equal number of a’s and b’s
• Decompose the 4 strings above into u, b, v
– λbλ, λbabba, aabbbab, aaabbbbλ
7
Implication
• Case 1: x=ay
– y has one extra b
• Case 1 refined: x=aubv
– u, v belong to EQUAL
• Production rule for this case?
8
Case 2: x=bz
• Case 2: x=bz
– z has one extra a
• Case 2 refined: x=buav
– u, v belong to EQUAL
• Production rule for this case?
9
Final Grammar
• EG = (V, S, S, P)
–
–
–
–
V = {S}
Σ = {a,b}
S=S
P:
10
Download