03 Regular
03 Regular
Grammars
Week 3
1
Topics
• Regular Expressions
• Connection between Regular Expressions and Regular Languages
• Regular Grammars
2
Regular Expressions
3
Regular Expression Formal Definition
De nition 3.1
4
fi
fi
ff
Regular Expressions
Example 3.1
5
Languages Associated w/ RegExp
De nition 3.2
6
fi
fi
Languages Associated w/ RegExp
De nition 3.2
• These are the rules to recursively reduce languages into simpler forms.
7
fi
Languages Associated w/ RegExp
Example 3.2
• Rules 4~7.
• L(r1 + r2) = L(r1) ∪ L(r2) , L(r1 ⋅ r2) = L(r1)L(r2)
• L((r1)) = L(r1), L(r*
1
) = (L(r1 ))*
• Consider a ⋅ b + c.
• r1 = a ⋅ b, r2 = c ➞ L(a ⋅ b + c) = {ab, c}.
• r1 = a, r2 = b + c ➞ L(a ⋅ b + c) = {ab, ac}.
• Precedence rules: * > ⋅ > + .
9
Ambiguity in the Rules
Example 3.3
10
Ambiguity in the Rules
Example 3.4
• The expression
• r = (aa)*(bb)*b
• denotes the set of all strings with an even number of a's followed by an odd
number of b's.
2n 2m+1
• L(r) = {a b : n ≥ 0,m ≥ 0}.
• Get used to these notations.
11
Identify Regular Expressions
Example 3.5
12
Identify Regular Expressions
Example 3.6
14
fi
Identifying Regular Expressions
Exercise 6
• Show that r = (1 + 01)*(0 + 1*) also denotes the language in Example 3.6.
• Proof. It's actually quite simple.
• Note that 1* is a set of strings repeating zero or more 1s.
• So it includes λ in it.
• Extra 1s on the 2nd part can be attached to the 1st part.
• Can you nd a RegExp for a language with exactly one 00? (Exercise 18).
15
fi
Connection between Regular
Expressions and Languages
16
RegExps Denote Regular Languages
Theorem 3.1
17
RegExps Denote Regular Languages
Figure 3.1
Theorem 3.1 Proof
• We are using the argument that every NFA has an equivalent NFA w/ one nal
state, from Exercise 9, Section 2.3.
• Schematic representation.
Figure 3.2
18
fi
RegExps Denote Regular Languages
Theorem 3.1 Proof
• With M(r1) and M(r2), we can construct automata for the regular expressions
r1 + r2, r1r2, and r*.
Figure 3.4
Figure 3.5
Figure 3.3
19
RegExps Denote Regular Languages
Example 3.7
20
RegExps Denote Regular Languages
Example 3.7
• Putting the two automata together for concatenation gives us the nal
solution.
• r = (a + bb)*(ba* + λ).
Figure 3.7
21
fi
Generalized Transition Graphs
• A Generalized Transition Graph (GTG) is a transition graph whose edges are labeled with
regular expressions.
• The strings denoted by such expressions are a subset of the language accepted by the GTG.
• The union of such strings gives the full language.
• A complete GTG is a GTG with all edges are present.
• A graph of an NFA can be considered as a generalized transition graph too.
22
fi
Generalized Transition Graphs
• Hence for every regular language, there exists a GTG that accepts it.
• Conversely, every language accepted by a GTG is regular.
• Every walk in a GTG is a regular expression.
• By Theorem 3.1, a language represented by the regular expression is
regular.
23
fi
Generalized Transition Graphs
Example 3.8
24
Complete GTGs
Example 3.9
Figure 3.9
25
Complete GTGs
Example 3.10
• Once all edges are created, we can remove q2 and associated edges.
• Then we can obtain an equivalent graph in Figure 3.12.
27
Complete GTGs
Procedure: nfa-to-rex
• Then we apply the above expression to get the nal regular expression.
28
fi
Complete GTGs
Procedure: nfa-to-rex
1. Start with an NFA with states q0, q1, ⋯, qn, and a single nal state, distinct
from its initial state.
2. Convert the NFA into a complete GTG. Let rij stands for the label of (qi, qj).
3. If the GTG has only two states, with qi as its initial state and qj its nal state,
its associated regular expression is
• r = r* r (r
ii ij jj
+ r r* r
ji ii ij)* (3.2)
29
fi
fi
Complete GTGs
Procedure: nfa-to-rex
4. If the GTG has three states, with initial state qi, nal state qj, and third state qk, introduce new edges,
labeled
• rpq + rpkr*
kk
rkq (3.3)
• for p = i, j, q = i, j. When it's done, remove vertex qk and its associated edges.
5. If the GTG has four or more states, pick a state qk to be removed.
• We can convert this NFA to a complete GTG and apply procedure nfa-to-rex,
obtaining the required regular expression.
31
fi
Regular Expression for Simple Patterns
Exercise 17
<digit> <digit>
➞ ➞ e <sign> <digit>
q6
<sign>, <p>
e, <p> <digit>
➞ <sign>, e, <p>
q8 q7
<sign>, e, <p>
➞
<digit>
<sign>, <digit>, e, <p>
• Applying nfa-to-rex. q0
s
q1
d
q2
p
q3
d
q4
+(p + sp + (d + sd)d*p)dd*e)sd)d* s, d, e, p
d
34
Right- and Left-Linear Grammars
De nition 3.3
• G1: S → abS | a
• S ⇒ abS ⇒ ababS ⇒ ababa is a derivation with G1.
• L(G1) is the language denoted by the regular expression r = (ab)*a.
• G2: S → S1ab, S1 → S1ab | S2, S2 → a
• S ⇒ S1ab ⇒ S1abab ⇒ S2abab ⇒ aabab
• L(G2) is the regular language L(aab(ab)*).
37
Regular Grammars
Example 3.14
38
Regular Grammars and Regular Languages
39
Right-Linear Grammars generate Regular Languages
Theorem 3.3
40
Right-Linear Grammars generate Regular Languages
Theorem 3.3
• Proof: Assume that V = {V0, V1, ⋯}, S = V0, and we have productions of
the form V0 → v1Vi, Vi → v2Vj, ⋯ or Vn → vl, ⋯.
41
Right-Linear Grammars generate Regular Languages
Theorem 3.3
• With the initial state V0, there will be a non- nal state labeled Vi for each variable.
• For each production Vi → a1a2⋯amVj, we can consider transitions to connect Vi and Vj.
• δ*(Vi, a1a2⋯am) = Vj.
• Similarly, for each production Vi → a1a2⋯am,
• δ*(Vi, a1a2⋯am) = Vf, where Vf is a nal state.
Figure 3.16
42
fi
fi
Right-Linear Grammars generate Regular Languages
Theorem 3.3
43
fi
Right-Linear Grammars generate Regular Languages
Theorem 3.3
44
Right-Linear Grammars generate Regular Languages
Example 3.15
• V0 → aV1,
• V1 → abV0 | b, where V0 is the start variable.
• The language generated by the grammar and accepted by
the automaton is the regular language L((aab)*ab).
45
fi
Right-Linear Grammars for Regular Languages
46
fi
Right-Linear Grammars for Regular Languages
Theorem 3.4
• Proof: Let M = (Q, Σ, δ, q0, F) be a DFA that accepts L, and assume that
Q = {q0, q1, ⋯, qn} and Σ = {a1, a2, ⋯, am}.
• Construct the right-linear grammar G with V = {q0, q1, ⋯, qn} and S = q0.
• For each transition δ(qi, aj) = qk of M, we put the production qi → ajqk in P.
• If qk is in F, we add qk → λ to P.
47
Right-Linear Grammars for Regular Languages
Theorem 3.4
• The rst step of the proof is showing that G de ned in this way can generate
every string in L.
• Consider w ∈ L, w = aiaj⋯akal.
• If M accepts w, then there must be transitions like the following.
• δ(q0, ai) = qp, δ(qp, aj) = qr, ⋯, δ(qt, al) = qf ∈ F.
48
fi
fi
Right-Linear Grammars for Regular Languages
Theorem 3.4
• Proof: Check yourselves from the textbook. The idea is very simple, since we
can convert a left-linear grammar to a right-linear grammar generating the
reverse of L.
Figure 3.19
51
Summary
• You need to remember what is the regular expression and its notation.
• For a given regular expression, nd a language denoted by the expression, and vice versa.
• The relation between regular expressions and regular languages.
• What is a GTG and a complete GTG?
• How can we obtain a regular expression from an NFA?
• How to write a regular expression for a simple pattern?
• Practice the conversions between regular expressions, grammars, DFAs/NFAs and
languages.
52
fi