cs466(Prasad) L4CFG 1
Context-free Grammars
cs466(Prasad) L4CFG 2
• Recursive definition
• Grammar
• Derivation
,...},,{ aaaL
LaL
L
aaaSS
aSSS
cs466(Prasad) L4CFG 3
• What is the syntax of the description (meta-language)?
• External form of the grammar
• How do we associate a language with such a description?
• Semantics or use of the grammar
• Generators: Grammars specify languages by generating strings in the language.
• Recognizers: Grammars can be viewed as a notation for describing a family of recognition algorithms.
cs466(Prasad) L4CFG 4
CFG = (V, ,P,S)
• V : Finite set of variables/non-terminals
• : Alphabet/Finite set of terminals
• P : Finite set of rules/productions
• S : Start symbol
*)(V VA
A :
V
VS
Rule
cs466(Prasad) L4CFG 5
• a* represents a context-free language because we can write a CFG for it.
• A-rule, null-rule, -rule.
• Context-freeness:Context-freeness: An A-rule can be applied whenever A occurs in a string, irrespective of the context (that is, non-terminals and terminals around A).
• Cf. context-sensitive grammar (“declare-use”)
)},,{},{},({ SaSSSaS A
cs466(Prasad) L4CFG 6
• One-step Derivation (Defn: 3.1.2)
• w is derivable from v in CFG, if there is a finite sequence of rule applications such that:
From CFG to Language
A vuuAv
wwwv n ...1
cs466(Prasad) L4CFG 7
Let G=(V, , P, S) be a CFG.
• is a sentential form, if
.
• is a sentence, if .
• The language of G,
L(G) =
*)V( wwS *
G
*w wS *G
}S|*{ *G ww
}0|{)(
)},,{},,{},{{
nbaGL
SaSbSSbaSGnn
cs466(Prasad) L4CFG 8
• Derivation of aabb
• Derivation tree
aabbaaSbbaSbS
S
a bS
a b
cs466(Prasad) L4CFG 9
*)*()(
)
},|
,|,{
},,{},,,({
baLGL
S
BbB
aAAABS
baBASG
abaAbAbABbABS
abaBbaBaABABS
:DerivationRightmost
:erivationLeftmost D
cs466(Prasad) L4CFG 10
Derivation Tree (abstracts derivation)
S
A B
a A B b
cs466(Prasad) L4CFG 11
Examples: CFGs and CFLs
cs466(Prasad) L4CFG 12
bbBB
aBaaSaS
|
|
}00|{)(
}0|{)(
mnabaSL
mbBLnmn
m
cs466(Prasad) L4CFG 13
||
bBB
BaSaS
}00|{)( mnabaSL nmn
|abScS
}0|){()( ncabSL nn
Note that parenthesesare part of the meta-language.
cs466(Prasad) L4CFG 14
||
bBB
aaAA
ABS
)()(
}00|{)(*baLSL
nmbaSL mn
||
bBB
aBaSS
cs466(Prasad) L4CFG 15
bbBB
abScBS
|
|
)}01:( 0
|){()(1
i
n
i
mn
mniin
cbabSL i
cs466(Prasad) L4CFG 16
*** babaa
|aAA
AbAbAS
||
|
aCC
bCaAA
bAB
BaSS
Left to right generation of string.
cs466(Prasad) L4CFG 17
• A regular grammar regular grammar is a CFG in which each of the rules has the form:
(Right Linear Grammar)
Regular Grammar
. and , where
aVBA
A
aBA
aA
cs466(Prasad) L4CFG 18
• A regular grammar regular grammar is a CFG in which each of the rules has the form:
(Left Linear Grammar)
Equivalent Regular Grammar
. and , where
aVBA
A
BaA
aA
cs466(Prasad) L4CFG 19
} EVENis )(length |}*,{{ wbawL
bbEbaE
abEaaE
E
| |
| |
bEaEO
bOaOE
|
| |
}s' ofnumber EVEN has |}*,{{ bwbawL
bEaOO
bOaEE
|
| |
cs466(Prasad) L4CFG 20
| |
| |
|
| |
|
resetb
seenAaseenAB
seenABb
resetcseenAaseenA
seenAa
resetcresetbreset
} containnot does |}*,,{{ abcwcbawL
cs466(Prasad) L4CFG 21
Derivations
G. in derivationa has
)(
iffGL
G. in derivationleftmost a has
)(
iffGL
Note that the above result does not hold for sentential forms.
ASAABS
ABS
l
B A