0% found this document useful (0 votes)
14 views50 pages

13 Linear Grammars

Uploaded by

junioradamec
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)
14 views50 pages

13 Linear Grammars

Uploaded by

junioradamec
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/ 50

Linear Grammars

Athanasios Aris Panagopoulos


Section Summary
Linear Grammars
Left-linear
Right-linear
Mixing left and right
Recall: Formal Grammars (Chomsky)
Avram Noam Chomsky
(Born 1928)

In the classic formalization of generative grammars first proposed by Noam


Chomsky in the 1950s, a grammar G is a quadruple (N, Σ, P, S), where:

1. N is a finite set of non-terminal symbols (that is disjoint from the strings


formed from G)
2. Σ is a finite set of elements called terminal symbols (language tokens)
(this is disjoint from Ν) This is typically also called the vocabulary or alphabet
3. P is a finite set of production rules where each rule follows the form:
∗ ∗ ∗
𝛴∪𝑁 𝑁 𝛴∪𝑁 → 𝛴∪𝑁
(where ∗ is the Kleene star operator and ∪ denotes set union)
4. S ∈ N is the start symbol Every production in P must contain at
least one nonterminal on its left side
Linear grammars
Definition: Grammars with at most one non-terminal at the right side
of a production rule are known as linear grammars

Example:
Example of non-linear:
S → αSb S → SS
S → αSb | ε S→ε
S→ε
S → αSb
S → bSα
Grammars with more than one non-terminal at the
right side of a production are known as non-linear
Example: Linear Grammar Derivation
S → αSb
S → αSb | ε
S→ε

Derivation: S ⇒ αSb ⇒ ααSbb ⇒ αααSbbb ⇒ αααbbb

What is the language generated?


αnbn
This grammar is non regular as we have seen before
Linear grammars
A linear grammar can be:
1. left-linear
2. right-linear
3. mixed-linear
Left-linear and Right-linear Grammars
A left-linear grammar is one in which all rules obey one of:
• A → α - where A is a non-terminal in N and α is a terminal in Σ
• A → Bw - where A and B are in N and w is in Σ
• A → ε - where A is in N and ε is the empty string
A right-linear grammar is one in which all rules obey one of:
• A → α - where A is a non-terminal in N and α is a terminal in Σ
• A → wB - where A and B are in N and w is in Σ
• A → ε - where A is in N and ε is the empty string

➢ They are equivalent and there is a straightforward way to transform from


one to the other
➢ Can be transformed to one-another in a straightforward manner
Left-linear to Right-linear Grammar
The basic steps to convert left to right:
1. If the left-linear grammar has a rule with the start symbol S on the right-
hand side, simply add a new initial state and the following rule: S0 → S1
2. If the left-linear grammar has a rule S → p, then make that a rule in the
right-linear grammar
3. If the left-linear grammar has a rule W1 → p, then add the following rule
to the right-linear grammar: S → pW1
4. If the left-linear grammar has a rule W1 → W2p, add the following rule to
the right-linear grammar: W2 → pW1
5. If the left-linear grammar has a rule S → W2 p, then add the following
rule to the right-linear grammar: W2 → p
➢ The procedure for right to left is similar
Example: Left-linear to right-linear
Convert the following left-linear grammar to the equivalent right-linear:
S → Ab
S → Sb
S→α
A →Aα
Α→α
Example: Left-linear to right-linear
Solution:
1. If the left-linear grammar has a rule with the start symbol S on the right-hand
side, simply add a new initial state and the following rule: S0 → S1

S → Ab S0 → S
S → Sb S → Ab
S→α S → Sb
A →Aα S→α
Α→α A →Aα
Α→α
Example: Left-linear to right-linear
Solution:
2. If the left-linear grammar has a rule S → p, then keep that rule in the right-
linear grammar

S0 → S S0 → α
S → Ab
S → Sb
S→α
A → Aα
Α→α
Example: Left-linear to right-linear
Solution:
3. If the left-linear grammar has a rule 𝑊1 → 𝑝, then add the following rule to the
right-linear grammar: 𝑆 → 𝑝𝑊1

