View
213
Download
0
Category
Preview:
Citation preview
LING/C SC/PSYC 438/538
Lecture 1210/4
Sandiway Fong
Administrivia
• Homework 3 out today– Usual rules, due next Monday (11th October)
XKCD
Acknowledgement:Erwin Chan
Let’s write a regular grammar
• Example from Lecture 10
• Regular expression:b+(ab+)* | ε
1 2 3 4>
b
ab b
b
ε
• FSA:
Let’s write a regular grammar
• Test cases (* denotes string not in the language):– *ab *ba [a,b] [b,a]
– bab [b,a,b]
– λ (empty string) []
– bb [b,b]
– *baba [b,a,b,a]
– babab [b,a,b,a,b]
Let’s write a regular grammar
• Regular Grammar in Prolog notation:– s --> []. (s = ”start state”)– s --> [b], b. (b = ”seen a b”)– s --> [b], s.
– b --> [a], c. (c = ”expect a b”)
– c --> [b].– c --> [b], b.– c --> [b], c.
Let’s write a regular grammar• Compare the FSA with our Regular Grammar (RG)
– s --> []. (s = ”start state”)– s --> [b], b. (b = ”seen a b”)– s --> [b], s.
– b --> [a], c. (c = ”expect a b”)
– c --> [b].– c --> [b], b.– c --> [b], c.
1 2 3 4>
b
ab b
b
ε
There is a straightforward correspondence between right recursive RGs and FSA
RG to FSA• Informally, we can convert RG to a FSA
– by treating – non-terminals as states– and introducing (new) states for rules of the form x --> [a].
s>
b
cabb eb
b
b
1. s --> [].2. s --> [b], b.
3. s --> [b], s.
4. b --> [a], c.5. c --> [b].6. c --> [b], b.7. c --> [b], c.
[Powerpoint animation]in order of the RG rules
Let’s write a regular grammar• Test cases • (* denotes string not in the language):
– *ab *ba [a,b][b,a]
– bab [b,a,b]
– λ (empty string) []
– bb[b,b]
– *baba[b,a,b,a]
– babab [b,a,b,a,b]
• Output:Licensed to SP4arizona.edu [g].% compiling /Users/sandiway/Desktop/g.pl...% compiled /Users/sandiway/Desktop/g.pl in
module user, 0 msec -16 bytesyes| ?- s([a,b],[]).no| ?- s([b,a],[]).no| ?- s([b,a,b],[]).yes| ?- s([],[]).yes| ?- s([b,b],[]).yes| ?- s([b,a,b,a],[]).no| ?- s([b,a,b,a,b],[]).yes
Set Enumeration using Prolog
• Regular Grammar1. s --> [].
2. s --> [b], b.
3. s --> [b], s.
4. b --> [a], c.5. c --> [b].6. c --> [b], b.7. c --> [b], c.
• Normally, we ask the set membership question when posing a Prolog query:– e.g.
?- s([a,b],[]).no
• Prolog enumeration:?- s(X,[]).
– X is a Prolog variable– asks the question for what values of
X is s(X,[]) true?
– ; is disjunction (look for alternative answers)
why? Prolog matches rules in the order in which they’re written
Set Enumeration using Prolog
Let’s swap rules 2 and 3• Regular Grammar
1. s --> [].2. s --> [b], s.3. s --> [b], b. 4. b --> [a], c.5. c --> [b].6. c --> [b], b.7. c --> [b], c.
• Prolog enumeration:?- s(X,[]).
Set Enumeration using Prolog• Similarly, if we swap rules 6 and 7• Regular Grammar
– s --> [].– s --> [b], b.– s --> [b], s. – b --> [a], c.– c --> [b].– c --> [b], c.– c --> [b], b.
• Prolog enumeration:?- s(X,[]).
Set Enumeration using Prolog• Regular Grammar
1. s --> [].2. s --> [b], b. 3. s --> [b], s.4. b --> [a], c.5. c --> [b].6. c --> [b], b.7. c --> [b], c.
• Unfortunately, the default Prolog grammar rule matching convention does not permit us to properly enumerate the set (language).
• Enumeration (in order of size):– Length: 0– []– Length: 1– [b]– Length: 2– [b,b]– Length: 3– [b,b,b] [b,a,b]– Length: 4– [b,b,b,b] [b,b,a,b]– [b,a,b,b]– Length: 5– [b,b,b,b,b] [b,b,b,a,b] – [b,b,a,b,b] [b,a,b,b,b] – [b,a,b,a,b]– etc.
Converting FSA to REs
• Example:– Give a RE for the FSA:
• State by-pass method:– Delete one state at a
time– Calculate the possible
paths passing through the deleted state
– E.g. • eliminate state 3• then 2…
12
3
>
1
0
1
1
1
1
0
Converting FSA to REs• eliminate state 3 • eliminate state 2
1>2
1
01
1>
1
1+01+1
0(1+0|1)*1+1
1>0(1+0|1)*1+1 | 1
[Powerpoint animation]
(0(1+0|1)*1+1 | 1)*
Homework 3
• Consider the language L given by– L = a*b* ∪ (ab)*
We can express this as:– L = L1 ∪ L2
– L1 = a*b*
– L2 = (ab)*
• Question 1 (438/538) 15 points• Part 1
– Give a regular grammar for L1 in Prolog
• Part 2– Give a regular grammar for L2 in
Prolog
• Part 3– Give a regular grammar for L (=L1
∪ L2) in Prolog
• Instruction:– In each case show your program
and sample output
Homework 3
• Consider the language L given by– L = a*b* ∪ (ab)*
We can express this as:– L = L1 ∪ L2
– L1 = a*b*
– L2 = (ab)*
• Question 2 (438/538) 15 points
• Give a deterministic FSA for L– Draw a diagram– Implement it in Perl– show your program and
sample output
Homework 3
• Question 3 (538 obligatory, 438 extra credit) 15 points– Give a FSA that accepts strings of a’s and b’s (in any
order) such that the total number of a’s in the string must be even and the total number of b’s in string must be odd
e.g.– aab baababa– *b *ab *aabbbb *aa
(* indicates string not in the language)
Recommended