CS355 - Theory of Computation Regular Expressions

Download Report

Transcript CS355 - Theory of Computation Regular Expressions

CS355 - Theory of
Computation
Regular Expressions
Regular Expressions
• In maths one can use operations + and × to
build up expressions like: (12 + 5) × 2 (= 34)
• Similarly, can build regular operations to build
expressions describing languages.
• An example is: (0 1)1* = a language!!
• In this case it is the language consisting of all
strings starting with a 0 or 1 followed by zero or
more 1’s (1 *).
Dr. A. Mooney, Dept. of Computer Science, NUI Maynooth
Regular Expressions
• We let R+ represent RR*
– In other words, R+ has all strings that are one
or more concatenation of strings from R i.e.
RR*
• (01+)* = {w | every 0 in w is followed by at
least one 1}
Dr. A. Mooney, Dept. of Computer Science, NUI Maynooth
Regular Expressions
• Which of these are value languages for the regular
expression: (00)*1(0 1)*?
• 0, 1, 010, 0010, 000011, 0011010, 0000101010
•
•
•
•
•
•
•
0
1
010
0010
000011
0011010
0000101010
No - must be at least one 1
Yes
No – must be two 0’s
Yes
Yes
Yes
Yes
Dr. A. Mooney, Dept. of Computer Science, NUI Maynooth
Regular Expressions
• In regular expression shorthand is used and
concatenation symbol ()is not used
– (0 1)0* is shorthand for (0 1) 0*
• The precedence order in regular expressions is:
star operation, concatenation and then union,
unless parentheses are used to change the
order.
Dr. A. Mooney, Dept. of Computer Science, NUI Maynooth
Regular Expressions
• The expression (0 1)* is the language consisting of all
possible strings of 0’s and 1’s.
• If the alphabet Σ = {0,1}, we can write Σ as shorthand for
(0  1) .
• If Σ is any alphabet, the regular expression Σ describes the
language of all strings of length 1.
• Σ* is the language of all strings over the alphabet.
• Σ*0 is the language that contains all strings ending in a 0.
• The language (0Σ*) (Σ*1) consists of all strings that either
start with a 0 or end with a 1.
Dr. A. Mooney, Dept. of Computer Science, NUI Maynooth
Regular Expression
• A language is regular if some regular
expression describes it.
Dr. A. Mooney, Dept. of Computer Science, NUI Maynooth
Nonregular Languages
• Finite automaton are powerful machines
but they do have limitations.
• There are some languages that can not be
recognised by any finite automaton.
• At first glance some languages may seem
regular but are in fact nonregular and vice
versa!!!
Dr. A. Mooney, Dept. of Computer Science, NUI Maynooth
Pumping Lemma
• Pumping Lemma is a technique used to show
that if a language does not have a special
property then it is not regular.
• The property states that all strings in the
language can be “pumped” and still belong to
that class
• A language can be pumped if any sufficiently
long string, having length at least the pumping
length, in the language can be broken into
pieces that can be repeated to produce an even
longer string in the language.
Dr. A. Mooney, Dept. of Computer Science, NUI Maynooth
Pumping Lemma
• Thus, if there is a pumping lemma for a
given language class, any language in the
class will contain an infinite set of finite
strings all produced by a simple rule given
by the lemma.
Dr. A. Mooney, Dept. of Computer Science, NUI Maynooth
Pumping Lemma - Theorem
•
If A is a regular language, then there is a
number p (the pumping length) where, if s is
any string in A of length at least p, then s may
be divided into three pieces, s = xyz, satisfying
the following conditions:
1. For each i ≥ 0, xyiz A,
2. |y| > 0, and
3. |xy| ≤ p.
•
x or z may be ε, but condition 2 does not allow
y to be ε.
Dr. A. Mooney, Dept. of Computer Science, NUI Maynooth
Pumping Lemma to prove a
language is not regular
• Assume language B is regular in order to obtain a
contradiction.
• Use pumping lemma to guarantee existence of a pumping
length p such that all strings of length p or greater in B can
be pumped.
• Find a string in B that has length p or greater but that
cannot be pumped.
• Finally, demonstrate that s cant be pumped by considering
all ways of dividing s into x, y and z and for each such
division, finding a value i where xyiz  B.
• Let us look at examples of this.
Dr. A. Mooney, Dept. of Computer Science, NUI Maynooth
Limitations of the Pumping Lemma
• The Pumping Lemma can be used to
prove that a language is not regular,
however, it can’t be used to prove that a
language is regular.
• Just because you can’t think up the right
string does not mean that someone else
cant.
Dr. A. Mooney, Dept. of Computer Science, NUI Maynooth
Examples
• Examples.
• Think about:
– Is L = {0ix | i ≥ 0, x {0,1}* and |x| ≤ i} regular?
– Is L = {0i | i is prime} regular?
Dr. A. Mooney, Dept. of Computer Science, NUI Maynooth