形式语言与自动机 04 Regular Expressions

本文介绍了正则表达式的定义及其与有限自动机之间的等价性,探讨了正则语言的闭包属性和决策属性,并展示了如何通过构造转换实现正则表达式到有限自动机及反之的转化。

Regular Expressions

Definition

RE’ s: Introduction

  • Regular expressions describe languages by an algebra
  • They describe exactly the regular languages
  • If E is a regular expression, then L(E) is the language it defines
  • We’ll describe RE’ s and their languages recursively

Operations on Languages

Union

{01,111,10}∪{00,01}={01,111,10,00}\{01,111,10\} \cup \{00,01\} = \{01,111,10,00\}{01,111,10}{00,01}={01,111,10,00}

Concatenation

  • The concatenation of languages LLL and MMM is denoted LMLMLM
  • It contains every string wxwxwx such that www is in LLL and xxx is in MMM

{01,111,10}{00,01}={0100,0101,11100,11101,1000,1001}\{ 01,111,10\} \{00,01\} = \{0100,0101,11100,11101,1000,1001\}{01,111,10}{00,01}={0100,0101,11100,11101,1000,1001}

Kleene Star

  • L∗={ϵ}∪L∪LL∪LLL...}L^* = \{\epsilon \} \cup L \cup LL \cup LLL ...\}L={ϵ}LLLLLL...}

{0,10}∗=ϵ∪{0,10,00,010,100,1010,...}\{0,10\}^* = \epsilon \cup \{0,10,00,010,100,1010,...\}{0,10}=ϵ{0,10,00,010,100,1010,...}

RE’ s Definition

  • Basis 1: If aaa is any symbol. then aaa is a RE, and L(aaa) = {a}

    • Note: {a} is the language containing one string, and that string is of length 1
  • Basic 2: ϵ\epsilonϵ is a RE, and L(ϵ\epsilonϵ) = {ϵ\epsilonϵ}

  • Basic 3: ∅\emptyset is a RE, and L(∅\emptyset) = ∅\emptyset

  • Induction 1: If E1E_1E1 and E2E_2E2 are regular expressions, then E1+E2E_1 + E_2E1+E2 is a regular expression, and L(E1+E2E_1+E_2E1+E2) = L(E1E_1E1) + L(E2E_2E2)

  • Induction 2: If E1E_1E1 and E2E_2E2 are regular expressions, then E1E2E_1E_2E1E2 is a regular expression, and L(E1E2E_1E_2E1E2) = L(E1E_1E1)L(E2E_2E2)

  • Induction 3: If EEE is a RERERE, then E∗E^*E is a RERERE, and L(E∗E^*E) = (L(EEE))∗^*

Precedence of Operators

  • L(01) = {01}
  • L(01+0) = {01,0}
  • L(0(1+0)) = {01,00}
  • L(0∗0^*0) = {ϵ,0,00,000,...\epsilon,0,00,000,...ϵ,0,00,000,...}
  • L((0+10)∗^* (ϵ\epsilonϵ + 1)) = all strings of 0’ s and 1’ s without two consecutive 1’ s

Equivalence to Finite Automata

We need to show that for every RE, there is a finite automaton that accepts

And for every finite automaton, there is a RE defining its language

Converting a RE to an ϵ\epsilonϵ-NFA

  • Proof is an induction on the number of operators(+,concatenation,*) in RE
Basic

Union

Concatenation
Closure

DFA to RE

k-Paths
  • A k-path is a path through the graph of the DFA that goes through no state numbered higher than k
  • n-paths are unrestricted

RE is the union of RE’ s for the n-paths from the start state to each final state

  • **Basis: ** k=0; only arcs or a node by itself
  • **Induction: ** construct RE’ s for paths allowed to pass through state k from paths allowed only up to k-1
k-Path Induction
  • Let RijkR_{ij}^kRijk be the regular expression for the set of labels of k-paths from state i to state j
  • Basis: k=0. Rij0R_{ij}^0Rij0 = sum of labels of arc from i to j
    • ∅\emptyset if no such arc
    • But add ∈\in if i=j
k-Path Inductive Case

Rijk=Rijk−1+Rikk−1(Rkkk−1)∗Rkjk−1R_{ij}^k = R_{ij}^{k-1} + R_{ik}^{k-1}(R_{kk}^{k-1})^*R_{kj}^{k-1}Rijk=Rijk1+Rikk1(Rkkk1)Rkjk1

Final Step
  • The RE with the same language as the DFA is the sum (union) of Rijn,R_{ij}^n,Rijn, where:
    1. n is the number of states; i.e., paths are unconstrained
    2. i is the start state
    3. j is one of the final states
Summary

Each of the three types of automata (DFA,NFA,ϵ\epsilonϵ-NFA) we discussed, and regular expressions as well, define exactly the same set of languages: the regular languages

Algebraic Laws for RE’ s

Identities and Annihilators
  • ∅\empty is the identity for +
    • R + ∅\empty = R
  • ϵ\epsilonϵ is the identity for concatenation
    • ϵR=Rϵ=R\epsilon R = R \epsilon = RϵR=Rϵ=R
  • ∅\emptyset is the annihilator for concatenation
    • ∅R=R∅=∅\emptyset R = R \emptyset = \emptysetR=R=

Decision Properties of Regular Languages

General Discussion of “Properties”

Properties of Language Classes

  • A language class is a set of languages
  • Language classes have two important kinds of properties
    • Decision properties
    • Closure properties

Closure Properties

A closure property of a language class says that given languages in the class, an operation(e.g) produces another language in the same class

example: the regular language are closed under union, concatenation and Kleene closure

Representation of Languages

formal or informal

