1. Gyanastra
  2. Theory of Computation
  3. Context-Free Grammars
  4. Definition of Grammar (V, T, P, S) and Language Generated by a Grammar
Free lesson, no account needed 6 min read 14 practice questions after it
Asked in past papers29 times
  • GATE CSE2026202320172016200820072006200420001995
  • ISRO2023202020182017201520142011
  • UGC NET202220192018201620152014201320122010200920072006

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 G=(V,T,P,S)G = (V, T, P, S), 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 G=(V,T,P,S)G = (V, T, P, S); 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 VV from which every derivation begins.
  • Derivation — a step-by-step generation of a string: begin with SS 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:

  1. Find V (variables): collect every symbol that appears on a left-hand side of some production. Variables are exactly the left-hand-side symbols.
  2. 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.
  3. Find P (productions): PP is simply the entire set of rules written for the grammar.
  4. Find S (start symbol): SS is the designated symbol from which derivations start.

To find the language, try out what strings the grammar can derive, starting from SS and repeatedly applying productions. At every step exactly one variable is replaced by the right-hand side of one of its productions.

Formulas

  • G=(V,T,P,S)G = (V, T, P, S) — VV: set of variables (non-terminals); TT: set of terminals; PP: set of productions; SS: start symbol, a distinguished variable from VV.

  • S⇒aSBS \Rightarrow aSB — one derivation step: the variable SS is replaced by the right-hand side aSBaSB 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 SS and BB. V={S,B}V = \{ S, B \}

Step 2 — Identify T (terminals): the remaining symbols are the small letters aa and bb. T={a,b}T = \{ a, b \}

Step 3 — Identify P (productions): P={S→aSB, B→b}P = \{ S \to aSB,\ B \to b \}

Step 4 — Identify S (start symbol): S=SS = S.

So the grammar is G=({S,B}, {a,b}, {S→aSB, B→b}, S)G = ( \{S, B\},\ \{a, b\},\ \{ S \to aSB,\ B \to b \},\ S ).

Step 5 — Derivation of the string aabbaabb: start from SS and apply S→aSBS \to aSB: S⇒aSBS \Rightarrow aSB

At every step exactly one variable is replaced by its right-hand side — here either SS can be replaced (using S→aSBS \to aSB) or BB can be replaced (using B→bB \to b).
The derivation continues by substituting the inner SS with aSBaSB (applying S→aSBS \to aSB again), and then replacing the first BB by its right-hand side bb, continuing until the string aabbaabb 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 G=(V,T,P,S)G = (V, T, P, S): 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 SS.
  • 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 SS 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 (ana^{n} bnb^{n}), 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 apbq(p,q≥0,p+q≥1),i.e.a⋅b⋅a^{p} b^{q} (p, q \ge 0, p + q \ge 1), i.e. a\cdot b\cdot without ε;
    it does not force both an a and a b.
    Asked inGATE 2023GATE 2008GATE 2006
  • A grammar with rules S → aSb | ab generates { ana^{n} bnb^{n} | n ≥ 1 };
    adding S → epsilon gives n ≥ 0.
    Adding S → aS | Sb as well gives { apa^{p} bqb^{q} | p, q ≥ 1 } (and a^*b∗b^{*} 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
Pages 1–2 of 2