0% found this document useful (0 votes)
5 views55 pages

Lec05-CFG ContextFreeLanguages

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)
5 views55 pages

Lec05-CFG ContextFreeLanguages

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/ 55

Context-Free Grammars

and Context-Free Languages

• Context-Free Grammars
• Derivations
• Parse Trees
• Ambiguity

BBM401 Automata Theory and Formal Languages 1


Context-Free Grammars

BBM401 Automata Theory and Formal Languages 2


Context-Free Grammars
• We have seen that many languages cannot be regular.
– We need to consider larger classes of languages.
– Context-Free Languages (CFLs) is a larger class of languages (larger than
regular languages).
– Every regular language is a context-free language.

• Regular expressions are used to define regular languages.

• Context-Free Grammars (CFGs) are used to define Context-Free Languages


(CFLs).

• Pushdown Automatons recognize CFLs.

• Context-Free Grammars (CFGs) played a central role in natural language processing,


and compilers.
– The syntax of a programming language is described by a CFG.
– A parser for a programming language is a pushdown automaton.
BBM401 Automata Theory and Formal Languages 3
Context-Free Grammars and
Context-Free Languages
• A context-free grammar is a notation for describing languages.
• CFGs are more powerful than REs, but they cannot still define all possible languages.
– CFGs define Context-Free Languages (CFLs).
– CFGs are useful to describe nested structures.
– Since every regular language is a CFL, it can be defined by a CFG.
– There are also languages that are not CFLs.

Languages

Context-Free Languages

Regular Languages

BBM401 Automata Theory and Formal Languages 4


CFG – Example
• The language { 0n1n | n ≥ 0} is not a regular language, but it is a CFL.
– It can be defined by a CFG.

• A CFG for { 0n1n | n ≥ 0} is:

S→
S → 0S1

• 0 and 1 are terminals.  ={0,1} is the alphabet of the language.


• S is a variable (or nonterminal).
• S is also the start symbol of this CFG.
• S →  and S → 0S1 are productions (or rules)

BBM401 Automata Theory and Formal Languages 5


CFG – Example
Basis:
• Production S →  says that  is in the language.

Induction:
• Production S → 0S1 says that if w is in the language then is 0w1 is in the language.

•  is in the language.
• since  is in the language, 01 is in the language.
• since 01 is in the language, 0011 is in the language.
• …
• Thus, the language of this CFG is { 0n1n | n ≥ 0}

BBM401 Automata Theory and Formal Languages 6


Formal Definition of CFGs
• A context-free grammar G is a quadruple
G = (V, T, P, S)
where

• V is a finite set of variables (non-terminals).


– Each variable represents a language.
• T is a finite set of terminals.
– T is the alphabet of the language defined by the CFG.
• P is a finite set of productions of the form A →  , where A is a variable and
  (V∪T)*
– The left side of a production is a variable and its right side is a string of variables and
terminals.
• S is a designated variable called the start symbol.
– The start symbol is the variable whose language is defined.

BBM401 Automata Theory and Formal Languages 7


CFG – Example 2
• Consider the language of palindromes
Lpal = {w* : w = wR }
• Some members of Lpal : abba bob ses tat
• Lpal is NOT regular, but Lpal is a context-free language.

• Let  ={0,1} be the alphabet for Lpal.


• In this case, ,0,1,00,11,000,010,101,111,0110,… will be in Lpal.
• A CFG Gpal for Lpal is:
Gpal = ( {S}, {0,1}, { S → , S → 0, S → 1, S → 0S0, S → 1S1 }, S )

• Sometimes, we use a shorthand for a list of productions with the same left side.
S →  | 0 | 1 | 0S0 | 1S1

BBM401 Automata Theory and Formal Languages 8


Derivation
• Initially, we have a string that only contains the start symbol.
• We expand the start symbol using one of its productions (i.e., using a production
whose left side (head) is the start symbol).
– i.e. we replace the start symbol with a string which appears on the right side of a production
rule belongs to the start symbol.
• If the resulting string contains at least one variable, we further expand the resulting
string by replacing one of its variables with the right side (body) of one of its
productions.
– We can continue these replacements until we derive a string consisting entirely of terminals.
• The language of the grammar is the set of all strings of terminals that we can be
obtained in this way.
• Replacement of a variable (in a string) with the right side of one of its
productions is called as derivation.

