Context Free Grammar

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 41

Context Free Grammar

Context Free Grammar


• Definition of Type 2 Grammar
• Generation of Strings using Grammar
• Construction of Tree using the grammar
• Identification of Language given a Grammar
• Construction of Grammar
• Left derivation
• Right Derivation
• Ambiguous Grammar
• Chomsky Normal form
• Membership algorithm (CYK algorithm)
Grammars and Languages
Derivation
S
Tree o
r Tree
Syntax
NP1 VP
o
r Tree
Parse
NP2
PN VER ART ADJ

Napoleon
is a great warrior
Derivation
<S> → <NP1> <VP> Rules
or
<NP1> → <PN> Productions
<PN> → Napoleon or
Production
<VP> → <VER> <ART> <ADV> < rules

NP2>
<VER> → is → - rewritten
as
<ART> → a  - directly
derives
<ADV> → great
< NP2> → warrior
<S> <NP1> <VP>
 <NP1><VER> <ART> <ADV> < NP2>

<NP1><VER> <ART> <ADV> warrior

<NP1><VER> <ART> great warrior

 <NP1><VER> a great warrior
 <NP1> is a great warrior
Napoleon is a great warrior

Rightmost Derivation
Derivation
• Non terminals - N
• Terminals – T
• Total alphabet - V = N  T
*
 ,  V *
are strings in V
    is obtained from  in one step
 *   is obtained from in zero or more steps

reflexive transitive closure of


 * 
Grammar
Definition: A grammar is a 4-tuple G = (N, T, P, S),
where
• N is a finite set of symbols called non-terminals
(uppercase alph)
• T is a finite set of symbols called non-terminals
(lowercase alph)

• S N is the start symbol
• P is the set of productions of the form
  

N T  
Note:
Grammar (Examples)
G = ({S, A}, {a, b}, P, S), where
P: S → aA
A→b S

a A

L(G) = { ab}
Grammar
G = ({S}, {a, b}, P, S), where
P: S → aS
S S S S
S→b
a S a S
b a S
a S a S
b
a S
b
L(G) = { b, ab, a2b, a3b,. . . } b

L(G) = { anb / n ≥ 0 }
Grammar (Examples)
G= ({E}, {+, *, (, ), id }, P, E), where
P: E → E + E (rule 1)
E E
E → E * E (rule 2)
E
E → (E) (rule 3) i E + E E *E
E → id (rule 4) d i
i i i
Leftmost derivation d d d d
1 4 2 4 4
E  E  E  id  E  id  E * E  id  id * E  id  id * id

Rightmost derivation
1 2 4 4 4
E  E  E  E  E * E  E  E * id  E  id * id  id  id * id
Grammar
Let G = (N, T, P, S) be a grammar.
Then L(G) is called the language generated by G.
*
L(G) = { w  T * / S  w }

• Grammars are language generating device


• Automata are language accepting device
Classification of Grammars
• RE Type – 0 Unrestricted or Phrase structured grammar
uv u V NV
* *

• CS Type – 1 Context-sensitive or length increasing


grammar 
A    ,  V
*
A N  V

• CF Type – 2 Context-free grammar


A   V
*
A N
• REG Type – 3 Regular grammar
A  aB A orb A, B  N a  T b T { }
Chomsky Hierarchy
  Language Grammar Machine Example
Regular grammars 
Finite-
Type • Right-linear
Regular languages state a*
3 grammars automata
• Left-linear grammars
Push-
Type Context-free
2 Context-free languages grammars down anbn
automata
Linear-
Type Context-sensitive Context-sensitive
1 languages grammars bound anbncn
automata

any
Type Recursive languages Unrestricted Turing computab
Recursively
0 grammars machines le
enumerable languages
function
Chomsky Hierarchy
Non-recursively
enumerable

Recursively-enumerable

Recursive

Context-sensitive

Context-free

Regular
Grammars and Languages
1) G= ({S}, {a}, P, S), where
P: S → aS (rule 1) S a
S → a (rule 2) S  aS aa
S  aS aaS aaa
L(G) = { an / n ≥ 1 }
2) G = ({S}, {a, b}, P, S), where

P: S → aS (rule 1) S b 
S → b (rule 2) S
 aS ab
S aS aaS aab
L(G) = { anb/ n ≥ 0 }
Grammars and Languages
3) G= ({S}, {a, b }, P, S), where
P: S → aSb (rule 1) S  ab

S → ab (rule 2) S  aSb 
 aabb
L(G) = { anbn / n ≥ 1 } S aSb aaSbb aaabbb

4) G= ({S}, {a, b, c }, P, S), where 


P: S → aSa (rule 1) S c 
S → bSb (rule 2) S aSa  aca
S  
S → c (rule 3)  bSb  bcb

S aSa abSba abcba
 
L(G) = { wcwR/ w {a, b}* } S bSb baSab bacab
Grammars and Languages

 

 
 

 
 
Grammars and Languages
   
   

   


