- Trending Categories
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}*}
- Related Articles
- How to generate the language for context free grammar?
- Generate a CNF for a given context free grammar
- Explain about CYK Algorithm for Context Free Grammar
- What is Context-Free Grammar?
- Explain Pumping lemma for context free language
- Convert the given Context free grammar to CNF
- What are the closure properties for context free language?
- Explain about pumping lemma for context free language?
- Explain the simplification of context free grammar in TOC
- What is context free grammar? Explain with examples
- Explain removing unit productions in context free grammar
- Construct a PDA for language L = {0n 1m2m3n | n>=1, m>=1}
- Explain the context free language closure under concatenation?
- How to convert context free grammar to push down automata?
- Explain the context free language closure under union operation?