BBM401 Automata Theory and Formal Languages 9


Derivation 
• Suppose G = (V, T, P, S) is a CFG.
• Let A be a string of terminals and variables where A is a variable.
– i.e.  and  are strings in (V∪T)*, and A is V.
• Let A→ be a production of G.
• Then, we say that
A ֜  is a derivation
𝑮
• If G is understood, we just say that
A ֜  is a derivation

• One derivation step can replace any variable in the string with the right side (body) of
one of its productions.

BBM401 Automata Theory and Formal Languages 10



Derivation Sequence ֜
• We can extend the derivation (֜) relationship to represent zero or more derivation
steps.

• We use symbol ֜ to denote zero or more steps of a derivation sequence.

Derivation Sequence:
Basis:

– For any string  of terminals and variables, we say  ֜ .
– That is, any string derives itself.
Induction:
∗ ∗
– If  ֜  and  ֜ , then  ֜  .
– That is, if  can become  by zero or more steps, and one more step takes  to ,
then  can become  by a derivation sequence.
BBM401 Automata Theory and Formal Languages 11

Derivation Sequence ֜

• In other words,  ֜  means that there is a sequence of strings 1, 2, … , n for
some n ≥ 1 such that

1.  = 1 ,

2.  = n , and

3. for i=1,2,…,n-1, we have i ֜ i+1

BBM401 Automata Theory and Formal Languages 12


Derivation Sequence – Example 1
• Let CFG G = ( {S}, {0,1}, { S→, S→0S1 }, S )

• S ֜ 0S1 ֜ 00S11 ֜ 000S111 ֜ 000111 is a derivation sequence.


– S derives 000111; or 000111 is derived from S.


• That is, S ֜ 000111 and also

– S ֜ 000S111

– S ֜ 00S11

– 0S1 ֜ 000S111

– 00S11 ֜ 000111

• S ֜ 0S1 ֜ 00S11 ֜ 0011 is a derivation sequence.

BBM401 Automata Theory and Formal Languages 13


Derivation Sequence – Example 2
A CFG:
S → ASB | c
A →  | aA
B →  | bB

Derivation Sequences of acb from S.


S ֜ ASB ֜ aASB ֜ aSB ֜ acB ֜ acbB ֜ acb
S ֜ ASB ֜ ASbB ֜ ASb ֜ Acb ֜ aAcb ֜ acb
S ֜ ASB ֜ AcB ֜ aAcB ֜ aAcbB ֜ acbB ֜ acb

• We may select any non-terminal (variable) of the string for the replacement in each
derivation step.

BBM401 Automata Theory and Formal Languages 14


Leftmost and Rightmost Derivations
• Leftmost Derivation always replaces the leftmost variable (in the string) with one of
its rule-bodies. ֜lm

S ֜lm ASB ֜lm aASB ֜lm aSB ֜lm acB ֜lm acbB ֜lm acb

• Rightmost Derivation always replaces the righmost variable (in the string) by one of
its rule-bodies. ֜rm

S ֜rm ASB ֜rm ASbB ֜rm ASb ֜rm Acb ֜rm aAcb ֜rm acb

BBM401 Automata Theory and Formal Languages 15


Leftmost and Rightmost Derivations
S → ASB | c
A →  | aA
B →  | bB

Derivation Sequences of acb from S.


S ֜ ASB ֜ aASB ֜ aSB ֜ acB ֜ acbB ֜ acb is a leftmost derivation

S ֜ ASB ֜ ASbB ֜ ASb ֜ Acb ֜ aAcb ֜ acb is a rightmost derivation

S ֜ ASB ֜ AcB ֜ aAcB ֜ aAcbB ֜ acbB ֜ acb


is NOT a leftmost or rightmost derivation.

BBM401 Automata Theory and Formal Languages 16


Sentential Forms
• Let G = (V, T, P, S) be a CFG, and (V∪T)*

• If S ֜  , we say that  is a sentential form.


• If S ֜lm  , we say that  is a left-sentential form.

• If S ֜rm  , we say that  is a right-sentential form.

• L(G) is those sentential forms that are in T*.

BBM401 Automata Theory and Formal Languages 17


The Language of a CFG
• If G = (V, T, P, S) is a CFG, then the language of G is

