Chapter 4
Chapter 4
Context-Free Language (CFL) is a language generated by a Context-Free Grammar (CFG). CFG is a set
of rules for deriving (or generating) strings (or sentences) in a language.
Definition: Let L be a language. Then L is a context-free language if and only if there exists a
context-free grammar G such that L = L(G).
Every regular grammar is context-free, so a regular language is also a context- free one, so we
see that the family of regular languages is a proper subset of the family of context-free
languages. Context- free grammars derive their name from the fact that the substitution of the
variable on the left of a production can be made any time such a variable appears in a sentential
form. It does not depend on the symbols in the rest of the sentential form (the context). This
feature is the consequence of allowing only a single variable on the left side of the production.
Definition: Let G = (V, T, P, S) be a CFG. A tree is a derivation (or parse) tree if:
– Every vertex has a label from V U T U {ε}
– The label of the root is S
– If a vertex with label A has children with labels X1, X2,…, Xn, from left to right,
then
A –> X1, X2,…, Xn
must be a production in P
If a vertex has label ε, then that vertex is a leaf and the only child of its’ parent
More generally, a derivation tree can be defined with any non-terminal as the root.
Example
Page | 1
For generating a language that generates equal number of a’s and b’s in the form
anbn . the context free grammar is defined as G= {(S, A), (a, b), (S aAb, A
aAb/ ε)}
SaAb
aaAbb (AaAb)
aaaAbbb (A aAb)
aaabb (A ε)
aaabbb a3b3 anbn
Page | 2
For example:
Consider the grammar with productions
S → aAB A → bBb B → A | ε
Then,
S ⇒aAB⇒abBbB⇒abAbB⇒abbBbbB⇒abbbbB⇒ abbbb is a leftmost derivation of the string
abbbb. A rightmost
derivation of the same string is
S ⇒ aAB ⇒ aA ⇒ abBb ⇒ abAb ⇒ abbBbb ⇒ abbbb.
Page | 3
N is a set of non-terminal symbols.
T is a set of terminals where N ∩ T = NULL.
P is a set of rules, P: N → (N ∪ T)*, i.e., the left-hand side of the production
rule P does have any right context or left context.
S is the start symbol.
Page | 4
Example
Derivation Order
Consider the following example, grammar with 5 productions:
Derivation Trees
Consider the same example grammar:
S AB A aaA | B Bb |
Page | 5
Page | 6
Ambiguity in Grammars and Languages:
A context free grammar G is said to be ambiguous if there exists some w ∈ L(G) which has at least two
distinct derivation trees. Alternatively ambiguity implies the existence of two or more leftmost or
rightmost derivations.
Page | 7
each statement, ambiguity must be removed when possible. Often we can achieve this by
rewriting the grammar in an equivalent, unambiguous form.
Page | 8
No other derivation tree is possible for this string: the grammar is unambiguous.
Page | 9
Removing Useless Productions
Let G = (N, T, S, P) be a context free grammar. A variable A ∈ N is said to be useful if and only if there
is at least one w ∈ L(G) such that S ⇒αAβ ⇒w where α and β are any strings. A is a Non-
terminal and w is a sentence
In words, a variable is useful if and only if it occurs in at least one derivation. A variable or non-terminal
that is not useful is called Useless. A production is useless if it involves any useless variable.
For example, consider the context free grammar
G = (N, T, S, P)
with N = {S, A, B}, T = {a, b} and, P having the productions
S→A
A → aA | ε
B → bA
In this case the non-terminal or variable B is useless and so is the production B → bA. Although B can
derive a terminal string, there is no way we can achieve S ⇒ xBy , because B cannot be reached from
the start symbol. A method to identify and eliminate the non-terminals or variables that cannot be reached
from the start symbol is by constructing a dependency graph for the non-terminals. Dependency graphs
are a way of visualizing complex relationships and are found in many applications. For context-free
grammars, a dependency graph has its vertices labeled with non-terminals or variables, with an edge
between the vertices C and D if and only if there is a production rule
C → xDy
where C and D are non-terminals x and y are terminals.
The dependency graph for the above context-free grammar is shown in the figure below:
Page | 10
A non-terminal or variable is useful only if there is a path from the vertex of the start symbol to the vertex
of that non-terminal or variable. In our case the vertex labeled with the non-terminal or variable B cannot
be reached from the vertex of the start symbol labeled S. Removing it and the corresponding production
rule we obtain the resulting grammar
G = ( N , T , S , P)
where N = {S, A}
T = {a}
P = { S → A, A → aA | ε }
Page | 11