Theory of Computation
Unit-3 Context Free Grammar
PPT
Prepared By
Archana Kadam
Assistant Professor,
Department of Computer Engineering, PCCOE
1
Unit-3 Context-Free Grammar
Introduction, Regular Grammar, Context Free Grammar- Definition, Derivation. Sentential form, parse tree, Ambiguous Grammar, Simplification of CFG: Eliminating unit productions, useless production, useless symbols, Greibach normal form, Chomsky normal form. Types of Grammar: Chomsky Hierarchy, Context Free Language (CFL): Closure properties of CFL.
Case Study- CFG for Parenthesis Match- XML and Document Type Definitions, Natural Language Processing- Text Parsing
Definition
Example
f → 11f, f → ε }
How do we use rules?
xAy derivates xBy.
if S * x.
Example
S -> 0S1 -> 00S11 -> 0011.
Context-Free Language (CFL)
Corollary
CFL
Regular
Context Free Grammar
P : S-> aSb | ab
Construct the CFG for L of palindrome strings. S - > aSa | bSb | Ɛ
9
Derivations
10
Derivations Examples
11
Left most derivation is
S-> aAS…………Rule 1
-> aSbAS……...Rule 3
->aabAS……….Rule 2
->a a b b a S….Rule 5
-> a a b b a a…...Rule 2
Derivations Examples
12
Right most derivation is
S-> aAS…………Rule 1
-> aAa….……...Rule 2
->aSbAa……….Rule 3
->a S b b a a….Rule 5
-> a a b b a a…...Rule 2
Derivations Examples
13
Derivation Tree
S
a
A
S
S
b
A
b
b
a
a
Ambiguous Context Free Grammar
14
Ambiguous Context Free Grammar
Now to derive a string id + id * id there are two leftmost derivation given below
So this grammar is ambiguous
15
E => E + E
=> id + E
=> id + E * E
=> id + id * E
=> id + id * id
E => E * E
=> E + E * E
=> id + E * E
=> id + id * E
=> id + id * id
Ambiguous Context Free Grammar
Check the ambiguity in following grammar
S -> iCtS | iCtSeS | a
C -> b
Consider string ‘ibtibtaea’
16
Ambiguous Context Free Grammar
S -> iCtS | iCtSeS | a
C -> b
Consider string ‘ibtibtaea’
So this grammar is ambiguous
17
S -> iCtS
-> ibtS
-> ibtiCtSeS
-> ibtibtaea
S -> iCtSeS
-> ibtSeS
-> ibtiCtSeS
->ibtibtaea
Ambiguous Context Free Grammar
Check whether the grammar is ambiguous or not.
A->AA
A->(A)
A->a
For the string "a(a)(a)a"
18
19
As two derivation tree Grammar is Ambiguous
Removal of Ambiguity
Example :
E -> E + E | E * E | id
Postpone higher priority operators from start symbol
Use other Non terminals to take care of it
E-> E + T | T
T - > T * F | F
F -> id
20
CFG Simplification
21
Three ways to simplify/clean a CFG
(clean)
(simplify)
22
A => ε
A => B
Eliminating useless symbols
A symbol X is reachable if there exists:
A symbol X is generating if there exists:
For a symbol X to be “useful”, it has to be both reachable and generating
23
reachable
generating
Algorithm to detect useless symbols
24
Example: Useless symbols
25
Eliminating ε-productions
Theorem: If G=(V,T,P,S) is a CFG for a language L, then L\ {ε} has a CFG without ε-productions
Definition: A is “nullable” if A🡺* ε
26
A 🡺 ε
What’s the point of removing ε-productions?
Algorithm to detect all nullable variables
27
Eliminating ε-productions
Given: G=(V,T,P,S)
Algorithm:
28
Example: Eliminating ε-productions
Goal: To construct G1, which is the grammar for L-{ε}
29
G1:
+
Simplified�grammar
Eliminating unit productions
30
A => B
B has to be a variable
What’s the point of removing unit transitions ?
A=>B | …
B=>C | …
C=>D | …
D=>xxx | yyy | zzz
A=>xxx | yyy | zzz | …
B=> xxx | yyy | zzz | …
C=> xxx | yyy | zzz | …
D=>xxx | yyy | zzz
Will save #substitutions
E.g.,
before
after
Putting all this together…
Step 1) eliminate ε -productions
Step 2) eliminate unit productions
Step 3) eliminate useless symbols
31
Again,
the order is�important!
Why?
Normal Forms
32
Why normal forms?
33
Chomsky Normal Form (CNF)
Let G be a CFG for some L-{ε}
Definition:
G is said to be in Chomsky Normal Form if all its productions are in one of the following two forms:
34
CNF checklist
35
G1:
Checklist:
Is this grammar in CNF?
So, the grammar is not in CNF
How to convert a G into CNF?
36
B1 C1
B2 C2
and so on…
Example #1
37
G:
S => AS | BABC
A => A1 | 0A1 | 01
B => 0B | 0
C => 1C | 1
X0 => 0
X1 => 1
S => AS | BY1
Y1 => AY2
Y2 => BC
A => AX1 | X0Y3 | X0X1
Y3 => AX1
B => X0B | 0
C => X1C | 1
G in CNF:
All productions are of the form: A=>BC or A=>a
Example #2
38
G:
Step (1)
Step (2)
Other Normal Forms
A==>a α
Where a is terminal and α is string of zero or more non-terminals
39
Greibach Normal Form (GNF)�
A->ABA | AB
A-> aA | a
Using theorem 1 of substitution for B in production of first grammsr
A-> aABA | aBA | aAB| aB
Now all grammar production are in required format.
(Kindly note do not replace for grammar production already in format)
40
Greibach Normal Form (GNF)�
A -> Aα1 | Aα2 |….| A αr | β1 | β2 |….| βn
Then equivalent grammar can be formed as
A -> β1 | β2 |….| βn
A -> β1 Z | β2 Z|….| βn Z
Z -> α1 | α2 |….| αr
Z -> α1 Z | α2 Z |….| αr Z
41
Greibach Normal Form (GNF)�
Example :
A -> AB| aB | bB| c
B -> b
To bring this grammar in GNF will apply theorem 2 to handle left recursive grammar of A -> AB
Here, α1 = B,
β1 = aB, β2 = bB, , β3 = c
Then,
A -> aB | bB| c
A -> | aBZ | bBZ| cZ
Z -> B | BZ
42
Now A’s production are in GNF.
Need to handle Z, will apply theorem one of substitution
So,
Z-> b | bZ
Final Answer is
A -> aB | bB| c
A -> | aBZ | bBZ| cZ
Z -> b | bZ
Chomsky Hierarchy
43
26-09-2023
Introduction
Type-0 (Unrestricted grammar)
Type-1 (Context sensitive grammar)
Type-2 (Context free grammar)
Type-3 (Regular grammar)
44
26-09-2023
Type-0 (Unrestricted grammar)
45
26-09-2023
Type-1 (Context sensitive grammar)
Restriction on production rules are as follows:
1. for α −> β, the length of β is at least as much as length of α, except for S −> ε
2. The rule S −> ε is allowed only if S doesn’t appear on RHS
3. Context sensitive because of productions of form α1Aiα2 −> α1βα2 , β ≠ ε and α1 , α2 may or may not be empty
Context sensitive language (CSL) recognized by turing machine
Example: A −>aBC, aB −>ab, bC−>bc
Derivation:
A=>aBC=>abC ;
abC=>abc ; finally A=>abc
46
26-09-2023
Type-2 (Context free grammar)
47
26-09-2023
Type-3 (Regular grammar)
48
26-09-2023
Type-3 (Regular grammar)
S −>Ca|Bb ,
C −>Bb ,
B −>Ba|b
49
26-09-2023
Type-3 (Regular grammar)
2. Right-linear grammar:
50
26-09-2023
CFL Closure Properties
51
Closure Property Results
52
Substitution of a CFL: example
53
CFG for L:
S=> 0S0|1S1|ε
CFG for s(0):
S0=> aS0b | ab
CFG for s(1):
S1=> xx | yy
Therefore, CFG for s(L):
S=> S0SS0 | S1 S S1 |ε
S0=> aS0b | ab
S1=> xx | yy
CFLs are closed under union
Let L1 and L2 be CFLs
To show: L2 U L2 is also a CFL
==> s(Lnew) == same as == L1 U L2
54
Let us show by using the result of Substitution
CFLs are closed under concatenation
==> L1 L2 = s(Lnew)
S->S1.S2
S1-> aSa|bSb|a|b|Ɛ
S2-> aSb |Ɛ
55
Let us show by using the result of Substitution
CFLs are closed under Kleene Closure
56
CFLs are closed under Reversal
57
We won’t use substitution to prove this result
CFLs are not closed under Intersection
58
Some negative closure results
CFLs are not closed under complementation
59
Some negative closure results
Logic: if CFLs were to be closed under complementation � 🡺 the whole right hand side becomes a CFL (because � CFL is closed for union)
🡺 the left hand side (intersection) is also a CFL
🡺 but we just showed CFLs are � NOT closed under intersection!
🡺 CFLs cannot be closed under complementation.
CFLs are not closed under difference
60
Some negative closure results
Decision Properties
61
Application of CFL
62
Application of CFL
63
64
memo :
addressee: sender date title body;
addressee : TEXT;
sender : TEXT;
date : DATE;
title : TEXT;
body : TEXT;
For example, the following would be a valid memo:
<memo> <addressee>John</addressee> <sender>Carla</addressee> <date>1998-09-01</date> <title>New coffee maker</title>
<body> The new coffee maker has been installed! Operation is simple: put a cup in the opening and press the red button. </body>
</memo>
“Undecidable” problems for CFL
65