S0 → S S0 → α
S → Ab S0 → αΑ
S → Sb
S→α
A → Aα
Α→α
Example: Left-linear to right-linear
Solution:
4. If the left-linear grammar has a rule 𝑊1 → 𝑊2 𝑝, add the following rule to the
right-linear grammar: 𝑊2 → 𝑝𝑊1

S0 → S S0 → α
S → Ab S0 → αΑ
S → Sb A → bS
S→α A → αΑ
A → Aα S → bS
Α→α
Example: Left-linear to right-linear
Solution:
5. If the left-linear grammar has a rule 𝑆 → 𝑊2 𝑝, then add the following rule to
the right-linear grammar: 𝑊2 → 𝑝

S0 → S S0 → α
S → Ab S0 → αΑ
S → Sb A → bS
S→α A → αΑ Both grammars generate {a+b+}
A → Aα S → bS
Α→α S→ε
Example: Left-linear to right-linear
Convert the following left-linear grammar to the equivalent right-linear:
S → Aα
A → Bb
B → Aα
A → αb
Β→α
Example: Left-linear to right-linear
Solution:
1. If the left-linear grammar has a rule with the start symbol S on the right-hand
side, simply add a new initial state and the following rule: S0 → S1

S → Aα S → Aα
A → Bb A → Bb
B → Aα B → Aα
A → αb A → αb
Β→α Β→α
Example: Left-linear to right-linear
Solution:
2. If the left-linear grammar has a rule S → p, then keep that rule in the right-
linear grammar

S → Aα
A → Bb
B → Aα
A → αb
Β→α
Example: Left-linear to right-linear
Solution:
3. If the left-linear grammar has a rule 𝑊1 → 𝑝, then add the following rule to the
right-linear grammar: 𝑆 → 𝑝𝑊1

S → Aα S → αbA
A → Bb S → αB
B → Aα
A → αb
Β→α
Example: Left-linear to right-linear
Solution:
4. If the left-linear grammar has a rule 𝑊1 → 𝑊2 𝑝, add the following rule to the
right-linear grammar: 𝑊2 → 𝑝𝑊1

S → Aα S → αbA
A → Bb S → αB
B → Aα Α → αΒ
A → αb Β → bA
Β→α
Example: Left-linear to right-linear
Solution:
5. If the left-linear grammar has a rule 𝑆 → 𝑊2 𝑝, then add the following rule to
the right-linear grammar: 𝑊2 → 𝑝

S → Aα S → αbA
A → Bb S → αB
B → Aα Α → αΒ
A → αb Β → bA
Both grammars generate {a+b+}
Β→α A→α
Recall: Left-linear and Right-linear Grammars
A left-linear grammar is one in which all rules obey one of:
• A → α - where A is a non-terminal in N and α is a terminal in Σ
• A → Bw - where A and B are in N and w is in Σ
• A → ε - where A is in N and ε is the empty string
A right-linear grammar is one in which all rules obey one of:
• A → α - where A is a non-terminal in N and α is a terminal in Σ
• A → wB - where A and B are in N and w is in Σ
• A → ε - where A is in N and ε is the empty string
Extended Left-linear and Right-linear
Grammars
An extended left-linear grammar is one in which all rules obey one of:
• A → α - where A is a non-terminal in N and α is a terminal in Σ
• A → Bw - where A and B are in N and w is in Σ*
• A → ε - where A is in N and ε is the empty string
An extended right-linear grammar is one in which all rules obey one of:
• A → α - where A is a non-terminal in N and α is a terminal in Σ
• A → wB - where A and B are in N and w is in Σ*
• A → ε - where A is in N and ε is the empty string
Linear Grammars and REs
➢A regular grammar is a left or right linear grammar and vice versa and
can produce regular expressions

We can use NFAs to do the following transformations:


1. From Regular Grammars to REs
2. From REs to Regular Grammars
Regular Grammars to REs (via NFA)
Step 1:
• If the grammar does not produce any null strings, then the number of states of the NFA is equal to
the number of non-terminals of the regular grammar plus one
• The start symbol of the regular grammar becomes the initial state of the NFA
• If the language generated by the regular grammar contains a null string, then the initial state is
also a final state of the NFA
Step 2:
• For a production A → αB, make a transition δ(A, α) → B
• For a production A → α, make a transition δ(A, α) → final state
• For a production A → ε, make a transition δ(A, ε) → A, where A is a final state
Step 3:
• Get the RE from the NFA (by using for instance the state elimination method we have seen
before)
➢ The procedure for right linear is similar
Example: Regular Grammars to REs
Construct an RE from the following regular grammar:
S → αA|bB|α|b
A → αS|bB|b
B → αA|bS
Example: Regular Grammars to REs
Solution

S → αA|bB|α|b
A → αS|bB|b
B → αA|bS

Step 1: In the grammar, there are three non-terminals, namely S, A,


and B. Therefore, the NFA has four states, and we can let C be the final
one.
Example: Regular Grammars to REs
Solution

S → αA|bB|α|b
α
A → αS|bB|b S A
b
B → αA|bS
B

Step 2: For the production S → αA|bB, the


transitional diagram is
Example: Regular Grammars to REs
Solution

S → αA|bB|α|b α
S A
A → αS|bB|b b
α, b

B → αA|bS C
B

Step 2: For the production S → α|b, the


transitional diagram including the previous
one is
Example: Regular Grammars to REs
Solution

α
S → αA|bB|α|b S
α
A
b
A → αS|bB|b b α, b b

B → αA|bS B C

Step 2: For the production A → αS|bB|b, the


transitional diagram including the previous
one is
Example: Regular Grammars to REs
Solution

α
S → αA|bB|α|b S
α
A
A → αS|bB|b b b α, b b b
α
B → αA|bS B C

Step 2: For the production B → αA|bS, the


transitional diagram including the previous
one is
Example: Regular Grammars to REs
Solution: Step 3: State elimination
α α αα
α e α α
S A D S A D A
α, b
b b α, b b b b b α, b b b b b
α α b α
B C B C E B C E
e e
bb

α U b U (α(αα)*b) e
D C E
α(αα)*b e
D C E D E
b
(α U b U (α(αα)*b)) U (α U b U (α(αα)*b)) U
B (b(bb)*α(αα)*b)
bb (b(bb)*α(αα)*b)
Example: Regular Grammars to REs
Construct an RE from the following regular grammar:
S → bA|b
A → αA|b
Example: Regular Grammars to REs
Solution

S → bA|b
A → αA|b

Step 1: In the grammar, there are two non-terminals, namely S, and A.


Therefore, the NFA has three states. Let B be the final state.
Example: Regular Grammars to REs
Solution
b
S A

S → bA|b
A → αA|b S
b
A

Step 2: b
B
For the production S → bA, the transitional diagram is
shown on the right α
For the production S → b, the transitional diagram S
b
A
including the previous one is shown on the right
For the production A → αA|b, the transitional diagram b
b
including the previous one is shown on the right B
Example: Regular Grammars to REs
Solution: Step 3: State elimination
α α
b e b
S A C S A
b b
b b e
B B D

α
b
C A
b U bα*b e b U bα*b
b C B D C D
b e
B D
Linear Grammars and REs
➢A regular grammar is a left or right linear grammar and vice versa and
can produce regular expressions

We can use NFAs to do the following transformations:


