You are on page 1of 36

Context Free Languages

 Context Free Grammars


 Leftmost and rightmost derivation of strings
 Derivation tree
 Parsing Arithmetic Expression
 Ambiguity in CFGs

September 24, 2021 Formal Language Theory 1


Introduction

 The production in regular grammar are


restricted in two ways.
 To create a grammar that are more powerful,
we must relax some of the restrictions.
 By retaining the restriction on the left side, but
permitting anything on the right side context
free grammar.

September 24, 2021 Formal Language Theory 2


Context Free Grammar (CFG)
 Definition: A CFG, G, is G=(N, T, P, S) context free if all
productions in P have 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
September 24, 2021 Formal Language Theory 3
CFG: cont’d
 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. The root of the
tree is S and x is the collection of leaves from
left to right.
 Left most derivation: employs the reduction
of the left most non-terminal
 Right most derivation: employs the reduction
of the right most non-terminal

September 24, 2021 Formal Language Theory 4


Left and right…

 Given the grammar


 SaAB
 AbBb
 BA|λ
 Then the leftmost derivation is
 SaAB abBbBabAbBabbBbbBabbbbBabbbb
 Right most derivation is
 SaABaAabBbabAbabbBbbabbbb

September 24, 2021 Formal Language Theory 5


September 24, 2021 Formal Language Theory 6
Derivation/parse tree
 Let G=(N,T,P, S) is CFG each production is represented
with tree satisfying the following condition:
1. The root is labelled S
2. Every leaf has a label from TU{λ}
3. Every interior vertex has a label from N
4. If a vertex has lebel AϵN and its from left to right a1a2,…
an then P must contain a production of the form Aa1a2,…
an
5. A leaf labelled λ has no siblings that is a vertex with a child
labelled λ can have no other children.
Partial derivation:

September 24, 2021 Formal Language Theory 7


 Consider the
grammar G
 SaAB
 AbBb
 BA|λ
 The first is partial
derivation tree
 Second is
derivation/parse
tree

September 24, 2021 Formal Language Theory 8


CFG: cont’d
 If a derivation of a string x has two different left most
derivations, then the grammar is said to be ambiguous.
Otherwise unambiguous.
(i.e. a grammar is ambiguous if it can produce more than one
parse tree for a particular sentence.
 Ex.
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

September 24, 2021 Formal Language Theory 9


CFG: cont’d

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?

September 24, 2021 Formal Language Theory 10


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

September 24, 2021 Formal Language Theory 11


Ambiguity of CFG

 Show the following are ambiguous grammar


1. Sss|a|b
2. SA|B|b
AaAB|ab
BabB|λ
Provide a parse tree for each of them
 A language generated from ambiguous
grammar is called ambiguous language.

September 24, 2021 Formal Language Theory 12


September 24, 2021 Formal Language Theory 13
Chapter five

Simplification of context-free grammar


and normal forms
Outlines

 Methods for Transforming Grammars


 Normal for grammars and parsing
 Chomsky’s hierarchy of grammars

September 24, 2021 Formal Language Theory 15


Introduction
 The definition of context free grammars (CFGs) allows us to develop a wide
variety of grammars and impose no restriction what so ever to the right side of a
production.
 Most of the time, some of the productions of CFGs are not useful and are
redundant.
 This happens because the definition of CFGs does not restrict us from making
these redundant productions.
 By simplifying CFGs we remove all these redundant productions from a
grammar , while keeping the transformed grammar equivalent to the original
grammar.
 Because of this, we need a method to transform a CFG into its equivalent one
that satisfies certain restriction.
 Transformation
 Substitutions
 Two grammars are called equivalent if they produce the same language.
Simplifying CFGs is necessary to later convert them into Normal forms
Introduction

 We also investigate normal forms for CFG.


 CNF
 GNF
 Types of redundant productions and the
procedure of removing them are mentioned
below.
 Remove λ-productions.
 Remove unit-productions.
 Remove useless-productions
Removing useless production

 We want to remove productions that can


never take part in any derivation.
SaSb | λ|A
AaA

 S A is redundant as A cannot be
transformed into a terminal string.
 Removing this production leaves the
language unaffected.
Removing useless production
 Let G = (N, T, S, P) be a CFG A ϵV is useful iff there is w ϵL(G) such that:
S*xAy*w with x, y in (NUT)*
 A production is useless if it involves any useless variable.
 E.g.
 G = ({S, A, B}, {a, b}, S, P)
 SA
 AaA|λ
 BbA
 The variable B is useless, either it is no reachable from the start sysmble or generating a terminal string.
 Eliminate the useless symbbols and production from G =(V, T, S, P) where V/N=(A,
B, C) and T={a,b} with P consisting
 SaS | A | C
SaS|A
 Aa 
Aa
 Baa
 CaCb
 The remaining are useless production
Removing λ-Productions
Removing λ-Productions
Example
Removing Unit-Productions

 Any production of a context-free grammar of


the form: A B is called a unit-production.
 unit rules: A B
 whenever A B, replace the rule with rules Aβ
for each rule in which B β
Removing Unit-Productions
Removing Unit-Productions
 Example

Finally: the grammar is


SAa | a | bc | bb
Aa | bc | bb
Ba | bc | bb

Note: the removal of unit production has made B and the associated
productions useless.
Simplification and Normal Forms
 Let L be a context-free language that does not contain λ.
Then there exists a CFG that generates L and does not have
any useless productions, O -productions, or unit-productions.
 Proof
 Remove λ-productions.
 Remove unit-productions.

 Remove useless-productions

 So before converting to one of the normal forms, eliminate these three


kinds of rules.
Normal Forms

 Chomsky normal form.


 Greibach normal form.
Chomsky Normal Form (CNF)
Chomsky Normal Form (CNF)
CNF
CNF
 Egxample
 S ABa
 A aab
 B Ac
 As per the theorem, the grammar does not have any λ-production, unit production and useless
production.
CNF
Greibach Normal Form (GNF)
GNF
GNF-Example
 The grammar
GNF

 Convert the grammar


SabSb|aa into GNF

You might also like