You are on page 1of 41

Chapter Five

Contents

◼ Context Free Grammars


◼ Parsing Arithmetic Expression
◼ Removing λ-productions
◼ Normal forms

June 15, 2022 FLAT Chapter Five 2


Context Free Grammar (CFG)

◼ Definition: A CFG, G, is a PSG G=(N, T, P, S) with


productions of the form A → β, A Є N, β Є (NUT)*.
◼ CFGs are used in defining the syntax of programming
languages and in parsing arithmetic expressions.
◼ A language generated from CFG is called Context Free
Language (CFL).
◼ Ex.
a) S → aB b) S → aB|A
B → bA|b aA → aA|a|CBA
A→a B→λ
C→c

June 15, 2022 FLAT Chapter Five 3


◼ Examples of CFGs:
The grammar ({A}, {a, b, c}, P, A),
P : A → aA, A → abc.
The grammar ({S, a, b}, {a, b}, P, S),
P: S → aSa, S → bSb, S → λ
The grammar ({S, F}, {0, 1}, P, S),
P: S → 00S | 11F, F → 00F | λ

June 15, 2022 FLAT Chapter Five 4


Derivation
◼ Derivation is a sequence of production rules.
◼ It is used to get the input string through these
production rules.
◼ During parsing we have to take two decisions. These are
as follows:
❑ We have to decide the non-terminal which is to be replaced.
❑ We have to decide the production rule by which the non-
terminal will be replaced.
◼ We have two options to decide which non-terminal to be
replaced with production rule.
1. Left-most Derivation

2. Right-most Derivation

June 15, 2022 FLAT Chapter Five 5


Left-most Derivation
◼ In the left most derivation, the input is scanned and
replaced with the production rule from left to right. So in
left most derivatives we read the input string from left to
right.
◼ Left most derivation: employs the reduction of the left
most non-terminal
a-b+c
Example:
S=S+S
S=S-S
S = a | b |c
Input: a-b+c
S=S+S
S=S-S+S
S=a-S+S
S=a-b+S
S=a-b+c
June 15, 2022 FLAT Chapter Five 6
◼ Right-most Derivation
◼ In the right most derivation, the input is scanned and
replaced with the production rule from right to left. So in
right most derivatives we read the input string from right
to left.
◼ Right most derivation: employs the reduction of the
right most non-terminal

Example: Right most Derivation


S=S+S S=S-S
S=S-S+S
S=S-S
S=S-S+c
S = a | b |c S=S-b+c
Input:a-b+c S=a-b+c

June 15, 2022 FLAT Chapter Five 7


◼ Parse Tree
• Let G be a CFG, then x Є L(G) iff S ➔ x in zero or more steps
over G.
• x Є L(G) can as well be obtained from a derivation tree or parse
tree.
▪ Parse tree is a hierarchical structure which represents the
derivation of the grammar to yield input strings
▪ Root node of parse tree has the start symbol of the given
grammar from where the derivation proceeds.
▪ Leaves of parse tree represent terminals.
▪ Each interior node represents productions of grammar.

June 15, 2022 FLAT Chapter Five 8


June 15, 2022 FLAT Chapter Five 9
◼ Derivation or Yield of a Tree
◼ The derivation or the yield of a parse tree is the final
string obtained by concatenating the labels of the
leaves of the tree from left to right, ignoring the
Nulls.
◼ However, if all the leaves are Null, derivation is Null.

June 15, 2022 FLAT Chapter Five 10


Example
Let a CFG {N,T,P,S} be
N = {S}, T = {a, b}, Starting symbol = S, P = S → SS | aSb | ε

One derivation from the above CFG is “abaabb”


S → SS → aSbS → abS → abaSb → abaaSbb → abaabb

June 15, 2022 FLAT Chapter Five 11


◼ Ambiguity in Context-Free Grammars
❑ If a context free grammar G has more than
one derivation tree for some string w ∈ L(G),
it is called an ambiguous grammar.
❑ There exist multiple right-most or left-most
derivations for some string generated from
that grammar.
Example:
Check whether the grammar G with production rules
S→ S+S | S*S |S| a is ambiguous or not.

