0% found this document useful (0 votes)
361 views59 pages

Formal Languages and Automata Theory: CH 4: Context Free Languages

Uploaded by

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

Formal Languages and Automata Theory: CH 4: Context Free Languages

Uploaded by

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

Formal Languages and

Automata Theory
CH 4:
Context Free Languages
Outline
• Introduction
• Context Free Grammars
• Parsing and Ambiguity
• Context Free Grammars and Programming
Languages
Introduction
• Context-free languages are applied in parser design
(compilers).
• They are also useful for describing block structures
in programming languages.
• It is easy to visualize derivations in context-free
languages as we can represent derivations using
tree structures.
• In this chapter we will discuss about Context Free
Grammars, Parsing and Ambiguity, and connection
between CFGs and Programming Languages.
Context Free Grammar (CFG)
Definition of CFG
• A context-free grammar is a 4-tuple (V, T, S, P) where
i. V is a finite set called the variables
ii. T is a finite set, disjoint from V, called the terminals
iii. P is a finite set of rules, with each rule being a variable and a
string of variables and terminals, and
iv. SV is the start variable.
• Every production in a CFG is of the form A ® a, where
AÎV and aÎ(V È T)*.
• If u, v and w are strings of variables and terminals, and A
w is a rule of the grammar, we say that uAv yields uwv,
written uAv ⇒ uwv.
Context Free Grammar (CFG)
Example of CFG
• Given a grammar G = ({S}, {a, b}, P, S).
• The set of rules or productions is
S  aSb
S  SS
Se
• This grammar generates strings such as
abab, aaabbb, aababb, …
• And If we assume that a is left parenthesis ‘(’ and b is right
parenthesis ‘)’, then L(G) is the language of all strings of
properly nested parenthesis.
()(), ((())), (()()), …
Context Free Grammar (CFG)
Example: Construct a context-free grammar G generating all integers
(with sign).
Solution: Let G = (VN, S, P, S) , where
V = { S, ásignñ, ádigitñ, áintegerñ },
T = {0, 1, 2, 3, . . . , 9, +, – } and
P consists of
S ® ásignñ áintegerñ ,
ásignñ ® +| – ,
áintegerñ ® ádigitñ áintegerñ | ádigitñ ,
ádigitñ ® 0|1|2|...|9
• So, L(G) = the set of all integers.
• For example, the derivation of -17 can be obtained as follows:
S Þ ásignñ áintegerñ Þ – á integerñ Þ – á digitñ áintegerñ
Þ – 1 áintegerñ Þ – 1 ádigitñ Þ – 17
Context Free Grammar (CFG)
Definition: A derivation tree (also called a parse tree) for
a CFG G = (VN, S, P, S) is a tree satisfying the
following conditions:
i. Every vertex has a label which is a variable or terminal or
e.
ii. The root has label S.
iii. The label of an internal vertex is a variable or nonterminal.
iv. If the vertices n1, n2 , . . . , nk written with labels X1, X2 , . . .
, Xk are the sons of vertex n with label A, then A ® X1X2 . .
. Xk is a production in P.
v. A vertex n is a leaf if its label is a Î S or e; n is the only son
of its father if its label is e.
Context Free Grammar (CFG)
Example: Let G = ({S, A}, {a, b}, P, S), where P consists of S
® aAS | a | SS, A ® SbA | ba.
– The figure below is an example of a derivation tree.
Context Free Grammar (CFG)
Ordering of Leaves from the Left in DT
Definition: The yield of a derivation tree is the
concatenation of the labels of the leaves
without repetition in the left-to-right ordering.
– For example, the yield of the derivation tree of above
example is aabaa.
• And the yield of the derivation tree is a sentential
form in G.
– For example, S ® aAS | a | SS, A ® SbA | ba , the derivation of aabaa
is
S Þ SS Þ aS Þ aaAS Þ aabaS Þ aabaa
Context Free Grammar (CFG)
Definition: A subtree of a derivation tree T is a tree
i. whose root is some vertex v of T,
ii. whose vertices are the descendants of v together with
their labels, and
iii. whose edges are those connecting the descendants.
Context Free Grammar (CFG)
Leftmost and Rightmost Derivations
Definition: ∗

 
• A derivation A w is called a leftmost derivation if we apply
a production only to the leftmost variable at every step.
S Þ aAS Þ asbAS Þ aabAS Þ aabbaS Þaabbaa
Definition:
• A derivation A ⇒

 w is a rightmost derivation if we apply


