0% found this document useful (0 votes)
7 views

207 Regular Grammar Unit 1 2 3

The document discusses the theory of computation, focusing on regular grammars and their role in describing regular languages. It covers various topics including state machines, formal grammars, and the conversion between finite automata and regular grammars. Additionally, it presents examples of palindromes and the derivation of strings in different types of grammars, emphasizing the distinctions between right-linear, left-linear, and linear grammars.

Uploaded by

Ria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

207 Regular Grammar Unit 1 2 3

The document discusses the theory of computation, focusing on regular grammars and their role in describing regular languages. It covers various topics including state machines, formal grammars, and the conversion between finite automata and regular grammars. Additionally, it presents examples of palindromes and the derivation of strings in different types of grammars, emphasizing the distinctions between right-linear, left-linear, and linear grammars.

Uploaded by

Ria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

ULC703

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

■ State machine: ε-NFA → NFA → DFA.


■ Formal notational scheme: Regular expression and Regular Grammar.
■ Agenda
• Grammars
• Representing Palindrome language
• Formal grammars
• Right Linear Grammars (RLG)
• FA to RLG conversion
• Definition of Right- and Left- linear grammar
• Regular grammars
• FA to LLG conversion
• Linear grammars and examples.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 2 / 25
Using English-type Grammar
■ This the following sentence grammatically correct.

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

Figure: At every step use only one production/ basic rule.


. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 4 / 25
The Language Lpal is not regular

1. The language Lpal ={w ∈ {0,1} | w is palindrome.}


2. [Sake of contradiction] An FA M exists with n states and accepts L.
3. We choose x =0n 110n which satisfies x ∈ L and |x| ≥ n.
4. There exists u, v and w such that x = uvw and the conditions 1-3 in
the theorem is true.
5. Present a contradiction
• Condition 1: |uv| ≤ n and the first n symbols of x are 0’s, all the
symbols in u and v must be 0’s.
• Condition 2:|v| >0, therefore, v =0q for some q >0. Also, u =0p
, and w =0r 110n s.t., p + q + r = n
• Condition 3 of PL says uv i w must be in L for all i >=0.
But for all i ̸= 1, uv i w will not be in the language
Example: uv 0 w=0p 0r 110n = 0p+r 110n ∈
/ L since p + r < n as |v| = q ≥1 . This is a contradiction.

• 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

■ There is a natural recursive definition of when a string of 0’s and 1’s


is in Lpal

Figure: A grammar for palindromes


. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 6 / 25
Important components of grammatical description - I

■ Terminals: There is a finite set of symbols that form the strings of


the language being defined.
• Palindrome: {0,1}
• English-type: {He, looked, dog, at, the, an, one,a,eye, with}
■ Variables/ Non-terminals: There is a finite set of Variables. Each
variable represents a language i.e., a set of strings.
• Language by P : {ε, 0110,010,101,110011}
• Language by N oun[N ]: {He, dog, eye}
• Language by V erb[V ]: {looking}
• Language by Sentence[S]: {He looked at the dog with one eye,
He looked with the eye at one dog,
dog looked, eye looked, · · · }

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(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:

Figure: An specific description, not . a . generalized


. . . . . . . . . one.
. . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 8 / 25
Formal Definition of a Grammar
■ A is grammar G is represented by its four components, G = (V, T, P, S),
where
• V is the finite set of variables
• T the finite set of terminals
• P the finite set of productions and
• S the start symbol
■ Example:

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 9 / 25
Deriving Strings in Lpal language

To show that string w=01011010 is in Lpal

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

■ Grammars are often an alternative way of specifying languages.


■ Regular grammar generates a regular language.
■ There is a correspondence between a FA and regular grammar.
■ Conversion of DFA to grammar:
Step-0 Consider the DFA shown below.
q3 0
1
0
0
q1 q2 1
1

0 q4
1

DFA accepting L having strings with second last symbol being 0

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(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

To show that string w=0111000 is in DFA considered.

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

■ A grammar G = (V, T, S, P ) is said to be right-linear if all productions


are of the form
A → xB
A→y
where A, B ∈ V and x, y ∈ T ∗ .
■ A grammar G = (V, T, S, P ) is said to be left-linear if all productions
are of the form
A → Bx
A→y
where A, B ∈ V and x, y ∈ T ∗ .
Regular grammar
A regular grammar is one that is either right-linear or left-linear.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(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

• Let A1 , A2 and A3 corresponds to the states q1 , q2 , and q3 .


• The right-linear grammar is as follows:
A1 → 1A1 A1 → 0A2
A2 → 1A1 A2 → 0A3
A3 → 0A3 A3 → 1A1 A3 → ε
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 15 / 25
DFA to left-linear grammar (LLG) conversion Algorithm/ Steps

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 16 / 25
Using LLG for deriving strings in last example

To show that string w=01100 is in DFA considered.

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

■ A grammar G = (V, T, S, P ) is said to be Linear if all productions are


of the form
A → xBw
B → y where A, B ∈ V and x, w, y ∈ T ∗ .
■ General production for linear grammar: A → α where α ∈ (V ∪ T )∗
s.t., α contains at most a single variable.
■ Not all linear grammars are regular
• Although every production may be either in right-linear or left-
linear form, the grammar itself may neither right-linear nor left-
linear, and therefore is not regular.
■ All regular grammars are linear.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(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: Use above to convince yourself with


.
. . .
each rule.
. . . . . . . . . .
. . . . . . . . .
. . . .
. . . .
.
.
.
.
.
.
.
.
.

(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 21 / 25


FA to recognize identifiers (Application: Lexical analysis)

Figure: identifier recogniser.


. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
(ToC, Dr. Ashish Gupta) ULC703 : Unit-I/II/III 22 / 25
FA for recognizing relational operators

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

You might also like