1. From Regular Grammars to REs
2. From REs to Regular Grammars
REs to Regular Grammars (via NFA)
Step 1:
Construct the equivalent NFA for the given RE (Thompson's construction algorithm)
Step 2:
Eliminate the null moves (by transforming to the equivalent DFA)
• If we do not eliminate e-transitions we will end up in an extended regular grammar
Step 3:
• The FA states become the non-terminals of the grammar
• For all δ(q1, α) → q2, where q1, q2 ∈ Q and α ∈ Σ, the production rule becomes Q1 → αQ2. If q2
is a final state, then for δ(q1, α) → q2 the production rules are Q1 → αQ2 and Q1 → α
• The starting symbol corresponds to the initial state
➢ The procedure for right linear is similar
Example: RE to Regular Grammar (via NFA)
Construct a regular grammar for the following RE: α*(α U b)b*
Example: RE to Regular Grammar (via NFA)
Solution:

Step 1:
Construct the equivalent NFA for the given RE (Thompson's
construction algorithm)
α
The NFA for the RE α*(α U b)b* is: α b
q0 q1
b
Example: RE to Regular Grammar (via NFA)
Solution:

Step 2:
Eliminate the null moves (by transforming to the equivalent DFA)
No null moves exist
α
α b
q0 q1
b
Example: RE to Regular Grammar (via NFA)
Solution:
Step 3:
• The NFA has two states which means that there will be two non-terminals in the
grammar
• We consider A for q0 and B for q1
• For the transition δ(q0, α) → q0, the production rule will be A → αA
• For the transition δ(q0, α) → q1, the production rule will be A → αB | α as q1 is a
final state
• Similarly: α
α
b
• δ(q0, b) → q1: A → bB | b (as q1 is a final state)
• δ(q1, b) → q1: B → bB | b (as q1 is a final state) q0 q1
• The start symbol will be A since q0 is the initial state b
Example: RE to Regular Grammar (via NFA)
Solution:

Step 3:
• The regular grammar G for the RE α*(α U b)b* is {VN, Σ, P, S} where
• VN = {A, B}
• Σ = {α, b}
• P : {A → αA|bB|α|b, B → bB|b}
• S :{A} α
α
b
q0 q1
b
Example: RE to Regular Grammar (via NFA)
Construct a possibly extended regular grammar for the following RE:
α*(α U eα*b)b*
Example: RE to Regular Grammar (via NFA)
Solution:

Step 1:
Construct the equivalent NFA for the given RE (Thompson's
construction algorithm) α
The NFA for the RE α*(α U b)b* is: b
q0 q1
α

b
e
q2
α
Example: RE to Regular Grammar (via NFA)
Solution:

Step 2:
Eliminate the null moves (by transforming α
to the equivalent DFA) b
We do not ned to do this step as an q0 q1
α
extended grammar is acceptable
b
e
q2
α
Example: RE to Regular Grammar (via NFA)
Solution:
Step 3:
• The number of states of the FA is three, as many as the non-terminals in
the grammar α

• We consider A for q0, B for q1 and C for q2 b


• For δ(q0, α) → q0, A → αA q0 q1
• For δ(q0, α) → q1, A → αB α
• For δ(q0, ε) → q2, A → C b
• For δ(q2, α) → q2, C → αC e
q2
• For δ(q2, b) → q1, C → bB α
• For δ(q1, b) → q1, B → bB
Example: RE to Regular Grammar (via NFA)
Solution:
Step 3:
• The number of states of the FA is three, as many as the non-terminals in the grammar
• We consider A for q0, B for q1 and C for q2
• For δ(q0, α) → q0, A → αA α
• For δ(q0, α) → q1, A → αB
• For δ(q0, ε) → q2, A → C b
• For δ(q2, α) → q2, C → αC q0 q1
• For δ(q2, b) → q1, C → bB α
• For δ(q1, b) → q1, B → bB
b
• q1 is a final state so: e
• A→α q2
• C→b α
• B→b
Answer
• The grammar G for the NFA is the <VN, Σ, P, S> where:
• VN = {A, B, C}
• Σ = {α, b}
• P : {A → αA|αB|C|α, B → bB|b, C → αC|bB|b} α

• S :{A} b
q0 q1
α

b
e
q2
α
Bonus: Right-linear to left-linear via NFA
α b
S0 → α
S0 → αΑ S0 Α S
A → bS α b
A → αΑ α
S → bS
S→ε
Reverse
S → Ab
S → Sb
S→α α b
A → Aα
Α→α S0 A b S
α

α
Mixing Left-linear and Right-linear grammars
• If we mix left-linear and right-linear rules in a single grammar, the
result will not necessarily generate a regular language
• For instance, given w ∈ {α, b}*, a left linear regular L can be:
• A → Aw | ε
• LR will be right linear regular:
• B → wRB | ε
• But L’ = {wwR} is not regular:
• A → Aw | B
• B → wRB | ε

https://transang.me/formal-language-theory/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy