7. Section 3.3
7. Section 3.3
Regular Grammars
Section 3.3
1
Notation (described earlier)
Grammar G V , T , S , P
V : Set of variables
Examples:
S Ab SA
S aSb
A aAb A aB |
S
A B Ab
4
A Non-Linear Grammar
Grammar G: S SS
S
S aSb
S bSa
5
Linear Grammars
Linear Grammars can be right linear
grammars or left linear grammars
depending on the position of the non
terminal in the production rule.
6
Right-Linear Grammars
All productions have form: A xB
or
A x
Ba
Note: The non terminal is the left most symbol in the production rule
8
Regular Grammars
9
Regular Grammars
A regular grammar is any right-linear or left-
linear grammar (If a grammar mixes the concepts
of right and left linearity, it is not a regular grammar)
10
Regular Grammars
and
Regular Languages
11
Theorem
Languages
Generated by
Regular Grammars
Regular
Languages
12
Note:
14
Example
Grammar G is right-linear
Example: S aA | B
A aa B
Bb B|a
15
Example (continued)
Construct NFA M such that
every state is a grammar variable:
A
special
S VF
final state
B
S aA | B
A aa B
Bb B|a 16
Example (continued)
Construction procedure: Add edges for each
production as follows
a A
S VF
B
S aA
17
Example (continued)
a A
S VF
B
S aA | B
18
Example (continued)
A
a a
S a VF
B
S aA | B
A aa B
19
Example (continued)
A
a a
S a VF
B
S aA | B
b
A aa B
B bB 20
Example (continued)
A
a a
S a VF
a
B
S aA | B
b
A aa B
B bB | a 21
Theorem
Theorem: Let G = (V,T,S,P) be a right-linear
grammar. Then L(G) is a regular language.
22
Proof (continued)
We construct the NFA M such that:
V1 V3
V0
VF
V2 V4 special
final state
23
Proof (continued)
For each production: Vi a1a2 amV j
Vi a1 a2 ………
am V
j
24
Proof (continued)
For each production: Vi a1a2 am
we add transitions and intermediate nodes
Vi a1 a2 ………
am
VF
a2 a4
a1 V1 V3
a3 a5
V0
a3 a4
VF
a9
V2 a5 a8
V4
25
Proof (continued)
If w ∈ L(G), then there is a path in the NFA (by
construction) from V0 to VF
26
Previous Example
29
Previous Example
Given G= ({S,A,B},{a,b},S,P} where P:
30
Regular Grammars for Regular Languages
31
Proof (continued)
Let finite acceptor M Q, , , q0 , F accept L.
Assume that Q= (q0,q1,…qn) and let =(a1,a2,…am).
Construct the right-linear grammar G=(V, ,S,P),
with V=(q0,q1,…qn) and S = q0
For each transition (qi,aj) = qk of M, add a
production in P qi aj qk
If qf is in F, add qf
32
Proof (continued)
If w= ai,aj,…akaf ∈ L. For M to accept this string, there
exists a path (make moves in M)
(q0,ai) = qp (qp,aj) = qr ……………. (qs,ai) = qt (qs,af) = qf ∈ F
By construction, there will be productions for each of
the ‘s. i.e., of the form qx ay qz
We can make a derivation q0 ai qp …… ai,aj,…akaf with
the grammar G and w ∈ L(G)
33
Example
Given an NFA, M
M Q, , , q0 , F
Where Q={q0,q1,q2,q3},
=(a,b}, F={q3}, given by
34
Another Example
Given an NFA,
35
Summary
• Regular languages (expressed as Regular
expressions or accepted by NFA) and
Regular grammars are equivalent.
• NFA and DFA are equivalent.
• The construction algorithms in this
chapter enable us to convert back and
forth between the various representations
of Regular languages (Regular expression,
NFA or DFA, Regular Grammars)
36
Summary
37
Homework 3.3
(I) Do Exercises: # 1,2
(II) Write an NFA for L and then convert to a right-linear grammar for
Exercises: #3, #6
(IV) Write a right linear grammar for the NFA described in Exercise #1,
#2, #3,#10, #12 (Section 3.2)
(V) Consider the grammar G=( {S, A},{a,b}, S, P) with P given by:
S → aS|bA A → bA A→
Construct an equivalent NFA
38