0% found this document useful (0 votes)
4 views45 pages

CS242 - Module 3

The document covers the theory of computing, specifically focusing on regular expressions and finite automata, including Mealy and Moore machines. It outlines key concepts, definitions, and examples related to regular expressions, their operators, and their applications in various programming environments. The material is based on the textbook 'Introduction to Automata Theory, Languages, and Computation' and includes recommended readings for further understanding.

Uploaded by

iHACK Project
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views45 pages

CS242 - Module 3

The document covers the theory of computing, specifically focusing on regular expressions and finite automata, including Mealy and Moore machines. It outlines key concepts, definitions, and examples related to regular expressions, their operators, and their applications in various programming environments. The material is based on the textbook 'Introduction to Automata Theory, Languages, and Computation' and includes recommended readings for further understanding.

Uploaded by

iHACK Project
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

‫ر‬

‫الجامعة السعودية االلكتونية‬


‫ر‬
‫االلكتونية‬ ‫الجامعة السعودية‬

‫‪26/12/2021‬‬
Theory of Computing

Headline separator Module 3


Regular Expressions and Languages
Contents
1. Finite Automata with output (Mealy and Moore machines)
2. Regular expressions
3. Finite Automata and Regular Expressions
4. Applications of Regular Expressions
5. Algebraic Laws for Regular Expressions
Weekly Learning Outcomes
1. Explain regular expressions.
2. Describe Mealy and Moore machines.
Required Reading
1. Finite Automata with output (Mealy and Moore machines)
2. Regular expressions
3. Finite Automata and Regular Expressions
4. Applications of Regular Expressions
5. Algebraic Laws for Regular Expressions
(Introduction to Automata Theory, Languages, and Computation
(2013) Global Edition 3rd Edition)

Recommended Reading
https://people.eecs.berkeley.edu/~sseshia/172/lectures/Lecture4.pdf
https://slideplayer.com/slide/13617026/
https://www.tutorialspoint.com/automata_theory/pdf/moore_and_mealy_machin
es.pdf

This Presentation is mainly dependent on the textbook: Introduction to Automata Theory, Languages, and Computation: Global Edition, 3rd edition (2013) PHI
by John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullman
• Finite Automata with output (Mealy and Moore machines)
Two Types of Automata with Outputs
Differ in how outputs are produced
• Moore Machine:
• Outputs are independent of the inputs, i.e. outputs are produced from
within the state of the state machine
• Mealy Machine:
• Outputs can be determined by the present state alone, or by the present
state and the present inputs, i.e. outputs are produced as the machine makes
a transition from one state to another
• Any Moore machine can be turned into a Mealy machine (and vice
versa)

7
Moore Machine

The Moore State Machine


output remains the same as
long as the state machine
remains in that state. State 1
The output can be arbitrarily a,b
complex but must be the q,r
same every time the Input condition that
machine enters that state. must exist in order
i,j
to execute these
transitions from
State 1
Output condition that State 2
results from being in x,y
a particular present state

8
Mealy Machine
The Mealy State Machine
generates outputs based on:
 The Present State, and
 The Inputs to the M/c.
So, same state can generate
a,b
many different patterns of State 1 q,r
output signals, depending on
the inputs.
Outputs are shown on Input condition that
i,j must exist in order
transitions since they are x,y
determined in the same way as to execute these
is the next state. transitions from
State 1
Output condition that
results from being in State 2
a particular present state

9
FSM as Recognizer/Translator
• Outputs a ‘0’ if an even # of 1’s is received and outputs a ‘1’
otherwise
• What is state?
• Two states: whether an even # of 1s have been received, or an odd # of 1s
have been received

Mealy Machine Moore Machine


0/0 0
Even Even
Input [0] Output
1/0 1/1
1 1
Output Input
Odd Odd
[1]

0/1 0
10
Formal Definition
A Moore machine (finite-state machine with output ) is a six-
tuple M = (S, I, O, f, g, s0)
consists of:
▪ A finite set S of states
▪ A finite input alphabet I
▪ A finite output alphabet O
▪ A transition function f that assigns to each sate and input
pair a new state
▪ An output function g that assigns to each sate and input
pair an output
▪ An initial state s0

11
Examples (1)
The following state table describes a finite-state machine with
S = {s0, s1, s2, s3} , I = {0, 1} and O = {0, 1} the values of the
transition function f are displayed in the first two columns,
and the values of the output function g are displayed in the
last two columns.
State Transition Table State Transition Diagram

12
Examples (2)
Construct the state table for the finite-state machine with the
following state diagram.
State Transition Table State Transition Diagram

13
Examples (3)
Find the output string generated by the following finite-state
machine if the input string is 101011.
The obtained output is 001000 as shown in the table.

14
Examples (4)
Unit-Delay Machine produces as output the input string
delayed by a specific amount of time.
Input: x1x2x3…xn output: 0x1x2x3…xn-1

Previous input
is 1

Previous input
is 0