Grammars and Languages
7) Grammar for FORTRAN identifier
Length – 6 First symbol is the letter
Letter = {A, B, C, …, Z}
Digit = {0, 1, …,9}
S → (A / B / C /…./ Z) S1
S1 → 
S1 → (A / B / C /…./ Z / 0 / 1 / 2 / …./ 9) S2
S2 → 
S2 → (A / B / C /…./ Z / 0 / 1 / 2 / …./ 9) S3
Grammars and Languages
S3 → 
S3 → (A / B / C /…./ Z / 0 / 1 / 2 / …./ 9) S4
S4 → 
S4 → (A / B / C /…./ Z / 0 / 1 / 2 / …./ 9) S5
S5→ 
S5 → (A / B / C /…./ Z / 0 / 1 / 2 / …./ 9) S6
S6→ 
S  RS1  RAS 2  RADS3  RADIS 4  RADIUS5 
RADIUSS 6  RADIUS
S  AS1  A2 S 2  A2
Problems
Construct the grammar for the following languages:
1) L(G) = { ancan / n ≥ 0 }
2) L(G) = { anb2n / n ≥ 0 }
3) L(G) = { an+2bn / n ≥ 1 }
4) L(G) = { anbn-3 / n ≥ 3 }
5) L(G) = { anbm / n ≥ 0, m ˃ n }
6) L(G) = { anbn cm / n, m ≥ 1 }
7) L(G) = { anbn cm dm / n, m ≥ 1 }
8) L(G) = { anbm cm dn / n, m ≥ 1 }
9) L(G) = { anbm / n, m ≥ 1 m ≠ n}
10) L(G) = { w {a, b}* / na(w) = nb(w) + 1 }
1) L(G) = { ancan / n ≥ 0 }
G= ({S}, {a, c }, P, S), where
P: S → aSa
S→c
2) L(G) = { anb2n / n ≥ 0 }
G= ({S}, {a, b }, P, S), where
P: S → aSbb
S→ 
3) L(G) = { an+2bn / n ≥ 1 }
G= ({S}, {a, b }, P, S), where
P: S → aSb
S → aaab
4) L(G) = { anbn-3 / n ≥ 3 }
G= ({S}, {a, b }, P, S), where
P: S → aSb
S → aaa

5) L(G) = { anbm / n ≥ 0, m ˃ n }
G= ({S, S1}, {a, b }, P, S), where
P: S → aSb / S1
S1 → bS1 / b
6) L(G) = { anbn cm / n, m ≥ 1 }
G= ({S, S1, S2}, {a, b, c }, P, S), where
P: S → S1S2
S1 → aS1b / ab
S2 → cS2 / c

7) L(G) = { anbn cm dm / n, m ≥ 1 }
G= ({S, S1, S2}, {a, b, c, d}, P, S), where
P: S → S1S2
S1 → aS1b / ab
8) L(G) = { anbm cm dn / n, m ≥ 1 }
G= ({S, S1}, {a, b, c, d}, P, S), where
P: S → aSd / aS1d
S1 → bS1c /bc

9) L(G) = { anbm / n, m ≥ 1, m ≠ n}
G= ({S, A, B, S1 }, {a, b}, P, S), where
P: S → AS1 / S1B
S1 → aS1b / ab
A → aA / a
B → bB / b
10) L(G) = { w / w =uav, u, v  {a, b}*, ui ≠ vn-i+1 ,1≤ i ≤ |
u|, na(w) = nb(w) + 1 }
G= ({S}, {a, b}, P, S), where ababa , ababababa
P: S → aSb / bSa / a
For the grammar given below, find out the context free language. The grammar G = ({S}, {a, b}, S, P) with
the productions are;
• S → aSa,           (Rule: 1)
S → bSb            (Rule: 2)
S →  ε               (Rule: 3)

First compute some strings generated by  the production rules of


the grammar G in the above;
• (i)            S ⇒ aSa,                (Rule: 1)
⇒ aεa,   (Rule: 3)
i.e.          ⇒ aa
• (ii)           S ⇒ bSb,                (Rule: 2)
⇒ bεb,               (Rule: 3)
i.e.          ⇒ bb
• (iii)          S ⇒ aSa,               (Rule: 1)
⇒ abSba,           (Rule: 2)
⇒ abεba            (Rule: 3)
i.e.          ⇒ abba
• (iv)         S ⇒ bSb,                (Rule: 2)
⇒ baSab,           (Rule: 1)
⇒ ba ε ab         (Rule: 3)
i.e.          ⇒ baab
•  (v)          S ⇒ aSa,               (Rule: 1)
⇒ aaSaa,           (Rule: 1)
⇒ aabSbaa,      (Rule: 2)
⇒ aabbSbbaa, (Rule: 2)
⇒ aabb ε bbaa (Rule: 3)
i.e.       ⇒ aabbbbaa
•  Hence; Language generated by the above grammar
L(G) = {aa, bb, abba, baab, aabbaa, aabbbbaa,.. .. .. .. }
• The above generated string form the grammar G, there has a
similar pattern in all computed strings, i.e.
• The length of the string is even always i.e. length of the string L(w)
≥ 2 × i | i = 1, 2, 3, 4.. .
• From the half length of the string, string is the reverser of each
other side i.e. string is generated by the language is palindrome.
• Thus we can write the language of the grammar L(G) = {ww R : w ∈
{a, b}*};
 
