0% found this document useful (0 votes)
24 views26 pages

Chapter 3 - CFG

Uploaded by

Bulbula Kumeda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views26 pages

Chapter 3 - CFG

Uploaded by

Bulbula Kumeda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Chapter – 3

Context Free
Language and
Grammar
Contents
• Context Free Languages
• – Parsing and ambiguity
• – Sentential forms
• – leftmost and rightmost derivation
• – Derivation tree or Parse tree
• – Definition and relationship between
parse tree and derivation
• - Simplification of context free
grammar
• – Properties of Context Free
Context Free Language(CFL)

 To overcome the problem in regular language. We


needed the solution is called context free
language.
 Context free language is generated by the
context free grammar.
 Regular grammar is simple but context grammar
is complex compared to the regular grammar.

 From the diagram; regular language is a Sub set of


the CFL. CFL is complex one and it can solve the
3
Context Free Grammar

Definition:
G = (N, T, P, S)
N – Set of non terminals
T – set of terminals
P – Production rules: this rules are used to derive
the certain string
S – starting state

4
Derivation and ont.,

Generation Languages:
of language using specific rules is called
derivation.

Example 4.1: Construct the CFG for the language


having any number of a’s over the set Σ = { a }.
R.E = a* = {, a, aa, aaa, aaaa, ….}
Production rules for the same:
S  aS rule 1
S rule 2
Now if you want to derive the string ‘aaa’. Start with the start
symbol.
S
aS rule1
aaS rule1
aaaS rule1 5
Example
Example 4.2: Construct the CFG for the given
language
L = {an bn | n > 0 }
L = { ab, aabb, aaabbb, ….} = { equal number of a and b}
Production rule for the same:
S  aSb rule1
S rule2
Now if you want to derive the string ‘aabb’. You can start the
derivation from the start symbol
S
aSb rule1
aaSbb rule1
aabb rule1
aabb rule2
6
Leftmost Derivation and Rightmost
Derivation
• Generation of the string using production rules is
called derivation.
• In derivation of string: replacing the non -terminal
by appropriate rule.
• If there are more non-terminal available then
which non-terminal has to replace first; which is
based on the two methods are listed below:
•Leftmost Derivation
- It’s a derivation in which, leftmost non-
terminal is replaced first from the sentential form
•Rightmost Derivation
- It’s a derivation in which, rightmost non-
terminal is replaced first from the sentential form 7
Derivation Tree

Derivation tree is a graphical representation for the


derivation of the given production rules for the given
CFG. Its called parse tree.
Properties:
• Root node indicating start symbol
• Leaf nodes are terminals
• The interior nodes are non terminals

8
Cont...
Example 4.3: Let G be the grammar
S  aB | bA
A  a | aS | bAA
B  b | bS | aBB
For the string baaabbabba find leftmost, rightmost
derivation and Derivation tree
First write the production rules separately like below
S  aB rule1
S  bA rule2
Aa rule3
A  aS rule4
A  bAA rule5
Bb rule6
B  bS rule7
B  aBB rule8 9
Cont...
Leftmost Derivation Rightmost
Derivation
S S
bA r1 bA r2
baS r4 baS r4
baaB r1 baaB r1
baaaBB r8 baaaBB r8
baaabSB r7 baaaBbS r7
baaabbAB r2 baaaBbbA r2
baaabbaB r3 baaaBbba r3
baaabbabS r6 baaabSbba
r7
baaabbabbA r1 baaabbAbba r2
10
baaabbabba r3 baaabbabba
Cont...

11
Cont...
Try yourself:
1. Consider the following production
S  aB| bA
A a |aS |bAA
B  b | bS | aBB
Find the leftmost derivation, rightmost derivation
and parse tree.

12
Parsing

Parsing is the process of analyzing a string of


symbols, either in natural or computer language.

13

Ambiguity
• Grammar can be derived in either leftmost or
rightmost derivation.
• We can draw the derivation tree called as parse
tree or syntax tree. Parse tree is a unique one
though the derivation is leftmost or rightmost.
• If there exists more than one parse tree for a
given grammar that means more than one
rightmost or leftmost is possible: that
grammar is said to be ambiguous grammar.
14
Conct....