L(G) = { w  T* : S ֜ w }

• i.e. the set of strings of terminals (strings over T*) that are derivable from S

• If we call L(G) as a context-free language.


– Ex: L(Gpal) is a context-free language.

• For each CFL, there is a CFG, and each CFG generates a CFL.
• Every regular language is a CFL.
– That is, regular languages are a proper subset of context-free languages

BBM401 Automata Theory and Formal Languages 18


The Language of a CFG – A Proof Example
• Gpal = ( {S}, {0,1}, { S → , S → 0, S → 1, S → 0S0, S → 1S1 }, S )
• Lpal = {w* : w = wR }

Theorem: L(Gpal) = Lpal

Proof:
In order to prove this equality,
( Direction): We have to prove that every member of Lpal is also a member of L(Gpal).
( Direction): We have to prove that every member of L(Gpal) is also a member of Lpal.

BBM401 Automata Theory and Formal Languages 19


The Language of a CFG – A Proof Example
 Direction
Proof: ( Direction) If wLpal then wL(Gpal), i.e. Gpal can generate w
• Suppose w = wR ( wLpal )
• We prove by induction on the length of w ( |w| ) that wL(Gpal)

Basis:
• |w|=0, or |w|=1.
• Then, w is , 0, or 1
• Since S→ , S→0 and S→1 are productions of Gpal,

we can conclude that S ֜ w in all base cases.

– S֜

– S֜0

– S֜1

BBM401 Automata Theory and Formal Languages 20


The Language of a CFG – A Proof Example
 Direction

IH: If wLpal and |w|n then wL(Gpal) i.e. S ֜ w

Induction:
• Suppose |w|=n+12
• Since w=wR, we have w=0x0, or w=1x1, and x=xR
Case1:

– If w=0x0, by IH we know that S ֜ x

– Then, by the structure of the grammar S ֜ 0S0 ֜ 0x0 where 0x0=w
Case2:

– If w=1x1, by IH we know that S ֜ x

– Then, by the structure of the grammar S ֜ 1S1 ֜ 1x1 where 1x1=w
BBM401 Automata Theory and Formal Languages 21
The Language of a CFG – A Proof Example
 Direction
Proof: ( Direction)
• We assume that wL(Gpal) and we must show that w=wR.

• Since wL(Gpal), we have S ֜ w

• We prove by induction of the length of ֜ (the length of the derivation sequence)

Basis:

• The derivation S ֜ w is done in one step.
• Then w must be , 0, or 1, they are all palindromes.

BBM401 Automata Theory and Formal Languages 22


The Language of a CFG – A Proof Example
 Direction

IH: If S ֜ w with less than n derivation steps and 1<n then wLpal
Induction:

• Let n2, i.e. S ֜ w derivation takes n steps
• Derivation must be

– S ֜ 0S0 ֜ 0x0 = w or

– S ֜ 1S1 ֜ 1x1 = w
• Since n2, and the productions S→0S0 and S→1S1 are the only productions that
allows additional steps of a derivation.

• Note that, in either case, S ֜ x takes n-1 steps.
• By the inductive hypothesis, we know that x is a palindrome;
• But if so, then 0x0 and lxl are also palindromes.
• We conclude that w is a palindrome, which completes the proof.

BBM401 Automata Theory and Formal Languages 23


Parse Trees

BBM401 Automata Theory and Formal Languages 24


Parse Trees
• Parse trees are an alternative representation to derivations.

• If wL(G), for some CFG, then w has a parse tree, which tells us the (syntactic)
structure of w.
– If G is unambiguous, w can have only one parse tree.
– If G is ambiguous, w may have more than one parse tree.
– Ideally there should be only one parse tree for each string in the language. This means that
the grammar should be unambiguous.
• We may remove the ambiguity from some of ambiguous grammars in order to obtain
unambiguous grammars by making certain assumptions.
• Unfortunately, some CFLs are inherently ambiguous and they can be only defined by
ambiguous grammars.

BBM401 Automata Theory and Formal Languages 25


Constructing Parse Trees
• Let G = (V, T, P, S) be a CFG.

• A tree is a parse tree for G if:

1. Each interior node is labeled by a variable in V.


• The root must be labeled by the start symbol S.

2. Each leaf is labeled by a symbol in T ∪ {}.


• Any -labeled leaf is the only child of its parent.