June 15, 2022 FLAT Chapter Five 12


Solution
◼ Let’s find out the derivation tree for the string
"a+a*a". It has two leftmost derivations.
◼ Derivation 1

S => S+S => a +S => a+ S*S=>a+a*S => a+a*a


◼ Derivation 2
S => S*S => S+S*S=> a+ S*S=>a+a*S => a+a*a

June 15, 2022 FLAT Chapter Five 13


◼ Parse Tree-1: Parse Tree-2:

Since there are two parse trees for a single string "a+a*a", the
grammar G is ambiguous

June 15, 2022 FLAT Chapter Five 14


Exercises:
1. G1 = (N, T, P, S) with productions:
S → AB
A → aA|a
B → bB|b
let x = aaabbb
a) find a left most and right most derivations for x
b) draw the parse tree for x
2. G2 = (N, T, P, S) with productions:
S → SbS|ScS|a
let x = abaca Є L(G2)
a) find a left most and right most derivations for x
b) draw the parse tree for x
3. Is G1 ambiguous? Is G2?

June 15, 2022 FLAT Chapter Five 15


Parsing Arithmetic Expression

◼ Consider the following grammar:


E→T|E+T|E–T
T → F | T * F | T/F
F → a | b | c | (E)

Draw parse trees for


a) a*b+c b) a+b*c c) (a+b)*c d) a-b-c

June 15, 2022 FLAT Chapter Five 16


◼ Removing λ-productions
◼ Let G be a CFG and A → α, A Є N
1. If α = λ, then A → α is a λ-production

2. If α ≠ λ for all productions, then G is λ-free

3. If A => λ in zero or more steps, then A is called


nullable or λ-generating non-terminal
◼ Ex. Let G=(N, T, P, S) be a CFG with productions:
S → AB
A → a| λ
B → b| λ
S,A,B are nullable on-terminal.

June 15, 2022 FLAT Chapter Five 17


◼ Ex. Let G=(N, T, P, S) be a CFG with productions:
S → ABaC
A→B
B → b| λ
C → c| λ
Find the non-terminals which are nullable.

June 15, 2022 FLAT Chapter Five 18


Removing λ-productions: cont’d
◼ Let G=(N, T, P, S) be a CFG with λ-productions.
We want to construct G’=(N, T, P’, S), a λ-free CFG
such that L(G’)=L(G)\{λ}.
Construction of G’:

1. Put all non λ-productions of P in P’


2. Find nullable nonterminals.
❑ Then for every production with one or more
nullable appearing in the right hand side of the
production, add to P’ any λ free production by
eliminating one or more nullables.

June 15, 2022 FLAT Chapter Five 19


Example:
Given the following CFG G:
S →AaC
A → a| λ
C→b|λ

Find G’ λ free CFG.

Solution:
Nullable: A,C
P’:
S →AaC | aC | Aa | a
A→a
C→b
EX: For the exercise on slide 17, find find G’ λ free CFG.

June 15, 2022 FLAT Chapter Five 20


Normal Forms
When the productions in a CFG G satisfy certain
restrictions, G is said to be in a normal form.
We’ll see two normal forms:
i. Chomsky Normal Form(CNF)
ii. Greibach Normal Form (GNF)

June 15, 2022 FLAT Chapter Five 21


Chomsky Normal Form (CNF)
Preliminary

1. Let G=(N, T, P, S) be a CFG and A → α be a


production of G.
a) If α is in N, then A → α is called a Unit production
b) If |α|>1 and there exists a terminal substring of α,
then A → α is called a Secondary production
c) If α contains more than two non-terminals, then A → α is called
a Tertiary production
Example:
S->A unit production
S →A| aBC
S-> aBC secondary production
A →ABC A->ABC tertiary production
B→b

June 15, 2022 FLAT Chapter Five 22


2. Let G = (N, T, P, S) be a λ-free CFG, then G is said
to be in CNF if all its productions are of the form:
A → BC where A, B, C Є N
OR
A → a, a Є T, A Є N
Ex. G with productions
S → AB
A→a
B→b

June 15, 2022 FLAT Chapter Five 23


