CS 3186
Regular Grammars
   Section 3.3
                   1
     Notation (described earlier)
Grammar    G  V , T , S , P 
    V : Set of variables
    T : Set of terminal symbols
     S:   Start variable
    P:    Set of Production rules
                                    2
                Production rules
Recall from Chapter 1, that the general form of
For Regular Grammars, there is a severe restriction
  on the production rules. i.e., on both “x” and “y”.
 • x can only be one variable
 • Y can contain only have at most one variable and
   more restriction on where the variable can occur.
Regular grammars are further described by first
  defining a class of Linear Grammars.
                                                       3
             Linear Grammars
Linear Grammar: Grammars with one variable
  on the left side and at most one variable at
  the right side of a production
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
Example:     S  abS                      string of
             S a                         terminals
     Note: The non terminal is the right most symbol in
     the production rule.
                                                          7
               Left-Linear Grammars
 All productions have form:                 A  Bx
                                                 or
                                             A x
 Example:         S  Aab                         string of
                  A  Aab | B                     terminals
                  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)
Examples for regular grammars ({S}, {a, b}, S, P) with P given by
      S  abS                        S  Aab
      S a                            A  Aab | B
                                      Ba
Example of a non regular grammar ({S}, {a, b}, S, P) with P given by
                                                                  10
Regular Grammars
       and
Regular Languages
                    11
                   Theorem
Languages
Generated by
Regular Grammars
                                    Regular
                                     Languages
(1)Regular grammars generates Regular
   languages.
(2) Regular grammars for Regular
   languages
Both (1) and (2) are proven by construction
                                                 12
Note:
Regular grammars: Consider right-linear
 grammars.
Regular languages: There exists a nfa or dfa
 that accepts the language.
We seek to show the equivalence of language
 generated by right-linear grammar and
 language accepted by a nfa.
                                               13
Regular grammars generates Regular languages
Let   G   be a right-linear grammar
We will prove:    L(G ) is regular
Proof idea:      We will construct NFA   M
                 with L( M )  L(G )
                                             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.
Proof: By construction of NFA.
Let V = {V0, V1,…}; S= V0 ;
Productions P are of the form
                  Vi  a1a2 amV j
                  Vi  a1a2 am
                                               22
            Proof (continued)
We construct the NFA     M such that:
each variable    Vi corresponds to a node:
                 V1        V3
      V0
                                    VF
                V2        V4      special
                                  final state
                                                23
            Proof (continued)
For each production:   Vi  a1a2 amV j
we add transitions and intermediate nodes
  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
Resulting NFA may look like this
                              a9
                        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
If w is accepted by the NFA, then by construction,
  we can produce a derivation sequence V0   w
Then L(G) = L(M)
                                                   26
             Previous Example
For the derivation sequence for w=aaaba
 S  aA  aaaB  aaabB  aaaba
There exists a path from S to VF on w=aaaba
If w ∈ L(G) then w ∈ L(M)                     27
             Previous Example
There is a path from S to VF on w=ba
Then there is a corresponding derivation sequence
If w ∈ L(M) then w ∈ L(G)                       28
  The case of Left-Linear Grammars
Let G be a left-linear grammar
We can construct an equivalent right-linear
  grammar. (skip this construction proof)
Hence, the language L(G) generated by any
 regular grammar (left-linear or right-
 linear) is regular.
                                              29
            Previous Example
Given G= ({S,A,B},{a,b},S,P} where P:
Constructed an equivalent nfa
({S,A,I,B,Vf},{a,b}, ,S,Vf), where is given by
the transition graph:
                                                 30
Regular Grammars for Regular Languages
 Theorem: If L is a regular language over then
 there exists a right-linear grammar G=(V, , ,S,P),
 such that L=L(G)
 Proof: (By construction) The idea is to start with
 the finite acceptor (DFA or NFA) of the language
 and reverse the earlier construction. i.e., the
 states of the acceptor become the variables, and
 the symbols causing the transitions become the
 terminals in the grammar.
                                                      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)
Conversely, if w ∈ L(G), then there is a derivation of the
  form q0       ai qp ……        ai,aj,…akaf . This implies that
     (q0, ai,aj,…akaf ) = qf . This implies ai,aj,…akaf is accepted.
                                                                 33
                     Example
Given an NFA, M
  M  Q, ,  , q0 , F 
Where Q={q0,q1,q2,q3},
  =(a,b}, F={q3}, given by
The equivalent right-linear grammar, G is
({q0,q1,q2,q3}, {a,b},q0,P), where P are
defined by
                                            34
               Another Example
Given an NFA,
The equivalent right-linear grammar G=({S,I,J,K},
 (a,b}, S, P) where P are defined by:
                                                    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
(III) Write a right-linear grammar for Exercise #10
(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
(V) Construct an equivalent NFA for the grammar below
S→       S → aT      S → bT      T→a T→b             T → aS        T → bS
                                                                          38