Theory of Languages and Automata Chapter 1- Regular Languages & Finite State Automaton Sharif University of Technology
Finite State Automaton We begin with the simplest model of Computation, called finite state machine or finite automaton. are good models for computers with an extremely limited amount of memory. ➜ Embedded Systems Markov Chains are the probabilistic counterpart of Finite Automata Theory of Languages and Automata Prof. Movaghar
Simple Example Automatic door Front Pad Rear Pad Door Theory of Languages and Automata Prof. Movaghar
Simple Example (cont.) State Diagram State Transition Table Neither Front Rear Both Closed Open Theory of Languages and Automata Prof. Movaghar
Formal Definition A finite automaton is a 5-tuple (Q ,Σ ,δ ,q0, F), where Q is a finite set called states, Σ is a finite set called the alphabet, δ : Q ×Σ ⟶ Q is the transition function, q0 ∈ Q is the start state, and F ⊆ Q is the set of accept states. Theory of Languages and Automata Prof. Movaghar
Example M1 = (Q, Σ, δ, q0, F) , where Q = {q1, q2, q3}, Σ = {0,1}, δ is described as q1 is the start state, and F = {q2}. 1 q1 q2 q3 Theory of Languages and Automata Prof. Movaghar
Language of a Finite machine If A is the set of all strings that machine M accepts, we say that A is the language of machine M and write: L(M) = A. ✓ We say that M recognizes A or that M accepts A. Theory of Languages and Automata Prof. Movaghar
Example L(M1) = {w | w contains at least one 1 and even number of 0s follow the last 1}. Theory of Languages and Automata Prof. Movaghar
Example M4 accepts all strings that start and end with a or with b. Theory of Languages and Automata Prof. Movaghar
Formal Definition M = (Q, Σ, δ, q0, F) w = w1w2 … wn ∀i, wi ∈ Σ M accepts w ⇔ ∃ r0, r1, …, rn ∀i, ri ∈ Q r0 = q0 , δ(ri, wi+1) = ri+1, for i = 0, … , n-1, rn ∈ F. Theory of Languages and Automata Prof. Movaghar
Regular Language A language is called a regular language if some finite automaton recognizes it. Theory of Languages and Automata Prof. Movaghar
Example L (M5) = {w | the sum of the symbols in w is 0 modulo 3, except that <RESET> resets the count to 0}. ✓ As M5 recognizes this language, it is a regular language. Theory of Languages and Automata Prof. Movaghar
Designing Finite Automata Put yourself in the place of the machine and then see how you would go about performing the machine’s task. Design a finite automaton to recognize the regular language of all strings that contain the string 001 as a substring. Theory of Languages and Automata Prof. Movaghar
Designing Finite Automata (cont.) There are four possibilities: You haven’t just seen any symbols of the pattern, have just seen a 0, have just seen 00, or have seen the entire pattern 001. Theory of Languages and Automata Prof. Movaghar
The Regular Operations Let A and B be languages. We define the regular operations union, concatenation, and star as follows. Union: A∪B = {x | x∈A or x∈B}. Concatenation: A∘B = {xy | x∈A and y∈B }. Star: A* = {x1x2…xk | k ≥ 0 and each xi∈A }. Theory of Languages and Automata Prof. Movaghar
Closure Under Union THEOREM The class of regular languages is closed under the union operation. Theory of Languages and Automata Prof. Movaghar
Proof Let M1 = (Q1, Σ1, δ1, q1, F1) recognize A1, and Construct M = (Q, Σ, δ, q0, F) to recognize A1∪A2. Q = Q1 × Q2 Σ = Σ1 ∪ Σ2 δ((r1,r2),a) = (δ1(r1,a), δ2 (r2,a)). q0 is the pair (q1, q2). F is the set of pair in which either members in an accept state of M1 or M2. F = (F1× Q2 ) ∪ (Q1 × F2) F ≠ F1× F2 Theory of Languages and Automata Prof. Movaghar
Closure under Concatenation THEOREM The class of regular languages is closed under the concatenation operation. To prove this theorem we introduce a new technique called nondeterminism. Theory of Languages and Automata Prof. Movaghar
Nondeterminism In a nondeterministic machine, several choices may exit for the next state at any point. Nondeterminism is a generalization of determinism, so every deterministic finite automaton is automatically a nondeterministic finite automaton. Theory of Languages and Automata Prof. Movaghar
Differences between DFA & NFA First, very state of a DFA always has exactly one exiting transition arrow for each symbol in the alphabet. In an NFA a state may have zero, one, or more exiting arrows for each alphabet symbol. Second, in a DFA, labels on the transition arrows are symbols from the alphabet. An NFA may have arrows labeled with members of the alphabet or ε. Zero, one, or many arrows may exit from each state with the label ε. Theory of Languages and Automata Prof. Movaghar
Deterministic vs. Nondeterministic Theory of Languages and Automata Prof. Movaghar
Example Consider the computation of N1 on input 010110. Theory of Languages and Automata Prof. Movaghar
Example (cont.) Theory of Languages and Automata Prof. Movaghar
Formal Definition A nondeterministic finite automaton is a 5-tuple (Q ,Σ ,δ ,q0, F), where Q is a finite set of states, Σ is a finite alphabet, δ : Q ×Σε ⟶ P(Q) is the transition function, q0 ∈ Q is the start state, and F ⊆ Q is the set of accept states. Theory of Languages and Automata Prof. Movaghar
Example N1 = (Q, Σ, δ, q0, F) , where Q = {q1, q2, q3, q4}, Σ = {0,1}, δ is given as q1 is the start state, and F = {q4}. 1 ε q1 {q1} {q1,q2} ∅ q2 {q3} {q4} q3 q4 Theory of Languages and Automata Prof. Movaghar
Equivalence of NFAs & DFAs THEOREM Every nondeterministic finite automaton has an equivalent deterministic finite automaton. PROOF IDEA convert the NFA into an equivalent DFA that simulates the NFA. If k is the number of states of the NFA, so the DFA simulating the NFA will have 2 k states. Theory of Languages and Automata Prof. Movaghar
Proof Let N = (Q ,Σ ,δ ,q0, F) be the NFA recognizing A. We construct a DFA M =(Q' ,Σ' ,δ' ,q0', F’) recognizing A. let's first consider the easier case wherein N has no ε arrows. Q' = P(Q). q0’ = q0 . F' = {R∈ Q’ | R contains an accept state of N}. Theory of Languages and Automata Prof. Movaghar
Proof (cont.) Now we need to consider the ε arrows. for R ⊆ Q let E(R) = {q | q can be reached from R by traveling along 0 or more ε arrows}. Q' = P(Q). δ' (R,a) ={q∈ Q | q∈ E(δ(r,a)) for some r∈ R}. q0’ = E({q0}) . F' = {R∈ Q’ | R contains an accept state of N}. Theory of Languages and Automata Prof. Movaghar
Corollary A language is regular if and only if some nondeterministic finite automaton recognizes it. Theory of Languages and Automata Prof. Movaghar
Example D’s state set is {∅,{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}}. The start state is E({1}) = {1,3}. The accept states are {{1},{1,2},{1,3},{1,2,3}}. Theory of Languages and Automata Prof. Movaghar
Example (cont.) After removing unnecessary states Theory of Languages and Automata Prof. Movaghar
CLOSURE UNDER THE REGULAR OPERATIONS [Using NFA] Theory of Languages and Automata Prof. Movaghar
Closure Under Union The class of regular languages is closed under the Union operation. Let NFA1 recognize A1 and NFA2 recognize A2. Construct NFA3 to recognize A1 U A2. Theory of Languages and Automata Prof. Movaghar
Proof (cont.) Theory of Languages and Automata Prof. Movaghar
Closure Under Concatenation Operation The class of regular languages is closed under the concatenation operation. Theory of Languages and Automata Prof. Movaghar
Proof (cont.) Theory of Languages and Automata Prof. Movaghar
Closure Under Star operation The class of regular languages is closed under the star operation. We represent another NFA to recognize A*. Theory of Languages and Automata Prof. Movaghar
Proof (cont.) 𝑄= 𝑄 0 𝑈 𝑄 1 The states of N are the states of N1 plus a new start state. The state q0 is the new start state. 𝐹= 𝑞 0 𝑈 𝐹 1 The accept states are the old accept states plus the new start state. Define 𝛿 so that for any 𝑞∈𝑄 𝑎𝑛𝑑 𝑎∈ 𝜀 : Theory of Languages and Automata Prof. Movaghar
Regular Expression Say that R is a regular expression if R is: a for some a in the alphabet Σ 𝜀 ∅ 𝑅 1 𝑈 𝑅 2 , 𝑤ℎ𝑒𝑟𝑒 𝑅 1 𝑎𝑛𝑑 𝑅 2 𝑎𝑟𝑒 𝑟𝑒𝑔𝑢𝑙𝑎𝑟 𝑒𝑥𝑝. 𝑅 1 𝑜 𝑅 2 , 𝑤ℎ𝑒𝑟𝑒 𝑅 1 𝑎𝑛𝑑 𝑅 2 𝑎𝑟𝑒 𝑟𝑒𝑔𝑢𝑙𝑎𝑟 𝑒𝑥𝑝 𝑅 1 ∗ , 𝑤ℎ𝑒𝑟𝑒 𝑅 1 𝑎𝑛𝑑 𝑅 2 𝑎𝑟𝑒 𝑟𝑒𝑔𝑢𝑙𝑎𝑟 𝑒𝑥𝑝 Circular Definition? Theory of Languages and Automata Prof. Movaghar
Regular Expression Language Let R be a regular expression. L( R ) is the language that is defined by R: 𝑖𝑓 𝑅=𝑎 𝑓𝑜𝑟 𝑎∈Σ then L( R )={a} 𝑖𝑓 𝑅=𝜀 𝑡ℎ𝑒𝑛 𝐿 𝑅 ={𝜀} 𝑖𝑓 𝑅=∅ 𝑡ℎ𝑒𝑛 𝐿 𝑅 =∅ 𝑖𝑓 𝑅= 𝑅 1 𝑈 𝑅 2 𝑡ℎ𝑒𝑛 𝐿 𝑅 =𝐿 𝑅 1 𝑈𝐿 𝑅 2 𝑖𝑓 𝑅= 𝑅 1 ∩ 𝑅 2 𝑡ℎ𝑒𝑛 𝐿 𝑅 =𝐿 𝑅 1 ∩𝐿 𝑅 2 𝑖𝑓 𝑅= 𝑅 1 ∗ 𝑡ℎ𝑒𝑛 𝐿 𝑅 = 𝐿 𝑅 1 ∗ Theory of Languages and Automata Prof. Movaghar
Examples(cont.) 1. 0∪𝜀 1 ∗ = 01 ∗ ∪ 1 ∗ 2. Σ ∗ 1 Σ ∗ ={𝑤|𝑤 𝑐𝑜𝑛𝑡𝑎𝑖𝑛𝑠 𝑎𝑡 𝑙𝑒𝑎𝑠𝑡 𝑜𝑛𝑒 1} 3. 0 ∗ 10 ∗ ={𝑤|𝑤 𝑐𝑜𝑛𝑡𝑎𝑖𝑛𝑠 𝑎 𝑠𝑖𝑛𝑔𝑙𝑒 1} 4. Σ ∗ 001 Σ ∗ ={𝑤|𝑤 𝑐𝑜𝑛𝑡𝑎𝑖𝑛𝑠 001 𝑎𝑠 𝑎 𝑠𝑢𝑏𝑠𝑡𝑟𝑖𝑛𝑔} 5. 01𝑈10={01,10} 6. (ΣΣ) ∗ ={𝑤|𝑤 𝑖𝑠 𝑎 𝑠𝑡𝑟𝑖𝑛𝑔 𝑜𝑓 𝑒𝑣𝑒𝑛 𝑙𝑒𝑛𝑔𝑡ℎ} 7. (ΣΣΣ) ∗ ={𝑤|𝑡ℎ𝑒 𝑙𝑒𝑛𝑡𝑔ℎ 𝑜𝑓 𝑤 𝑖𝑠 𝑎 𝑚𝑢𝑙𝑡𝑖𝑝𝑙𝑒 𝑜𝑓 3} 8 (ΣΣΣΣ) ∗ ={𝑤|𝑡ℎ𝑒 𝑙𝑒𝑛𝑡𝑔ℎ 𝑜𝑓 𝑤 𝑖𝑠 𝑎 𝑚𝑢𝑙𝑡𝑖𝑝𝑙𝑒 𝑜𝑓 4} 9. 0∪𝜀 1 ∗ = 01 ∗ ∪ 1 ∗ 10. 0∪𝜀 (1∪𝜀) ={𝜀,0,1,01} 11. 1 ∗ ∅=∅ 12. ∅ ∗ ={𝜀} Theory of Languages and Automata Prof. Movaghar
Equivalence of DFA and Regular Expression A language is regular if and only if some regular expression describes it. Lemma: If a language is described by a regular expression, then it is regular. If a language is regular, then it is described by a regular expression. Theory of Languages and Automata Prof. Movaghar
Building an NFA from the Regular Expression We consider the six cases in the formal definition of regular expressions Theory of Languages and Automata Prof. Movaghar
Examples Theory of Languages and Automata Prof. Movaghar
Other direction of the proof We need to show that, if a language A is regular, a regular expression describes it! First we show how to convert DFAs into GNFAs, and then GNFAs into regular expressions. We can easily convert a DFA into a GNFA in the special form. 3-state DFA 5-state GNFA 4-state GNFA Regular Expression 2-state GNFA 3-state GNFA Theory of Languages and Automata Prof. Movaghar
Formal Definition A generalized nondeterministic finite automaton is a 5-tuple, a 5-tuple (Q ,Σ ,δ ,qstart , qaccept), where Q is a finite set called states, Σ is a the input alphabet, δ : (𝑄−{ 𝑞 𝑎𝑐𝑐𝑒𝑝𝑡 }) ×(Q−{ 𝑄 𝑠𝑡𝑎𝑟𝑡 } ⟶𝑅 is the transition function, qstart is the start state, and qaccept is the accept state. Theory of Languages and Automata Prof. Movaghar
Assumptions For convenience we require that GNFAs always have a special form that meets the following conditions: The start state has transition arrows going to every other state but no arrows coming in from any other state. There is only a single accept state, and it has arrows coming in from every other state but no arrows going to any other state. Furthermore, the accept state is not the same as the start state. Except for the start and accept states, one arrow goes from every state to every other state and also from each state to itself. Theory of Languages and Automata Prof. Movaghar
How to Eliminate a State? Theory of Languages and Automata Prof. Movaghar
Example Theory of Languages and Automata Prof. Movaghar
Example Theory of Languages and Automata Prof. Movaghar
Grammar A grammar G is a 4-tuple G = (V, Σ, R, S) where: V is a finite set of variables, Σ is a finite, disjoint from V, of terminals, R is a finite set of rules, S is the start variable. Theory of Languages and Automata Prof. Movaghar
Rule A rule is of the form 𝑥→𝑦 where 𝑥∈(𝑉∪Σ)+ and 𝑦∈(𝑉∪Σ)∗ The rules are applied in the following manner: given a string w of the form w = uxv, We say that the rule x → y is applicable to this string, and we may use it to replace x with y, thereby obtaining a new string z = uyv, This is written as 𝑤⇒𝑧. Theory of Languages and Automata Prof. Movaghar
Derivation If 𝑊 1 ⇒ 𝑊 2 ⇒ …⇒ 𝑊 𝑛 we say that W1 derives Wn and write 𝑊 1 ⇒ 𝑊 2 ⇒ …⇒ 𝑊 𝑛 we say that W1 derives Wn and write 𝑊 1 ⇒ ∗ 𝑊 𝑛 Thus, we always have 𝑊 ⇒ ∗ 𝑊 Theory of Languages and Automata Prof. Movaghar
Language of a Grammar Let G = (V, Σ, R, S) be a grammar. Then, the set 𝑳 𝑮 ={𝑾∈ 𝑻 ∗ :𝑺 ⇒ ∗ 𝑾} is the language generated by G. Theory of Languages and Automata Prof. Movaghar
Example Consider the grammar G = ({S}, {a,b}, P, S} with P given by S → aSb S → ε Then 𝑆→𝑎𝑆𝑏→𝑎𝑎𝑆𝑏𝑏→𝑎𝑎𝑏𝑏 So we can write 𝑆 ⇒ ∗ 𝑎𝑎𝑏𝑏 Then, L(G) = {an bn: n ≥ 0} Theory of Languages and Automata Prof. Movaghar
A Notation for Grammars Consider the grammar G = ({S}, {a,b}, P, S} with P given by S → aSb S → ε The above grammar is usually written as: G: S → aSb | ε Theory of Languages and Automata Prof. Movaghar
Regular Grammar A grammar G = (V, Σ, R, S) is said to be right-linear if all rules are of the form A → xB A → x Where A, B V, and X Σ*. A grammar is said to be left-linear if all rules are of the form A → Bx A regular grammar is one that is either right-linear or left-linear. Theory of Languages and Automata Prof. Movaghar
L(G) is a regular language. Theorem Let G = (V, Σ, R, S) be a right-linear grammar. Then: L(G) is a regular language. Theory of Languages and Automata Prof. Movaghar
Example Construct a NFA that accepts the language generated by the grammar V0 → aV1 V1 → abV0 | b a b V0 V1 Vf a b V2 Theory of Languages and Automata Prof. Movaghar
Theorem Let L be a regular language on the alphabet Σ. Then: There exists a right-linear grammar G = (V, Σ, R, S) Such that L = L(G). Theory of Languages and Automata Prof. Movaghar
Theorem Theorem A language is regular if and only if there exists a left-linear grammar G such that L = L(G). Outline of the proof: Given any left-linear grammar with rules of the form A → Bx A → x We can construct a right-linear Ĝ by replacing every such rule of G with A → xRB A → xR We have L(G) = L(Ĝ)R . Theory of Languages and Automata Prof. Movaghar
Theorem A language L is regular if and only if there exists a regular grammar G such that L = L(G). 𝐿 𝑖𝑠 𝒓𝒆𝒈𝒖𝒍𝒂𝒓 ↔∃ 𝐺:𝐿=𝐿 𝐺 ;𝐺 𝑖𝑠 𝒓𝒆𝒈𝒖𝒍𝒂𝒓 Theory of Languages and Automata Prof. Movaghar