Ungraded Homework Exercise

LING/C SC/PSYC 438/538
Lecture 13
Sandiway Fong
Administrivia
• Homework 6 not yet graded…
• but we'll go over it anyway
• More on FSA and Regex...
Ungraded Homework Exercise Review
• Converting a NDFSA into a DFSA
>
a
1
b
2
3
Note: this machine with an ε-transition
is non-deterministic
ε
Note: this machine is
deterministic
>
{1,3}
a
{2}
b
{3}
[Powerpoint animation]
Ungraded Homework Exercise Review
• Converting a NDFSA into a DFSA
>
a
1
b
2
3
Note: this machine with an ε-transition
is non-deterministic
ε
Note: this machine is
deterministic
>
{1,2}
a
{2}
b
{3}
b
[Powerpoint animation]
Homework 6 Review
• Question 1:
– Assume Σ={a,b}. Draw a non-deterministic FSA that
accepts all strings that contain the substring abba.
– Examples:
• *abbba, babba, *abb, abba, *aaba, aabbbabba
b
b
>
0
a
a
a
b
ab
b
abb
a
abba
a
[Powerpoint animation]
Homework 6 Review
• Question 2: convert the machine into a deterministic FSA.
b
b
b
>
0
a
a
ab
b
abb
b
{0,
abba
}
abba
a
b
a
a
{0,ab
b,abb
a}
a
a
b
b
>
{0}
a
{0,a}
a
{0,ab
}
b
a
b
{0,abb}
a
{0,a,
abba
}
b
{0,ab,
abba}
a
b
a
[Powerpoint animation]
Homework 6 Review
• Not guaranteed to be minimized
b
b
>
{0}
{0,a}
a
{0,ab
}
b
a
b
{0,abb}
a
{0,
abba
}
{0,a,
abba
}
a|b
a
b
a
{0,ab
b,abb
a}
b
a
b
b
>
{0}
a
{0,a}
a
{0,ab
}
b
a
b
{0,abb}
a
{0,a,
abba
}
b
{0,ab,
abba}
a
b
a
Homework 6 Review
• Question 3: Implement your machine in Perl
Converting FSA to REs
• Example:
• State by-pass method:
– Give a RE for the FSA:
1
> 1
1
0
2
1
1
3
1
0
1. Delete one state at a
time
2. Calculate the possible
paths passing through
the deleted state
3. Add the regex
calculated at each
stage as an arc
– e.g.
• eliminate state 3
• then 2…
Converting FSA to REs
1
• eliminate state 3
> 1
0
1
0
1
1
2
3
1+1
• eliminate state 2
2
1
> 1
1
1+0
1
> 1
0
0(1+0|1)*1+1
1
> 1
0(1+0|1)*1+1 | 1
Answer: (0(1+0|1)*1+1 | 1)*
[Powerpoint animation]
Homework 6 Revisited
b
• Recall:
0
1
a
>
2
b
a
3
b
4
a
a|b
a
b
• Let's eliminate state 1:
• 0 - aa*b -> 2
• 2 – aa*b -> 2
b
>
0
a
1
a
2
b
a
b
3
a
4
a|b
b
[Powerpoint animation]
Homework 6 Revisited
b
0
>
2
aa*b
3
b
4
a
aa*b
a|b
b
• Let's eliminate state 2:
• 0 – aa*b (aa*b)*b -> 3
b
>
0
2
aa*b
b
3
aa*b
a
4
a|b
b
[Powerpoint animation]
Homework 6 Revisited
b
0
>
3
aa*b(aa*b)*b
• Let's eliminate state 3:
• 0 – aa*b(aa*b)*ba -> 4
• 0 - aa*b(aa*b)*bb -> 0
4
a
a|b
b
b
>
0
3
aa*b(aa*b)*b
b
a
4
a|b
[Powerpoint animation]
Homework 6 Revisited
b
aa*b(aa*b)*ba
0
>
aa*b(aa*b)*bb
4
a|b
• Let's merge the state 0 loops:
b|aa*b(aa*b)*bb
>
0
aa*b(aa*b)*ba
4
a|b
[Powerpoint animation]
Homework 6 Revisited
b|aa*b(aa*b)*bb
>
0
aa*b(aa*b)*ba
4
a|b
• Make the whole machine vanish:
– (b|aa*b(aa*b)*bb)*aa*b(aa*b)*ba(a|b)*
Homework 6 Revisited
• Implement it:
• ^(b|aa*b(aa*b)*bb)*aa*b(aa*b)*ba(a|b)*$
Homework 6 Revisited
• FSA code:
• Regex code:
• Which one would you
prefer to write and
why?