0% found this document useful (0 votes)
47 views

6-Context-Free Grammars, Languages and Sample Derivations-13-09-2024

g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

6-Context-Free Grammars, Languages and Sample Derivations-13-09-2024

g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Context-Free Grammar

Definition
• A context-free grammar (CFG) G is a quadruple
(V, T, P, S) where
• V: a set of non-terminal symbols
• T: a set of terminals (V ∩ T = Ǿ)
• P: a set of rules (P: V → (V U T)*)
• S: a start symbol.
Context-Free Language
• A language L is context-free if there exists a
CFG G such that L = L(G).
Example
• If A → B, then xAy  xBy and
• V = {S, A,}
we say that xAy derives xBy.
• T = {0, 1}
• If s  …  …  t, then we
• R = {S → 11S, S → 00A, write s * t.
A → 11A, A → ε } • A string x in T* is generated by
(R= {S → 11S | 00A, A → 11A | ε }) G=(V,Σ,R,S) if S * x.

• L(G) = { x in T* | S * x}.

Language of the above grammar G is: {12n0012m: n,m >= 0}

Is it a REGULAR Language?

• YES !!!!! L = L(R) where R = (11)*00(11)*


Corollary
• Every regular language is a CFL.
• The class of regular languages is a proper
subclass of CFLs.

CFL

Regular

Why, proper?
Example
• G = ({S}, {0,1}. {S → 0S1 | ε }, S)
• ε in L(G) because S ε .
• 01 in L(G) because S 0S1 01.
• 0011 in L(G) because
S 0S1 00S11 0011.
n n n n
• 0 1 in L(G) because S * 0 1 .
n n
• L(G) = {0 1 | n > 0}. Below is a linear gr. For L
• Linear grammar: S → 0A | 01, A → S1
More Examples of CFL
• L = {wwR : w є T*} (note. strings are even length)
Grammar Rules: S → aSa | bSb | 
• L ={set of palindromes}
Grammar Rules: S → aSa | bSb |  | a | b
• L = {no. of a’s = no. of b’s}
Grammar Rules: S → aSb | bSa | SS | 
• L = {0n1m : n, m ≥0 and n  m}
Grammar Rules: S → aSb | A | B ;
A → aA | a ; B → bB | b
Leftmost and Rightmost
Derivations
• Now consider the grammar G = ({S, A, B, C}, {a, b, c}, S, P) where

P = {S → ABC, A →aA, A → , B → bB, B → , C → cC, C → }.

• With this grammar, there is a choice of variables to expand. Here is a


sample derivation for abbc:
• S  ABC  aABC  aABcC  aBcC  abBcC  abBc  abbBc  abbc

• If we always expanded the leftmost variable first (if applicable), we would


have a leftmost derivation:
• S  ABC  aABC  aBC  abBC  abbBC  abbC  abbcC  abbc

• Conversely, if we always expanded the rightmost variable first (if


applicable), we would have a rightmost derivation:

• S  ABC  ABcC  ABc  AbBc  AbbBc  Abbc  aAbbc  abbc


Consider the rules P = {S → aSb | bSa | SS |  }.

One way to (LEFT) Anotherway (RIGHT) Anotherway


generate abab is : to generate abb is: to generate abb is:
S  SS  aSbS S  SS  aSbS  S  SS  SaSb 
 aSbaSb  abS  abaSb  Sab  aSbab 
abaSb  abab abab abab

S
S

S S
a S b

a S b a S b
b S a



You might also like