- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate a Context-free grammar for the language L = {anbm| m≠n}?
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}*}