The document discusses context free languages and context free grammars. It defines context free grammar as consisting of an alphabet, set of nonterminals including a start symbol S, and productions. A context free language is the set of strings generated from the start symbol using productions. Examples of context free grammars and their generated languages are provided. The relationship between regular and context free languages is explored.
2. CONTEXT FREE GRAMMAR (CFG)
Context Free Grammar (CFG) is a collection of
three things:
1. An alphabet of letters called terminals from which
we are going to make strings that will be the words
of a language.
2. A set of symbols called nonterminals, one of which
is the symbol S, standing for "start here“ known as
“Start Symbol”.
3. A finite set of productions of the form one
nonterminal -) finite string of terminals and/or
nonterminals where the strings of terminals and
nonterminals can consist of only terminals or of
only nonterminals, or any mixture of terminals and
nonterminals or even the empty string.
3. CONTEXT FREE LANGUAGE (CFL)
The language generated by a CFG is the set
of all strings of terminals that can be
produced from the start symbol S using the
productions as substitutions.
A language generated by a CFG is called a
context-free language, abbreviated CFL.
4. EXAMPLES
S aS can be written as S aS |A
S A
L = {A a aa aaa aaaa ……}
S SS | a|A
L = {A a aa aaa aaaa ……}
S SS | a
L = {a aa aaa aaaa ……}
S aS | bS |a|b|A
L={A a b aa ab ba bb …….}
7. PARSE TREE
S--> aSa | bSb | a | b | A
L={A a b aa bb aaa aba bab bbb
......}
Parse tree uses for membership
verification
word aabaa L
8. S aS | Sa | a
L = {a aa aaa aaaa aaaaa ……..}
If there are multiple parse trees generated for
same input then grammar will be ambiguous
AMBIGUOUS GRAMMAR
10. REGULAR AND CONTEXT FREE LANGUAGES
Theorem
All regular languages are also context free
languages but not vise versa.
Proof
Method 1
Regular languages are defined by regular
expression.
Regular expression can be converted into context
free grammar using following rules:
Decompose regular expression of language L into sub-
regular expressions and assign them names as
nonterminals in CFG
Convert all + with either rules option as
a+b with S a| b
11. EXAMPLE
(a + b)*bbb(a + b)*
X Y X
Context Free Grammar
S XYX
X aX | bX | A
Y bbb
14. Method 2
Regular language can be defined with FA.
FA can be converted into CFG using following
method:
Assign names to all states of FA; normally S for start
state.
Rules for CFG will be defined as:
Sourse state name (transition contents) (target state name)
For final state, one additional rule will be defined as:
Final state name A
Ignore all those states which involve in those paths
which do not converge towards final state.
REGULAR AND CONTEXT FREE LANGUAGES
16. ANOTHER EXAMPLE
= {a b c}
S aM | bS |cS
M aM | bN | cS
N aM | bS | cO
O aP | bO | cO | A
P aP | bQ | cO | A
Q aP | bO | cS |A
17. NONREGULAR AND CONTEXT FREE LANGUAGES
Some of the nonregular languages can be
defined using Context Free Grammar, so
such nonregular languages which can be
define by CFG are also CFLs.
For example
Language of PALINDROME
S aSa | bSb | a | b
S aSb | A
18. UNIT AND LEMBDA PRODUCTIONS
A production is of the form
Nonterminal one nonterminal
is called unit production
A production is of the form
Nonterminal A
is called lembda production
19. CHOMSKY NORMAL FORM
If a CFG has only productions of the form
Nonterminal string of two Nonterminals
or of the form
Nonterminal one terminal
it is said to be in Chomsky Normal Form,
CNF.
21. ANOTHER EXAMPLE
S bA | aB
A bAA | aS | a
B aBB | bS | b
Conversion of this CFG into Chomsky Normal Form
S YA | XB
A YR1 | XS | a
B XR2 | YS | b
X a
Y b
R1 AA
R2 BB
22. LEFTMOST AND RIGHTMOST DERIVATIONS
If a word w is generated by a CFG by a certain
derivation and at each step in the derivation a
rule of production is applied to the leftmost
nonterminal in the working string, then this
derivation is called a leftmost derivation.
If a word w is generated by a CFG by a certain
derivation and at each step in the derivation a
rule of production is applied to the rightmost
nonterminal in the working string, then this
derivation is called a rightmost derivation.
23. EXAMPLE
Consider the CFG:
S aSX | b
X Xb | a
Leftmost derivation: Rightmost derivation:
S aSX S aSX
aaSXX aSa
aabXX aaSXa
aabXbX aaSXba
aababX aaSaba
aababa aababa