Unit III - TAFL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

TAFL 1

Unit- III

3.1 Definition of CFG (Context Free Grammar) and problems

3.2 Derivation

3.3 Ambiguity in Grammar

3.3.1 Inherent Ambiguity

3.3.2 Ambiguous to Unambiguous CFG

3.4 Simplification of CFGs

3.4.1 Removal of useless symbols

3.4.2 Elimination of Ԑ production

3.4.3 Removal of unit production

3.5 Normal Forms

3.5.1 Chomsky’s Normal Forms (CNF)

3.5.2 Greibach Normal Form (GNF)

3.6 Closure Properties of CFL

3.7 Decision Properties of CGL

3.8 Undecidable Problems

3.9 Applications of CFG

3.10 Pumping Lemma for CFL

3.11 CFG to finite Automata

lectureTales.com
TAFL 2

3.1 Definition of CFG (Context Free Grammar):

The CFG can be formally defined by G = {V,T,P,S} where

V = set of non terminals or variables

T = set of terminals

P = set of production

S = start symbol

Problems based on CFG

1. Write CFG for the following languages:

a) Having any number of a’s over the set {a}.


S → aS
S→Ԑ
b) Having regular expression (0+1)*.
S → 0S│1S
S→Ԑ
c) Containing strings of at least two a’s.
S → Aa Aa A
A → aA│bA│Ԑ
d) L = wcwT where w Ԑ (a,b)*
S → aSa
S → bSb
S→C
e) Which has all the strings which are all palindromes over {a,b}.
S → aSa
S → bSb
S → a│b│Ԑ

lectureTales.com
TAFL 3

f) Which consists of all the strings having at least one occurrence of 000.
S → ATA
A → 0A│1A│Ԑ
T → 000
g) There are no consecutive b’s, the string may or may not have consecutive a’s.
S → aS│bA│a│b│Ԑ
A → aS│a│Ԑ
h) At least one occurrence of double a.
S → BAB
A → aa
B → aB│bB│Ԑ
i) All the string of different first and last symbols over {0,1}.
S → 0A1│1A0
A → 0A│1A│Ԑ
j) L = anb2n where n ≥ 1.
S → aSbb│abb
k) L = axby where x ≠ y
S → aSb│A│B
A → aA│a
B → bB│b
l) For the regular expression (110+11)*(10)*
S → AB
A → 110A│11A│Ԑ
B → 10B │Ԑ
m) For generating the integers.
S → GI
G → +│ -
I → DI│D
D → 0│1│2….│9

lectureTales.com
TAFL 4

n) L = {0i1j2k│j > i+k}.


S → ABC
A → 0A1│Ԑ
B → 1B│1
C → 1C2│Ԑ
o) L = {0i1j2k│i = j}.
S → AB
A → 0A1│Ԑ
B → 2B│Ԑ
i j k
p) L = {0 1 2 │j ≤ k}.
S → AB
A → 0A│Ԑ
B →1B2│ C
C → 2C│Ԑ

lectureTales.com
TAFL 5

3.2 Derivation:

a. Left Most Derivation


b. Right Most Derivation

Example: Derive the string 1000111 for leftmost and rightmost derivation.

S → T00T

T → 0T│1T│Ԑ

Left Most Derivation Right Most Derivation


S S
T00T S → T00T T00T S → T00T
1T00T T → 1T T001T T → 1T
10T00T T → 0T T0011T T → 1T
10Ԑ00T T→Ԑ T00111T T → 1T
1000T T00111Ԑ T→Ԑ
10001T T → 1T T00111
100011T T → 1T 1T00111 T → 1T
1000111T T → 1T 10T00111 T → 0T
1000111Ԑ T→Ԑ 10Ԑ00111 T→Ԑ
1000111 1000111

3.2.1 Derivation Tree (Parse Tree):

Derivation Tree is a graphical representation for the derivation of the given production
rules for a given CFG. Following are properties of any derivation tree:

a. The root node is always a node starting symbol.


b. The derivation is read from left to right.
c. The leaf nodes are always terminal nodes.
d. The interior nodes are always the non terminal nodes.

lectureTales.com
TAFL 6

Example: Construct the derivation tree for the string “aabbabba” from the CFG given
below:

S → aB│bA

A → a│aS│bAA

B → b│bS│aBB

Solution:

3.3 Ambiguity in Grammar:

The grammar can be derived in either leftmost or rightmost derivation but if there exists more
than one left parse tree or more than one right parse tree than the grammar is said to be
ambiguous grammar.

lectureTales.com
TAFL 7

Example:

Example: Remove ambiguity from the following example:

E → E+E│E*E│id

Solution:

E→E+T

E→T

T→T*F

T→F

F → id

lectureTales.com
TAFL 8

3.3.1 Inherent Ambiguity:

A context free grammar is called inherently ambiguous if all the productions rules in the
grammar are ambiguous.

Example:

S → XYZ│aaYbb

X → aaY│aa

Y → baZ│ ba

Z → bZb│bb

3.3.2 Ambiguous to Unambiguous CFG:

3.4 Simplification of CFGs:

There are three steps for simplification of CFG (reduced grammar):

a) Removal of useless symbols.


b) Elimination of Ԑ production.
c) Removal of unit production.

3.4.1 Removal of useless symbols:

A symbol P is useful if there exists some derivation in the following form:

*
S → αpβ

*
αpβ →w

Then p is said to be useful symbol.

lectureTales.com
TAFL 9

Example: Solution:
S → aA│a│Bb│cC S → aA│a│Bb
A → aB A → aB
B → a│Aa B → a│Aa
C → cCD
D → ddd