◼ Theorem: Any λ-free CFL can be generated by a
CFG in CNF.
proof:
Let G = (N, T, P, S) be a λ-free CFG such that
L = L(G).
We want to construct a grammar G’ which is in CNF.
Steps:
1. Elimination of unit production:
◼ For A Є , U(A) = Set of unit productions

N(A)=Set of non unit productions

For each A Є N, such that U(A) ≠ Ø, replace U(A) by


{A->α | A=>B in one or more steps and B->α is in N(B) }

June 15, 2022 FLAT Chapter Five 24


2. Elimination of secondary productions
❑ If a is a substring on the R.H.S, then replace a by a
new no-terminal Aa and a new production Aa -> a

3.Elimination of tertiary productions


If A → B1B2…Bm, m>2, then replace the production by:
A → B1B1’
B1’ → B2B2’
B2’ → B3B3’

Bm-2’ → Bm-1Bm, where B1’, …, Bm-2’ are all unique
new non-terminals that do not appear in any other
production.

June 15, 2022 FLAT Chapter Five 25


◼ Example. Convert the following grammar to CNF
S → A | ABA
A → aA | a | B
B → bB | b

Solution:
1. Elimination of unit production:
U(S) = {S → A } , N(S) = {S → ABA }
U(A)={A → B} , N(A) ={A → aA | a }
U(B)= Ø
S=>+A and S=>+B
S=>+A => aA | a Є N(A)
S=>+B => bB | b Є N(B)
A=>+B => bB | b
June 15, 2022 FLAT Chapter Five 26
◼ Thus, the grammar without unit productions
S → aA | a|bB|b|ABA
A → aA | a | bB|b
B → bB | b
2. Elimination of secondary productions:
The secondary productions are:
S → aA | bB,
A → aA| bB,
B → bB
Introduce Aa and Aa ->a
Ab and Ab ->b
Then, the equivalent grammar without secondary productions is:
S → Aa A | a| Ab B|b|ABA
A → Aa A | a | Ab B|b
B → Ab B | b
Aa ->a
Ab ->b
June 15, 2022 FLAT Chapter Five 27
3. Elimination of tertiary production:
The tertiary production is:
S →ABA
Replace S →ABA with:
S →AB’
B’ →BA
Therefore, the grammar in CNF is :
S → Aa A | a| Ab B|b|AB’
B’ → BA
A → Aa A | a | Ab B|b
B → Ab B | b
Aa ->a
Ab ->b

June 15, 2022 FLAT Chapter Five 28


◼ Ex. Convert the following grammar to CNF

1. Let G be a CFG with productions:


S → aAD
A → aB | bAB
B→b
D→d

June 15, 2022 FLAT Chapter Five 29


Identification of non-CFLs

◼ Pumping lemma for CFLs


(Reading assignment)

June 15, 2022 FLAT Chapter Five 30


Greibach Normal Form (GNF)
◼ Let G=(N, T, P, S) be a λ-free CFG, then if all the
productions of G are of the form
A → aα, A Є N, a Є T, α Є N*
then G is said to be in GNF

◼ Theorem G1: If A → α1Bα2 is a production in a CFG


G and B → β1|β2|β3|…|βk are all productions with B
on the LHS, then
A → α1B α2 can be replaced by
A → α1β1α2| α1β2α2| … | α1βkα2
without affecting L(G).

June 15, 2022 FLAT Chapter Five 31


GNF: cont’d
◼ Theorem G2: If in a CFG there is a production
A → Aα1 | Aα2 | … | Aαn | β1|β2|…|βm, such a
production is called left recursive, and
A → β1|β2|β3|…|βm are the remaining productions with
A on the LHS.
Then an equivalent grammar can be constructed by
introducing a new non-terminal, A’, and replacing all
these productions by:
A → β1|β2|β3|…|βm| β1A’| β2A’|…| βmA’
A’ → α1| α2|…| αn| α1A’| α2A’|…| αnA’

June 15, 2022 FLAT Chapter Five 32