3. If an interior node is labeled by the variable A, and its children (from left to
right) labeled X1,X2,…,Xk then A → X1X2…Xk  P.

BBM401 Automata Theory and Formal Languages 26


Parse Tree - Example
Grammar: A Derivation Sequence of acb
S → ASB | c S ֜ ASB ֜ aASB ֜ aSB ֜ acB ֜ acbB ֜ acb
A →  | aA
B →  | bB

S
Parse tree of acb
A S B

a A c b B

 

BBM401 Automata Theory and Formal Languages 27


Parse Tree & Derivation
Grammar:
• Each derivation step corresponds to the creation
S → ASB | c of an inner node (by creating its children).
A →  | aA S
B →  | bB
S

BBM401 Automata Theory and Formal Languages 28


Parse Tree & Derivation
Grammar:
• Each derivation step corresponds to the creation
S → ASB | c of an inner node (by creating its children).
A →  | aA S ֜ ASB
B →  | bB
S

A S B

BBM401 Automata Theory and Formal Languages 29


Parse Tree & Derivation
Grammar:
• Each derivation step corresponds to the creation
S → ASB | c of an inner node (by creating its children).
A →  | aA S ֜ ASB ֜ aASB
B →  | bB
S

A S B

a A

BBM401 Automata Theory and Formal Languages 30


Parse Tree & Derivation
Grammar:
• Each derivation step corresponds to the creation
S → ASB | c of an inner node (by creating its children).
A →  | aA S ֜ ASB ֜ aASB ֜ aSB
B →  | bB
S

A S B

a A

BBM401 Automata Theory and Formal Languages 31


Parse Tree & Derivation
Grammar:
• Each derivation step corresponds to the creation
S → ASB | c of an inner node (by creating its children).
A →  | aA S ֜ ASB ֜ aASB ֜ aSB ֜ acB
B →  | bB
S

A S B

a A c

BBM401 Automata Theory and Formal Languages 32


Parse Tree & Derivation
Grammar:
• Each derivation step corresponds to the creation
S → ASB | c of an inner node (by creating its children).
A →  | aA S ֜ ASB ֜ aASB ֜ aSB ֜ acB ֜ acbB
B →  | bB
S

A S B

a A c b B

BBM401 Automata Theory and Formal Languages 33


Parse Tree & Derivation
Grammar:
• Each derivation step corresponds to the creation
S → ASB | c of an inner node (by creating its children).
A →  | aA S ֜ ASB ֜ aASB ֜ aSB ֜ acB ֜ acbB ֜ acb
B →  | bB
S

A S B

a A c b B

 

BBM401 Automata Theory and Formal Languages 34


The Yield of a Parse Tree
• The concatenation of the labels of the leaves in left-to-right order is called the
yield of the parse tree.

• The yield of the parse tree is a string of terminals.


– The set of all yields of all parse trees of a CFG G is the language of G.

• Yield Example: S
a  c b  = acb
A S B

a A c b B

 

BBM401 Automata Theory and Formal Languages 35


Parse Trees, Leftmost and Rightmost Derivations
Theorem: For every parse tree, there is a unique leftmost, and a unique rightmost
derivation.

• We will prove theorem for only leftmost derivations.

• We will prove:

Part 1: If there is a parse tree with root labeled A and yield w, then A ֜lm w.

Part 2: If A ֜lm w, then there is a parse tree with root A and yield w.

BBM401 Automata Theory and Formal Languages 36


Proof – Part 1

Part 1: If there is a parse tree with root labeled A and yield w, then A ֜lm w.
Proof: Induction on the height of the tree.
– The height of a tree is the length of the longest path from the root to a leaf.

Basis: Height is 1. Tree looks like A


– Its yield is a1…an
a1 … an

• A → a1…an must be a production.


• Thus, we have A ֜lm a1…an

• A ֜lm a1…an

BBM401 Automata Theory and Formal Languages 37


Proof – Part 1

Part 1: If there is a parse tree with root labeled A and yield w, then A ֜lm w.

IH: Part 1 holds for the trees with the height < h.

Induction: Take a tree whose height is h. Tree looks like A


– Its yield is w1…wn
– The height of each subtree headed by Xi is less than h. X1 … Xn

• A → X1…Xn must be a production. w1 wn


