- 2070 Bhadra · 6 marks
Context-sensitive, context-free, regular grammars
Short answer
Context-sensitive: αAβ → αγβ, γ non-empty, |LHS| ≤ |RHS|. Context-free: A → γ, single non-terminal LHS. Regular: A → aB or A → a (right-linear). Regular ⊂ Context-free ⊂ Context-sensitive. Example: {a^n b^n} is context-free not regular; {a^n b^n c^n} is context-sensitive not context-free.
Define and differentiate between context-sensitive, context free and regular grammars with suitable examples.
A grammar G = (V, T, P, S) has non-terminals V, terminals T, productions P and start symbol S. Chomsky classified grammars by the form of their productions.
Definitions
- Context-sensitive grammar (Type 1): every production is αAβ → αγβ, with γ non-empty. Equivalently |LHS| ≤ |RHS| (non-contracting; S → ε is allowed only if S never appears on a right side). A is replaced by γ only in the context α…β.
- Context-free grammar (Type 2): every production has the form A → γ, where A is a single non-terminal and γ is any string of terminals and non-terminals.
- Regular grammar (Type 3): every production is A → aB or A → a (right-linear), or A → Ba or A → a (left-linear), also A → ε. At most one non-terminal in the right side, and always at the same end.
Examples
- Context-sensitive, generating { aⁿbⁿcⁿ : n ≥ 1 }: S → aSBC | aBC, CB → BC, aB → ab, bB → bb, bC → bc, cC → cc. (CB → BC has a two-symbol left side, so it is not context-free.)
- Context-free, generating { aⁿbⁿ : n ≥ 0 }: S → aSb | ε.
- Regular, generating strings over {a, b} ending in ab: S → aS | bS | aA, A → b.
Differences
| Point | Context-sensitive | Context-free | Regular |
|---|---|---|---|
| Type | 1 | 2 | 3 |
| Production form | αAβ → αγβ, |LHS| ≤ |RHS| | A → γ | A → aB or A → a |
| LHS | any string with a non-terminal | one non-terminal | one non-terminal |
| Machine accepting it | Linear bounded automaton | Pushdown automaton | Finite automaton |
| Typical use | Natural language aspects | Programming language syntax | Lexical tokens, patterns |
| Power | Strongest of the three | Middle | Weakest |
Hierarchy: Regular ⊂ Context-free ⊂ Context-sensitive ⊂ Unrestricted (Type 0). Every regular language is context-free, but { aⁿbⁿ } is context-free and not regular; { aⁿbⁿcⁿ } is context-sensitive and not context-free.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- CFGs for L1, L2; L1∩L2 not CF · Theory of Computation, chapter 3
- Define CFG with example · Theory of Computation, chapter 3 · asked 2×
- Unrestricted grammar; superset of CFG · Theory of Computation, chapter 4 · asked 2×
- CNF: S→AB, A→aAA|ε, B→bBB|ε · Theory of Computation, chapter 3