Decision Properties

  • A decision property for a class of languages corresponds an algorithm that takes a formal description of a language and tell whether or not some property holds
  • Example: Is language L empty

Why Decision Properties

We might want a “smallest” representation for a language, a minimum-state DFA or a shortest RE

The Emptiness Problem

The Infiniteness Problem

  • Is a given regular language infinite?

  • **Key idea: ** if the DFA has n states, and the language contains any string of length n or more, then the language is inifinite

  • Otherwise the language is surely finite

  • **Second key idea: ** if there is a string of length ≥\ge n (= number of states) in L, then there is a string of length between n and 2n - 1

Proof

  • Test for membership all strings of length between n and 2n -1
    • If any are accepted, then infinite, else finite
  • A terrible algorithm
  • **Better: ** find cycles between the start state and a final state

Finding Cycles

  1. Eliminate states not reachable from the start state
  2. Eliminate states that do not reach a final state
  3. Test if then remaining transition graph has any cycle

The Pumping Lemma

泵引理

Statement of the Pumping Lemma

For every regular language L,

​ There is an integer n, such that

​ For every string w in L of length ≥\ge n

​ We can write w = xyz such that:

  1. |xy| ≤\le n
  2. |y| > 0
  3. For all i ≥\ge 0, xyi^iiz is in L
Example: Use of Pumping Lemma

泵引理帮助我们判断一些无穷语言是否为正则语言

{0n1n∣k≥1}\{0^n 1^n | k \ge 1 \}{0n1nk1} is not a regular language

Proof

  • Let w = 0n1n0^n1^n0n1n, then write x = xyz, and y consists of 0’ s, y ≠ϵ\ne \epsilon=ϵ

  • But xyyz would be in L ,thus impossible

Decision Property: Equivalence

DFA L and M

  • Let these DFA has sets of states Q and R

  • Product DFA has set of states Q x R

Decision Property: Containment

How do you define the final states [q.r] of the product so its language is empty iff L ⊆\subseteq M

**Answer: ** q is final; r is not

The Minimum-State DFA for a Regular Language

**Basis: ** Mark pairs with exactly one final state

**Induction: ** mark [q,r] if for some input symbol a, [δ(q,a),δ(r,a)\delta(q,a),\delta(r,a)δ(q,a),δ(r,a)] is marked

After no more marks are possible, the unmarked pairs are equivalent and can be into one state

Constructing the Minimum-State DFA
  • Suppose q1,...,qkq_1,...,q_kq1,...,qk are indistinguishable states
  • Replace them by one representative state q
  • Then δ(q1,a),...δ(qk,a)\delta(q_1,a),...\delta(q_k,a)δ(q1,a),...δ(qk,a) are all indistinguishable states.

Example

Eliminating Unreachable States

The proof involves minimizing the DFA we derived with the hypothetical better DFA

Proof: No unrelated, smaller DFA

IH: every state q of A is indistinguishable from some state of B

Proof

**Basis: ** Start states of A and B are indistinguishable

**Induction: ** Suppose w = xa, is a shortest string getting A to q

By IH, x gets to A to some state r that is indistinguishable from some state of B

Then δA(r,a)=q\delta_A(r,a) = qδA(r,a)=q is indistinguishable from δB(p,a)\delta_B(p,a)δB(p,a)

However, two states of A cannot be indistingruishable from the same state of B, thus, B has at least as many states as A

Closure Properties of Regular Languages

Union

  • If L and M are regular languages, so is L ∪\cup M

Intersection

  • If L and M are regular languages, then so is L ∩\cap M
  • Proof: Construct C, the product automaton of A and B

Difference

  • If L and M are regular languages, then so is L - M
  • Proof: Construct product automaton

Reversal

Proof: Let E be a regular expression for L, We show how to reverse E, to provide a regular expression ER^RR for LR^RR

**Basis: ** If E is a symbol a, ϵ\epsilonϵ, or ∅\emptyset, then ER^RR = E

**Induction: ** If E is

  • F + G, then E^R = F^R + G^R
  • FG, then E^R = GR^RR FR^RR
  • F∗^*, then E^R = (FR^RR)∗^*

Homomorphisms

同态

  • A homomorphism on an alphabet is a function that gives a string for each symbol in that alphabet
  • Example: h(0) = ab; h(1) = ϵ\epsilonϵ
  • Extend to strings by h(a1..._1...1...an_nn) = h(a1_11)…h(an_nn)
  • Example: h(01010) = ababab

Closure Under Homomorphism

  • If L is a regular language, and h is a homomorphism on its alphabet, then h(L) = {h(w)|w is in L} is also a regular language
  • Proof: Let E be a regular language expression for L
  • Apply h to each symbol in E
  • Language of resulting RE is h(L)

Inverse Homomorphisms

  • h$^{-1} = $ {w | h(w) is in L}

Example Inverse Homomorphisms

  • Let h(0) = ab; h(1) = ϵ\epsilonϵ
  • Let L = {abab,baba}
  • h−1^{-1}1(L) = L(1∗01∗01∗1^*01^*01^*10101)

Closure Proof

  • Start with a DFA A for L
  • Construct a DFA B for h−1^{-1}1(L) with
    • the same set of states
    • the same start
    • the same final
    • Input alphabet = the symbols to which homomorphism h applies

δB(q,a)=δA(q,h(a))\delta_B(q,a) = \delta_A(q,h(a))δB(q,a)=δA(q,h(a))

  • An induction on |w| (omitted) shows that δ(q0,w)=δA(q0,h(w))\delta(q_0,w) = \delta_A(q_0,h(w))δ(q0,w)=δA(q0,h(w))
  • Thus, B accepts w if and only if A accepts h(w)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值