1 of 85

Automata Theory �and�Compiler Design�(17CI15)

B.Tech. CSE – V Sem, A.Y:2020-21

2 of 85

Why Theory of Computation?

  • The Theory of Computation is the branch that deals with how efficiently problems can be solved on a model of computation using an algorithm.
  • It divides into three major areas Automata theory and formal languages, computability theory, and computational complexity theory.

Theory of computation

Automata theory and Formal Languages

Computability theory

Computational Complexity theory

  • Automata theory deals with

computational models

  • Formal languages are used to represent computational

problems

  • Computability theory deals about the what problems can be solvable using computer?
  • It deals with how efficiently

the problems can be solved

based on the resources time

and space complexity?

  • Classifies problems into P and NP classes

3 of 85

Automata Theory and Compiler Design

Unit-1

  • Finite Automata
  • Regular Languages

Unit-2

  • Context-free languages
  • Pushdown Automata

Unit-3

  • Design of Lexical analyzer
  • Design of Parser (Top down

and Bottom up parsing)

Unit-4

  • Syntax-directed Translation
  • Run-time Environment

Unit-5

  • Basics of Code optimization
  • Code generation

Text books:

  1. John E. Hopcraft and J.D.Ullman – Introduction to Automata Theory and Formal Languages and Computation, Narosa publications , 1999 ( unit-1 and 2)
  2. Alfred V. Aho ,Jeffrey Ullman , Ravi sethi – Compilers Prinicples, Techniques and Tools , Pearson Education 2nd edition ,2008( for 3,4,5 units)

Pre-requisite: Set theory and Graph theory, programming language and data structures

4 of 85

Course Outcomes

At the end of the course, the student shall be able to

CO 1: Design Finite Automata and Regular expressions for regular languages

CO 2: Design Context-free grammar and push-down automata for CFL

CO 3: Design and Implement Lexical and Syntax Analyzer

CO 4: Create framework for syntax directed translation schemes and

understand the run-time organization of the program.

CO 5: Analyze various code optimization techniques and understand the

design issues of code generation

5 of 85

Introduction to Automata Theory

  • The study of computational models (known as Abstract machines) and its characteristics is known as Automata theory
  • Automata (Known as Abstract machines) is used to model the problems.

( or)

A Mathematical model to provide a proof for the computational problems that are solvable by the computer

  • Automata is a self-acting machines i.e., consumes input and provide transition from one state to another state
  • Based on the computational capability and complexity the Automata are classified as
    • Finite Automata
    • Pushdown Automata
    • Linear Bounded Automata
    • Turing Machines

6 of 85

Components of Automata

The components of Automata are

  • Input tape
  • Read/write head
  • Finite Control( set of states)

Q1, Q2,…..Qn

Input tape

Read/Write Head

Finite Control

7 of 85

Introduction to Finite Automata

  • It is used to model the problems .
  • Model the switch(on/off)
    • A switch can be one of the two possible states (ON/OFF). If the switch is in ‘ON’ state on input ‘off(0)’ it goes to OFF state. Similarly, if the switch is in ‘OFF’ state on input ‘on(1)’it goes to ON state.

OFF

ON

on(1)

off(0)

State

on(1)

off(0)

OFF

ON

--

ON

--

OFF

8 of 85

Example 2 – Modelling the Binary Adder

Without carry

With carry

00 | 0

10 | 0

11 | 0

00 | 1

01 | 1

10 | 1

11 | 1

01 | 0

Note : So, problem will be represented as a language and problem-solving means constructing an equivalent automaton.

Carry

1

No

No

1

Input number1

1

1

0

1

Input number 2

1

0

0

1

Output

1

0

0

1

0

9 of 85

Mathematical Notations

  • Alphabet
    • An alphabet is an finite non-empty set of symbols.
    • It is represented by ∑.

Example s

∑ = {0,1} or {a,b} or { a-zA-Z0-9,+,-,*…}

  • String
    • A string is a finite sequence of one or more symbols of an alphabet.
    • Examples

“ababb” is a string over the alphabet {a,b}

“a+b*c” is a string over the alphabet {a-zA-Z0-9,+,*}

  • Language
    • A language is a set of strings.

Example

L1 = {0,1,00,11,01,10,….} over the alphabet ∑ ={ 0,1 }

L2= {ab, aab,aaab,aaaab,…..} over the alphabet ∑ ={ a,b }

10 of 85

Finite Automaton (FA)

  • Informally, a state diagram that comprehensively captures all possible states and transitions that a machine can take while responding to a stream or sequence of input symbols

  • Recognizer for “Regular Languages”.

  • Deterministic Finite Automata (DFA)
    • The machine can exist in only one state at any given time

  • Non-deterministic Finite Automata (NFA)
    • The machine can exist in multiple states at the same time

11 of 85

Deterministic Finite Automata

