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 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 .
- 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 | yis shorthand for two separate productions and . - 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
- Write the grammar's productions, expanding any
|shorthand into separate rules. - Collect the symbols on the left-hand sides of the productions: these form .
- Collect every symbol that does not appear on any left-hand side: these form .
- List the rules themselves as the set .
- Choose the start symbol from the variables in .
Formulas
-
— : set of variables (non-terminals); : set of terminals; : set of productions; : start symbol.
-
— shorthand for the two productions and .
Worked examples
Grammar: E -> E + E | E * E | id
Step 1 — Write the productions separately:
Step 2 — Left-hand sides: only appears on the left-hand side. Therefore .
Step 3 — Symbols not on any left-hand side: +, *, id.
Therefore .
Step 4 — The rules are the productions: .
Step 5 — The start symbol is a variable; the only variable is . Therefore .
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 ; 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 | idis three productions, not one.- The start symbol must be one of the variables (here , 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 { | i ≠ j} or {w | (w) > (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