0% found this document useful (0 votes)
17 views40 pages

Lecture11 12

good

Uploaded by

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

Lecture11 12

good

Uploaded by

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

Lecture No 11 & 12

Context Free Grammar (CFG)

Dr. Nazir Ahmad Zafar


Topics Covered
• Context Free Grammar
• Context Free Language
• Parsing by CFG
• Ambiguity in word generation
• Ambiguous Grammar
• Unambiguous Grammar
• Polish Notation
• Prefix and infix notation
• Total Language Tree

Dr. Nazir Ahmad Zafar


Why Context Free Grammar (CFG)?
• Like spoken languages, high level languages for
computer have also, certain grammar. But in case of
computers, the grammatical rules, don’t involve the
meaning of the words.
• It can be noted that the grammatical rules which involve
the meaning of words are called Semantics, while those
don’t involve the meaning of the words are called
Syntactic.
• In general, the rules of computer language grammar, are
all syntactic and not semantic. A law of grammar is in
reality a suggestion for possible substitutions.

Dr. Nazir Ahmad Zafar


CFG Components (Variables)
CFG Terminology
Terminals:
The symbols that can’t be replaced by anything are
called terminals.
Non-Terminals:
The symbols that must be replaced by other things are
called non-terminals.
Productions:
The grammatical rules are often called productions

Dr. Nazir Ahmad Zafar


CFG Definition
CFG is a collection of the followings
• An alphabet Σ of letters called terminals from which
the strings are formed, that will be the words of the
language.
• A set of symbols called non-terminals, one of which is
S, stands for “start here”.
• A finite set of productions of the form
non-terminal → finite string of terminals and /or non-
terminals.

Dr. Nazir Ahmad Zafar


Note

• The terminals are designated by small letters


• While the non-terminals are designated by capital letters.
• There is at least one production that has the non-terminal
S as its left side.
• S is start terminal

Dr. Nazir Ahmad Zafar


Context Free Language (CFL)
Language generated by CFG is called Context Free
Language.
Example 1: Σ = {a}; Productions
1. S → aS
2. S → Λ
What is the language generated by above CFG?
Solution: The grammar defines language expressed by a*.
For example, derivation of word aaaaaa
S → aS  aaS  aaaS  aaaaS  aaaaaS 
aaaaaaS  aaaaaaΛ  aaaaaa

Dr. Nazir Ahmad Zafar


Context Free Language (CFL)
Example 2: Σ = {a}
Productions:
1. S → SS
2. S → a
3. S → Λ
What is the language generated by above CFG?
Solution: This grammar also defines language expressed
by a*.
Note
• It is to be noted that Λ is not considered to be terminal.

Dr. Nazir Ahmad Zafar


Context Free Language (CFL)
Example 3: Σ = {a, b}, the set of productions
1. S → X
2. S → Y
3. X → Λ
4. Y → aY
5. Y → bY
6. Y → a
7. Y → b
• All words are of X or Y-type. The X-type give only Λ.
• Words of Y are finite strings of a’s or b’s or both.
• Thus the language defined is expressed by (a + b)*.
Dr. Nazir Ahmad Zafar
Context Free Language (CFL)
Example 4
If Σ = {a, b} and set of productions:
1. S → aS
2. S → bS
3. S → a
4. S → b
5. S → Λ
What is the language generated by above CFG?
Solution: This grammar also defines the same language (as
in example 3) expressed by (a + b)*

Dr. Nazir Ahmad Zafar


Context Free Language (CFL)
Example 5
If Σ = {a, b} and the set of productions:
1. S → XaaX
2. X → aX
3. X → bX
4. X → Λ
What is the language generated by above CFG?
Solution:
This grammar defines language (a + b)*aa(a + b)*.

Dr. Nazir Ahmad Zafar


Context Free Language (CFL)
Example 6: If Σ = {a, b} and the set of productions:
1. S → SS
2. S → XS
3. S → Λ
4. S → YSY
5. X → aa
6. X → bb
7. Y → ab
8. Y → ba
This grammar generates EVEN-EVEN language

Dr. Nazir Ahmad Zafar


Context Free Language (CFL)
Example 7: If Σ = {a, b} and the set of productions
1. S → aB
2. S → bA
3. A → a
4. A → aS
5. A → bAA
6. B → b
7. B → bS
8. B → aBB
This grammar generates the language non-null EQUAL

Dr. Nazir Ahmad Zafar


Some important notes
• It is noted that if the same non-terminal have more than
one productions, it can be written in single line, e.g.,
• S → aS, S → bS, S → Λ can be written as
S → aS|bS|Λ
• It may also be noted that the productions S → SS|Λ
always defines language closed w. r. t. concatenation, i.
e., the language expressed by RE of type r*
• It may also be noted that the production S → SS defines
the language expressed by r+.