Formally ,a DFA is defined by the 5-tuple M= {Q, ∑ , δ ,q0, F}

    • Q - a finite set of states
    • ∑ - a finite set of input symbols (alphabet)
    • δ - a transition function, which is a mapping between

Q x ∑ --> Q

    • q0 - a start state/ initial state
    • F - set of accepting states/ final states

12 of 85

Example

What is the language accepted by the following Finite Automaton?

q0

q1

0,1

0,1

q0

0,1

q0

0,1

M1 :

M2 :

q0

q1

a,b

a,b

L(M1) or L1= {ϵ, 0, 1, 00, 01,…}

L2 = {, 0, 1, 000,001,010…}

M3 :

L3 = {ϵ, aa, ba,ab,bb,…}

M4 :

L5 = Φ ( empty set)

13 of 85

q0

q1

0

1

1

0

M5:

L5 = {0, 00,10,000,010,100…..}

Strings ending with zero

q0

q1

q2

b

a

a, b

b

M6:

L6 = {ba,bab,babb,abab,aaaba,…….}

Strings ending with either one a (or) zero or more b

What is the language accepted by the following Finite Automaton?

14 of 85

States required to construct FA

States required to represent the string

Length of the string

Minimum No. of states

DFA or NFA

q0

q1

q2

0

1

q0

q1

w

a” (or) “0”

1

2

“01”

2

3

where w = a or b or 0 or 1

“abba”

4

5

“01001…..”

n

n+1

15 of 85

Example 1:

Step 1 : The minimum possible string having “ba” as a substring is “ba”. so, minimum number of states to represent “ba” is 3

Step 2: Place the missing transitions of each state with out changing the original property of the language(i.e., having “ba” as a substring).

q0

q1

q2

b

a

For q0 transition a is missing, the possibilities are (q0 , a)= q0 , (q0 , a)= q1, (q0 , a)= q2

a

a

a

x

x

q0

q1

q2

b

a

Problem: Construct a Deterministic Finite Automata for the Language L over the alphabet {a,b} and whose strings have “ba” as a substring

Among the possibilities (q0 , a)= q0 is valid and (q0 , a)= q1, (q0 , a)= q2 are invalid

q0

q1

q2

b

a

a

16 of 85

For q1 transition b is missing, the possibilities are (q1 , b)= q0 , (q1 , b)= q1, (q1 , b)= q2

q0

q1

q2

b

a

a

Among the possibilities (q1 , b)= q1 is valid and (q1, b)= q0, (q1, b)= q2 are invalid

b

b

b

x

x

For q2 transitions a, and b are missing, the possibilities are

(q2 , a)= q0 , (q2 , a)= q1, (q2 , a)= q2 and (q2 , b)= q0 , (q2 , b)= q1, (q2 , b)= q2

q0

q1

q2

b

a

a

b

q0

q1

q2

b

a

a

b

Among the possibilities (q2 , a)= q2 and (q2 , b)= q2 are valid and remaining are invalid

a,b

17 of 85

Equivalent representation of FA

q0

q1

q2

b

a

a

b

a,b

δ

a

b

q0

q0

q1

q1

q2

q1

q2

q2

q2

δ (q0 , a ) = q0

δ (q0 , b ) = q1

δ (q1 , a ) = q2

δ (q1 , b ) = q1

δ (q2 , a ) = q2

δ (q2 , b ) = q2

Transition diagram

Transition table

Transitions

A finite Automata M= {Q, ∑ , δ ,q0, F} where

Q= { q0,q1,q2} , ∑ = {a,b} , initial state= q0 , F={ q2}

where transition function δ is represented as

18 of 85

Language accepted by DFA(M)

q0

q1

q2

b

a

a

b

a,b

= δ( δ(q0, abb), a)

= δ(δ(δ(q0, ab), b)a)

= δ(δ(δ(δ(q0, a),b), b)a)

= δ(δ(δ(δ(δ(q0, ϵ),a),b), b)a)

= δ(δ(δ(δ(q0,a),b), b)a)

= δ(δ(δ(q0,b), b)a)

= δ(δ(q1,b)a)

= δ(q1,a) =q2 ( Accept state)

δ ( q0, abba)

^

^

^

^

^

L(M) = { w | δ(q0, w) is in F }

^

^

δ

( q, wa) =

^

δ

( q, w),a)