Example : For the grammar given below, find out the context free
language. The grammar G = ({S}, {a, b}, S, P) with the productions are;
• S → abB,           (Rule: 1)
A → aaBb          (Rule: 2)
A →  ε               (Rule: 3)
B → bbAa          (Rule: 4)
• First compute some strings generated by  the
production rules of the grammar G in the above;
• (i)            S ⇒ abB,              (Rule: 1)
⇒ ab bbAa        (Rule: 4)
⇒ ab bb ε a      (Rule: 3)
i.e.         ⇒ ab bba
•  
• (ii)           S ⇒ abB,                           (Rule: 1)
⇒ ab bbAa                     (Rule: 4)
⇒ ab bb aaBb a             (Rule: 2)
⇒ ab bb aa bbAa ba       (Rule: 4)
⇒ ab bb aa bb ε a ba      (Rule: 3)
i.e        ⇒ ab bb aa bba ba
• (iii)          S ⇒ abB,                                    (Rule: 1)
⇒ ab bbAa                                                (Rule: 4)
⇒ ab bb aaBb a                                         (Rule: 2)
⇒ ab bb aa bbAa ba                                   (Rule: 4)
⇒ ab bb aa bb aaBb a ba                            (Rule: 2)
⇒  ab bb aa bb aa bbAa b a ba                    (Rule: 4)
⇒  ab bb aa bb aa bb ε a b a ba                   (Rule: 3)
      ⇒  ab bb aa bb aa bba ba ba

(iv)         S ⇒ abB,                                                              (Rule: 1)


⇒ ab bbAa                                                                        (Rule: 4)
⇒ ab bb aaBb a                                                               (Rule: 2)
⇒ ab bb aa bbAa ba                                                       (Rule: 4)
⇒ ab bb aa bb aaBb a ba                                              (Rule: 2)
⇒  ab bb aa bb aa bbAa b a ba                                    (Rule: 4)
⇒  ab bb aa bb aa bb aaBb a ba ba                            (Rule: 2)
⇒  ab bb aa bb aa bb aa bbAa ba ba ba                    (Rule: 4)
⇒  ab bb aa bb aa bb aa bb ε a ba ba ba                   (Rule: 3)
i.e           ⇒  ab bb aa bb aa bb aa bba ba ba ba
• Hence; Language generated by the above grammar L(G) =   { ab bba  (minimum string)
ab bb aa bba ba   ab bb aa bb aa bba ba baab bb aa bb aa bb aa bba ba ba ba ⋮
}
• The above generated string form the grammar G,
there has a similar pattern in all computed
strings, i.e.
• The minimum length of the string consist ab bba
always.
• After obtained the minimum string there have
some repetition of strings comes i.e. the loop of
string (bb aa and ba) comes finite times. (Note:
Highlighted by underlined strings)
• Thus we can write the language of the grammar
L(G) = {ab (bb aa)n bba (ba)n : n ≥ 0}
Find grammars for the following languages on Σ = { a }.
a) L ={ w / |w| mod 3 = 0}
b) L ={ w / |w| mod 3 ˃ 0}
c) L ={ w / |w| mod 3 ≠ |w| mod 2}
d) L ={ w / |w| mod 3 ≥ |w| mod 2}
a) L ={ w / |w| mod 3 = 0}
w  a3 a6 a9 a12 a15 a18
|w| 0 3 6 9 12 15 18

P: S → aaaS / 

b) L ={ w / |w| mod 3 ˃ 0}
w a a2 a4 a5 a7 a8 a10 a11
|w| 1 2 4 5 7 8 10 11

P: S → a / aa
S → aaaS
c) L ={ w / |w| mod 3 ≠ |w| mod 2}
w  a a 2 a3 a 4 a 5 a 6 a 7
|w| mod 3 0 1 2 0 1 2 0 1
|w| mod 2 0 1 0 1 0 1 0 1

L ={a2 , a3 , a4 , a5 , a8, a9 , a10 , a11, a14 , a15 , a16 , a17 , . . . }

P: S → a2
S → a3
S → a4
S → a5
S → a6 S
d) L ={ w / |w| mod 3 ≥ |w| mod 2}
w  a a2 a3 a4 a5 a6 a7 a8 a9
|w| mod 3 0 1 2 0 1 2 0 1 2 0
|w| mod 2 0 1 0 1 0 1 0 1 0 1

L ={ , a ,a2 , a4 , a5, a6, a7 , a8 , a10 , a11 , a12 , a13, a14 , a16, a17 , a18 , a19 ,
a20 , . . . }

P: S →  / S1
S→a
S → a2
S1 → a4 / a5 / a6 / a7 / a8
S 1 → a6 S 1

You might also like