Dr. Nazir Ahmad Zafar


Context Free Language (CFL)
Example 8: If Σ = {a, b} and the set of productions
1. S → YXY
2. Y → aY|bY|Λ
3. X → bbb

• Please note, using prod. 2, Y generates Λ, a and b.


• Y also generates all the combinations of a and b.
• Thus Y generates the strings generated by (a + b) *.
• It may also be observed that the above CFG generates
the language expressed by (a + b)*bbb(a + b)*

Dr. Nazir Ahmad Zafar


Contd. Example 8
Derivation of “abbbb” and “bbbbabba” by the
above grammar
S → YXY
 aYbbbΛ
 abYbbb  abΛbbb
 abbbb
S →  YXY
 bYbbbaY  bΛbbbabY
 bbbbabbY  bbbbabbaY
 bbbbabbaΛ  bbbbabba

Dr. Nazir Ahmad Zafar


Context Free Language (CFL)
Example 9
Consider the following CFG
1. S → SS|XaXaX|Λ
2. X → bX|Λ
It can be observed that, using prod.2, X generates Λ.
X generates any number of b’s.
Thus X generates the strings generated by b*.
It may also be observed that the above CFG generates
the language expressed by (b*ab*ab*)*.

Dr. Nazir Ahmad Zafar


Context Free Language (CFL)
Example 10: If Σ = {a, b}
The set of productions:
S → aSa|bSb|a|b|Λ
The above CFG generates the language PALINDROME.

Example 11: If Σ = {a, b}. It may be noted that the CFG:


S → aSa|bSb|a|b|aa|bb
generates the language NON-NULLPALINDROME

Dr. Nazir Ahmad Zafar


Context Free Language (CFL)
Example 12: If Σ = {a, b}, Productions
S → aSb|ab|Λ
It can be observed that the CFG generates the language
{anbn | n = 0, 1, 2, 3, . . .}.

Example 13: If Σ = {a, b}, Productions


S → aSb|ab
It can be observed that the CFG generates the language
{anbn | n = 1, 2, 3, . . .}.

Dr. Nazir Ahmad Zafar


Context Free Language (CFL)
Example 14:
If Σ = {a, b}
Consider the following CFG
1. S → aXb|bXa
2. X → aX|bX|Λ

The above CFG generates the language of strings,


defined over Σ = {a, b}, beginning and ending in
different letters.

Dr. Nazir Ahmad Zafar


Parsing by CFG
• As in English language any sentence can be expressed
by parse tree, so any word generated by the given
CFG can also be expressed by the parse tree, e.g.
consider the following CFG
1. S → AA
2. A → AAA|bA|Ab|a

• Obviously, baab can be generated by the above CFG.


• To express the word baab as a parse tree, start with S.
• Replace S by the string AA, of nonterminals,
• Drawing the downward lines from S to each character
of this string as follows
Dr. Nazir Ahmad Zafar
Contd. Parsing by CFG
Example 15
1. S → AA
2. A → AAA|bA|Ab|a
Syntax tree/Generation tree/Derivation tree
S
S S
A A
A A
A A A A b
b
b A A b
a a
Thus the word ‘baab’ is generated.

Dr. Nazir Ahmad Zafar


Ambiguities in Words Generation
Example 16
Consider the following CFG
S → S+S|S*S|number
number → 0|1|2|3|4|5|6|7|8|9
Where S and number are non-terminals and the operators
behave like terminals.
• The above CFG creates ambiguity as the expression
3+4*5 has two possibilities:
• (3+4)*5 = 35 and
• 3+(4*5) = 23 Please see next
Dr. Nazir Ahmad Zafar
Ambiguities in Words Generation
Example 16 Contd.
S S

(i) S S (ii) S S
+ *

S S S + S
3 * 5

4 5 3 4

3+(4*5) = 23 (3+4)*5 = 35

Dr. Nazir Ahmad Zafar


Ambiguities in Words Generation
Example 16 Contd.
Expressions are calculated starting from bottom to top,
replacing each nonterminal by the result of calculation.
S S

(i) => 3 + S => 3 + 20 =>23

4 * 5 S S
Similarly
(ii) => 7 =>35
S * 5 * 5
=>
3 +4
Addressing Ambiguities in CFG
The ambiguity that has been observed in above example
can be removed with a change in the CFG as discussed
in the following example

Dr. Nazir Ahmad Zafar


Unambiguous CFG
Example 17
S → (S+S)|(S*S)|number
number → 0|1|2|3|4|5|6|7|8|9

where S and number are nonterminals, while (, *, +, ) and


the numbers are terminals.
Here it can be observed that
S => (S+S) => (S+(S*S)) => (3+(4*5)) = 23 and
S => (S*S) => ((S+S)*S) => ((3+4)*5) = 35

