Loading [MathJax]/jax/output/HTML-CSS/jax.js
Close
Close Window

OpenDSA Stand-alone Modules

Chapter 0 modules

Show Source |    | About   «  0.267. Regular Expressions   ::   Contents   ::   0.269. Closure Properties of Regular Grammars  »

Regular Grammars

1. Regular Grammars

Regular grammars are another way to describe regular languages. Recall that a grammar is made of of terminals, variables, and production rule. As the name implies, a regular grammar is a special type of grammar (we will see plenty of grammars later that are not regular). Which begs the question: What makes a grammar regular? Before we can properly answer that, we need some definitions.

Suppose we have the following Grammar G=(V,T,S,P) where,

represented byVvariables (nonterminals)A,B,..,Z(thatis,capitalletters)Tterminalsa,b,..,z,0,1,...9(lowercaselettersandnumbers)Sstart symbolPproductions (rules)

V, T, and P are finite sets.

Linear grammar: A grammar is linear if has a single variable in the RHS of every production rule.

All productions are of the formAxBACxAxwhere A,B,CV,xT

In this grammar, each production rule has at most one variable on the RHS. (Note: It does not need to be the same terminal x in every production!)

Right-linear grammar: This is a special case of linear grammar. If a grammar is linear and any variable, if it exists, always occurs at the right end of the RHS, then the grammar is called a Right-linear grammar. For example, the grammar:

AxBAxCAywhere A,B,CV,x,yT

Note: x or y is a string of length 0 or more. Each production has at most one variable, so the grammar is linear. The variables (B and C) are at the end of the RHS, so it is a right linear grammar.

Left-linear grammar: This is the same as Right-linear grammar, but the occurance of any variable, if it exists, is at the begining of each production RHS. For example,

ABxACyAxwhere A,B,CV,x,yT

Each production has at most one variable, so the grammar is linear. The variables (B and C) are at the start of the RHS, so it is a left linear grammar.

OK, so now we have the definitions that we need to define a regular grammar.

Definition:

A regular grammar is a right-linear or left-linear grammar.

Example 1

G=({S},{a,b},S,P),P=SabSSλSSab

The language is (ab).

However, cannot mix left/right rules! So this is not a regular grammar.

Example 2

G=({S},{a,b},S,P),P=SaB|bS|λBaS|bB

This is a right linear grammar representing the language L={strings with an even number of a's},Σ={a,b}

1.1. Our Next Step

What we have already done:
Definition: DFA represents regular language
Theorem: NFA DFA
Theorem: RE NFA
What we will do next:
Theorem: DFA regular grammar

1.2. NFA from Regular Grammar

Theorem: L is a regular language if and only if there exists a regular grammar G such that L=L(G).

(Doing here for RR grammar)
() Given a regular grammar G, Construct NFA M such that L(G)=L(M)
Make a state for each non-terminal.
Make a transition on each terminal in that production rule.
Make it final if there is a production without non-terminals.
For rules with multiple terminals, need intermediate states.

Example 3

SaB|bS|λ
BaS|bB

This is a right linear grammar representing the language
L={ strings with an even number of a’s },Σ={a,b}
Created with Raphaël 2.1.2
S
B
b
b
a
a
λ

What about a rule like SabB? Make two states (S to intermediate state on a, then intermediate state to B on b).

Or Sab? Make two states (S to intermediate state on a, then intermediate state to an accepting state on B.

1 / 10 Settings
<<<>>>

Suppose we need to convert this Regular Grammar to an NFA

  1. S
  2. aB
  1. S
  2. aA
  1. A
  2. aA
  1. A
  2. bS
  1. A
  2. bB
  1. B
  2. bS
  1. B
  2. λ
Proficient Saving... Error Saving
Server Error
Resubmit

1.3. Right-linear Regular Grammar from DFA

Theorem: L is a regular language if and only if there exists a regular grammar G such that L=L(G).

() Given a DFA M, construct regular grammar G such that L(G)=L(M)

The process is pretty much the same as when we made an NFA from RRG:
Each DFA state gets a non-terminal.
Each transition gets a production rule.

Construct the Regular Grammar for the NFA

Created with Raphaël 2.1.2
q0
q1
a
a
b
G=({S,B},{a,b},S,P),
P=
Q0aQ1
Q1aQ0|bQ1|λ
1 / 6 Settings
<<<>>>

Suppose we need to convert this NFA to a Regular Grammar

Created with Raphaël 2.1.2
Q0
Q1
b
a
a
Proficient Saving... Error Saving
Server Error
Resubmit

1.4. Something to Think About

Example 4

L={anbnn>0}

Is language L regular? Can you draw a DFA, regular expression, or Regular grammar for this language?

Consider this grammar:

SaSbab

Nice and easy… but this grammar is not regular!

We will come back to this question later.

   «  0.267. Regular Expressions   ::   Contents   ::   0.269. Closure Properties of Regular Grammars  »

nsf
Close Window