• A ֜lm X1…Xn since A → X1…Xn is a production.

• Xi ֜lm wi holds for each Xi by IH.

∗ ∗ ∗ ∗
• Thus, A ֜lm X1…Xn ֜lm w1X2…Xn ֜lm w1w2X3…Xn ֜lm … ֜lm w1w2…wn

BBM401 Automata Theory and Formal Languages 38


Proof – Part 2

Part 2: If A ֜lm w, then there is a parse tree with root A and yield w.
Proof:
• Given a leftmost derivation of a terminal string w, we need to prove the existence of a
parse tree with yield w.
• The proof is an induction on the length of the derivation.

Basis: The length of the derivation sequence A ֜lm a1…an is 1.
– That is, the derivation sequence is A ֜lm a1…an

• A → a1…an must be a production.


A
• Thus, there must be a parse tree looks like
– Its yield is a1…an a1 … an

BBM401 Automata Theory and Formal Languages 39


Proof – Part 2

Part 2: If A ֜lm w, then there is a parse tree with root A and yield w.
IH: Part 2 holds for the leftmost derivations with fewer steps than k.

Induction: Take a derivation sequence A ֜lm w with k steps.
– The first step of the derivation sequence is A ֜lm X1…Xn
• w can be divided so the first portion w1 is derived from X1, the next w2 is derived from
X2, and so on. If Xi is a terminal, then wi = Xi.

• That is, each variable Xi has a derivation sequence Xi ֜lm wi .
– And the each derivation takes fewer steps than k steps.
• By the IH, if Xi is a variable, then there is
A
a parse tree with root Xi and yield wi.
• Thus, there is a parse tree. X1 … Xn
– Its yield is w1w2…wn = w
w1 wn
BBM401 Automata Theory and Formal Languages 40
Ambiguity

BBM401 Automata Theory and Formal Languages 41


Ambiguous Grammars
• A CFG is ambiguous if it produces more than one parse tree for a string in the
language.
– i.e. there is a string in the language that is the yield of two or more parse trees.

Example:
S → SaS | b is an ambiguous grammar.
There are two parse trees for the string babab
S S

S a S S a S

S a S b b S a S

b b b b
BBM401 Automata Theory and Formal Languages 42
Ambiguity, Leftmost and Rightmost Derivations
• If there are two different parse trees for a string in the language, they must produce
two different leftmost derivations for that string.
– Conversely, two different leftmost derivations of a string produce two different parse trees
for that string.
• Likewise for rightmost derivations.

• Thus, equivalent definitions of ambiguous grammar are:


1. A CFG is ambiguous if there is a string in the language that has two different
leftmost derivations.
2. A CFG is ambiguous if there is a string in the language that has two different
rightmost derivations.

BBM401 Automata Theory and Formal Languages 43


Ambiguity, Leftmost and Rightmost Derivations
S → SaS | b

• There are two leftmost derivation sequences for the string babab
1. S ֜lm SaS ֜lm SaSaS ֜lm baSaS ֜lm babaS ֜lm babab
2. S ֜lm SaS ֜lm baS ֜lm baSaS ֜lm babaS ֜lm babab

• There are two rightmost derivation sequences for the string babab
1. S ֜rm SaS ֜rm Sab ֜rm SaSab ֜rm Sabab ֜rm babab
2. S ֜rm SaS ֜rm SaSaS ֜rm SaSab ֜rm Sabab ֜rm babab

BBM401 Automata Theory and Formal Languages 44


Ambiguity
• We can create an equivalent CFG (which produces the same language) by eliminating
the ambiguity from the following ambiguous CFG.
S → SaS | b
• In the following unambiguous CFG, we prefer left groupings.
S → Sab | b
• Now, there is only one parse tree for the string babab

S a b

S a b

BBM401 Automata Theory and Formal Languages 45


Ambiguity
• An ambiguous grammar for expressions: E → E+E | E*E | E^E | id | (E)
• 2 parse trees, 2 leftmost and 2 rightmost derivations for the expression id+id*id
E E

E * E E + E

E + E id id E * E

id id id id

E ֜lm E*E ֜lm E+E*E ֜lm id+E*E E ֜lm E+E ֜lm id+E ֜lm id+E*E
֜lm id+id*E ֜lm id+id*id ֜lm id+id*E ֜lm id+id*id

