6-Context-Free Grammars, Languages and Sample Derivations-13-09-2024
6-Context-Free Grammars, Languages and Sample Derivations-13-09-2024
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}.
Is it a REGULAR Language?
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
S
S
S S
a S b
a S b a S b
b S a