Professional Documents
Culture Documents
Ch3 - Regular Expression: Subhash Sagar Email
Ch3 - Regular Expression: Subhash Sagar Email
Ch3 - Regular Expression: Subhash Sagar Email
Subhash Sagar
Email: subhash.sagar@nu.edu.pk
1
Defining Languages using Regular Expressions
2
Regular Expression
A compact, easy-to-read language description.
4
Regular Expression
Definition: A regular expression over an alphabet Σ is recursively
defined as follows:
1. ø denotes language ø
2. ε denotes language {ε}
3. a denotes language {a}, for all a Σ.
4. (P + Q) denotes L(P) U L(Q), where P, Q are r.e.’s.
5. (PQ) denotes L(P)·L(Q), where P, Q are r.e.’s.
6. P* denotes L(P)*, where P is r.e.
Recursive definition of Regular Expression (RE)
Step 1: Every letter of Σ including Λ is a regular expression.
Step 2: If r1 and r2 are regular expressions then
1.(r1)
2.r1 r2
3.r1 + r2 and
4. r1*
are also regular expressions.
Step 3: Nothing else is a regular expression.
6
Precedence of Regular-Expression Operators
To prevent excessive parentheses, we assume left associativity, with the following
operator precedence hierarchy, from most to least binding: *, ·, +
The star (*) operator is of highest precedence. That is, it applies only to the smallest
sequence of symbols to its left that is a well-formed regular expressions.
After grouping all stars to their operands, we group concatenation (.) operators to
their operands.
Finally, all unions (+) are grouped with their operands.
Example:
– 01* + 1 = ( 0 . ((1)*) ) + 1
7
Algebraic Laws of Regular Expressions
Commutative:
– E+F = F+E
Associative:
– (E+F)+G = E+(F+G)
– (EF)G = E(FG)
Identity:
– E+Φ = E
– E=E=E
Annihilator:
– ΦE = EΦ = Φ
Algebraic Laws…
Distributive:
– E(F+G) = EF + EG
– (F+G)E = FE+GE
Idempotent: E + E = E
Involving Kleene's closures:
– (E*)* = E*
– Φ* =
– * =
– E+ = EE*
Applications
Web Search Engines
– filter input strings or to match parts of such strings.
Lexical Analyzer
– Converting a statement into token using predefined Regular Expression.
10
Examples
A regular expression: a b c
*
(c )
L
L
La a
Languages of Regular Expressions
Example
L (a b c) , a, bc, aa, abc, bca,...
*
Example
a b a*
Regular expression:
La b a * L a b L a *
La b L a*
La Lb La
*
r a b a bb
*
Regular expression
r aa bb b
* *
Regular expression
Lr {a b2n 2m
b : n, m 0}
Example
Regular expression r (0 1) 00 (0 1)
* *
Definition:
L(r1) L(r2 )
True or False?
Let R and S be two regular expressions. Then:
1. ((R*)*)* = R*?
2. (R+S)* = R* + S*?
3. (RS + R)* RS = (RR*S)*?
Example: how to use these regular expression properties
and language operators?
L = { w | w is a binary string which does not contain two consecutive 0s or two consecutive
1s anywhere)
22
Regular Expressions
and
Regular Languages
Theorem
Languages
Generated by
Regular Expressions
Regular
Languages
Proof:
Languages
Generated by Regular
Languages
Regular Expressions
Languages
Generated by Regular
Languages
Regular Expressions
Proof - Part 1
Languages
Generated by Regular
Languages
Regular Expressions
Lr1 r2
Lr1 r2 Are regular
Lr 1
* Languages
Lr1
L r Lr1
1
* *
Languages
Generated by Regular
Languages
Regular Expressions
– Similarly the language L={x, xx, xxx,…}, defined over Σ = {x}, can be
expressed by the regular expression x+.
31
Example
Now consider another language L, consisting of all possible strings,
defined over Σ = {a, b}. This language can also be expressed by the
regular expression
(a + b)*
32
Example
Now consider another language L, of even length, defined over Σ =
{a, b}, then it’s regular expression may be
((a+b)(a+b))*
33
Remark
It may be noted that a language may be expressed by more than one
regular expressions, while given a regular expression there exist a
unique language generated by that regular expression.
34
Example
Consider the language, defined over
Σ={a,b} of words having at least one a, may be expressed by a
regular expression
(a+b)*a(a+b)*
35
Example
Consider the language, defined over
Σ={a, b}, of words starting with double a and ending in double b
then its regular expression may be
aa(a+b)*bb
36
Standard Representations
of Regular Languages
Regular Languages
DFAs
Regular Expressions
NFAs