δ(

δ

^

(q, ϵ) = q

Extended transition function ( )

^

δ

q0

a

q0

b

q1

b

q1

a

q2

Informally, transition path is represented as

19 of 85

Language accepted by FA

The language of a DFA M = (Q, Σ, δ, q0 , F), denoted L(M) is defined by

The language of M is the set of strings w that take the start state q0 to one of the accepting states.

If L is a L(M) from some DFA, then L is a regular language

^

L(M) = { w | δ(q0, w) is in F }

20 of 85

Deterministic Finite Automata

q0

start

q1

0

1

0,1

0

1

q2

Accepting

state

  • Q = {q0,q1,q2}
  • ∑ = {0,1}
  • start state = q0
  • F = {q2}
  • Transition table

q2

q2

q2

q2

q1

q1

q0

q1

q0

1

0

states

symbols

Construct DFA for the Language L over the alphabet {0,1} and whose strings

contains 01 as a substring

21 of 85

Example

Construct DFA for the language over the alphabet {0,1} and whose string have

    • Even length
    • Odd length
    • Even number of ones
    • Odd number of ones
    • Even number of zeros
    • Odd number of zeros
    • Even number of zeros and ones - EE
    • Even number of zeros and odd number of ones - EO
    • Odd number of zeros and even number of ones - OE
    • Odd number of zeros and odd number of ones - OO
    • Even number of zeros or ones - { EE,EO,OE } - OO
    • Even number of zeros or odd number of ones -except OE
    • Odd number of zeros or even number of ones -Except EO
    • Odd number of zeros or odd number of ones -except EE

22 of 85

Example

Construct DFA for the language L over the alphabet {a,b} and

whose string length is divisible by 3 (or) multiple of 3

Construct DFA for the following language L

L ={ w ϵ { a,b}* | |w| mod 3 = 0 }

(or)

Construct DFA for the following language L

      • L ={ w ϵ { a,b}* | |w| mod 3 = 1 }
      • L ={ w ϵ { a,b}* | |w| mod 3 = 2 }
      • L ={ w ϵ { a,b}* | |na (w)| mod 3 = 0 }
      • L ={ w ϵ { a,b}* | |nb (w)| mod 3 = 0 }
      • L ={ w ϵ { a,b}* | |na (w)| mod 3 = 1 }
      • L ={ w ϵ { a,b}* | |nb (w)| mod 3 = 1}
      • L ={ w ϵ { a,b}* | |na (w)| mod 3 = 2}
      • L ={ w ϵ { a,b}* | |nb (w)| mod 3 = 2}

23 of 85

Example

Construct DFA for the language L over the alphabet {a,b} and whose strings having

    • Substring abba
    • No substring abba

Construct DFA for the language L over the alphabet {a,b} and whose strings having

    • Substring abba
    • No substring abba

24 of 85

Non-deterministic Finite Automata(NFA)

Mathematically a NFA is defined by the 5-tuple M= {Q, ∑ , δ ,q0, F}

    • Q - a finite set of states
    • ∑ - a finite set of input symbols (alphabet)
    • δ - a transition function, which is a mapping between

Q x ∑ --> 2Q

    • q0 - a start state/ initial state
    • F - set of accepting states/ final states

25 of 85

Non-deterministic Finite Automata (NFA)

  • A Non-deterministic Finite Automaton (NFA)
    • “non-deterministic”
      • Implying that the machine can exist in more than one state at the same time
      • Transitions could be non-deterministic

qi

1

1

qj

qk

  • Each transition function therefore �maps to a set of states

26 of 85

How to use an NFA?

  • Input: a string w in ∑*
  • Question: Is w acceptable by the NFA?
  • Steps:
    • Start at the “start state” q0
    • For every input symbol in the sequence w do
      • Determine all possible next states from all current states for the given the current input symbol in w and the transition function
    • If after all symbols in w are consumed and if at least one of the current states is a final state then accept w;
    • Otherwise, reject w.

27 of 85

NFA for strings containing substring 01

q0

start

q1

0

0,1

0,1

1

q2

Final

state

  • Q = {q0,q1,q2}
  • Σ = {0,1}
  • start state = q0
  • F = {q2}
  • Transition table

{q2}

{q2}

q2

{q2}

Φ

q1

{q0}

{q0,q1}

q0

1

0

states

symbols

What will happen if at state q1 �an input of 0 is received?

NFA will halt and says, w is not accepted

More Natural way of constructing the automata.

3 possible cases strings may contain 01

  1. At the Begin i.e., 01 followed by any no. of 0’s and 1’s
  2. At the End i.e., any no of 0’s and 1’s followed by 01.
  3. At some position . any no of 0’s and 1’s (01) any no. of 0’s and 1’s

28 of 85

q0

start

q1

0

0,1

0,1

1

q2

Final

state

δ ( q0, 000)

δ ( q0, 00)

δ ( q1, 00)

δ ( q0, 0)

δ ( q1, 0)

q0

0

0

0

0

0

Among the leaves { q0 } , there is no

final state . So, the string 1000 is

not accepted

δ ( q0, 010)

δ ( q0, 10)

δ ( q1, 10)

δ ( q0, 0)

δ ( q2, 0)

q0

q2

0

0

1

1

0

0

δ ( q0, 1000)

1

Halt, No

transition

for q1 on 0

X

X

0

Among the leaves { q0 , q1,q2 } , there is a

final state . So, the string 010 is accepted

0

Halt, No transition for q1 on 0

Acceptance of strings by NFA

q1

0

29 of 85

NFA to DFA construction: Example

  • L = {w | w ends in 01}

q0

q1

0

0,1

q2

1

NFA:

δN

0

1

q0

{q0,q1}

{q0}

q1

Ø

{q2}

q2

Ø

Ø

DFA:

δD

0

1

Ø

Ø

Ø

[q0]

[q0,q1]

[q0]

[q1]

Ø

{q2}

*[q2]

Ø

Ø

[q0,q1]

{q0,q1}

{q0,q2}

*[q0,q2]

{q0,q1}

{q0}

*[q1,q2]

Ø

{q2}

*[q0,q1,q2]

{q0,q1}

{q0,q2}

2. Determine transitions

δD

0

1

[q0]

[q0,q1]

[q0]

[q0,q1]

[q0,q1]

[q0,q2]

*[q0,q2]

[q0,q1]

[q0]

[q0]

1

0

[q0,q1]

1

[q0,q2]

0

0

1

To avoid enumerating all of power set, do �“lazy creation of states”

3. Retain only those states � reachable from {q0}

1. Enumerate all possible subsets

30 of 85

NFA to DFA: Repeating the example using �LAZY CREATION

  • L = {w | w ends in 01}

q0

q1

0

0,1

q2

1

NFA:

δN

0

1

q0

{q0,q1}

{q0}

q1

Ø

{q2}

q2

Ø

Ø

DFA:

δD

0

1

[q0]

[q0,q1]

[q0]

[q0,q1]

[q0,q1]

[q0,q2]

*[q0,q2]

[q0,q1]

[q0]

[q0]

1

0

[q0,q1]

1

[q0,q2]

0

0

1

Idea: Introduce states as you go

(on a need basis)

31 of 85

Summary

  • Construction of DFA/NFA is equivalent to writing the program for the given problem.

  • Depending up on the given problem( i.e., language) , we decide the number of states Q

,initial state and set of final states.

  • There can be infinitely many DFAs exists for the given problem, but the minimal DFA is

unique.

q0

q1

q2

b

a

a

b

a,b

q0

q1

b

a

a,b

i)

