Professional Documents
Culture Documents
CFG - CFL
SVNIT, CoED
B. Tech - II
2/9/2015
2/9/2015
2/9/2015
Definition,
Derivation trees & Ambiguity, Inherent ambiguity, Parse
tree,
Application of CFG,
Simplification of CFG,
Normal form of CFG,
Chomsky Normal form and Chomsky Hierarchy,
Unrestricted grammars,
Context-sensitive languages,
Relations between classes of languages,
Properties of Context Free Languages: The Pumping
Lemma, Closure properties, Decision properties of CFL.
2/9/2015
2/9/2015
Definitions,
Languages of PDA,
Equivalence of PDA and CFG ,
Deterministic PDA
2/9/2015
2/9/2015
BOOKS RECOMMENDED
2/9/2015
Functions: f : A B
Relations:
Languages:
2/9/2015
Basic
2/9/2015
10
Basic
2/9/2015
11
Basic
2/9/2015
List of symbols
Rules for forming a string (word)
Rules for forming a sentence
12
Basic
a *b *
2/9/2015
( a b) *
13
Basic
Context-Free Languages
(CFG-PDA)
Regular Languages
FA/RE
2/9/2015
14
Context-Free Languages
Context-Free
Grammars
Pushdown
Automata
stack
automaton
2/9/2015
15
Context-Free Grammar
2/9/2015
16
CFG
2/9/2015
17
More Applications
2/9/2015
18
Example
A context-free grammar
S aSb
S
A derivation:
19
Example
A context-free grammar
S aSb
S
Another derivation:
20
CFG Example
Language of palindromes
2/9/2015
21
2/9/2015
22
2/9/2015
23
2/9/2015
24
Variables
G (V , T , S , P )
Terminal
symbols
Start
variable
A x
Variable
2/9/2015
String of variables
and terminals
SVNIT, CoED, B. Tech II, TCS.
25
CFG Notation
S : A starting non-terminal (S is in V)
CFG for the language of binary palindromes:
G=({A},{0,1},P,A)
P: A ==> 0 A 0 | 1 A 1 | 0 | 1 |
2/9/2015
26
Example
A context-free grammar
S aSa
S bSb
S
A derivation:
27
Example
A context-free grammar
S aSa
S bSb
S
Another derivation:
28
Example
S aSa
S bSb
S
29
Example
A context-free grammar
S aSb
S SS
S
A derivation:
S SS aSbS abS ab
2/9/2015
30
Example
A context-free grammar
S aSb
S SS
S
A derivation:
31
Examples
G: S => (S) | SS |
G: S => 0S1 | A
A => 0A |
2/9/2015
32
Example CFG:
G = ({S}, {0, 1}, P, S)
P:
(1) S > 0S1
or just simply S > 0S1 |
(2) S >
Example Derivations:
S => 0S1
(1)
S =>
(2)
=> 01
(2)
S => 0S1
=> 00S11
=> 000S111
=> 000111
(1)
(1)
(1)
(2)
2/9/2015
33
Example CFG:
G = ({A, B, C, S}, {a, b, c}, P, S)
P: (1) S > ABC
(2) A > aA
A > aA |
(3) A >
(4) B > bB
B > bB |
(5) B >
(6) C > cC
C > cC |
(7) C >
Example Derivations:
S => ABC
(1)
S => ABC
=> BC
(3)
=> aABC
=> C
(5)
=> aaABC
=>
(7)
=> aaBC
=> aabBC
=> aabC
=> aabcC
=> aabc
SVNIT, CoED, B. Tech II, TCS.
Note that G generates the
language a*b*c*
2/9/2015
(1)
(2)
(2)
(3)
(4)
(5)
(6)
(7)
34
2/9/2015
35
Variables
G (V , T , S , P )
Terminal
symbols
Start
variable
A x
Variable
2/9/2015
String of variables
and terminals
SVNIT, CoED, B. Tech II, TCS.
36
G (V , T , S , P )
L(G ) {w : S w, w T *}
2/9/2015
37
A language
if and only if
is context-free
2/9/2015
38
Derivation Order
1. S AB
2. A aaA
4. B Bb
3. A
5. B
Leftmost derivation:
39
S aAB
A bBb
B A|
Leftmost derivation:
40
Derivation Trees
2/9/2015
41
A aaA |
S AB
B Bb |
S AB
S
2/9/2015
42
A aaA |
S AB
B Bb |
S AB aaAB
S
2/9/2015
B
A
43
A aaA |
S AB
B Bb |
S AB aaAB aaABb
S
2/9/2015
B
A
44
A aaA |
S AB
B Bb |
2/9/2015
B
A
45
A aaA |
S AB
B Bb |
Derivation Tree
2/9/2015
B
A
46
A aaA |
S AB
B Bb |
Derivation Tree
B
A
yield
aab
aab
2/9/2015
47
B Bb |
S AB
S
A
2/9/2015
B
48
S AB aaAB
a
2/9/2015
B
A
49
S AB aaAB
sentential
form
B
yield
a
2/9/2015
aaAB
50
a
2/9/2015
B
A
b
51
2/9/2015
52
Parser
input
string
2/9/2015
grammar
derivation
53
Example:
Parser
input
aabb
S SS
S aSb
S bSa
derivation
S
2/9/2015
54
Exhaustive Search
S SS | aSb | bSa |
Phase 1:
S SS
Find derivation of
S aSb
S bSa
aabb
S
All possible derivations of length 1
2/9/2015
55
S SS
aabb
S aSb
S bSa
S
2/9/2015
56
Phase 2
Phase 1
S SS
S aSb
2/9/2015
S SS | aSb | bSa |
S SS SSS
S SS aSbS
S SS bSaS
aabb
S SS S
S aSb aSSb
S aSb aaSbb
S aSb abSab
S aSb ab
SVNIT, CoED, B. Tech II, TCS.
57
S SS | aSb | bSa |
Phase 2
S SS SSS
S SS aSbS
S SS S
aabb
S aSb aSSb
S aSb aaSbb
Phase 3
58
input
aabb
S SS
S aSb
S bSa
S
derivation
59
A
A B
Number of phases for string
Why?
2/9/2015
is
2| w|
60
rules
2/9/2015
possible derivations
61
2/9/2015
possible derivations
62