Theory of Computation · Context-Free Grammars
Definition of Grammar (V, T, P, S) and Language Generated by a Grammar
Overview
A grammar is a formal device, written as a 4-tuple , that generates all the strings of its language. Reading the four components off a written grammar and deriving strings from the start symbol are the basic skills for working with context-free grammars.
Key concepts
- Grammar — represented by the 4-tuple ; using these four components, the grammar generates all the strings of its language.
- V (variables / non-terminals) — the set of all symbols that appear on the left-hand side of productions; they can be replaced.
- T (terminals) — the set of all symbols that appear only in the final strings; they cannot be replaced.
- P (productions) — the set of all rewrite rules of the grammar.
- S (start symbol) — a distinguished variable from from which every derivation begins.
- Derivation — a step-by-step generation of a string: begin with and at each step replace one variable by the right-hand side of one of its productions, until only terminals remain.
- Language generated by the grammar — the collection of all strings derivable in this way.
How it works
To read the four components off a written grammar:
- Find V (variables): collect every symbol that appears on a left-hand side of some production. Variables are exactly the left-hand-side symbols.
- Find T (terminals): collect every symbol that appears in the grammar but is not a variable — these are the symbols that survive in the generated strings.
- Find P (productions): is simply the entire set of rules written for the grammar.
- Find S (start symbol): is the designated symbol from which derivations start.
To find the language, try out what strings the grammar can derive, starting from and repeatedly applying productions. At every step exactly one variable is replaced by the right-hand side of one of its productions.
Formulas
-
— : set of variables (non-terminals); : set of terminals; : set of productions; : start symbol, a distinguished variable from .
-
— one derivation step: the variable is replaced by the right-hand side of its production.
Worked examples
Example grammar:
S -> a S B
B -> b
Step 1 — Identify V (variables): the left-hand sides that occur are and .
Step 2 — Identify T (terminals): the remaining symbols are the small letters and .
Step 3 — Identify P (productions):
Step 4 — Identify S (start symbol): .
So the grammar is .
Step 5 — Derivation of the string : start from and apply :
At every step exactly one variable is replaced by its right-hand side — here either can be replaced (using ) or can be replaced (using ).
The derivation continues by substituting the inner with (applying again), and then replacing the first by its right-hand side , continuing until the string is obtained.
The full step-by-step trace of this derivation (leftmost and rightmost versions) is developed in the following part.
Revision summary
- A grammar is exactly the 4-tuple : variables, terminals, productions, start symbol.
- Variables are exactly the symbols appearing on the left-hand side of productions; terminals are the symbols that appear in the final generated strings.
- Every derivation begins from the start symbol .
- At each derivation step exactly one variable is replaced by the right-hand side of one of its productions.
- A string is in the language only when it is derivable from and consists of terminals only.
Previous-year exam insights
- The language of a grammar is L(G) = { w in T* : S =>* w }, i.e. only terminal-only strings derivable from the start symbol;
any string containing a non-terminal is a sentential form, not a member of L(G).
Asked inGATE 2017GATE 2016GATE 2008 - To find L(G) from productions, look for the structural pattern: paired/recursive rules such as A → aAb generate equal counts ( ), rules like A → aA | Aa | ... generate arbitrary placement, and a chain S → aSb | X with X → aX | Xb | a | b generates every non-empty string of the form without ε;
it does not force both an a and a b.
Asked inGATE 2023GATE 2008GATE 2006 - A grammar with rules S → aSb | ab generates { | n ≥ 1 };
adding S → epsilon gives n ≥ 0.
Adding S → aS | Sb as well gives { | p, q ≥ 1 } (and a^* only if S → epsilon is also present) — always check whether epsilon is derivable before fixing the lower bound.
Asked inGATE 2000GATE 2006 - To test whether a given string is generated, do a top-down derivation (or build the parse tree) from S; a string is in L(G) iff some sequence of productions yields exactly it.
For 'which string is NOT generated' questions, try to derive each option and eliminate.
Asked inGATE 2017GATE 2007 - A grammar is context-free when every production has a single non-terminal on the left (A → alpha, alpha in (V ∪ T)*); regular grammars are the special case with right-linear (A → aB | a) or left-linear (A → Ba | a) rules only, so every regular grammar is context-free but not conversely.
Asked inGATE 2007 - Ambiguity is a property of the grammar, not of the language: a CFL may have both ambiguous and unambiguous grammars, and some CFLs (inherently ambiguous) have only ambiguous grammars; an unambiguous grammar gives exactly one parse tree per string in L(G).
Asked inGATE 2004