0% found this document useful (0 votes)
12 views19 pages

Lesson_17

Uploaded by

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

Lesson_17

Uploaded by

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

Lecture # 17

 Context Free Grammar


 CFT Terminologies(Terminal, Non-
terminal, Production)
 Context Free Language
 Examples (a*, (a+b)*, (a+b)*aa(a+b)*,
EVEN-EVEN, EQUAL, (a+b)*bbb(a+b)*,
(b*ab*ab*ab*)+, PALINDROME, NON-
NULL PALINDROME, anbn language,
beginning and ending in different
letter)
Context Free Grammar

 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.
 CFG are the grammatical rules for
describing the languages (all regular and
some non-regular languages).
CFG Terminologies

 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.
CFG

 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.
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.
Context Free Language

 The language generated by CFG is called


Context Free Language (CFL).
 Example
Σ = {a}
productions:
S →aS
S→∧
Example continues…

 Applying production (1) six times and then production


(2) once, the word aaaaaa is generated as
 S ⇒ aS  It can be observed that prod
⇒ aaS (2) generates Λ, a can be
⇒ aaaS generated applying prod. (1)
⇒ aaaaS once and then prod. (2), aa
can be generated applying
⇒ aaaaaS prod. (1) twice and then
⇒ aaaaaaS prod. (2) and so on.
⇒ aaaaaaΛ  This shows that the grammar
= aaaaaa defines the language
expressed by a*.
Example
 Σ = {a}
 productions:
S→SS
S→a
S→Λ
 This grammar also defines the language expressed by a*.
 Note:
 It is to be noted that Λ is not considered to be terminal. It
has a special status. If for a certain non-terminal N, there
may be a production N→Λ.
 This simply means that N can be deleted when it comes in
the working string.
Example
 Σ = {a,b}
 productions:
S→X S→Y
X→Λ
Y→aY Y→bY Y→a Y→b
 All words of this language are of either X-type or of
Y-type. i.e. while generating a word the first
production used is S→X or S→Y. The words of X-type
give only Λ, while the words of Y-type are words of
finite strings of a’s or b’s or both i.e. (a+b)+. Thus
the language defined is expressed by (a+b)*.
Example

 Σ = {a,b}
 productions:
S→aS
S→bS
S→a
S→b
S→Λ
 This grammar also defines the language
expressed by (a+b)*.
Example

 Σ = {a,b}
 productions:
S→XaaX
X→aX
X→bX
X→Λ
 This grammar defines the language
expressed by (a+b)*aa(a+b)*.
Example
 Σ = {a,b}
 productions:
S → SS
S → XS
S→Λ
S → YSY
X → aa
X → bb
Y → ab
Y → ba
 This grammar generates EVEN-EVEN language.
Example
 Σ = {a,b}
 productions:
S → aB S → bA
A→a A → aS A → bAA
B→b B → bS B → aBB
 This grammar generates the language
EQUAL(The language of strings, with
number of a’s equal to number of b’s).
Note

 It is to be 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 the language which is 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+.
Example
 Σ = {a,b}
 productions:
S → YXY
Y → aY|bY|Λ
X → bbb
 It can be observed that, using prod.2, Y generates Λ. Y
generates a. Y generates 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)*.
Example

 CFG for the ={a,b} for the language L of the


strings in which total number of a’s is divisible by
3 no matter how they are distributed.
S → SS|XaXaXaX
X → bX|Λ
 The above CFG is for (b*ab*ab*ab*)+
Example PALINDROME

 Consider the following CFG for Σ = {a,b}


 productions:
S → aSa|bSb|a|b|Λ
 The above CFG generates the language
PALINDROME. It may be noted that the CFG
 S → aSa|bSb|a|b
 The above CFG generates the language
NON-NULLPALINDROME.
Example anbn

 Consider the following CFG for Σ = {a,b}


 productions:
S → aSb|ab|Λ
 It can be observed that the CFG generates
the language {anbn: n = 0,1,2,3, …}.
 It may also be noted that the language
{anbn: n=1,2,3, …} can be generated by
the CFG, S → aSb|ab
Example a(a+b)*b+b(a+b)*a

 Consider the following CFG


S → aXb|bXa
X → aX|bX|Λ
 The above CFG generates the language of
strings, defined over Σ={a,b}, beginning
and ending in different letters.

You might also like