3.4.2 Elimination of Ԑ production

Example: Solution:
S → XYX S →XYX│XY│YX│Y│X
X → 0X│Ԑ X → 0X│0
Y → 1Y│Ԑ Y → 1Y│1

3.4.3 Removal of unit production.

Example: Solution
S → 0A│1B│C S → 0A│1B│01
A → 0S│00 A → 0S│00
B → 1│A B → 1│0S│00
C → 01 C → 01

Exercise: Simplify the following CFGs

S → A│0C1 Solution
A → B│01│10 S → 01│10
C → Ԑ│CD

lectureTales.com
TAFL 10

3.5 Normal Forms:

3.5.1 Chomsky’s Normal Forms (CNF):

The CNF can be defined in the following form:


Non terminal → Non Terminal . Non Terminal
Non terminal → Terminal

Example: Convert the following CFG into CNF:


1. Solution:
S → aaaaS S → CaC1
S → aaaa C1 → CaC2
C2 → CaC3
C3 → CaS
S → C5C5
C5 → CaCa
Ca → a
2. Solution:
S → aSa│bSb│a│b S → CaC1
C1 → SCa
Ca → a
S → CbC2
C2 → SCb
Cb → b
S→a
S→b

3.5.2 Greibach Normal Form (GNF):

The GNF can be defined in the following form:


Non terminal → One Terminal . Any number of non-terminals
Non terminal → One terminal

lectureTales.com
TAFL 11

To convert given CFG into GNF we can use two lemmas based on which it is easy to convert
given CFG to GNF:
A. Lemma 1:

G = (V, T, P, S) be a given CFG and if there is a production


A → Ba and B →β1│β2│….│βn
Then we can convert A rule to GNF as
A→β1a│β2a│….│βna

B. Lemma 2:
G = (V, T, P, S) be a given CFG and if there is a production
A → Aa1│Aa2│….│Aan│ β1│β2│….│βn
Such that βi do not start with A then equivalent grammar in GNF can be:
A →β1│β2│….│βn
A →β1Z│β2 Z│….│βnZ
Z → a1│a2│….│an
Z → a1Z│a2Z│….│anZ

Example: Convert the following grammar to GNF:


Example 1.
S → abSb
S → aa
Solution:
S → aBSB
S → aA
B→b
A→a
Example 2.
S → ABA
A → aA│Ԑ
B → bB│Ԑ

lectureTales.com
TAFL 12

Remove Ԑ in the CFG:


S → ABA│AB│BA│AA│AA│A│B
A → aA│a
B → bB│b

Remove unit production:


S → ABA│AB│BA│AA│AA│aA│a│bB│b
A → aA│a
B → bB│b

In GNF we can write

S → aABA│aAB│aBA│aB│aAA│aA│a│bBA│bA│bB│b
A → aA│a
B → bB│b

Example 3.

S → AA│0

A → SS│1

Step 1: (for A)

A → SS│1

Put the valve of S in left most

A → AAS│0S│1

From lemma 2: a1= AS, β1 = 0S, β2 = 1

lectureTales.com
TAFL 13

A → 0S│1

A → 0SZ│1Z

Z → AS

Z → ASZ

Step 2:

S → AA│0

Put the above value of A in left most:

S → 0SA│1A│0SZA│1ZA│0

Put the value of A for Z:

Z → 0SS│1S│0SZS│1ZS

Z → 0SSZ│1SZ│0SZSZ│1ZSZ

The final solution is

S → 0SA│1A│0SZA│1ZA│0

A → 0S│1│0SZ│1Z

Z → 0SS│1S│0SZS│1ZS│0SSZ│1SZ│0SZSZ│1ZSZ

Exercise: convert the following CFG into GNF

S → AB

A → BS│b

B → SA│a

lectureTales.com
TAFL 14

3.6 Closure Properties of CFL:

a) The context free languages are closed under union.


b) The context free languages are closed concatenation.
c) The context free languages are closed under kleen closure.
d) The context free languages are not closed under intersection.
e) The context free languages are not closed under complement.

3.7 Decision Properties of CGL:

a) Emptiness: The given context free grammar cannot generate any string at all.
b) Finiteness: The given context free grammar generates a finite language.
c) Membership: whether given string belongs to given grammar.

3.8 Undecidable Problems:

There are no algorithms to answer these questions. Hence these problems are known as
Undecidable Problems:

a) Whether or not two different context free languages define the same language?
b) Whether given CFL is ambiguous or not?
c) Whether complement of given CFL is context free language?
d) Whether the intersection of two context free languages is context free?

3.9 Applications of CFG:

When any high level program like C or PASCAL is compiled, the compiler checks the
syntax of every programming statement by constructing syntax tree, And for building the syntax
tree, it is necessary to write context free grammar for each statement in the program.

lectureTales.com
TAFL 15

There are various Parsing Techniques:

Types of Parser

Top Down
Bottom Up
Parser
Parser

Backtracking Predictive Parser Shift Reduce LR Parser

Parser

SLR Parser LALR Parser LR Parser

3.10 Pumping Lemma for CFL:

Let L be any context free language, then there is a constant n, which depends only upon
L, such that there exist a string w Ԑ L and │w│≥ n where w = pqrst such that

a) │qs│ ≥ 1
b) │prs│ ≤ n and
c) For all i ≥ 0 pqi rsi t is in L.

Example: Proof whether the given language L = {SST│S Ԑ (a,b)*} is context free or not?

lectureTales.com
TAFL 16

3.11 CFG to Finite Automata:

S → 0A│1B│0│1

A → 0S│1B│1

B → 0A│1S

lectureTales.com

You might also like