Mobile Computing and Software Engineering p. 1/3 Formal Languages and Automata Chapter 3 Regular languages and Regular Grammars Chuan-Ming Liu cmliu@csie.ntut.edu.tw Department of Computer Science and Information Engineering National Taipei University of Technology Taipei, TAIWAN
Mobile Computing and Software Engineering p. 2/3 Contents Regular Expressions Connection between Regular Expressions and Regular Languages Regular Grammars
Mobile Computing and Software Engineering p. 3/3 Regular Expressions Let Σ be a given alphabet. Then, 1, λ, and a Σ are all regular expressions. These are called primitive regular expression. 2 if r 1 and r 2 are regular expression, so are r 1 + r 2,r 1 r 2,r 1, and (r 1). 3 A string is a regular expression if and only if it can be derived from the primitive regular expressions by a finite number of applications of the rules in 2. Note that we use + to denote union, for concatenation, and for star-closure.
Mobile Computing and Software Engineering p. 4/3 Example 1 For Σ = {a, b, c}, the string is a regular expression. (a + b c) (c + )
Regular Expression Languages Let r be a regular expression. Then, the language L(r) is defined by the following rules: 1. is a regular expression denoting the empty set, 2. λ is a regular expression denoting {λ}, 3. For every a Σ, a is a regular expression denoting {a}. If r 1 and r 2 are regular expressions, then 4. L(r 1 + r 2 ) = L(r 1 ) L(r 2 ), 5. L(r 1 L 2 ) = L(r 1 )L(r 2 ), 6. L((r 1 )) = L(r 1 ), 7. L(r 1) = (L(r 1 )). Mobile Computing and Software Engineering p. 5/3
Mobile Computing and Software Engineering p. 6/3 Example 2 Exhibit the language L(a (a + b)) in set notation. L(a (a + b)) = L(a )L(a + b) = (L(a)) (L(a) L(b)) = {λ,a,aa,aaa,...}{a,b} = {a,aa,aaa,...,b,ab,aab,...}.
Mobile Computing and Software Engineering p. 7/3 Example 3 Problem: Find the language generated by the following regular expression where Σ = {a,b}. r = (a + b) (a + ab) Solution: It denotes the language L(r) = {a,bb,aa,abb,ba, bbb,...}.
Mobile Computing and Software Engineering p. 8/3 Example 4 Problem: Describe the language which has the following regular expression r = (aa) (bb) b. Solution: It denotes the set of all strings with an even number of a s followed by an odd number of b s; that is, L(r) = {a 2n b 2m+1 : n 0,m 0}.
Mobile Computing and Software Engineering p. 9/3 Example 5 Problem: For Σ = {0, 1}, give a regular expression r such that L(r) = {w Σ : w has at least one pair of Solution: consecutive zeros}. r = (0 + 1) 00(0 + 1).
Mobile Computing and Software Engineering p. 10/3 Example 6 Problem: Find a regular expression for the language L = {w {0, 1} : w has no pair of consecutive zeros}. Solution: r = (1 011 ) (0 + λ) + 1 (0 + λ). An alternative shorter expression r = (1 + 01) (0 + λ). Note: Two regular expressions are equivalent if they denote the same language.
Mobile Computing and Software Engineering p. 11/3 Contents Regular Expressions Connection between Regular Expressions and Regular Languages Regular Grammars
Mobile Computing and Software Engineering p. 12/3 Regular Expressions and Languages The two concepts of regular expression and regular language are essentially the same: for every regular language there is a regular expression. for every regular expression there is a regular language. Note: a regular language a dfa accepts it an nfa accepts it
Mobile Computing and Software Engineering p. 13/3 Theorem Let r be a regular expression. Then there exists some nondeterministic finite automaton that accepts L(r). Consequently, L(r) is a regular language. Proof: To show L(r) is a regular language, we need to find an nfa which accepts L(r). We will use the definition of the languages associated with regular expressions. Note: The basic idea is to construct an nfa which mimics the process of producing L(r) for a given regular expression.
Mobile Computing and Software Engineering p. 14/3 Constructing an Nfa (1) (1) Consider the rules for the termination conditions of the recursion on the regular expression. For each of the simple regular expressions, λ, and a Σ, we can easily construct an nfa which accepts it. : λ: a:
Mobile Computing and Software Engineering p. 15/3 Constructing an Nfa (2) (2) We now consider the recursion part of the definition for L(r). Let r 1 and r 2 be two arbitrary regular expressions and suppose automata M(r 1 ) and M(r 2 ) accept L(r 1 ) and L(r 2 ), respectively. Then, using the state diagrams, we can have the following conceptual state diagrams:
Mobile Computing and Software Engineering p. 16/3 Constructing an Nfa (3) (3) Construct the automata for the regular expressions r 1 + r 2, r 1 r 2, and r 1: For L(r 1 + r 2 ): M(r 1 ) M(r 2 ) Note: The original final states are dismissed.
Mobile Computing and Software Engineering p. 17/3 Constructing an Nfa (4) For L(r 1 r 2 ):
Mobile Computing and Software Engineering p. 18/3 Constructing an Nfa (5) For L(r 1): Using these fundamental components, we can build automata for arbitrary complex regular expressions.
Example 7 Problem: Find an nfa that accepts L(r), where Solution: r = (a + bb) (ba + λ) L(a + bb) L(ba + λ) = L((a + bb) (ba + λ)) Mobile Computing and Software Engineering p. 19/3
Mobile Computing and Software Engineering p. 20/3 Generalized Transition Graphs We now consider that for every regular language, there exists a corresponding regular expression. Definition: A generalized transition graph (GTG) is a transition graph whose edges are labeled with regular expressions; otherwise, it is the same as the usual transition graph. Note: The strings of the labels are subsets of the language accepted by the GTG.
Mobile Computing and Software Engineering p. 21/3 Example GTG L(a + a (a + b)c )
Mobile Computing and Software Engineering p. 22/3 Complete GTG s Observation: The state diagram of an nfa is a GTG. = for every regular language, there exists a GTG that accepts it. Note: The language accepted by an nfa is regular. Hence, the observation says, in some sense, that the language accepted by a GTG is regular. Definition: A complete GTG is a graph in which all edges are present.
Mobile Computing and Software Engineering p. 23/3 Example Complete GTG (a) A GTG (b) The complete GTG of (a)
Mobile Computing and Software Engineering p. 24/3 Two State Complete GTG s The regular expression r = r 1 r 2 (r 4 + r 3 r 1r 2 ) covers all possible paths and so is the correct regular expression associated with the graph.
Mobile Computing and Software Engineering p. 25/3 Example 8 Note: When a GTG has more than two states, we can find an equivalent GTG by renaming one state at a time. =
Mobile Computing and Software Engineering p. 26/3 Example 9 Problem:Find a regular expression for the language L = {w {a,b} : n a (w) is even and n b (w) is odd}, where n a (w) and n b (w) are the number of occurrences of a and b in w respectively.
Mobile Computing and Software Engineering p. 27/3 Theorem Let L be a regular language. Then there exists a regular expression r such that L = L(r). Proof: L is regular an nfa accepting it a GTG to present it can find a regular expression for it.
Mobile Computing and Software Engineering p. 28/3 Contents Regular Expressions Connection between Regular Expressions and Regular Languages Regular Grammars
Mobile Computing and Software Engineering p. 29/3 Regular Grammars We have learned that a regular language can be represented by an nfa (or a dfa) a regular expression We now discuss a third way to describe regular languages grammars
Mobile Computing and Software Engineering p. 30/3 Definitions A grammar G = (V,T,s,P) is said to be right-linear (left-linear) if all productions are of the form A xb (A Bx) or A x, where A,B V and x T. A regular grammar (or type-3 grammar) is one that is either right-linear or left-linear. Note: At most one variable appears on the right side of any production.
Mobile Computing and Software Engineering p. 31/3 Example 10 and 11 1. The grammar G 1 = ({S}, {a,b},s,p 1 ) with P 1 given as S abs a is right-linear. 2. The grammar G 2 = ({S,S 1,S 2 }, {a,b},s,p 2 ) with productions is left-linear. S S 1 ab, S 1 S 1 ab S 2, S 2 a,
Mobile Computing and Software Engineering p. 32/3 Example 12 The grammar G = ({S,A,B}, {a,b},s,p) with productions is not regular. S A, A ab λ, B Ab, Note: The grammar is an instance of a linear grammar.
Mobile Computing and Software Engineering p. 33/3 Linear Grammar Definition: A linear grammar is a grammar in which at most one variable can occur on the right side of any production, without restriction on the position of this variable. Note: A regular grammar is always linear, but not all linear grammars are regular.
Mobile Computing and Software Engineering p. 34/3 Theorem Let G = (V, T, S, P) be a right-linear grammar. Then L(G) is a regular language. proof: To prove this, we consider to construct an nfa M that mimics the derivations of a right-linear grammar: Let M = (Q, Σ,δ,q 0,F) where Σ = T, q 0 = S, 1. Q= the set of variables in all productions, plus an additional state A; { {S,A} if S λ exists; 2. F =, {A} O.W. 3. δ(b,a) contains all C s if B ac and A δ(b,a) if B C.
Mobile Computing and Software Engineering p. 35/3 Example 13 Problem: Construct a finite automaton that accepts the language generated by the following grammar Solution: V 0 = a 1 V 1 V 1 = abv 0 b
Mobile Computing and Software Engineering p. 36/3 Theorem If L is a regular language on the alphabet Σ, then there exists a right-linear grammar G = (V, Σ, S, P). proof: W.L.O.G., let M = (Q, Σ,δ,q 0,F) be a dfa that accepts L. We construct a right-linear grammar G = (Q, Σ,q 0,P) as follows. 1. B ac P if δ(b,a) = C, 2. B C P if δ(b,a) = C and C F. We then need to show that (1) T(M) L(G) and (2) L(G) T(M).
Mobile Computing and Software Engineering p. 37/3 Example 14 Problem: Construct a right-linear grammar for L(aab a). Solution: G = ({q 0,q 1,q 2,q 3 }, {a,b},q 0,P ) where P includes: 1. q 0 aq 1, 2. q 1 aq 2, 3. q 2 bq 2,
Mobile Computing and Software Engineering p. 38/3 Theorems L is regular there exists a left-linear grammar G such that L = L(G). L is regular there exists a regular grammar G such that L = L(G).