Class 18 Context Free Grammar
Class 18 Context Free Grammar
CFG G=(V,T,P,S)
Example
The grammar ({A}, {a, b, c}, P, A)
P : A → aA, A → abc.
S ⇾ aSb | ε
The implication is that V = {S}, Σ = {a,b}, the start
symbols is S and the rules are:
S ⇾ aSb
S⇾ε
Notational Conventions
1. These symbols are terminals:
i) Lower-case letters early in the alphabet: a, b, c
ii) Operator symbols: +, -
iii) Punctuation symbols: parentheses, comma
iv) Digits: 0, 1, …,9
v) Boldface strings: id, if
2. These symbols are nonterminal:
i) Upper-case letters early in alphabet: A, B, C
ii) The letter S: start symbol
iii) Lower-case italic names: expr, stmt
Notational Conventions (cont.)
3. Upper-case letters late in alphabet: X, Y, Z,
represent grammar symbols (terminals or
nonterminal)
4. Lower-case letters late in alphabet: u, v,…z,
represent string of terminals
5. Lower-case Greek letters : , , , represent
string of grammar symbols
6. A-productions (all productions): A 1| 2|…|
k
7. Start symbol: the left side of the first
production
Derivation:
A replacement according to a production is known
as derivation. (or)
A derivation is a sequence of steps which begins
with the start symbol, uses the rules to do
replacements, and ends with a terminal string
Representation Technique
•Root vertex − Must be labeled by the start symbol.
•Vertex − Labeled by a non-terminal symbol.
•Leaves − Labeled by a terminal symbol or ε.
If S → x1x2 …… xn is a production rule in a CFG, then the parse tree / derivation tree
will be as follows −
Derivation
Expand the start symbol using one of its
productions and further expand the result
string by replacing one of the variable by
the body of one of its production, until we
derive a string consisting entirely of
terminals.
Derivations
E E+E | E*E| (E) | - E | id
E derives -E
– E-E
The derivation of -(id) from E
− E - E - (E) -(id)
A , if A
Example
Let a CFG {N,T,P,S} be
N = {S}
T = {a, b}
Starting symbol = S
P = S → SS | aSb | ε
One derivation from the above CFG is “abaabb”
S → SS → aSbS → abS → abaSb → abaaSbb → abaabb
Sentential Form and Partial Derivation Tree
A partial derivation tree is a sub-tree of a derivation tree/parse
tree such that either all of its children are in the sub-tree or none
of them are in the sub-tree.
Example
If in any CFG the productions are −
S → AB, A → aaA | ε, B → Bb| ε
the partial derivation tree can be the following
Derivation: Reduction:
EE+E Id+(id*id)
id +E E+(id*id)
id +(E) E+(E*id)
id+(E*E) E+(E*E)
id+(id*E) E+(E)
id+(id*id) E+E
E
Parse Tree
The parse tree for G are trees with the following
condition.
1.Each interior node is labeled by a variable in V.
2.Each leaf is labeled either by a variable, a
terminal or epsilon
3. If an interior node is labeled A, and its children
are labeled X1,X2,….Xk respectively, from the
left, then AX1,X2,….Xk is a production P.
Parse Tree and Derivations
E E+E | E*E | (E) | - E | id
17
Parse Tree and Derivations
(cont.)
Left Most Derivation and Right
Most Derivation
Leftmost Derivation:
At each step, we replace leftmost variable by one
of its production bodies.
we indicate that a derivation is left most by,
Rightmost Derivation:
We replace rightmost variable is replaced by one
of its bodies.
Example
Let any set of production rules in a CFG be
X → X+X | X*X |X| a
over an alphabet {a}.
The leftmost derivation for the string "a+a*a" may be −
X → X+X → a+X → a + X*X → a+a*X → a+a*a
S → bB / aA
A → b / bS / aAA
B → a / aS / bBB
For the string w = bbaababa,
Ambiguity
• More than one parse tree for some sentences
• More than leftmost derivation for some
sentences
• More than rightmost derivation for some
sentences
Check whether the grammar G with production rules −
X → X+X | X*X |X| a is ambiguous or not.
Solution
Let’s find out the derivation tree for the string "a+a*a". It has two leftmost derivations.
Derivation 1 Derivation 2
X → X+X X → X*X
→ a +X → X+X*X
→ a+ X*X → a+ X*X
→ a+a*X → a+a*X
→ a+a*a → a+a*a
Parse tree 1 − Parse tree 2 −
Since there are two parse trees for a single string "a+a*a", the grammar G is
ambiguous.
Suppose we have a context free grammar G with production rules :
S -> aSb | bSa | SS | e
String : abab
Since there are two parse trees for a single string "3 * 2 + 5", the grammar G
is ambiguous.
Check whether the given grammar G is ambiguous or not.
E→E+E
E→E-E
E → id
From the above grammar String "id + id - id"
Recall: (a | b)*abb
a
start a b b
0 1 2 3
b
Resolving Problems/Difficulties – (3)
Construct CFG as follows:
1. Each State I has non-terminal Ai : A0, A1, A2, A3
i a j
2. If then Ai a Aj
i b j
3. If then Ai bAj
4. If I is an accepting state, Ai : A3
vs.
A0 aA0, A0 aA1
A0 bA0, A1 bA2
A2 bA3, A3
Left Recursive Grammars
In a context-free grammar G, if there is a production in the
form X → Xa where X is a non-terminal and ‘a’ is a string of
terminals, it is called a left recursive production. The
grammar having a left recursive production is called a left
recursive grammar.