ii)

32 of 85

Properties of DFAs and NFAs

  • The machine never really terminates.
    • It is always waiting for the next input symbol or making transitions.

  • The machine decides when to consume the next symbol from the input and when to ignore it.
    • (but the machine can never skip a symbol)

  • A transition can happen even without really consuming an input symbol (think of consuming ε as a free token) – if this happens, then it becomes an ε-NFA .

  • A single transition cannot consume more than one (non-ε) symbol.

33 of 85

Finite Automata(NFA) with ε-Transitions

  • We can allow explicit ε-transitions in finite automata
    • i.e., a transition from one state to another state without consuming any additional input symbol
    • Explicit ε-transitions between different states introduce non-determinism.
    • Makes it easier sometimes to construct NFAs

Definition: ε -NFAs are those NFAs with at least one explicit ε-transition defined.

  • ε -NFAs have one more column in their transition table

34 of 85

Automata

Components

Transition function

Extended transition function

( δ )

DFA

( Q , ∑, δ, q0, F )

δ : Q x ∑ ---> Q

NFA

( Q , ∑, δ, q0, F )

δ : Q x ∑ ---> 2Q

NFA with Epsilion

( Q , ∑, δ, q0, F )

δ : Q x ∑ υ { ϵ} --> 2Q

^

(q, ϵ ) = q

δ

^

(q, wa ) =

δ

^

δ

^

δ (

(q, w), a )

(q, ϵ ) = q

δ

^

(q, wa ) =

δ

^

δ

^

δ (

(q, w), a )

(q, ϵ ) = ϵ- closure(q)

δ

^

(q, wa ) =

δ

^

δ

^

δ (

(q, w), a )

35 of 85

Example of an ε-NFA

  • ε-closure of a state q, is the set of all states (including itself) that can be reached from q by repeatedly making an arbitrary number of ε-transitions.

L = {w | w is empty, or if non-empty will end in 01}

δE

0

1

ε

q0

Ø

Ø

{q1}

q1

{q0,q1}

{q0}

Ø

q2

Ø

{q2}

Ø

q3

Ø

Ø

Ø

start

q1

q2

0

0,1

1

q3

q0

ε

ε-closure (q0)

= { q0 , q1 }

ε-closure(q1)

= { q1 }

ε-closure(q2)

= { q2 }

ε-closure(q3)

= { q3 }

36 of 85

Example of an ε-NFA

q0

q1

q2

ϵ

a

b

c

ϵ

For the string aac, we can view as a, a ,ϵ, ϵ, c

Identity

a. ϵ = ϵ . a = a

a. Φ = a. Φ = Φ

q0

a

37 of 85

Example of an ε-NFA

  • ε-closure of a state q, is the set of all states (including itself) that can be reached from q by repeatedly making an arbitrary number of ε-transitions.

L = {w | w is empty, or if non-empty will end in 01}

δE

0

1

ε

q0

Ø

Ø

{q0,q0}

q0

{q0,q1}