production to the rightmost variable at every step.
S Þ aAS Þ aAa Þ aSbAa Þ aSbbaa Þ aabbaa
Note:

• If A ⇒
  w in G, then there is a leftmost derivation of w.
Context Free Grammar (CFG)
Example: Let G be the grammar
S ® 0B | 1A, A ® 0 | 0S | 1AA, B ® 1 | 1S | 0BB.
• For the string 00110101, find
(a) the leftmost derivation,
(b) the rightmost derivation, and
(c) the derivation tree.

Solution:
a) S Þ 0B Þ 00BB Þ 001B Þ 0011S Þ 00110B Þ
001101S Þ 0011010B Þ 00110101
b) S Þ 0B Þ 00BB Þ 00B1S Þ 00B10B Þ 00B101S
Þ 00B1010B Þ 00Bl0101 Þ 00110101.
Context Free Grammar (CFG)
Example: cont...
c) Derivation tree for the given string 0011010:
Parsing and Ambiguity
Parsing
• A grammar can be used in two ways:
a) Using the grammar to generate strings of the language.
b) Using the grammar to recognize the strings.
• “Parsing” a string is finding a derivation (or a
derivation tree) for that string.
– Parsing a string is like recognizing a string.
– The only realistic way to recognize a string of a
context-free grammar is to parse it.
Parsing and Ambiguity
Exhaustive Search Parsing
• The basic idea is to parse a string w, generate all
strings in L and check if w is among them.
• Problem arises when L is an infinite language.
– Therefore a systematic approach is needed to achieve this,
as it is required to know that no strings are overlooked.
– And also it is necessary so as to stop after a finite number of
steps.
• The idea of exhaustive search parsing for a string is to
generate all strings of length no greater than |w|, and
see if w is among them.
Parsing and Ambiguity
Exhaustive Search Parsing
• The restrictions that are placed on the
grammar will allow us to generate any string
w∈L in at most 2|w|–1 derivation steps.
• Exhaustive search parsing is inefficient.
– It requires time exponential in |w|.
• There are ways to further restrict context free
grammar so that strings may be parsed in linear
or non-linear time.
Parsing and Ambiguity
Topdown / Bottomup Parsing
• If the sequence of rules or productions are applied in a leftmost
derivation then it is called Topdown parsing.
• If the sequence of rules or productions are applied in a rightmost
derivation then it is called Bottomup parsing.
• Consider the grammar G with production
1. S  aSS
2. S  b
– The parse trees are as follows.
Topdown Parsing of Bottomup Parsing of
aababbb → Left aababbb → Right
parse of the string parse of the string
with the sequence with the sequence
1121222. 2221121.
Parsing and Ambiguity
Ambiguity
Definition:
• A terminal string wÎL(G) is ambiguous if
(i) there exist two or more derivation trees for w, or
(ii) there exist two or more leftmost derivations of w.

Definition:
• A context-free grammar G is ambiguous if
there exists some wÎL(G) which is ambiguous.
Parsing and Ambiguity
Example:
• Consider G = ({S}, {a, b, +, *}, P, S), where P
consists of S ® S + S | S * S | a | b.
– Then, we have two derivation trees for w=a+ab:
which is ambiguous:
Parsing and Ambiguity
Example:
• If G is the grammar S ® SbS | a, show that G is ambiguous.
Solution:
• To prove that G is ambiguous, we have to find a w Î L(G), which is
ambiguous:
– Consider w = abababa Î L(G).
– Then we get two derivation trees for w, as follows:

