1. Gyanastra
  2. Compiler Design
  3. Grammars and Ambiguity
  4. Grammars: Variables, Terminals, Productions, and Start Symbol
Free lesson, no account needed 5 min read 18 practice questions after it
Asked in past papers17 times
  • GATE CSE2024200820072006200419981996199519941992
  • ISRO20202011
  • UGC NET20192015201420132007

Compiler Design · Grammars and Ambiguity

Grammars: Variables, Terminals, Productions, and Start Symbol

Overview

A grammar is the formal specification that a parser uses to check whether an input string is valid. It is defined as a 4-tuple G=(V,T,P,S)G = (V, T, P, S) whose components are found directly from the grammar's rules, and its purpose is to generate all strings of the language.

Key concepts

  • Grammar: The input to a syntax analyzer (parser), together with the input string; the parser checks the input string according to the grammar. Grammars must therefore be understood before studying the syntax analyzer.
  • 4-tuple form: A grammar is represented as G=(V,T,P,S)G = (V, T, P, S).
    • V = set of variables (non-terminals)
    • T = set of terminals
    • P = set of productions (rules)
    • S = start symbol
  • Variables (non-terminals): The symbols appearing on the left-hand side of the productions.
  • Terminals: All symbols that do not appear on the left-hand side of any production.
  • Productions: The rules of the grammar. The alternative notation A -> x | y is shorthand for two separate productions A→xA \to x and A→yA \to y.
  • Start symbol: One of the variables; it is the variable from which generation begins.
  • Purpose of a grammar: To generate all the strings of the language (for example, id + id * id).

How it works

  1. Write the grammar's productions, expanding any | shorthand into separate rules.
  2. Collect the symbols on the left-hand sides of the productions: these form VV.
  3. Collect every symbol that does not appear on any left-hand side: these form TT.
  4. List the rules themselves as the set PP.
  5. Choose the start symbol SS from the variables in VV.

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→x∣yA \to x \mid y — shorthand for the two productions A→xA \to x and A→yA \to y.

Worked examples

Grammar: E -> E + E | E * E | id

Step 1 — Write the productions separately:

  • E→E+EE \to E + E
  • E→E∗EE \to E * E
  • E→idE \to id

Step 2 — Left-hand sides: only EE appears on the left-hand side. Therefore V={E}V = \{ E \}.

Step 3 — Symbols not on any left-hand side: +, *, id. Therefore T={+,∗,id}T = \{ +, *, id \}.

Step 4 — The rules are the productions: P={E→E+E, E→E∗E, E→id}P = \{ E \to E + E,\ E \to E * E,\ E \to id \}.

Step 5 — The start symbol is a variable; the only variable is EE. Therefore S=ES = E.

Step 6 — The grammar generates strings such as id + id * id (derived using the grammar's productions).

Revision summary

  • A grammar is exactly the 4-tuple G=(V,T,P,S)G = (V, T, P, S); know what each letter stands for.
  • Variables are found on the left-hand sides; terminals are everything that never appears on a left-hand side.
  • | is only shorthand — E -> E + E | E * E | id is three productions, not one.
  • The start symbol must be one of the variables (here EE, the only variable).
  • The parser's input is a grammar plus the input string; the grammar is what the string is checked against.
  • The purpose of a grammar is to generate all strings of the language, e.g. id + id * id.

Previous-year exam insights

  • A grammar G = (V, T, P, S) is context-free iff every production is of the form A → α with A a single variable (A ∈ V) and α ∈ (V ∪ T)*;
    type-0/1/2/3 grammars are distinguished by restrictions on production left- and right-hand sides (regular grammars allow only A → aB or A → a).
    Asked inGATE 1995
  • A grammar is ambiguous iff some string in L(G) has two distinct parse trees (equivalently two distinct leftmost derivations); ambiguity of a CFG is undecidable in general, and no algorithm can remove ambiguity from every ambiguous grammar.
    Asked inGATE 2024UGC NET 2013
  • For a CFG in Chomsky Normal Form (productions A → BC or A → a), every derivation of a string of length l ≥ 1 has exactly 2l - 1 steps, so the parse tree has 2l - 1 internal (non-terminal) nodes and l leaves, i.e. 3l - 1 nodes in total;
    converting a grammar with no λ- or unit-productions whose longest right side has K symbols yields at most (K - 1)|P| + |T| productions in CNF.
    Asked inGATE 2024GATE 1992UGC NET 2019
  • Context-free grammars cannot capture context-sensitive constraints such as 'a variable must be declared before use', type agreement, or matching numbers of parameters; these require semantic analysis.
    CFGs can express nested/recursive structure (if-then-else, recursive procedures) and identifiers of arbitrary length.
    Asked inGATE 1994
  • Standard closure/decidability facts: emptiness, finiteness and membership for CFGs are decidable; equivalence and ambiguity of CFGs are undecidable; CFLs are closed under union, concatenation, Kleene star and intersection with a regular language, but not under intersection or complement.
    Asked inUGC NET 2013
  • To prove a grammar generates a language like {aia^{i} bjb^{j} | i ≠ j} or {w | n0n_{0}(w) > n1n_{1}(w)}, use the standard trick of a union of two cases (i < j and i > j), each built by generating matched pairs with a recursive rule and adding the extra symbols on one side; for counting conditions, generate one symbol per recursive step and let the other symbol be produced freely.
    Asked inGATE 2006UGC NET 2015UGC NET 2013
Pages 1–2 of 2