Unit-Delay Machine 15
• Regular expressions
Definition of a Regular Expression
R is a regular expression if it is:
1. R = a, if a ∈ .
2. R = ε, standing for the language {ε}.
3. R = Ø, standing for the empty language.
4. R = R1+R2, where R1 and R2 are regular expressions, and ‘+’
signifies union.
5. R = R1R2, where R1 and R2 are regular expressions and ‘R1R2’
signifies concatenation.
6. R = R’*, where R’ is a regular expression and ‘*’ signifies
closure.
7. R = (R’), where R’ is a regular expression and ‘()’ signifies
parenthesizing.
8. R is a regular expression if it is obtained by a finite number of
applications of 1-7.
9. Nothing else is a regular expression.

Precedence: Parentheses have the highest precedence, followed by *,


concatenation, and union.
Examples
▪ L(101) = {101}
▪ L((0+1)*( = {ε ,0, 1, 00, 01, 10, 11, … } (i.e. *)
▪ L(1+10*) = { 1, 10, 100, 1000, 10000, … }
▪ L(1*10*) = {1, 11, 110, 1110, 11100, …}
▪ L(1*01*) = {0, 10, 01, 101, 110, 011, 1101, 11011, 1011, …} , {w | w
has exactly a single 0}
▪ L((0(0+1))*) = { ε, 00, 01, 0000, 0001, 0100, 0101, …}
▪ L((0+ε)(1+ ε)) = {ε, 0, 1, 01}
▪ Rε = R
▪ R+Ø = R
▪ RØ = Ø
▪ RØ = R, if R = Ø.
Regular Expressions

Regular = Finite Automata


expressions (DFA, NFA, -NFA)
Syntactical
Automata/machines
expressions

Regular Formal language


Languages classes

19
Regular expression Operators (1)
◼ Union of two languages:
◼ L U M = All strings that are either in L or M or in
both
◼ Note: A union of two languages produces a third
language
◼ Concatenation of two languages:
◼ L . M = All strings that are of the form xy
s.t., x  L and y  M
◼ The dot (.) operator is usually omitted
◼ i.e., L . M is written as LM

20
Regular expression Operators (2)
◼ Kleene Closure (*) of a given language L:
◼ L0= {}
◼ L1= {w | for some w  L}
◼ L2= { w1w2 | w1  L, w2  L (duplicates allowed)}
◼ Li= { w1w2…wi | all w’s chosen  L (duplicates allowed)}
◼ “i” here refers to how many strings to concatenate from the parent language L to
produce strings in the language Li
◼ (Note: the choice of each wi is independent)
◼ L* = Ui≥0 Li (arbitrary number of concatenations)
Example:
◼ Let L = { 1, 00}
◼ L0= {}
◼ L1= {1,00}
◼ L2= {11,100,001,0000}
◼ L3= {111,1100,1001,10000,000000,00001,00100,0011}
◼ L* = L0 U L1 U L2 U …

21
Regular expression Operators (3)
Kleene Closure (special notes)
◼ L* is an infinite set iff |L| ≥ 1 and L ≠ {}

◼ If L = {}, then L* = {}

◼ If L = Φ, then L* = {}

Σ* denotes the set of all words over an alphabet Σ


◼ Therefore, an abbreviated way of saying there is an
arbitrary language L over an alphabet Σ is:
◼ L  Σ*

22
Building Regular Expressions
◼ Let E and F be a any two regular expressions and the
language represented by E is L(E) and F is L(F),
◼ Then:
◼ (E) = E
◼ L(E + F) = L(E) U L(F)
◼ L(E F) = L(E) L(F)
◼ L(E*) = (L(E))*

23
Example: Regular Expressions
◼ L = { w | w is a binary string which does not contain two consecutive 0s or two
consecutive 1s anywhere)
◼ E.g., w = 01010101 is in L, while w = 10010 is not in L
◼ Goal: Build a regular expression for L
◼ Four cases for w:
◼ Case A: w starts with 0 and |w| is even
◼ Case B: w starts with 1 and |w| is even
◼ Case C: w starts with 0 and |w| is odd
◼ Case D: w starts with 1 and |w| is odd
◼ Regular expression for the four cases:
◼ Case A: (01)*
◼ Case B: (10)*
◼ Case C: 0(10)*
◼ Case D: 1(01)*
◼ Since L is the union of all 4 cases:
◼ Reg Exp for L = (01)* + (10)* + 0(10)* + 1(01)*
◼ If we introduce  then the regular expression can be simplified to:
◼ Reg Exp for L = ( +1)(01)*( +0)

24
Precedence Rule of Operators
◼ Highest to lowest
◼ * (star)

◼ . (concatenation)
◼ + operator

◼ Example:
◼ 01* + 1 = ( 0 . ((1)*) ) + 1

25
• Finite Automata and Regular Expressions
FA & RegEx
◼ To show that they are interchangeable, consider the
following theorems:
◼ Theorem 1: For every DFA A there exists a regular
expression R such that L(R)=L(A)
◼ Theorem 2: For every regular expression R there exists an
-NFA E such that L(E)=L(R)

-NFA NFA
Theorem 2 Kleene Theorem

RegEx DFA
Theorem 1

27
DFA to RegEx construction
Informally, trace all distinct paths (traversing cycles only
once) from the start state to each of the final states and
enumerate all the expressions along the way

DFA Reg Ex
Theorem 1
Example: 1 0 0,1

q0 0 q1 1 q2

(1*) 0 (0*) 1 (0 + 1)*


1* 00* 1 (0+1)*

1*00*1(0+1)*
28
RegEx to -NFA construction
RegEx -NFA
Theorem 2

Example: (0+1)*01(0+1)*

(0+1)* 01 (0+1)*

 


0
 
0

 0 1

 1
  1

 

29
Converting a RegEx to an Automata
◼ We have shown we can convert an automata to a
RegEx. To show equivalence we must also go the
other direction, convert a RegEx to an automaton.
◼ We can do this easily by converting a RegEx to an ε-
NFA
◼ Inductive construction
◼ Start with a simple basis, use that to build more complex
parts of the NFA
RE to ε-NFA (1)

◼ Basis:

R=a
a

R=ε ε

R=Ø
RE to ε-NFA (2)

ε S ε
R=S+T

ε ε
T

ε
R = ST
S T

ε ε
R= S*
S

ε
RE to ε-NFA Example (1)

◼ Convert R = (ab+a)* to an NFA


◼ We proceed in stages, starting from simple elements and working
our way up

a
a

b
b

a ε b
ab
RE to ε-NFA Example (2)

ab+a
a ε b
ε ε

a
ε ε

(ab+a)*
a ε b
ε ε
ε ε

a
ε ε

ε
RE to ε-NFA Example (3)

ab+a
a ε b
ε ε

a
ε ε

(ab+a)*
a ε b
ε ε
ε ε

a
ε ε

ε
• Applications of Regular Expressions
Applications of Regular Expressions
• Offers a declarative way to express the pattern of any
string we want to accept
• E.g., 01*+ 10*

• Automata are more machine-like


< input: string , output: [accept/reject] >
• Regular expressions are more program syntax-like

• Unix environments heavily use regular expressions


• E.g., bash shell, grep, vi & other editors, sed
• Perl scripting is good for string processing
• Used in Lexical analyzers such as Lex or Flex

37
• Algebraic Laws for Regular Expressions
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Φ = Φ
◼ Distributive:
◼ E (F + G) = EF + EG
◼ (F + G) E = FE + GE
◼ Idempotent: E + E = E
◼ Involving Kleene closures:
◼ (E*)* = E*
◼ Φ* =
◼ * =
◼ E+ = E E*
◼ E? =+E

39
Test
Let R and S be two regular expressions. Then:

1. ((R*)*)* = R* ?

2. (R+S)* = R* + S* ?

3. (RS + R)* RS = (RR*S)* ?

40
Checking a Law
◼ Suppose we are told that the law
(R + S)* = (R*S*)*
holds for regular expressions. How would we check that this claim is
true?

1. Convert the RegEx’s to DFA’s and minimize the DFA’s to see if they
are equivalent.
2. We can use the “concretization” test:
◼ Think of R and S as if they were single symbols, rather than
placeholders for languages, i.e., R = {0} and S = {1}.
◼ Test whether the law holds under the concrete symbols. If so, then this
is a true law, and if not then the law is false.
Concretization Test

◼ For our example


(R + S)* = (R*S*)*
We can substitute 0 for R and 1 for S.
The left side is clearly any sequence of 0's and 1's. The
right side also denotes any string of 0's and 1's, since 0
and 1 are each in L(0*1*).
Concretization Test
◼ NOTE: extensions of the test beyond regular expressions may fail.
◼ Consider the “law” L  M  N = L  M.
◼ This is clearly false
◼ Let L = M = {a} and N = Ø. {a}  Ø.
◼ But if L = {a} and M = {b} and N = {c} then
◼ L  M does equal L  M  N which is empty.
◼ The test would say this law is true, but it is not because we are
applying the test beyond regular expressions.
◼ We’ll see soon various languages that do not have corresponding
regular expressions.
Main Reference
1. Regular expressions
2. Finite Automata and Regular Expressions
3. Applications of Regular Expressions
4. Algebraic Laws for Regular Expressions
Finite Automata with output (Mealy and Moore machines)
(Introduction to Automata Theory, Languages, and Computation
(2013) Global Edition 3rd Edition)
Additional References
https://people.eecs.berkeley.edu/~sseshia/172/lectures/Lecture4.pdf
https://slideplayer.com/slide/13617026/
https://www.tutorialspoint.com/automata_theory/pdf/moore_and_mealy_machin
es.pdf

This Presentation is mainly dependent on the textbook: Introduction to Automata Theory, Languages, and Computation: Global Edition, 3rd edition (2013) PHI
by John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullman
Thank You

You might also like