{q0}

{q0}

q1

Ø

{q2}

{q1}

q2

Ø

Ø

{q2}

ε-closure (q’0)

ε-closure(q0)

start

q0

q1

0

0,1

1

q2

q’0

ε

ε-closure(q1)

ε-closure(q2)

38 of 85

Simulation of ε-NFA

Simulate for w=101

L = {w | w is empty, or if non-empty will end in 01}

q0

q1

q0

ε

ε

q1

0

q1

1

q1

1

Ø

1

x

To simulate any transition:� Step 1) Go to all immediate destination states.

Step 2) From there go to all their ε-closure states as well.

start

q1

q2

0

0,1

1

q3

q0

ε

δE

0

1

ε

q0

Ø

Ø

{q1}

q1

{q1,q2}

{q1}

Ø

q2

Ø

{q3}

Ø

q3

Ø

Ø

Ø

q2

0

q3

1

39 of 85

Example of another ε-NFA

Simulate for w=101:

?

δE

0

1

ε

*q’0

Ø

Ø

{q’0,q0,q3}

q0

{q0,q1}

{q0}

{q0,q3}

q1

Ø

{q2}

{q1}

*q2

Ø

Ø

{q2}

q3

Ø

{q2}

{q3}

start

q0

q1

0

0,1

1

q2

q’0

ε

ε

q3

1

To simulate any transition:� Step 1) Go to all immediate destination states.

Step 2) From there go to all their ε-closure states as well.

40 of 85

Conversion of ε-NFA to DFA

40

L = {w | w is empty, or if non-empty will end in 01}

start

q0

q1

0

0,1

1

q2

q’0

ε

δE

0

1

ε

*q’0

Ø

Ø

{q’0,q0}

q0

{q0,q1}

{q0}

{q0}

q1

Ø

{q2}

{q1}

*q2

Ø

Ø

{q2}

δD

0

1

*{q’0,q0}

41 of 85

Conversion of ε-NFA to DFA

41

L = {w | w is empty, or if non-empty will end in 01}

start

q1

q2

0

0,1

1

q3

q0

ε

δE

0

1

ε

*q0

Ø

Ø

{q0,q1}

q1

{q1,q2}

{q1}

{q1}

q2

Ø

{q3}

{q2}

*q3

Ø

Ø

{q3}

δD

0

1

*{q0,q1}

{q1,q2}

{q1}

{q1,q2}

{q1,q2}

{q1,q3}

{q1}

{q1,q2}

{q1}

*{q1,q3}

{q1,q2}

{q1}

{q0, q1}

0

start

{q1,q2}

{q1,q3}

1

0

q1

1

1

0

0

1

union

ECLOSE

42 of 85

Equivalency of DFA, NFA, ε-NFA

  • Theorem: A language L is accepted by some ε-NFA if and only if L is accepted by some DFA

  • Implication:
    • DFA ≡ NFA ≡ ε-NFA
    • (all accept Regular Languages)

43 of 85

Applications of Interest

  • Comparing two DFAs:
    • L(DFA1) == L(DFA2)?

  • How to minimize a DFA?
    1. Remove unreachable states
    2. Identify & condense equivalent states into one

43

44 of 85

When to call two states in a DFA “equivalent”?

Two states p and q are said to be equivalent iff:

    • Any string w accepted by starting at p is also accepted by starting at q;

    • Any string w rejected by starting at p is also rejected by starting at q.

p

q

AND

w

p

q

w

🡺 p≡ q

45 of 85

Computing equivalent states in a DFA

45

A

C

E

G

B

D

F

H

0

1

1

1

1

1

1

1

0

0

0

0

0

0

0

1

Table Filling Algorithm

A

=

B

=

=

C

x

x

=

D

x

x

x

=

E

x

x

x

x

=

F

x

x

x

x

x

=

G

x

x

x

=

x

x

=

H

x

x

=

x

x

x

x

=

A

B

C

D

E

F

G

H

Pass #0

    • Mark accepting states ≠ non-accepting states

Pass #1

    • Compare every pair of states
    • Distinguish by one symbol transition
    • Mark = or ≠ or blank(tbd)

Pass #2

    • Compare every pair of states
    • Distinguish by up to two symbol transitions (until different or same or tbd)

….

(keep repeating until table complete)

46 of 85

Table Filling Algorithm - step by step

46

A

C

E

G

B

D

F

H

0

1

1

1

1

1

1

1

0

0

0

0

0

0

0

1

A

=

B

=

C

=

D

=

E

=

F

=

G

=

H

=

A

B

C

D

E

F

G

H

47 of 85

Table Filling Algorithm - step by step

47

A

C

E

G

B

D

F

H

0

1

1

1

1

1

1

1

0

0

0

0

0

0

0

1

A

=

B

=

C

=

D

=

E

X

X

X

X

=

F

X

=

G

X

=

H

X

=

A

B

C

D

E

F

G

