207 Regular Grammar Unit 1 2 3
207 Regular Grammar Unit 1 2 3
Theory of Computation
Regular Grammar:
Describing Regular Languages
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 1 / 25
Ways to describe a regular language
Figure: Some rules from English grammar for our English-type grammar.
Constructs in <> are optional. . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 3 / 25
Deriving the sentence from the rules
• Since Lpal can not be accepted by any FA, hence, it is not regular.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 5 / 25
Grammar for Palindrome
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 7 / 25
Important components of grammatical description - II
■ Start symbol: One of the variables represents the language being
defined.
• Palindrome: P
• English-type: Sentence
■ Productions: There is a finite set of productions or rules that represent
the recursive definition of a language. Each production consists of:
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 9 / 25
Deriving Strings in Lpal language
P =⇒ 0P 0 . . . Using production(4)
=⇒ 01P 10 . . . Using production(5)
=⇒ 010P 010 . . . Using production(4)
=⇒ 0101P 1010 . . . Using production(5)
=⇒ 0101ε1010 . . . Using production(1)
=⇒ 01011010
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 10 / 25
Grammar describing regular languages
0 q4
1
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 11 / 25
Grammar describing regular languages
Step-1 Make a variable for every state of the DFA. For each state qi , we
make a new variable.
Let A1 , A2 , A3 , and A4 be variables for q1 , q2 , q3 , and q4 , respec-
tively.
Step-2 Add the rule Ai → aAj if δ(qi , a) = qj where a∈ Σ
(1) A1 → 1A1 (2) A1 → 0A2
(3) A2 → 0A3 (4) A2 → 1A4
(5) A3 → 0A3 (6) A3 → 1A4
(7) A4 → 0A2 (8) A4 → 1A1
Step-3 Add the rule Ak → ε if qk happens to be an accept state.
(9) A3 → ε
(10) A4 → ε
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 12 / 25
Deriving strings in last example
A1 =⇒ 0A2 . . . Using(2)
=⇒ 01A4 . . . Using(4)
=⇒ 011A1 . . . Using production(8)
=⇒ 0111A1 . . . Using production(1)
=⇒ 01110A2 . . . Using production(2)
=⇒ 011100A3 . . . Using production(3)
=⇒ 0111000A3 . . . Using production(3)
=⇒ 0111000ε . . . Using production(9)
=⇒ 0111000 (Derived)
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 13 / 25
Right- and Left-Linear Grammars
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 14 / 25
Example-2: Convert DFA to right-linear grammar
(RLG)
0 0
q1 q2 q3 0
1
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 16 / 25
Using LLG for deriving strings in last example
A3 =⇒ A2 0 . . . Using A3 → A3 0
=⇒ A1 00 . . . Using A2 → A1 0
=⇒ A1 100 . . . Using A1 → A1 1
=⇒ A2 1100 . . . Using A1 → A2 1
=⇒ A1 01100 . . . Using A2 → A1 0
=⇒ ε01100 . . . Using A1 → ε
=⇒ 01100 (Derived)
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 17 / 25
Regular expression to RLG and LLG steps
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 18 / 25
Linear Grammar
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 19 / 25
Linear Grammars
■ L = {an bn |n ≥ 0}
• S → aSb|ε LINEAR BUT NOT REGULAR
■ L= {an+1 bn |n
> 0}
• S → aSb|a LINEAR BUT NOT REGULAR
■ (0+1)∗ 00
• LINEAR AND REGULAR
A1 → 1A1 A1 → 0A2
A2 → 1A1 A2 → 0A3
A3 → 0A3 A3 → 1A1 A3 → ε
■ Example of L(G) s.t., G is linear but not Regular
S → aA
A → Bb
B→a
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 20 / 25
RE to RLG and LLG conversion rules.
RE RLG LLG
a S→a S→a
a+b S→a | b S→a | b
ab S→aA, A→ b S→Ab, A→ a
a∗ S→aS, S→ ε S→Sa, S→ ε
a+ S→aS, S→ a S→Sa, S→ a
(a + b)∗ S→aS | bS | ε S→Sa | Sb | ε
(a + b)+ S→aS | bS | a | b S→Sa | Sb | a | b
(ab)∗ S→aA | ε, A→bS S→Ab | ε, A→Sa
(ab)+ S→aA , A→bS | b S→Ab , A→Sa | a
Table: RE and their corresponding left-and right-linear
Figure: RO recogniser. . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 23 / 25
FA for unsigned numbers
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) Figure: FAULC703
for unsigned
: Unit-I/II/III numbers. 24 / 25
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 25 / 25