Skip to main content
DEPARTMENT OF COMPUTER SCIENCE AND APPLICATION
Atal Bihari Vajpayee Vishwavidayalaya
,Bilaspur (C.G.)
SESSION :- 2022-
2023
Subject- Theory Of Computing
Presentation topic :- Context - Free Grammer
PRESENTED TO -
MISS PRERNA MAM Geeta Kumari
Chandani
Yamini
Jankee
MSc Ist
PRESENTED BY -
CONTENT -
⮚Context - Free Grammer
⮚Formal definition of CFG
⮚Derivation
Leftmost Derivation
Rightmost Derivation
⮚Derivation tree
⮚Ambiguity in Grammer
Context-Free Grammar (CFG)
CFG stands for context-free grammar. It is a set of rules which is used
to generate all possible patterns of strings in a given formal language.
Context-free grammar G can be defined by four tuples as:
G = (V, T, P, S)
G is the grammar, which consists of a set of the production rule. It
is used to generate the string of a language.
T = set of Terminal symbols . It is denoted by small letters .
P = set of Production rules
S = Start symbol
EXAMPLE 1 : -
Construct a CFG for the regular expression a* .
production rules :- S aS rule 1
S ε rule 2
V = set of Non - Terminal symbols . It is denoted by capital letters
If we want to derive a string “aaaa” , we can start with start
symbol
S
aS rule 1
aaS rule 1
aaaS rule 1
aaaaS rule 1
aaaaε rule 2
aaaa
.
The r . e . = a* can generate a set of string {ε , a , aa , aaa,…..} .
Solution :-
Production rules :- S 0S / 1S
S ε
SOLUTION :-
the r . e . = (0+1)* can generate a set string { ε , 0 , 1
01 , 10 , 00 , 11 , ……..} .
Construct a CFG for the regular expression (0+1)* .
EXAMPLE 2 :-
Derivation
Derivation is a sequence of production rules. It is used to get the input string
through these production rules. During parsing, we have to take two decisions.
These are as follows:
⮚We have to decide the non-terminal which is to be replaced.
⮚We have to decide the production rule by which the non-terminal will be
replaced.
We have two options to decide which non-terminal to be placed with
production rule.
1. Leftmost Derivation :
In the leftmost derivation, the input is scanned and replaced with the
production rule from left to right. So in leftmost derivation, we read the
input string from left to right.
Example:
E = E + E
E = E - E
E = a | b
Input
a - b + a
The leftmost derivation is:
E = E + E
E = E - E + E
E = a - E + E
E = a - b + E
E = a - b + a
2. Rightmost Derivation :
In rightmost derivation, the input is scanned and replaced with the
production rule from right to left. So in rightmost derivation, we read the
input string from right to left.
Example :-
Production rules:
E = E + E
E = E - E
E = a | b
Input
a - b + a
The rightmost derivation is:
E = E - E
E = E - E + E
E = E - E + a
E = E - b + a
E = a - b + a
When we use the leftmost derivation or rightmost derivation, we may get the
same string. This type of derivation does not affect on getting of a string.
Examples of Derivation:
Example 1:
Derive the string "abb" for leftmost derivation and rightmost derivation using
a CFG given by,
S → AB | ε
A → aB
B → Sb
Solution:
Leftmost derivation:
S
AB
aB B
a Sb B
a ε bB
ab ε Sb
ab ε b
abb
Rightmost derivation:
Example 2:
Derive the string "aabbabba" for leftmost derivation and rightmost
derivation using a CFG given by,
S → aB | bA
A → a | aS | bAA
B → b | bS | aBB
Solution:
Leftmost derivation:
S
aB S → aB
aaBB B → aBB
aabB B → b
aabbS B → bS
aabbaB S → aB
aabbabS B → bS
aabbabbA S → bA
aabbabba A → a
Rightmost derivation:
S
aB S → aB
aaBB B → aBB
aaBbS B → bS
aaBbbA S → bA
aaBbba A → a
aabSbba B → bS
aabbAbba S → bA
aabbabba A → a
Example 3:
Derive the string "00101" for leftmost derivation and rightmost
derivation using a CFG given by,
S → A1B
A → 0A | ε
B → 0B | 1B | ε
Solution:
Leftmost derivation:
S
A1B
0A1B
00A1B
001B
0010B
00101B
00101
Rightmost derivation:
S
A1B
A10B
A101B
A101
0A101
00A101
00101
Derivation Tree
Derivation tree is a graphical representation for the derivation of the given
production rules . It is the simple way to show how the derivation can be done to
obtain some string from a given set of production rules. The derivation tree is also
called a parse tree.
A parse tree contains the following properties:
⮚The root node is always a node indicating start symbols.
⮚The derivation is read from left to right
⮚The leaf node is always terminal nodes.
⮚The interior nodes are always the non-terminal nodes.
Example 1:
Production rules:
E = E + E
E = E * E
E = a | b | c
Input
a * b + c
Step 1:
Step 2:
Step 3 :
Step 4:
Step 5:
Example 2:
Draw a derivation tree for the string "bbabb" from the CFG given by
S → bSb
S a
S b
Solution:
Now, the derivation tree for the string "bbabb" is as follows:
The above tree is a derivation tree drawn for deriving a string bbabb. By
simply reading the leaf nodes, we can obtain the desired string. The same
tree can also be denoted by,
Ambiguity in Grammar
A grammar is said to be ambiguous if there exists more than one leftmost
derivation or more than one rightmost derivation or more than one parse
tree for the given input string.
If the grammar is not ambiguous, then it is called unambiguous.
Example 1:-
Let us consider a grammar G with the production rule
S Sa
S aS
S a
Solution :-
For given grammer there are two ways to derive a string “aaa”
S
S a
S a
a
S
S a
a S
a
First Leftmost Derivation Second Leftmost Derivation
Since there are two leftmost derivation for a single sting “aaa” , the
grammer is Ambigous grammer .
Example 2 :-
Check whether the given grammar G is ambiguous or not.
E → E + E
E → E - E
E → id
Solution :-
From the above grammar String "id + id - id" can be derived in 2 ways:
First Leftmost derivation
E → E + E
→ id + E
→ id + E - E
→ id + id - E
→ id + id- id
Second Leftmost derivation
E → E - E
→ E + E - E
→ id + E - E
→ id + id - E
→ id + id - id
Since there are two leftmost derivation for a single string "id + id - id", the
grammar G is ambiguous.
EXAMPLE 3 :-
Check whether the given grammer is ambigous or not .
S AB
A aA / b
B bB / a
Solution :-
S
A B
a A b B
b b
There are only one derivatio tree form for given grammer ,so this grammer
is Unambigous grammer .
Check whether the given grammer is Ambigous or not ?
1. G : E E + E
E id
W : id + id + id
2. G : S AB / aaB
A a / Aa
B b
W : “aab”
3. G : S aSa
S bSb
S ε
W : “abba”
THANK YOU