H

  1. Mark X between accepting vs. non-accepting state

48 of 85

Table Filling Algorithm - step by step

48

A

C

E

G

B

D

F

H

0

1

1

1

1

1

1

1

0

0

0

0

0

0

0

1

A

=

B

=

C

X

=

D

X

=

E

X

X

X

X

=

F

X

=

G

X

X

=

H

X

X

=

A

B

C

D

E

F

G

H

  1. Mark X between accepting vs. non-accepting state
  2. Look 1- hop away for distinguishing states or strings

49 of 85

Table Filling Algorithm - step by step

49

A

C

E

G

B

D

F

H

0

1

1

1

1

1

1

1

0

0

0

0

0

0

0

1

A

=

B

=

C

X

X

=

D

X

X

=

E

X

X

X

X

=

F

X

=

G

X

X

X

=

H

X

X

X

=

A

B

C

D

E

F

G

H

  1. Mark X between accepting vs. non-accepting state
  2. Look 1- hop away for distinguishing states or strings

50 of 85

Table Filling Algorithm - step by step

50

A

C

E

G

B

D

F

H

0

1

1

1

1

1

1

1

0

0

0

0

0

0

0

1

A

=

B

=

C

X

X

=

D

X

X

X

=

E

X

X

X

X

=

F

X

X

=

G

X

X

X

X

=

H

X

X

=

X

=

A

B

C

D

E

F

G

H

  1. Mark X between accepting vs. non-accepting state
  2. Look 1- hop away for distinguishing states or strings

51 of 85

Table Filling Algorithm - step by step

51

A

C

E

G

B

D

F

H

0

1

1

1

1

1

1

1

0

0

0

0

0

0

0

1

A

=

B

=

C

X

X

=

D

X

X

X

=

E

X

X

X

X

=

F

X

X

X

=

G

X

X

X

=

X

=

H

X

X

=

X

X

=

A

B

C

D

E

F

G

H

  1. Mark X between accepting vs. non-accepting state
  2. Look 1- hop away for distinguishing states or strings

52 of 85

Table Filling Algorithm - step by step

52

A

C

E

G

B

D

F

H

0

1

1

1

1

1

1

1

0

0

0

0

0

0

0

1

A

=

B

=

C

X

X

=

D

X

X

X

=

E

X

X

X

X

=

F

X

X

X

=

G

X

X

X

=

X

X

=

H

X

X

=

X

X

X

=

A

B

C

D

E

F

G

H

  1. Mark X between accepting vs. non-accepting state
  2. Look 1- hop away for distinguishing states or strings

53 of 85

Table Filling Algorithm - step by step

53

A

C

E

G

B

D

F

H

0

1

1

1

1

1

1

1

0

0

0

0

0

0

0

1

A

=

B

=

C

X

X

=

D

X

X

X

=

E

X

X

X

X

=

F

X

X

X

=

G

X

X

X

=

X

X

=

H

X

X

=

X

X

X

X

=

A

B

C

D

E

F

G

H

  1. Mark X between accepting vs. non-accepting state
  2. Look 1- hop away for distinguishing states or strings

54 of 85

Table Filling Algorithm - step by step

54

A

C

E

G

B

D

F

H

0

1

1

1

1

1

1

1

0

0

0

0

0

0

0

1

A

=

B

=

=

C

X

X

=

D

X

X

X

=

E

X

X

X

X

=

F

X

X

X

X

X

=

G

X

X

X

=

X

X

=

H

X

X

=

X

X

X

X

=

A

B

C

D

E

F

G

H

  1. Mark X between accepting vs. non-accepting state
  2. Pass 1: � Look 1- hop away for distinguishing states or strings
  3. Pass 2:

Look 1-hop away again for distinguishing states or strings

continue….

55 of 85

Table Filling Algorithm - step by step

55

A

C

E

G

B

D

F

H

0

1

1

1

1

1

1

1

0

0

0

0

0

0

0

1

A

=

B

=

=

C

X

X

=

D

X

X

X

=

E

X

X

X

X

=

F

X

X

X

X

X

=

G

X

X

X

=

X

X

=

H

X

X

=

X

X

X

X

=

A

B

C

D

E

F

G

H

Equivalences:

  • A=B
  • C=H
  • D=G
  1. Mark X between accepting vs. non-accepting state
  2. Pass 1: � Look 1- hop away for distinguishing states or strings
  3. Pass 2:

Look 1-hop away again for distinguishing states or strings

continue….

56 of 85

Table Filling Algorithm - step by step

56

A

C

E

G

B

D

F

H

0

1

1

1

1

1

1

1

0

0

0

0

0

0

0

1

A

C

E

D

F

0

1

1

0

0

0

1

1

0

1

Equivalences:

  • A=B
  • C=H
  • D=G

Retrain only one copy for � each equivalence set of states

57 of 85

Table Filling Algorithm – special case

57

A

=

B

=

C

=

D

=

E

=

F

=

G

=

H