E ֜rm E*E ֜rm E*id ֜rm E+E*id E ֜rm E+E ֜rm E+E*E ֜rm E+E*id
֜rm E+id*id ֜rm id+id*id ֜rm E+id*id ֜rm id+id*id

BBM401 Automata Theory and Formal Languages 46


Ambiguity – Operator Precedence
• Ambiguous grammars (because of ambiguous operators) can be disambiguated
according to the precedence and associativity rules.
E → E+E | E*E | E^E | id | (E)
• Disambiguate this grammar using the following precedence and associativity rules.
Precedence: ^ (right to left)
* (left to right)
+ (left to right)

• Disambiguated grammar:

E → E+T | T
T → T*F | F
F → G^F | G
G → id | (E)

BBM401 Automata Theory and Formal Languages 47


Ambiguity – Operator Precedence
E → E+T | T
T → T*F | F
F → G^F | G
G → id | (E) E

E + T

T T * F
parse tree for id+id*id
F F G

G G id

id id

BBM401 Automata Theory and Formal Languages 48


Inherent Ambiguity
• Some CFLs may have both ambiguous grammars and unambiguous grammar.
– In this case, we may disambiguate their ambiguous grammars.
• Unfortunately, there are some CFLs that do not have any unambiguous grammar.

• A context free language L is said to be inherently ambiguous if all its grammars


are ambiguous.

• If even one grammar for L is unambiguous, then L is an unambiguous language.


– Our expression language is an unambiguous language.
– Even though the first grammar for expressions is ambiguous, there is another language for
the expressions language is unambiguous.

BBM401 Automata Theory and Formal Languages 49


Inherent Ambiguity
• A context free language L is said to be inherently ambiguous if all its grammars
are ambiguous.

Example: Consider L = {anbncmdm : n≥1, m≥1} ∪ {anbmcmdn : n≥1, m≥1}

A grammar for L is
S → AB | C
A → aAb | ab
B → cBd | cd
C → aCd | aDd
D → bDc | bc

BBM401 Automata Theory and Formal Languages 50


Inherent Ambiguity
• The parse trees for the string aabbccdd.

S ֜lm AB ֜lm aAbB ֜lm aabbB


֜lm aabbcBd ֜lm aabbccdd S ֜lm C ֜lm aCd֜lm aaCdd
֜lm aabDcdd ֜lm aabbccdd
• It can be shown that every grammar for L behaves like the one above. The language L
is inherently ambiguous.
BBM401 Automata Theory and Formal Languages 51
CFG – Questions
• Design context-free grammars for the following languages:

• {0n1m : n>m≥0}

S → 0S1 | 0A
A →  | 0A

• The strings of 0’s and 1’s that contain equal number of 0’s and 1’s.

S → 0S1S | 1S0S | 

BBM401 Automata Theory and Formal Languages 52


CFG – Questions
• Design context-free grammars for the following language:
• {0n1n : n≥0} ∪ {1n 0n : n≥0}
S→ A | B
A → 0A1 | 
B → 1B0 | 
• Is this grammar ambiguous?
YES: Two leftmost derivations for 
S ֜lm A ֜lm  S ֜lm B ֜lm 

• Disambiguate this grammar.


S→ A | B | 
A → 0A1 | 01
B → 1B0 | 10
BBM401 Automata Theory and Formal Languages 53
Is Every Regular Language a CFL?
• Every regular language is a CFL.

Create a CFG for a given regular language whose DFA is given:


• A DFA M = (Q,Σ,δ,q0,F) is given, define the CFG G = (V,T,P,S) as follows:
V = { Si | qi is in Q }
T=Σ
P = { Si → aSj | δ(qi,a) = qj } ∪ { Si → ε | qi is in F}
S = S0

Then prove the correctness.

BBM401 Automata Theory and Formal Languages 54


Is Every Regular Language a CFL?
• Create a CFG for the following DFA:

S0 → 0S2 | 1S1 | ε
S1 → 0S3 | 1S0
S2 → 0S0 | 1S3
S3 → 0S1 | 1S2

• Every regular language can be defined by a right linear grammar.


• A right linear grammar rule must be in one of the following forms:
A→ε
A→a
A → aB where A,B are variables and a is a terminal

BBM401 Automata Theory and Formal Languages 55

You might also like