You are on page 1of 27

Context-Free Grammars

By
Dr. Prem Nath
Associate Professor
Computer Science & Engineering Department,
H N B Garhwal University
(A Central University)

1
Outline
Introduction
Derivation and Parse Tree
Ambiguous Grammars
Context-Free Grammars(CFGs)
A grammar G=(Vn, , P, S) is said to be
context-free grammar (CFG) if the
production rules of G are of the form:
A→, where (Vn)* and AVn
Examples: S→aSabSb
Many programming languages have
recursive structure that can be defined by
context-free grammars (CFGs).

Dr. Prem Nath 3


Derivation
We categorize the derivation of context-free
grammars in two categories: leftmost and
rightmost derivations.
➢Leftmost Derivation
➢Rightmost Derivation

Dr. Prem Nath 4


Leftmost and Rightmost Derivation
➢ If G=(Vn, , P, S) is a context-free grammar (CFG)
and wL(G), then a derivation S * w is called
leftmost derivation if and only if all steps involved in
derivation have leftmost variable replacement.

➢ If G=(Vn, , P, S) is a context-free grammar (CFG)


and wL(G), then a derivation S * w is called
rightmost derivation if and only if all steps involved in
derivation have rightmost variable replacement.

Dr. Prem Nath 5


Leftmost and Rightmost Derivation…
For example,
Consider the production rules of a grammar: S→S+SS*Sab. Find the type of
grammar and find leftmost and rightmost derivations for string w=a*a+b.
Solution: Let P1: S→S+S (RHS is variable terminal variable; type 2)
P2: S→S*S (RHS is variable terminal variable; type 2)
P3: S→a (RHS is terminal; type 3)
P4: S→b (RHS is terminal; type 3)
Hence, the given grammar is type 2 grammar or context-free grammar (CFG).
Leftmost derivation for w=a*a+b:
S  S*S (Using S→S*S)
 a*S (The first left hand symbol is ‘a’, so using S→a)
 a*S+S (Using S→S+S, in order to get a+b)
 a*a+S (Third symbol from the left is ‘a’, so using S→a)
 a*a+b (The last symbol from the left is ‘b’, so using S→b)

Dr. Prem Nath 6


Leftmost and Rightmost Derivation…
Rightmost derivation for w=a*a+b:

S  S*S (Using rule S→S*S)


 S*S+S
(Since, in the above sentential form second symbol from the right is *. So, we cannot
use rules S→ab. Therefore, using S→S+S)
 S*S+b (Using rule S→b)
 S*a+b (Using rule S→a)
 a*a+b (Using rule S→a)

Dr. Prem Nath 7


Derivation Tree or Parse Tree
Let G=(Vn, , P, S) be a context-free grammar
(CFG). Each production rule of G can be
represented with a tree satisfying the following:
If A→123…n is a production rule in P, then
A becomes the parent of nodes labeled 1, 2, 3,
…, n, and

The collection of children of a derivation tree


from left to right yields 123…n, where 1, 2,
3, …, and n are labels of children nodes.
Dr. Prem Nath 8
Derivation Tree or Parse Tree…
For example, consider production rules of a
context-free grammar: S→S+SS*Sab and
represent each production rule by tree.
S
For the production
S Rule: S→S*S
For the production
Rule: S→S+S S * S
S + S
Fig. 6.1(b)
Fig. 6.1(a)

S
For the production
S Rule: S→b
For the production
Rule: S→a b
a
Fig. 6.1(d)
Fig. 6.1(c)

Dr. Prem Nath

9
Derivation Tree or Parse Tree…
Now, let us see what derivation tree is. If wL(G), where G is a
context-free grammar then w is represented by a tree known
derivation tree or parse tree satisfying the following:

(i) The root has label S (the starting symbol of grammar),


(ii) All internal vertices (or nodes) are labeled with variables,
(iii) The leaves or terminal nodes are labeled with  or terminal
symbols,
If A→123…n is a production rule in G then variable A
becomes the parent of nodes labeled 1, 2, 3, …, n, and
(iv) The collection of leaves from left to right yields the string w.

Dr. Prem Nath


10
Derivation Tree or Parse Tree…
Example: Consider a grammar G having production rules:
S→aAS│a, A→SbA│SS│ba. Show that w=aabbaa is in
L(G) and construct a derivation tree whose yield is w.
Solution:
S
SaASaSbASaabASaabbaSaabbaa
Hence, S aabbaa
Parse tree is shown in Fig. 6.7. a A S

S b A a

a b a

Fig. 6.7 Parse tree yielding aabbaa

Dr. Prem Nath


11
Ambiguity and Context-free Grammars
➢ A grammar G is said to be ambiguous if there exists some
string wL(G) for which there are two or more distinct
derivation trees, or there are two or more distinct leftmost
derivations.
➢ Ambiguity is a negative property of a grammar. It is
usually (but not always) possible to find an equivalent
unambiguous grammar. The language generated by an
ambiguous context-free grammar is known as ambiguous
language. An inherently ambiguous language is a language
for which no unambiguous grammar exists.

Dr. Prem Nath 12


Ambiguity and Context-free Grammars…
For example, consider the production rules of a context-free grammar:
S→S+SS*Sab and string w=a*a+a. Check whether grammar is ambiguous
or not?
Solution: Let us see how many leftmost derivations are there for string w.
First leftmost derivation for w=a*a+a:
SS*S (Using rule S→S*S)
a*S (Using rule S→a)
a*S+S (Using rule S→S+S)
a*a+S (Using rule S→a)
a*a+a (Using rule S→a)

