"Context-Free Grammar" From John Martin (3 Edition)
"Context-Free Grammar" From John Martin (3 Edition)
"Context-Free Grammar" From John Martin (3 Edition)
L we can define;
L
For any S L, Sa L
For any S L, Sb L.
RULE 1
these steps indicates that rules 1,2,3 are applied to obtain the
string bba.
CONTEXT-FREE GRAMMAR:
DERIVITION:
The sequence of applications of the rules that produce the
finishing string of terminals from the starting symbol is
called a derivation.
DEFINITION OF CFG (cohen 251)
The language generated by a CFG is the set of strings of
terminals that can be produced from the start symbol S
using the productions as substitutions
CFGs derive their name from the fact that the substitution of
the variables on the left of production can be made any
time such a variable appears in a sentential form, it does
not depend on the symbol in the rest of the sentential
form (the context). This feature is consequence allowing
only a single variable on the left side of the production
FOR EXAMPLE
=> G
Means that the string can be obtained from the string by
replacing some variable that appears on the left of a
production (in G) by the corresponding right side ,or that
=> 1 A 2
=> 1 2
A =>
NOTE:
The grammar in this chapter is called CFG because the
production rule can be applied to terminals/non-terminals
whenever and wherever it occurs the context places no
limitations on the applicability of production rules.
Example 5.1:
The grammar G = ({S},{a,b},S,P) with productions
S aSa
S bSb
S
Is context-free a typical derivation in this grammar is
S=>aSa=>aaSa=>aabSbaa=>aab baa=>aabbaa.
This makes it clear that :
L(G)={WWR: W {a,b}*} (palindrome problem)
THIS LANGUAGE IS CONTEXT-FREE BUT NOT REGULAR
Example:
EXAMPLE:
the language L= {an b m : n m} is context-free.
To show this we need to produce a CFG for the language L.
Suppose n > m . We 1st generate CFG for an b m ,then will
add extra as on the left. The production rules will be :
For (n>m)
S AR
R aRb|
A aA|a
For (n<m)
S AR|RB
R aRb|
A aA|a
B bB|b
The resulting grammar is CF, therefore L is CFL. Although
there are many other equivalent CFGs
Example :
Consider the grammar with productions
S aSb|SS|
strings
John eats a hamburger
transformation rules
Two possible rules for the variables <sentence> are
1. <sentence> <noun phrase> <verb phrase>
2. <sentence> <noun phrase> <verb><direct-object
phrase>
derivative S *=>w in G.
3. A string w * is a sentence of G if there is a derivation
S*=>w in G.
4. The language of G , denoted L(G) , is the set of
{w *|S*=>w}.
Details on above definition:
A language has been defined as set of strings over an
alphabet
A grammar consists of an alphabet & method of
generating strings.
A=> aAb
A=>aA
A=>Ab
A=>AA
=>aAb
=>aA
=>Ab
A=>AA
=>aaAbb
=>aA
=>Ab
=>AA
=>aaaAbbb
=>aaaA
=>Abbb
=>AAAA
..
EXAMPLE:
S=> AA
S=>AA
S=>AA
S=>AA
aA
AAAA
Aa
aA
aAAA
aAAA
AAAa
aAAA
abAAA
abAAA
AAbAa
aAAa
abaAA
abaAA
Aabaa
ABaaA
ababAA
ababAA
AbAbaa
abAbAa
ababaA
ababaA
Ababaa
ababAa
ababaa
ababaa
ababaa
ababaa
Right
Right
LEft
Both
the rules are applied but the manner in which each variable
is transformed to terminal string.
Example:
S=>AA
=>AAAa
=>AAbAa S=>AA
=>AAAa
=>AAbAa
=>Aabaa
=>AbAaa
=>Ababaa
=>ababaa
=>Aabaa
=>AbAaa
=>Ababaa
=>ababaa
TREE
Ordering of leaves
S=> AA
S
a,A
a,A,A,A
A
A
a,b,A,A,A
A
A
b
A
A
S
A
A
b
A
a,b,a,A,A
A
a
S
a,b,a,b,a,A
A
b
A
A
a
A
A
a,b,a,b,a,a
S
A
A
b
A
A
a
A
a
A
a
S
A
ababaa
ababaa
A
a
a
A
b
A
A
a
A
a
A
A
a
A
a
ababaa
A
a
A
a
A
a
ababaa
A
a
A