Strings and Languages Operations Strings and Language

advertisement
Strings and Language Operations
• Concatenation
• Exponentiation
• Kleene star
Strings and Languages Operations
• Pages 28-32 of the recommended text
• Regular expressions
Concatenation
Exponentiation
Kleene Star
Regular Expressions
• Pages 85-90 of the recommended text
1
String Concatenation
2
Properties of String Concatenation
• If x and y are strings over alphabet Σ, the
concatenation of x and y is the string xy
formed by writing the symbols of x and the
symbols of y consecutively.
• Suppose x = abb and y = ba
• Suppose x, y, and z are strings.
• Concatenation is not commutative.
• xy is not guaranteed to be equal to yx
• Concatenation is associative
• (xy)z = x(yz) = xyz
• xy = abbba
• yx = baabb
• The empty string is the identity for
concatenation
• x/\ = /\x = x
3
4
Language Concatenation is not
commutative
Language Concatenation
• Suppose L1 and L2 are languages (sets of strings).
• The concatenation of L1 and L2, denoted L1L2,is
defined as
• Let L1 = { aa, bb, ba } and L2 = { /\, aba }
• Let x1= aa, x2= bb, x3=ba, y1= /\, y2= aba
• L1L2 = { x1y1, x1y2, x2y1, x2y2, x3y1, x3y2 }
= { aa, aaaba, bb, bbaba, ba, baaba }
• L2L1 = { y1x1, y1x2, y1x3, y2x1, y2x2, y2x3 }
= { aa, bb, ba, abaaa, ababb, ababa }
• L2L2 = { y1y1, y1y2, y2y1, y2y2 }
= { /\, aba, aba, abaaba }
= { /\, aba, abaaba } (dropped extra aba)
• L1L2 = { xy | x ∈ L1 and y ∈ L2 }
• Example,
• Let L1 = { ab, bba } and L2 = { aa, b, ba }
• What is L1L2?
• Solution
• Let x1= ab, x2= bba, y1= aa, y2= b, y3= ba
• L1L2 = { x1y1, x1y2, x1y3, x2y1, x2y2, x2y3 }
= { abaa, abb, abba, bbaaa, bbab, bbaba}
5
6
1
Associativity of Language
Concatenation
Special Cases
• What language is the identity for language
concatenation?
• (L1L2)L3 = L1(L2L3) = L1L2L3
• Example
• The set containing only the empty string /\: {/\}
• Example
• Let L1={a,b}, L2={c,d}, and L3={e,f}
• L1L2L3=({a,b}{c,d}){e,f}
={ac, ad, bc, bd}{e,f}
={ ace,acf,ade,aef,bce,bcf,bde,bdf }
• L1L2L3={a,b}({c,d}{e,f})
={a,b}{ce, df, ce, df}
={ ace,acf,ade,aef,bce,bcf,bde,bdf }
• {aab,ba,abc}{/\} = {/\}{aab,ba,abc} = {aab,ba,abc}
• What about {}?
• For any language L, L {} = {} L = {}
• Thus {} for concatenation is like 0 for multiplication
• Example
• {aab,ba,abc}{} = {}{aab,ba,abc} = {}
• The intuitive reason is that we must choose a string from both
sets that are being concatenated, but there is nothing to choose
from {}.
7
8
Exponentiation
Examples of Exponentiation
• We use exponentiation to indicate the number of
items being concatenated
•
•
•
•
•
•
•
•
Let x=abb, Σ={a,b}, L={ab,b}
a4 = aaaa
x3 = (abb)(abb)(abb) = abbabbabb
Σ3 = ΣΣΣ = {a,b}{a,b}{a,b}
={aaa,aab,aba,abb,baa,bab,bba,bbb}
• L3 = LLL = {ab,b}{ab,b}{ab,b}
= {ababab,ababb,abbab,abbb,
babab,babb,bbab,bbb}
•
•
•
•
Symbols
Strings
Set of symbols (Σ for example)
Set of strings (languages)
a3 = aaa
x3 = xxx
Σ3 = ΣΣΣ = { x ∈ Σ* | |x|=3 }
L3 = LLL
9
10
Results of Exponentiation
Special Cases of Exponentiation
• Exponentiation of a symbol or a string
results in a string.
• Exponentiation of a set of symbols or a set
of strings results in a set of strings
•
•
•
•
•
•
•
•
a symbol a string
a string a string
a set of symbols a set of strings
a set of strings a set of strings
a0 = /\
x0 = /\
Σ0 = { /\ }
L0 = { /\ } for any language L
•
•
•
•
11
{aa,bb}0 = { /\ }
{ a, aa, aaa, aaaa, …}0 = { /\ }
{ /\ }0 = { /\ }
φ0 = { }0 = { /\ }
12
2
Kleene Star
Example of Kleene Star
• Kleene * is a unary operation on languages.
• Kleene * is not an operation on strings
•
•
•
•
•
•
•
•
• However, see the pages on regular expressions.
• L* represents any finite number of concatenations
of L.
L* = Uk>0 Lk = L0 U L1 U L2 U …
• For any L, /\ is always an element of L*
• because L0 = { /\ }
• Thus, for any L, L* != φ
Let L={aa}
L0={ /\ }
L1=L={aa }
L2={ aaaa }
L3= …
L* = L0 ∪ L1 ∪ L2 ∪ L3 …
= { /\, aa, aaaa, aaaaaa, … }
= set of all strings that can be obtained by
concatenating 0 or more copies of aa
13
•
•
•
•
•
•
•
14
Example of Kleene Star
Regular Languages
Let L={aa, b}
L0={ /\ }
L1=L={aa,b}
L2= LL={ aaaa, aab, baa, bb}
L3= …
L* = L0 ∪ L1 ∪ L2 ∪ L3 …
= set of all strings that can be obtained by
concatenating 0 or more copies of aa and b
• Regular languages are languages that can be
obtained from the very simple languages
over Σ, using only
• Union
• Concatenation
• Kleene Star
15
Examples of Regular Languages
16
Regular Expressions
• {aab} (i.e. {a}{a}{b} )
• {aa,b} (i.e. {a}{a} ∪ {b} )
• {a,b}*
language of strings that can be obtained by
concatenating any number of a’s and b’s
• {bb}{a,b}* language of strings that begin with bb
(followed by any number of a’s and b’s)
• {a}*{bb,/\} language of strings that begin with any
number of a’s and end with an optional bb.
• {a}*∪{b}* language of strings that consist of only a’s or
only b’s and /\.
• We can simplify the formula for regular
languages slightly by
• leaving out the set brackets { } and
• replacing ∪ with +
• The results are called regular expressions.
17
18
3
Examples of Regular Expressions
Set notation
String or Language?
Regular
Expressions
aab
{aab}
{aa,b}
= {aa}∪{b}
aa+b
{a,b}*
= ({a}∪{b})*
(a+b)*
{bb}{a,b}* = {bb}({a}∪{b})*
bb(a+b)*
{a}*{bb,/\} = {a}*({bb}∪{/\})
a*(bb+/\)
{a}*∪{b}*
a*+b*
• Consider the regular expression a*(bb+/\)
• a*(bb+/\) is a string over alphabet {a, b, *, +, /\, (, ), φ }
• a*(bb+/\) represents a language over alphabet {a, b}
• It represents the language of strings over {a,b} that begin with any
number of a’s and end with an optional bb.
• Some regular expressions look just like strings over
alphabet {a,b}
• Regular expression aaba represents the language {aaba}
• Regular expression /\ represents the language {/\}
• It should be clear from the context whether a sequence of
symbols is a regular expression or just a string.
19
20
4
Download