A Context-Free Grammar (CFG) is a formal grammar used to define the syntax of programming languages through production rules.
- Syntax Analysis: Helps the parser check whether source code follows the grammar rules.
- Production Rules: Define how terminals and non-terminals combine to form valid strings.
- Parse Tree: Helps represent the syntactic structure of source code for subsequent compiler phases.
Components of a CFG
A Context-Free Grammar consists of four main components:
- Non-terminals: These are symbols that can be replaced by other symbols using production rules. They represent syntactic categories such as statements and expressions. Examples: S, A, B.
- Terminals: These are basic symbols that cannot be replaced further. They represent the actual symbols or tokens of the language, such as keywords, operators, and identifiers. Examples: a, b, +, if, id.
- Start Symbol: This is the non-terminal from which the derivation begins. It represents the starting point for generating strings in the language. Example: S.
- Production Rules: These rules define how non-terminals can be expanded into sequences of terminals and/or non-terminals. Example: S → AB, A → a, B → b.
Example of a CFG
Consider the following CFG for arithmetic expressions:
E → E + T | T
T → T * F | F
F → (E) | id- E represents an expression.
- T represents a term.
- F represents a factor.
- + and * are operators.
- id represents an identifier.
This grammar defines the structure of arithmetic expressions and establishes operator precedence, where multiplication has higher precedence than addition.
Classification of CFG
Context-Free Grammars can be classified based on properties such as recursion and ambiguity.
1. Based on Recursion
Non-Recursive Grammar
A CFG is called non-recursive if no non-terminal can derive a sentential form containing itself.
S → Aa
A → b | c
Language generated: {ba, ca}
This grammar is non-recursive because no non-terminal can derive a sentential form containing itself.
Recursive Grammar
A CFG is called recursive if at least one non-terminal can derive a sentential form containing itself.
Example 1:
S → SaS
S → b
Language generated: {b, bab, babab, ...}
Example 2:
S → Aa
A → Ab | c
Language generated: {ca, cba, cbba, ...}
Both grammars are recursive because their productions allow a non-terminal to derive a sentential form containing itself.
Types of Recursive Grammars
Recursive grammars can be classified based on the position of the recursive non-terminal in a production.
- Left-Recursive Grammar: A grammar is left-recursive if a non-terminal appears as the leftmost symbol in its own production.Example:
S → Sα - Right-Recursive Grammar: A grammar is right-recursive if a non-terminal appears as the rightmost symbol in its own production.Example:
S → αS - General Recursive Grammar: A grammar has general recursion when the recursive non-terminal appears between other symbols in a production.Example:
S → aSb
2. Based on Ambiguity
Unambiguous Grammar
A CFG is unambiguous if every string generated by the grammar has exactly one parse tree. Equivalently, every string has exactly one leftmost derivation and exactly one rightmost derivation.
Example of Unambiguous CFG:
E → E + T | TT → T * F | FF → (E) | id
This grammar defines operator precedence, where multiplication has higher precedence than addition.
For the expression a + b * c, only one parse tree is possible because multiplication is performed before addition. Therefore, the grammar is unambiguous.
Ambiguous Grammar
A CFG is ambiguous if at least one string generated by the grammar has more than one parse tree. Equivalently, that string has multiple leftmost derivations or multiple rightmost derivations.
Example of Ambiguous CFG:
E → E + E | E * E | idThis grammar does not define operator precedence.
For the expression a + b * c, two different parse trees are possible:
- a + (b * c)
- (a + b) * c
Since the expression can have more than one parse tree, the grammar is ambiguous.
Note: A linear grammar is a type of CFG in which each production has at most one non-terminal on its right-hand side.

