Unit-2 4 Sem Cs TOC
Unit-2 4 Sem Cs TOC
Unit-2 4 Sem Cs TOC
1
Unit-2 4th Sem CS TOC
UNIT-2 (TOC)
LECTURE NO:1
Context-Free Grammar
Example
The grammar ({S, F}, {0, 1}, P, S), P: S → 00S | 11F, F → 00F | ε
context-free grammar.
Representation Technique
2
Unit-2 4th Sem CS TOC
Root vertex − Must be labeled by the start symbol.
3
Unit-2 4th Sem CS TOC
Derivation Tree
1. Top-down Approach −
2. Bottom-up Approach −
4
Unit-2 4th Sem CS TOC
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.
Example
Yield of a Tree
5
Unit-2 4th Sem CS TOC
that either all of its children are in the sub-tree or none of them are in
the sub-tree.
Example
6
Unit-2 4th Sem CS TOC
Leftmost and Rightmost Derivation of a
String
7
Unit-2 4th Sem CS TOC
Example
6
Unit-2 4th Sem CS TOC
7
Unit-2 4th Sem CS TOC
LECTURE NO:-2
Problem
is ambiguous or not.
Solution
Let’s find out the derivation tree for the string "a+a*a". It has
two leftmost derivations.
Parse tree 1 −
8
Unit-2 4th Sem CS TOC
Derivation 2 − X → X*X → X+X*X → a+ X*X → a+a*X → a+a*a
Parse tree 2 −
9
Unit-2 4th Sem CS TOC
Since there are two parse trees for a single string "a+a*a", the
grammar G is ambiguous.
10
Unit-2 4th Sem CS TOC
LECTURE NO:-3
• Union
• Concatenation
Union
Example
Concatenation
12
Unit-2 4th Sem CS TOC
Kleene Star
If L is a context free language, then L* is also context free.
Example
13
Unit-2 4th Sem CS TOC
LECTURE NO:-4
CFG Simplification
Reduction of CFG
Removal of Unit Productions
Removal of Null Productions
Reduction of CFG
Step 1 − Include all symbols, W1, that derive some terminal and
initialize i=1.
Step 2 − Include all symbols, Wi+1, that derive Wi.
14
Unit-2 4th Sem CS TOC
Step 2 − Include all symbols, Yi+1, that can be derived from Yi
and include all production rules that have been applied.
Step 3 − Increment i and repeat Step 2, until Yi+1 = Yi.
15
Unit-2 4th Sem CS TOC
Problem
Phase 1 −
T = { a, c, e }
= { A, C, E } U { S } from rule S → AC
W3 = { A, C, E, S } U ∅
Since W2 = W3, we can derive G’ as − G’ =
{ { A, C, E, S }, { a, c, e }, P, {S}} where P: S →
AC, A → a, C → c , E → aA | e Phase 2 −
Y1 = { S }
Y2 = { S, A, C } from rule S → AC
Y4 = { S, A, C, a, c }
= { { A, C, S }, { a, c }, P, {S}} where P: S
→ AC, A → a, C → c
16
Unit-2 4th Sem CS TOC
Removal Procedure −
→ XY, X → a, Y → Z | b, Z → M, M → N, N → a
Solution −
→ Z, Z → M, and M → N
S → XY, X → a, Y → Z | b, Z → M, M → a, N → a
S → XY, X → a, Y → Z | b, Z → a, M → a, N → a
17
Unit-2 4th Sem CS TOC
S → XY, X → a, Y → a | b, Z → a, M → a, N → a
18
Unit-2 4th Sem CS TOC
S → XY, X → a, Y → a | b
ε: A → .......… → ε
Removal Procedure
→ ASA | aB | b, A → B, B → b | ∈
Solution
−
S→ASA | aB | b | a, A ε B| b | &epsilon, B → b
S→ASA | aB | b | a | SA | AS | S, A → B| b, B → b
19
Unit-2 4th Sem CS TOC
This is the final production set without null transition.
20
Unit-2 4th Sem CS TOC
LECTURE NO:-5
→ ASA | aB, A → B | S, B → b | ε
Solution
22
Unit-2 4th Sem CS TOC
S0→S, S→ ASA | aB | a, A → B | S | ∈, B → b
A → B | S, B → b
B→b
|ASA | aB | a | AS | SA, B → b
(4) Now we will find out more than two variables in the R.H.S Here,
17
Unit-2 4th Sem CS TOC
S→ AX | aB | a | AS | SA
A → b |AX | aB | a | AS | SA
18
Unit-2 4th Sem CS TOC
B→b
X → SA
S0→ AX | YB | a | AS | SA S→
AX | YB | a | AS | SA
A → b A → b |AX | YB | a | AS | SA B →
X → SA
Y→a
19
Unit-2 4th Sem CS TOC
LECTURE -6
Chomsky Classification of Grammars
Grammar Grammar
Language Accepted Automaton
Type Accepted
Unrestricted Recursively
Type 0 Turing Machine
grammar enumerable language
Context-free Pushdown
Type 2 Context-free language
grammar automaton
Finite state
Type 3 Regular grammar Regular language
automaton
Type1, Type0
20
Unit-2 4th Sem CS TOC
Type - 3 Grammar
Type-3 grammars generate regular languages. Type-3 grammars must have
non-terminal.
and a ∈ T (Terminal)
The rule S → ε is allowed if S does not appear on the right side of any
rule.
Example
X→ε
X → a | aY
Y→b
Type - 2 Grammar
Example
S →Xa
X→a
X → aX X
20
Unit-2 4th Sem CS TOC
→ abc X
→ε
Type - 1 Grammar
21
Unit-2 4th Sem CS TOC
α A β→α γ β
where A ∈ N (Non-terminal)
and α, β, γ ∈ (T ∪ N)* (Strings of terminals and non-terminals)
→ ε is allowed if S does not appear on the right side of any rule. The
bounded automaton.
Example
AB → AbBc
A → bcA
B→b
Type - 0 Grammar
machine.
Example
S → ACaB
Bc → acB
22
Unit-2 4th Sem CS TOC
CB → DB
aD → Db
23
Unit-2 4th Sem CS TOC
LECTURE-7
Greibach Normal Form
S →ε
→ XY | Xn | p
X → mX | m
Y → Xn | o
24
Unit-2 4th Sem CS TOC
Solution
Here, S does not appear on the right side of any production and
there are no unit or null productions in the production rule set.
So, we can skip Step 1 to Step 3.
Step 4
X in S → XY | Xo | p
with
mX | m
we obtain
S → mXY | mY | mXo | mo | p.
X in Y → Xn | o
X → mX | m
we obtain
Y → mXn | mn | o.
X → mX | m
Y → mXD | mD | o
O →o
P→p
25
Unit-2 4th Sem CS TOC
LECTURE-8
Pumping Lemma for CFG
Lemma
Hence vwx cannot involve both 0s and 2s, since the last 0 and
the first 2 are at least (n+1) positions apart. There are two cases
−
Case 1 − vwx has no 2s. Then vx has only 0s and 1s. Then uwy,
which would have to be in L, has n 2s, but fewer than n 0s or 1s.
Case 2 − vwx has no 0s.
26
Unit-2 4th Sem CS TOC
Hence, L is not a context-free language.
27
Unit-2 4th Sem CS TOC
28