• Thus G is Ambiguous.
Context Free Grammars and Programming
Languages
• Context free grammars are used to describe
programming languages.
• There is a mathematical way of turning language
description as a CFG into a parser.
• Where a parser is a component of a compiler that
discovers the structure of the source program and
represents that structure by a parse tree.
• Another use of a CFG is that, it describes the
allowable tags and the ways in which these tags
may be nested in XML Document Type Definition.
Formal Languages and
Automata Theory
CH 5:
Simplification of Context Free Grammars
and Normal Forms
Outline
• Introduction
• Methods for Transforming Grammars
• Normal Forms
– Chomsky Normal Form(CNF)
– Greibach Normal Form(GNF)
Introduction
• In a Context Free Grammar(CFG), it may not be necessary to
use
– all the symbols in (VN  S) , or
– all the production rules in P while deriving sentences.
• Let us try to eliminate symbols and productions in G which are
not useful in deriving sentences.
• Let G = (VN, S, S, P) be a context-free grammar.
– Suppose that P contains a production of the form
A  x1Bx2.
– Assume that A and B are different variables and that
B  y1|y2|…|yn
is the set of all productions in P which have B as the left side.
Introduction
  = (VN, S, S, ) be the grammar in which is constructed
• Let
by deleting A  x1Bx2 from P, and adding to it
A  x1y1x2 | x1y2x2 | . . . | x1ynx2
• Then L (G) = L().
Substitution Rule
• A production A  x1Bx2 can be eliminated from a
grammar if we put in its place the set of productions in
which B is replaced by all strings it derives in one step.
• In this result, it is necessary that A and B are different
variables.
Methods for Transforming Grammars
• For example, consider G = ({S, A, B, C, E}, {a, b, c}, P, S) where
P = {S ® AB, A ® a, B ® b, B ® C, E ® c | e }
– It is easy to see that L(G) = {ab}.
– Now, let G' = ({S, A, B}, {a, b}, P', S), where P' consists of
S ® AB, A ® a, B ® b,
and then L(G) = L(G').
– We have eliminated the symbols C, E and c and the productions B ® C, E ® c and E ® e,
from previous grammar G.
• We note the following points regarding the symbols and productions which are
eliminated:
– C does not derive any terminal string.
– E and c do not appear in any sentential form.
– E ® e is a null production.
– B ® C is a unit production, simply replaces B by C.
• So we simplify a given grammar by eliminating this type of variables, terminals and
productions.
Methods for Transforming Grammars

• In order to get a simplified CFG G we have to


eliminate:
i. Variables not deriving terminal strings,
ii. Symbols not appearing in any sentential form,
iii. Null productions of the form A ® e, and
iv. Unit productions of the form A ® B.
• In the following sections, we will see the
construction techniques to eliminate useless
symbols and useless productions.
Methods for Transforming Grammars

Elimination of Useless Productions


• In the grammar G with P, S  aSb|λ|A, A  aA
• – The production SA does not play any role because A cannot be transformed into a terminal
string.
– ‘A’ can occur in a string derived from S, this can never lead to a sentential form.
– Hence this production rule can be removed, which does not affect the language.
Definition: Let G = (VN, S, S, P) be a CFG.
• A variable A ∈ VN is said to be “useful” iff there is at least one w L(G) such that
S xAy w with x, y in (VNS)*,
• That is, a variable is useful iff it occurs in at least one derivation.
Illustration:
– Consider the grammar G with P,
S  A,
A  aA | λ,
B  bA
– Here the variable B is said to be “useless”, hence the production B  bA is also “useless”,
since there is no way to achieve S xBy .
Methods for Transforming Grammars

Elimination of Useless Productions


•  
Theorem:
• Let G = (VN, S, S, P) be a CFG. There exists an equivalent grammar = (, , S, ) that does not
contain any useless variables or productions.
Procedure:
• The first Part-A is to find G1 using the algorithm.
Step 1: Set V1 to ∅
Step 2: Repeat the following step until no more variables are added to V1.
– For every A∈ VN for which P has a production of the form
– A  x1x2 . . . xn , with all xi in V1S , then add A to V1.
Step 3: Take P1 as all the productions in P whose symbols are all in (V1S).
– Thus the grammar G1 can be generated from G by the above algorithm.
– Here G = (V1, S2, S, P1) such that V1 contains only variables A for which
A w S*
– The next step is to check whether every A for which A w = ab . . . is added to V1 before the procedure
terminates.
• Step below describes the second Part-B.
– “Dependency graph” is drawn to find all the variables that cannot be reached from the start symbol S.
– These variables are removed from the variable set and also all the productions involving the variables.
• The resultant obtained is .
Methods for Transforming Grammars

Elimination of Useless Productions


•  Null/Empty Production Removal
(a)
• The productions of context-free grammars can be coerced into a
variety of forms without affecting the expressive power of the
grammars.
• If the empty string does not belong to a language, then there is a way
to eliminate the productions of the form Aλ from the grammar.
– If the empty string belongs to a language, then we can eliminate λ from all
productions save for the single production S → λ.
– In this case we can also eliminate any occurrences of S from the right-hand
side of productions.

• Any production of a CFG of the form Aλ is called a λ-production.


• Any variable A for which the derivation A λ is possible, is called
“Nullable”.
Methods for Transforming Grammars

Elimination of Useless Productions


•  Null/Empty Production Removal
(a)
• Let G be any CFG with λ not in L(G). Then there exists an
equivalent grammar having no λ-productions.
Procedure to find CFG without λ-Productions
Step 1: For all productions Aλ, put A into V’.
Step 2: Repeat the following steps until no further variables are added to
V’.
• For all productions
B  A1A2 … An ,
where A1, A2, … An are in V’, put B into V’.
• To find , let us consider all productions in P of the form A  x 1 x 2 … xm ,
m≥1 for each xi(VNS).
• For each such production of P, we put into that production as well as all those
generated by replacing nullable variables with λ in all possible combinations.
– If all xi are nullable, the production A  λ is not put into .
Methods for Transforming Grammars

Elimination of Useless Productions


•(b)  Unit Production Removal
• Any production of a CFG of the form AB where
A, BVN, is called a “Unit-production”.
– Having variable one on either side of a production is
sometimes undesirable.
– We use the substitution rule for removing the unit-
productions.
Definition:
– Given G = (VN, S, S, P), a CFG with no λ-productions,
there exists a CFG = (, , S, ) that does not have any unit-
productions and that is equivalent to G.
Methods for Transforming Grammars

Elimination of Useless Productions


•  Unit Production Removal
(b)
Procedure to remove the unit productions:
• Find all variables B, for each A such that
AB
• This is done by sketching a “depending graph” with an edge
(C, D) whenever the grammar has unit-production CD,
then AB holds whenever there is a walk between A and B.
– The new grammar , equivalent to G is obtained by letting into all
non-unit productions of P.
– Then for all A and B satisfying AB, we add to
A  y1|y2|… |yn
where B  y1|y2|… |yn is the set of all rules in with B on the left.
Methods for Transforming Grammars

Elimination of Useless Productions


(c) Left Recursion Removal
Definition:
– A variable A is left-recursive if it occurs in a
production of the form AAx for any x(VN S)*.

– A grammar is left-recursive if it contains at least


one left-recursive variable.

– Every content-free language can be represented by


a grammar that is not left-recursive.
Methods for Transforming Grammars

Elimination of Useless Productions


•Example
  1:
• Given a CFG as G = ({S, A, B, C, E},{a, b, c}, P, S)
with production P given by
SAB
Aa
Bb
BC
Ec |λ
• Obtain L(G) and obtain an equivalent grammar
L() by eliminating useless terminals and
productions.
Methods for Transforming Grammars

Elimination of Useless Productions


•  
Example 1:
Solution:
• L(G) is obtained as follows:
S⇒AB⇒aB⇒ab.
– Therefore, L(G) = {ab}.
– Here =({S, A, B},{a, b}, S,) where P′ has the production rules
SAB
Aa
Bb
– We have eliminated C as it does not derive terminal string.
– E and C do not appear in any sentential form.
– E λ is a null production and hence eliminated.
– BC simply replaces B by C.
Methods for Transforming Grammars

Elimination of Useless Productions


Example 2: Given G = (VN, S, S, P) with P given by
S  aS|A|C
Aa
B  aa
C  aCb
– Eliminate the useless symbols and productions from G.
Solution:
• Here VN = {S, A, B, C}.
• Let us determine the set of variables that can lead to a terminal string.
– Since A  a and B  aa, implies A and B belong to this set.
– Also S belongs to this set, since S⇒A⇒a.
• But C does not belong to this set because C does not produce terminals since C  aCb.
– Thus C is removed and its corresponding productions are also removed.
S  aS|A
Aa
B  aa
– Here V1 = {S, A, B}.
Methods for Transforming Grammars

Elimination of Useless Productions


Example 3: Given a CFG with P given by
S  aS1b
S1  aS1b|λ
• Obtain a new set of P for a grammar same as the given CFG.
Solution:
• The given grammar generates the λ-free language given by L(G) = {anbn
| n ≥ 1}.
• The λ-production in P, S1  λ is removed after adding new
productions by substituting λ for S1 where it occurs on the right.
– Hence we get
S  aS1b |ab
S1  aS1b|ab
– which is the new set of P that produces the same language given by {anbn | n
≥ 1}.
Methods for Transforming Grammars

Elimination of Useless Productions


Example 4: Determine a CFG without λ-production equivalent to the grammar given
by P as
S  ABaC
A  BC
B  b|λ
C  D|λ
Dd
Solution
• We use the following steps to find CFG without λ-productions.
Step 1: The “Nullable variables” are A, B and C.
Step 2:

– The above set of rules represent P′ for the new CFG after elimination of λ-productions.
Methods for Transforming Grammars

Elimination of Useless Productions


•  
Example 5: Eliminate unit productions from the grammar G given by productions P:

Solution: A  a, B  b, E  a are nonunit production.


– Therefore will contain these productions.
– Since BE, and Ea is a non-unit production, include B  a in .
– Since C E, D E include C  a, D  a in .
• Hence we have the equivalent grammar without unit productions as defined by
=({S, A, B, C, D, E}, {a, b}, , S)
with given by
Methods for Transforming Grammars

Elimination of Useless Productions


•  
Example 6: Eliminate the unit-production from the CFG with P given by:

Solution:
• From the given P we shall draw the dependency graph as follows:

• From this we see that:

• Therefore these rules are added to the original non-unit productions

the following new rules in order to obtain

which is for the new grammar generated equivalent to the given grammar.
Methods for Transforming Grammars

Elimination of Useless Productions


•  
Example 7: Given the CFG with P given by

• Eliminate the λ-productions to obtain for an equivalent CFG


Solution:
• A and B are “Nullable Symbols” as they have λ-productions.
• S is also “Nullable”, because it has the production SAB, which has only “Nullable
symbols”, A and B.

• Therefore, the new set of productions for the grammar equivalent to the given CFG
is:
Normal Forms
• In a context-free grammar, the R.H.S. of a
production can be any string of variables and
terminals.
• When the productions in G satisfy certain
restrictions, then G is said to be in a 'normal
form'.
• Among several 'normal forms' we will study the
following tow forms:
– The Chomsky normal form (CNF) and
– The Greibach normal form (GNF).
Normal Forms
Chomsky Normal Form (CNF)
• Any context-free language L without any λ-production is
generated by a grammar is which productions are of the
form
A  BC or
A  a,
where A, BVN , and a  S.
Procedure to find Equivalent Grammar in CNF
i. Eliminate the unit productions, and λ-productions if any,
ii. Eliminate the terminals on the right hand side of length two
or more.
iii. Restrict the number of variables on the right hand side of
productions to two.
Normal Forms
Chomsky Normal Form (CNF)
•  
Proof:
• For Step (i): Apply the following theorem:
– “Every context free language can be generated by a grammar with no useless symbols and
no unit productions”.
– At the end of this step the RHS of any production has a single terminal or two or more
symbols.
– Let us assume the equivalent resulting grammar as G=(VN, S, P, S).
• For Step (ii): Consider any production of the form
A y1y2 . . . ym, m ≥ 2.
• If y1 is a terminal, say ‘a’, then introduce a new variable Baand a production
Ba a
• Repeat this for every terminal on RHS.
– Let P′ be the set of productions in P together with the new productions Baa.
– Let V’N be the set of variables in V’N together with Ba’s introduced for every terminal on
RHS.
– The resulting grammar G1 = (V’N, S, P’, S) is equivalent to G and every production in P′ has
either a single terminal or two or more variables.
Normal Forms
Chomsky Normal Form (CNF)
•Proof:
 
• For step (iii): Consider A B1B2 . . . Bm, m ≥ 2 where Bi’s are
variables and m ≥ 3.
– If m = 2, then A B1B2, is in proper form.
– The production A B1B2 . . . Bm is replaced by new productions
A B1D1
D1 B2D2
D2 B3D3
… …
Dm-2 Bm-1Bm
where Di’s are new variables.
– The grammar thus obtained is G2, which is in CNF.
Normal Forms
Chomsky Normal Form (CNF)
Example 1: Obtain a grammar in Chomsky Normal Form (CNF) equivalent to the grammar G with
productions P given
S  aAbB
A  aA|a
B  bB|b.
Solution:
i. There are no unit productions in the given set of P.
ii. Amongst the given productions, we have A  a, B  b, which are in proper form.
• For S  aAbB, we have S  BaABbB, Ba  a, Bb  b.
• For A  aA, we have A  BaA .
• For B  bB, we have B  BbB .
– Therefore, we have G1 given by G1 = ({ S, A, B, Ba, Bb}, { a, b}, P’, S) where P′ has the productions S B AB B A B
ABBBBaBbAaBb
S  BaABbB,
A  BaA,
B  BbB,
Ba  a,
Bb  b,
A  a,
B  b.
Normal Forms
Chomsky Normal Form (CNF)
Example 1: Obtain a grammar in Chomsky Normal Form (CNF) equivalent to the grammar G with
productions P given
S  aAbB
A  aA|a
B  bB|b.
Solution:
iii. In P′ above, we have only S  BaABbB not in proper form.
– Hence we assume new variables D1 and D2 and the productions
S  BaD1
D1  AaD2
D2  BbB
– Therefore the grammar in Chomsky Normal Form (CNF) is G2 with the productions given by
S  BaD1
D1  AaD2
D2  BbB,
A  BaA,
B  BbB,
Ba  a,
Bb  b,
A  a, and
B  b.
Normal Forms
Chomsky Normal Form (CNF)
Example 2: Obtain a grammar in Chomsky Normal Form (CNF) equivalent to the
grammar G with productions P given by
S  ABa
A  aab
B  Ac
Solution:
i. The given set P does not have any unit productions or λ-productions.
ii. None of the given rules is in proper form.
• For S  ABa, we have S  ABBa and Ba  a.
• For A  aab, we have A  BaBaBb and Bb  b.
• For B  Ac, we have B  ABc and Bc  c.
– Therefore G1 has a set of productions P’ given by
S  ABBa
A  BaBaBb
B  Abc
Ba  a
Bb  b
Bc  c
Normal Forms
Chomsky Normal Form (CNF)
Example 2: Obtain a grammar in Chomsky Normal Form (CNF) equivalent to the grammar G with
productions P given by
S  ABa
A  aab
B  Ac
Solution:

iii. In P’ above, we have S  ABBa and A  BaBaBb not in proper form.


– Hence we assume new variables D1 and D1 and the productions
S  AD1
D1  BBa
A  BaD2
D2  BaBb
– Thus the grammar in Chomsky Normal Form (CNF) is G2 given by the productions given by
S  AD1
D1  BBa
A  BaD2
D2  BaBb
B  Abc
Ba  a
Bb  b
Bc  c
Normal Forms
Greibach Normal Form (GNF)
• In Chomsky’s Normal Form (CNF), restrictions are put on the length of right
sides of a production, whereas
• In Greibach Normal Form (GNF), restriction are put on the positions in
which terminals and variables can appear.
• GNF is useful in simplifying some proofs and making constructions such as
Push Down Automaton (PDA) accepting a CFG.
Definition:
• A context-free grammar is in Greibach Normal Form if every production is of
the form
A ® a a,
where
a ÎVN* and a ÎS (a may be l) and
– S ® l is in G if l Î L(G), and in this case, S does not appear on the R.H.S. of any
production.
• For example, G given by S ® aAB | A, A ® bC, B ® b, C ® c is in GNF.
Normal Forms
Greibach Normal Form (GNF)
Theorem: (Reduction to GNF).
• Every context-free language L can be generated by a context-free grammar G
in Greibach Normal Form.
Proof: Case 1: When eÏL(G), we apply the following 5-steps:

Step 1: Eliminate null productions and then construct a grammar G in CNF generating L.
– We rename the variables as A1, A2, ... , An with S = A1.
– We write G as ({A1, A2, ... , An}, Σ, P, A1).

Step 2: Convert the Ai-productions (i = 1, 2, . . . , n-1)


Ai ® ag or Ai ® Ajg such that j > i.
– In order to get the Ai-productions in the required form, we apply the following lemma:
Lemma1:
• If A ® Bg and B ® b1|b1|. . .|bs are A- and B-productions in P then we define:
P1 = (P – {A ® Bg }) È {A ® big | 1 £ i £ s }.
Normal Forms
Greibach Normal Form (GNF)
Proof: Case 1: When eÏL(G), we apply the following 5-steps:

Step 3: Convert An-productions to the form An ® ag.


– Here, the productions of the form An ® Ang are eliminated using the following
lemma and introducing new variable Zn.
Lemma2:
• Let the set of A-productions be An ® Anα1|...|Anαr|b1|...|bs (bi's do not start with An).
• Let Zn be a new variable.
– Let G1 = (VN È{Z}, S, P1, S), where P1 is defined as follows:
• The set of A-productions in P1 are
An ® b1|b2|...|bs,
An ® b1Zn | b2Zn | ... |bsZn
• The set of Z-productions in P1 are
Zn ® a1 | a2 | . . . | ar ,
Zn ® a1Zn | a2Zn | . . . | arZn
• The productions for the other variables are as in P.
Normal Forms
Greibach Normal Form (GNF)
Proof: Case 1: When eÏL(G), we apply the following 5-steps:

Step 4: Modify the Ai-productions to the form Ai ® ag for i = 1, ... , n–1.


– At the end of Step 3,
• the An-productions are of the form An ® ag.
• the An-1-productions are of the form An-1 ® ag' or An-1 ® Ang.
• we eliminate productions of the form An-1 ® Ang by applying Lemma1 until resulting An-1-
productions are in the required form.
• We repeat the construction by considering An-2, An-3, ... , A1.

Step 5: Modify Zi-productions.


– Every time we apply Lemma2 in Step 3 for Ai-productions, we get a new variable Zi.
• The Zi-productlons are of the form Zi ® a Zi or Zi ® a, where a is obtained from Ai ® Aia, and
hence of the form Zi ® ag or Zi ® Akg for some k.
• At the end of Step 4, the R.H.S. of any Ak-production starts with a terminal.
• So we can apply method in Step 2 to eliminate Zi ® Akg.
– Thus at the end of Step 5, we get an equivalent grammar G1 in GNF.
Normal Forms
Greibach Normal Form (GNF)
Example: Construct a grammar in GNF equivalent to the grammar
S ® AA|a, A ® SS|b.
Solution:
Step1: Eliminate null productions and convert G to CNF.
– The given grammar is in CNF.
– S and A are renamed as A1 and A2, respectively.
• So the productions are A1 ® A2A2|a, and A2 ® A1A1|b.
– As the given grammar has no null productions and is in CNF we need not carry
out Step 1, so we proceed to Step 2.
Step 2: Convert the Ai-productions (i = 1, 2, ... , n–1)
Ai ® ag or Ai ® Ajg such that j > i.
(i) A1-productions are in the required form: A1 ® A2A2|a.
(ii) A2 ® b is in the required form and we apply Lemma1 to A2 ® A1A1.
• The resulting productions are: A2 ® A2A2A1, A2 ® aA1.
• Thus the A2-productions are: A2 ® A2A2A1, A2 ® aA1, A2 ® b.
Normal Forms
Greibach Normal Form (GNF)
Example: Construct a grammar in GNF equivalent to the grammar
S ® AA|a, A ® SS|b.
Solution:
Step 3: Convert An-productions of form An ® Ang to the form An ® ag, by applying Lemma2.
– We have to apply Lemma2 to A2-productions as we have A2 ® A2A2A1, and let Z2 to be the
new variable.
– The resulting productions are:
A2 ® aA1, A2 ® b
A2 ® aA1Z2, A2 ® bZ2
Z2 ® A2A1, Z2 ® A2A1Z2
Step 4: Modify the Ai-productions to the form Ai ® ag for i = 1,2, . . . , n–1 by applying
Lemma 1.
– The A2-productions are: A2 ® aA1|b|aA1Z2 | bZ2.
– Among the A1-productions we retain A1 ® a and eliminate A1® A2 A2 using Lemma 1.
– The resulting productions are: A1 ® aA1A2|bA2, A1 ® aA1Z2A2|bZ2A2
– The set of all (modified) A1-productions is: A1 ® a|aA1A2|bA2|aA1Z2A2|bZ2A2
Normal Forms
Greibach Normal Form (GNF)
Example: Construct a grammar in GNF equivalent to the grammar
S ® AA|a, A ® SS|b.
Solution:
Step 5: Modify Zi-productions, which are of the form
Zi ® ag or Zi ® Akg for some k.
– The Z2-productions to be modified are: Z2 ® A2A1, Z2 ® A2A1Z2.
– We apply Lemma 1 and get:
Z2 ® aA1A1|bA1|aA1Z2A1|bZ2A1
Z2 ® aA1A1Z2|bA1Z2|aA1Z2A1Z2|bZ2A1Z2
• Hence the equivalent grammar is G' = ({A1, A2, Z2}, {a, b}, P1, A1) where P1
consists of
A1 ® a|aA1A2|bA2|aA1Z2A2|bZ2A2
A2 ® aA1|b|aA1Z2 | bZ2
Z2 ® aA1A1|bA1|aA1Z2A1|bZ2A1
Z2 ® aA1A1Z2|bA1Z2|aA1Z2A1Z2|bZ2A1Z2
• G' is the required equivalent grammar in GNF.
Formal Languages and
Automata Theory
CH 6:
Pushdown Automata
Outline
• Introduction
• Nondeterministic Push down Automata
• Push down Automata and Context Free
Languages
• Deterministic Push down Automata and
Deterministic Context Free Languages

You might also like