=

A

B

C

D

E

F

G

H

Q) What happens if the input DFA� has more than one final state?� Can all final states initially be treated� as equivalent to one another?

A

C

E

G

B

D

F

H

0

1

1

1

1

1

1

1

0

0

0

0

0

0

0

1

?

58 of 85

Regular Expressions vs. Finite Automata

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

  • Automata => more machine-like

< input: string , output: [accept/reject] >

  • Regular expressions => more program syntax-like

  • Unix environments heavily use regular expressions
    • E.g., bash shell, grep, vi & other editors, sed

  • Perl scripting – good for string processing

  • Lexical analyzers such as Lex or Flex

59 of 85

Regular Expressions

Regular �expressions

Finite Automata�(DFA, NFA, ε-NFA)

Regular�Languages

=

Automata/machines

Syntactical �expressions

Formal language �classes

60 of 85

Language Operators

  • Union of two languages:
    • L U M = all strings that are either in L or M
    • 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., LM is same as L.M

61 of 85

Kleene Closure (the * operator)

  • 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 are L (duplicates allowed)}
    • (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

“i” here refers to how many strings to concatenate from the parent language L to produce strings in the language Li

62 of 85

Kleene Closure

  • 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 ⊆ Σ*

63 of 85

Example for construction of regular expression

  • 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)

64 of 85

Precedence of Operators

  • Highest to lowest
    • * operator (star)
    • . (concatenation)
    • + operator

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

65 of 85

Finite Automata (FA) & Regular Expressions (Reg Ex)

  • 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

DFA

Reg Ex

Theorem 2

Theorem 1

Kleene Theorem

66 of 85

DFA to RE construction

Example:

q0

q1

q2

0

1

1

0

0,1

(1*)

0

(0*)

1

(0 + 1)*

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

1*00*1(0+1)*

00*

1*

1

(0+1)*

Q) What is the language?

67 of 85

RE to ε-NFA construction

Example:

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

0

1

ε

ε

ε

ε

ε

ε

ε

0

1

0

1

ε

ε

ε

ε

ε

ε

(0+1)*

01

(0+1)*

68 of 85

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+ =EE*
    • E? = ε +E

69 of 85

Some languages are not regular

When is a language is regular? �if we are able to construct one of the following: DFA or NFA or ε -NFA or regular expression

When is it not?�If we can show that no FA can be built for a language

69

70 of 85

How to prove languages are not regular?

What if we cannot come up with any FA?

A) Can it be language that is not regular?

B) Or is it that we tried wrong approaches?

�How do we decisively prove that a language is not regular?

70

“The hardest thing of all is to find a black cat in a dark room, �especially if there is no cat!” -Confucius

71 of 85

Example of a non-regular language

Let L = {w | w is of the form 0n1n , for all n≥0}

  • Hypothesis: L is not regular
  • Intuitive rationale: How do you keep track of a running count in an FA?
  • A more formal rationale:
    • By contradition, if L is regular then there should exist a DFA for L.
    • Let k = number of states in that DFA.
    • Consider the special word w= 0k1k => w ∈ L
    • DFA is in some state pi, after consuming the first i symbols in w

71

72 of 85

Rationale…

    • Let {p0,p1,… pk} be the sequence of states that the DFA should have visited after consuming the first k symbols in w which is 0k
    • But there are only k states in the DFA!
    • ==> at least one state should repeat somewhere along the path (by ++ Principle)
    • ==> Let the repeating state be pi=pJ for i < j
    • ==> We can fool the DFA by inputing 0(k-(j-i))1k and still get it to accept (note: k-(j-i) is at most k-1).
    • ==> DFA accepts strings w/ unequal number of 0s and 1s, implying that the DFA is wrong!

Uses Pigeon Hole Principle

73 of 85

The Pumping Lemma for Regular Languages

What it is? �The Pumping Lemma is a property of all regular languages.

How is it used? �A technique that is used to show that a given language is not regular

73

74 of 85

Pumping Lemma for Regular Languages

Let L be a regular language

Then there exists some constant N such that for every string w ∈ L s.t. |w|≥N, there exists a way to break w into three parts, w=xyz, such that:

    • y≠ ε
    • |xy|≤N
    • For all k≥0, all strings of the form xykz L

74

This property should hold for all regular languages.

Definition: N is called the “Pumping Lemma Constant”

75 of 85

Pumping Lemma: Proof

  • L is regular => it should have a DFA.
    • Set N := number of states in the DFA
  • Any string w∈L, s.t. |w|≥N, should have the form: w=a1a2…am, where m≥N
  • Let the states traversed after reading the first N symbols be: {p0,p1,… pN}
    • ==> There are N+1 p-states, while there are only N DFA states
    • ==> at least one state has to repeat �i.e, pi= pJwhere 0≤i<j≤N (by PHP)

75

76 of 85