◼ Theorem GNF: Any λ-free CFG G can be converted into a
grammar in GNF.
Proof:
Let G be a λ-free CFG. To convert G into GNF use the steps
below:
1. Convert G into CNF, G’
2. Rename the non-terminals in G’ as
A1, A2, …, Am with A1 as S (m>=1)
3. Convert all the productions into
Ai → aα or Ai → Ajα with j > I
While there exists Ai → Ajα with j <I, do:
Apply Theorem 1 to get Ai → Ajα , j>i.
If there is left recursive production with Ai on the LHS,
then introduce a new non-terminal Ai’ and apply
Theorem G2.

June 15, 2022 FLAT Chapter Five 33


GNF: cont’d
4. After the 3rd step, the productions will be
of the form
i. Ai → Ajα, j > i
ii. Ai → aα, or
iii. Ai’ → Ajα
5. Remove productions of the form
Ai → Ajα, j > i
Ai’ → Ajα

June 15, 2022 FLAT Chapter Five 34


◼ Example: Convert the grammar below to
GNF.
S AS|AB
A BS|a
B AA|b
Solution:
1. Already in CNF
2. Rename the non terminals: A1 for S, A2 for A
and A3 for B
Thus,
A1 A2A1|A2A3
A2 A3A1|a
A3 A2A2|b
June 15, 2022 FLAT Chapter Five 35
3. Convert all the productions into
Ai → aα or Ai → Ajα with j > I
A3 -> A2A2 | b needs to be changed using theorem 1.

A3 -> A2A2 | b
A3-> (A3A1|a)A2 | b
A3->A3A1A 2| aA2 | b
G:
A1 -> A2A1|A2A3
A2 ->A3A1|a
A3->A3A1A2| aA2 | b
Removing the left recursive production by using
theorem 2:
A3-> aA2 | b | aA2 A3’| b A3’
A3’->A1A2 | A1A2 A3’
June 15, 2022 FLAT Chapter Five 36
G: A1 -> A2A1|A2A3
A2 ->A3A1|a
A3-> aA2 | b | aA2 A3’| b A3’
A3’->A1A2 | A1A2 A3’
4. Replace Aj in Ai->Ajα or Ai’ → Ajα using theorem 1.
A2 ->(aA2 | b | aA2 A3’| b A3’ ) A1|a
Or
A2 ->aA2 A1 | bA1 | aA2 A3’ A1| b A3’ A1|a
A1 -> A2A1|A2A3
Or
A1 -> A2A1|A2A3
Or A1 -> (aA2 A1 | bA1 | aA2 A3’ A1| b A3’ A1|a )A1|(aA2 A1 | bA1 | aA2
A3’ A1| b A3’ A1|a)A3 **
A3’-> (**)A2 | (**)A2 A3’
June 15, 2022 FLAT Chapter Five 37
So, the grammar in GNF is:
A1 -> (aA2 A1 | bA1 | aA2 A3’ A1| b A3’ A1|a )A1|(aA2 A1 | bA1 | aA2 A3’ A1|
b A3’ A1|a)A3 **
A2 ->aA2 A1 | bA1 | aA2 A3’ A1| b A3’ A1|a
A3-> aA2 | b | aA2 A3’| b A3’
A3’-> (**)A2 | (**)A2 A3’

June 15, 2022 FLAT Chapter Five 38


GNF: cont’d

Ex. Convert to GNF


1. Let G be with productions
S → AB
A → BS | b
B → SA | a
2. Let G be with productions
A1 → A2A 2 | a
A 2 → A 1A 2 | b
June 15, 2022 FLAT Chapter Five 39
Closure Properties of CFGs
◼ Theorem: CFGs are closed under:
a) Union
b) Concatenation
c) Kleen star(*)

June 15, 2022 FLAT Chapter Five 40


Reading Assignment: CYK algorithm
(a membership algorithm for CFLs)
◼ Read about the CYK algorithm and write a report (a
maximum of two pages) describing:
❑ The algorithm
❑ Its purpose
❑ Its applications
❑ Examples showing how the algorithm works
◼ NOTE: you should write what you have
understood in your own way. I don’t want copy-
pasted text!
◼ To be done individually
◼ Carries 10% weight

June 15, 2022 FLAT Chapter Five 41

You might also like