Dr. Prem Nath 13


Ambiguity and Context-free Grammars…
For example, consider the production rules of a context-free grammar:
S→S+SS*Sab and string w=a*a+a. Check whether grammar is ambiguous or
not?
Solution:
Second leftmost derivation for w=a*a+a:
SS+S (Using rule S→S+S)
S*S+S (Using rule S→S*S)
a*S+S (Using rule S→a)
a*a+S (Using rule S→a)
a*a+a (Using rule S→a)
So, we have two distinct leftmost derivations, which are given above and two
distinct parse trees are shown in Fig. 6.3(a) and Fig. 6.3(b). In other words, there is
ambiguity in given grammar for string w.

Dr. Prem Nath 14


Ambiguity and Context-free Grammars…

S S

S * S S + S

S + S S * S
a a

a a a a
Fig. 6.3(a) Parse tree for a*a+a Fig. 6.3(b) Parse tree for a*a+a

Dr. Prem Nath 15


Left Recursion
➢A production rule of the grammar G=(Vn, , P,
S) is said to be left recursive if it is of the form
A→A, where A is a variable and (Vn)*.
For example, production rules S→Sa, A→Aaa,
S→S*S are left recursive.
Sometimes left recursive production rules lead to
the ambiguous grammar.

Dr. Prem Nath 16


Elimination of Left Recursion
Let the variable A has left recursive productions
as follows:
A→A1│A2│A3│…│An│1│2│3│…│
m, where 1,2,3,…,m do not begin with A, then
we replace A-productions by following
production rules:
A→1A│2A│…│mA, where
A→1A│2A│3A│….│nA│

Dr. Prem Nath 17


Elimination of Left Recursion
For example, consider the production rules of a context-free
grammar (CFG) G: E→E+T│T, T→T*F│F, F→id, where E is the
starting symbol and id is the only terminal. Remove the left recursion
(if any).
Solution: Recursive production rules are:
E→E+T, T→T*F
Eliminating Left Recursion:
Production rule: E→E+T can be replaced by: E→TE, where
E→+TE│
Production rule: T→T*F can be replaced by: T→FT, where
T→*FT│
So, production rules of G without left recursion are: E→TE,
E→+TE│, T→FT, T→*FT│, F→id
Dr. Prem Nath 18
Left Factoring
Two or more production rules of a variable A of
the grammar G=(Vn, , P, S) are said to have left
factoring if A-productions are of the form
A→12...n, where i(Vn)* and does
not start (prefix) with .
The common factor () in A-productions create
problem in selecting appropriate production rule
for derivation.
The problem associated with left factoring
grammars is backtracking.
Dr. Prem Nath 19
Elimination of Left Factoring
Let the variable AVn has (left factoring)
production rules as given follows:
A→1│2│3│…│n│1│2│….│m, where
1, 2, 3,…,n and 1, 2, …,m do not contain  as
a prefix then we replace A-productions by:
A→A│1│2│….│m,
where A→1│2│…│n
➢Left factoring is a grammar transformation that is
useful for producing a grammar suitable for
predictive or top-down parsing.
Dr. Prem Nath 20
Elimination of Left Factoring…
For example, consider the context-free grammar
having production rules: S→aSa│aa and remove the
left factoring (if any).
Solution: Both production rules: S→aSa and S→aa
have =a as a left factor, so given grammar has left
factoring. We have, left (common) factor =a,
1=Sa, and 2=a.
After removing the left factor, we get the production
rules: S→aS, S→Sa│a

Dr. Prem Nath 21


Removal of Ambiguity
➢ We have no specific rule or method defined for
removing ambiguity as we have for left recursion and
left factoring. So, we will have to concentrate on
heuristic approach most of the time.
➢ Let us consider the production rules of an ambiguous
grammar: S→S+SS*Sab. If we analyze the
production rules then we find that two production rules
are left recursive. So, first we try to remove the left
recursion: S→S+S and S→S*S are replaced by
S→aS│bS│, S→+SS│*SS│

Dr. Prem Nath 22


Removal of Ambiguity…
Now, we check the derivation for string w=a*a+a and we
have only one leftmost derivation given below:
SaS a*SS a*aSS a*a+SSS a*a+aSSS
a*a+aSS a*a+aS a*a+a (a*a+a)

So, we see that removal of left recursion helps in removal of


ambiguity in the ambiguous grammars.

Dr. Prem Nath 23


Removal of Ambiguity…
For example, Remove the ambiguity from the following grammars:
S→aSb│aabS│
The given grammar has left factoring in productions S→aSb│aabS, so removing the left
factoring, we get
S→aS, S→Sb│abS
Consider the ambiguous string w=aaabb, now it has only one leftmost derivation given below:
S  aS (Using S→aS)
 aSb (Using S→Sb)
 aaSb (Using S→aS)
 aaabSb (Using S→abS)
 aaabb (Using S→)
Similarly, we can see for other ambiguous strings also.
So, we see that removal of left factoring helps in removal of ambiguity in the ambiguous
grammars.

Dr. Prem Nath 24


Removal of Ambiguity…
Removal of Ambiguity…
Idea: Sometimes we can re-write grammar to
eliminate ambiguity
A statement appearing between a then and an else must
be matched
Thanks

27

You might also like