Example 4.4: The given CFG is G = (N, T, P, S)


where
N={E}P={EE+E
T = { id } EE*E
S={E} E  id } Is the given grammar
ambiguous?
Now if the string is id * id + id then we can draw
the parse tree

15
Simplification of CFG

• All Grammars are not always optimized. That
means grammars are containing some
unnecessary symbols(non-terminals) and this will
increase the length of the grammar.
• Simplification of grammar means reduction of
grammar by removing useless symbols.
i. Removal of useless symbols
ii. Elimination of production
iii. Removal of unit production 16
What is the Simplification of CFG?
Below are some reduced characteristics of
grammar:
• A word in L is derived from each variable (i.e.,
non-terminal) and every terminal in G.
• Where X and Y are not terminal, there should
be no production as X → Y.
• There need not be a production of X → ε if it
is not in the L language.
1. Removal of useless
symbols:
• Remove the useless non-terminals from the production
rules. It will lead to reduce the length of the grammar.
Example 4.5: remove the useless symbols from the
given production rule:
P = {S  A11B | 11A
S  B | 11
A0
B  BB }

 In the above production rule: non-terminals S and A


are having some terminal in the rules.

18
Cont...
 In the above production rule: non-terminals S and A are
having some terminal in the rules.
 But B is not containing any terminal so it will not terminate
at the production so non-terminal B is useless symbol, so
remove the non-terminal B from the production rules.
 Remove the rules from the production rule which are
containing the non-terminal B in the rule, so the rule1, rule3
and rule6 are removed from the Production rule, so the
minimize CFG is:

P= { S  11A | 11 19
Cont...
Example 4.6: Simplify the given CFG
S  AB | CA
B  BC | AB
Aa
C  aB | b
• In the above production rule: non-terminals A and C
are having some terminals. But non-terminal B is not
containing any terminal, so it will not terminate the
derivation process. So remove the rules which are
containing the non-terminal B from the production
rule. 20
2. Elimination of production
rule:
• Remove the symbol from the grammar without
affecting the meaning of the grammar. Thus is not
necessary for the grammar.
Example 4.7: Remove the production from the
following CFG
S  XYX
X  0X | 
Y  1Y | 
• To remove the production, delete the production rule
and to preserve the meaning of the CFG, placing the
value in the other production instead of the
corresponding non-terminal.
Step1: remove the production
Remove the rules X   and Y   21
Cont...
Step2: To preserve the meaning of grammar:
Place the X   to rule 1: S  XY   XY
S   YX  YX
SY Y
Place the Y   to rule 1: S  X  X  XX
Place the X   and Y   to rule 1: S    X  X
Place the X   to rule 2: X00
Place the Y   to rule 4: Y11
Collectively can write the CFG:
S  XY | YX | XX | X |Y
X0
Y1
22
Cont...
Example 4.8: for the given CFG below remove the
 production
S  aSa
S  bSb
S
Apply the rule 3 in the rule1 and rule2
S  aSa  aa
S  bSb  bb
So the final CFG is
S  aSa
S  bSb
S  aa
23
S  bb
Cont...
Removing the Unit production:
• Unit productions are the productions in which one non-
terminal gives another non-terminal only without terminal.
X  Y and Y  Z
• Then you can minimize the rule like X  Z
Example 4.9: Remove the unit productions from given
CFG
S  0A | 1B | C
A  0S | 00
B1|A
C  01
In the above production rule: S  C is unit production so in
rule3 you can replace the C value by rule8.
S  0A | 1B | 01
A  0S | 00 24
Cont...
Example 4.10: Optimize the given CFG by reducing the
grammar
S  A | 0C1
A  B | 01 | 10
C   | CD
B and D are useless symbols because B and D is not available
S  A is a unit production
C   -  production
After removal of useless symbols
S  A | 0C1
A  01 | 10
C
Again remove the unit production S  A
S  01 | 10 | 0C1
25
A  01 | 10
Cont...
Remove the production:
S  10 | 01
A  01 | 10
Again in the above grammar A is useless symbol, so
remove that also. The final CFG is
S  10 | 01

26

You might also like