Pumping Lemma: Proof…

  • => We should be able to break w=xyz as follows:
    • x=a1a2..ai; y=ai+1ai+2..aJ; z=aJ+1aJ+2..am
    • x’s path will be p0..pi
    • y’s path will be pi pi+1..pJ (but pi=pJ implying a loop)
    • z’s path will be pJpJ+1..pm
  • Now consider another � string wk=xykz , where k≥0
  • Case k=0
    • DFA will reach the accept state pm
  • Case k>0
    • DFA will loop for yk, and finally reach the accept state pm for z
  • In either case, wk L

76

yk (for k loops)

p0

pi

pm

x

z

=pj

This proves part (3) of the lemma

77 of 85

Pumping Lemma: Proof…

  • For part (1):
    • Since i<j, y ε

  • For part (2):
    • By PHP, the repetition of states has to occur within the first N symbols in w
    • ==> |xy|≤N

77

p0

pi

pm

x

z

yk (for k loops)

=pj

78 of 85

The Purpose of the Pumping Lemma for RL

  • To prove that some languages cannot be regular.

78

79 of 85

How to use the pumping lemma?

Think of playing a 2 person game

    • Role 1: We claim that the language cannot be regular

    • Role 2: An adversary who claims the language is regular

    • We show that the adversary’s statement will lead to a contradiction that implyies pumping lemma cannot hold for the language.

    • We win!!

79

80 of 85

How to use the pumping lemma?�(The Steps)

  1. (we) L is not regular.
  2. (adv.) Claims that L is regular and gives you a value for N as its P/L constant
  3. (we) Using N, choose a string w ∈ L s.t.,
    1. |w| ≥ N,
    2. Using w as the template, construct other words wk of the form xykz and show that at least one such wk ∉ L

=> this implies we have successfully broken the pumping lemma for the language, and hence that the adversary is wrong.

(Note: In this process, we may have to try many values of k, starting with k=0, and then 2, 3, .. so on, until wk ∉ L )

80

81 of 85

Using the Pumping Lemma

  • What WE do?

3. Using N, we construct our template string w

4. Demonstrate to the adversary, either through pumping up or down on w, that some string wk ∉ L�(this should happen regardless of w=xyz)

  • What the Adversary does?

1. Claims L is regular

2. Provides N

81

Note: We don’t have any control over N, except that it is positive.� We also don’t have any control over how to split w=xyz, � but xyz should respect the P/L conditions (1) and (2).

82 of 85

Example of using the Pumping Lemma to prove that a language is not regular

Let Leq = {w | w is a binary string with equal number of 1s and 0s}

  • Your Claim: Leq is not regular
  • Proof:
    • By contradiction, let Leq be regular
    • P/L constant should exist
      • Let N = that P/L constant
    • Consider input w = 0N1N(your choice for the template string)
    • By pumping lemma, we should be able to break w=xyz, such that:
      1. yε
      2. |xy|≤N
      3. For all k≥0, the string xykz is also in L

82

🡺 adv.

🡺 you

🡺 adv.

🡺you

Note: This N can be anything (need not necessarily be the #states in the DFA. � It’s the adversary’s choice.)

83 of 85

Proof…

    • Because |xy|≤N, xy should contain only 0s
      • (This and because yε, implies y=0+)
    • Therefore x can contain at most N-1 0s
    • Also, all the N 1s must be inside z
    • By (3), any string of the form xykz ∈ Leq for all k≥0
    • Case k=0: xz has at most N-1 0s but has N 1s
    • Therefore, xy0z ∉ Leq
    • This violates the P/L (a contradiction)

83

Another way of proving this will be to show that if �the #0s is arbitrarily pumped up (e.g., k=2),�then the #0s will become exceed the #1s

🡺 you

Template string w = 0N1N = 00 …. 011 … 1

N

N

Setting k=0 is �referred to as�“pumping down”

Setting k>1 is �referred to as�“pumping up”

84 of 85

Exercise 2

Prove L = {0n10n | n≥ 1} is not regular

Note: This n is not to be confused with the pumping lemma constant N. That can be different.

In other words, the above question is same as proving:

    • L = {0m10m | m≥ 1} is not regular

84

85 of 85

Example 3: Pumping Lemma

Claim: L = { 0i | i is a perfect square} is not regular

  • Proof:
    • By contradiction, let L be regular.
    • P/L should apply
    • Let N = P/L constant
    • Choose w=0N2
    • By pumping lemma, w=xyz satisfying all three rules
    • By rules (1) & (2), y has between 1 and N 0s
    • By rule (3), any string of the form xykz is also in L for all k≥0
    • Case k=0:
      • #zeros (xy0z) = #zeros (xyz) - #zeros (y)
      • N2 – N ≤ #zeros (xy0z) ≤ N2 - 1
      • (N-1)2 < N2 - N ≤ #zeros (xy0z) ≤ N2 - 1 < N2
      • xy0z ∉ L
      • But the above will complete the proof ONLY IF N>1.
      • … (proof contd.. Next slide)

85