Generate a Context-free grammar for the language L = {anbm| m≠n}?

Data Structure AlgorithmsComputer ScienceComputers

A context-free grammar is a quadruple G = (N, T, P, S),

Where,

  • N is a finite set of nonterminal symbols,

  • T is a finite set of terminal symbols, N ∩ T = ∅,

  • P is a finite set of productions of the form A → α,

  • Where A ∈ N, α ∈ (N ∪ T)*,

  • S is the start symbol, S ∈ N.

Construct a Context free grammar for the language, L = {anbm| m ≠n}

Case 1

n > m − We generate a string with an equal number of a’s and b’s and add extra a’s on the left −

S → AS1, S1 → aS1b, S1 → λ, A → aA, A → a

Case 2

n < m − We add extra b’s on the right −

S → S1B, B → bB, B → b.

Typical derivations

S ⇒ AS1 ⇒ aAS1 ⇒ aaAS1 ⇒ aaaS1 ⇒ aaaaS1b ⇒ aaaab or

S ⇒ S1B ⇒ aS1bB ⇒ aS1bb ⇒ abb

Consider another example for Context-free grammars and languages

  • Every regular grammar is context-free, so a regular language is also a context-free one.

  • The family of regular languages is a proper subset of the family of context-free languages.

Example

Let G = ({S}, {a, b}, P, S) with

P = {S → aSa, S → bSb, S → λ}.

Typical derivation − S ⇒ aSa ⇒ aaSaa ⇒ aabSbaa ⇒ aabbaa.

L(G) = {wwR | w ∈ {a, b}*}

raja
Published on 16-Jun-2021 12:50:43
Advertisements