Unit III - TAFL
Unit III - TAFL
Unit III - TAFL
Unit- III
3.2 Derivation
lectureTales.com
TAFL 2
T = set of terminals
P = set of production
S = start symbol
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
lectureTales.com
TAFL 5
3.2 Derivation:
Example: Derive the string 1000111 for leftmost and rightmost derivation.
S → T00T
T → 0T│1T│Ԑ
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:
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:
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:
E → E+E│E*E│id
Solution:
E→E+T
E→T
T→T*F
T→F
F → id
lectureTales.com
TAFL 8
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
*
S → αpβ
*
αpβ →w
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
Example: Solution:
S → XYX S →XYX│XY│YX│Y│X
X → 0X│Ԑ X → 0X│0
Y → 1Y│Ԑ Y → 1Y│1
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
S → A│0C1 Solution
A → B│01│10 S → 01│10
C → Ԑ│CD
lectureTales.com
TAFL 10
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:
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
lectureTales.com
TAFL 12
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
A → AAS│0S│1
lectureTales.com
TAFL 13
A → 0S│1
A → 0SZ│1Z
Z → AS
Z → ASZ
Step 2:
S → AA│0
S → 0SA│1A│0SZA│1ZA│0
Z → 0SS│1S│0SZS│1ZS
Z → 0SSZ│1SZ│0SZSZ│1ZSZ
S → 0SA│1A│0SZA│1ZA│0
A → 0S│1│0SZ│1Z
Z → 0SS│1S│0SZS│1ZS│0SSZ│1SZ│0SZSZ│1ZSZ
S → AB
A → BS│b
B → SA│a
lectureTales.com
TAFL 14
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.
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?
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
Types of Parser
Top Down
Bottom Up
Parser
Parser
Parser
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
S → 0A│1B│0│1
A → 0S│1B│1
B → 0A│1S
lectureTales.com