Dr. Nazir Ahmad Zafar


Polish Notation (o-o-o)
Polish Notation: Operator-Operand-Operand (o-o-o)
is another notation for arithmetic expressions for CFG.

Example 18: S → S+S|S*S|number.


Consider the following derivation trees
S S

(i) S + S (ii) S * S

3 S * S S + S 5
4 5 3 4

Dr. Nazir Ahmad Zafar


Example 18: Contd. Polish Notation (o-o-o)
• Here most of the S’s are eliminated. Operators + and *
are no longer terminals as are replaced by numbers.
S S

(i) S + S => +

(i) 3 *
3 S * S
4 5 4 5
S S +3*45
(ii) S * S => *
S + S 5 (ii) + 5

3 4
* +3 4 5
4 3

Dr. Nazir Ahmad Zafar


Example 18: Contd. Polish Notation (o-o-o)
The below notations are called operator prefix notation.
(i) + 3 * 4 5 (ii) * +3 4 5
• If pattern “operator-operand-operand” is found evaluate

Evaluation Procedure:
+3*4 5 = +3 20 = 23
*+3 4 5 = * 7 5 = 35
Note
• 4*5+3 is an infix arithmetic expression
• While an arithmetic expression in (o-o-o) form is a
prefix arithmetic expression.

Dr. Nazir Ahmad Zafar


Polish Notation (o-o-o)
Example 19
Calculate arithmetic expression of the tree given below
Arithmetic Expression: *+*+1 2+3 4 5 6
The above arithmetic expression is in o-o-o :

Evaluation Procedure: S
*
*+*+1 2+3 4 5 6 = *+*3+3 4 5 6 + 6
= *+*3 7 5 6 = *+21 5 6 = *26 6 = 156. * 5
+ +
1 23 4

Dr. Nazir Ahmad Zafar


Conversion of Prefix into Infix Expression
Example 20: Convert the following prefix arithmetic
expression into infix arithmetic expression.
*+*+1 2+3 4 5 6
Conversion Procedure:
*+*+1 2+3 4 5 6
= *+*+1 2 (3+4) 5 6
= *+*(1+2) (3+4) 5 6
= *(((1+2)*(3+4)) + 5) 6
= (((1+2)*(3+4)) + 5)*6

Dr. Nazir Ahmad Zafar


Ambiguous Grammar
Ambiguous CFG: A CFG is said to be ambiguous if there
exists atleast one word of it’s language that can be
generated by different production trees.
Example 21
For CFG: S → aS|Sa|a, word aaa is generated by
following three different trees. Thus CFG is ambiguous,
S S S
a S a S S a
a S S S a
a
a a a

Dr. Nazir Ahmad Zafar


Un-ambiguous Grammar
Example 22
S → aS|a
is not ambiguous as neither the word aaa nor any other
word can be derived more than one production trees.
The derivation tree for aaa is as follows
S

a S

a S

Dr. Nazir Ahmad Zafar


Removing Ambiguity
Example 23
Consider the following CFG
S → aS | bS | aaS | Λ
It can be observed that the word aaa can be derived from
more than one production trees.
Thus, the above CFG is ambiguous.
This ambiguity is removed by removing the production:
S → aaS

Dr. Nazir Ahmad Zafar


Un-ambiguous Grammar
Example 24
Consider the CFG of the language PALINDROME
S → aSa|bSb|a|b|Λ
It may be noted that this CFG is un-ambiguous as all
the words of the language PALINDROME can only be
generated by a unique production tree.
It may be noted that if the production S → aaSaa is
added the CFG obtained will be again un-ambiguous.

Dr. Nazir Ahmad Zafar


Total Language Tree
For a given CFG, a tree with the start symbol S as its
root and whose nodes are working strings of terminals
and non-terminals.
The descendants of each node are all possible results of
applying every production to the working string.
This tree is called total language tree

Dr. Nazir Ahmad Zafar


Total Language Tree
Example 25
Total language tree for the following CFG
S → aa|bX|aXX
S
X → ab|b,
aa aXX
bX
aXb
abX abb
bab bb
aabb
aabX aXab
abab abb

aabab aabb aabab abab

Dr. Nazir Ahmad Zafar


Language Generated by Total Language Tree
Contd. Example 25:
It is observed from the tree that language generated is
{aa, bab, bb, aabab, aabb, abab, abb}
S

aa aXX
bX
aXb
abX abb
bab bb
aabb
aabX aXab
abab abb

aabab aabb aabab abab

Dr. Nazir Ahmad Zafar


Total Language Tree
Example 26
Consider the following CFG S

S → X|b, X → aX X b
Total language tree of the above CFG aX

aaX
It is to be noted that the only word in


aaa …aX
this language is b.

Dr. Nazir Ahmad Zafar

You might also like