You are on page 1of 11

Chapter 5

Context Free Grammars


10/13/03
Generalize from regular languages to context free languages.
Context free languages have important implications to programming languages.
Context free grammars
Keep restrictions on left side of productions:
Only a single variable allowed on left side of productions
No restrictions on right side
efinition
Grammar G = (V, T, , !" is conte#t free ($%" if and only if,
&ll productions have the form
& # (# is any sentencial form"
'here & V, # (V T"(
)anguage ) is conte#t free if, and only if,
a conte#t free grammar G such that ) = )(G"
!ee "xamples 5#1 $ 5#%.
&egular grammars are context free' but context free grammars are not
necessaril( regular#
)inear grammars are context free' but context free grammars are not
necessaril( linear#
&egular grammars are linear' but linear grammars are not necessaril(
regular#
for grammars: regular linear conte#t free
$*+* %all ,--* $hapter . /. Guydosh !age 0
)eftmost and rightmost derivations
1#ample: G = (2&, 3, $4, 2a, 54, , !"
!: &3 (0"
& aa& (,"
& (3)
3 35 (6"
3 (5)
0 , * 6 .
)eftmost &3 aa&3
aa3 aa35
aa5
0 6 , . *
7i#ed &3 &35
aa&35 aa&5
aa5
**+ Formalism ,canonical approach-
)eftmost derivation: &lways replace leftmost varia5le
(8eplace one varia5le per step"
&ightmost derivation: &lways replace rightmost varia5le
"xample. 5#5
a&3, & 535, 3 & 9
)eft most:
& ==: a&3 ==: a5353 ==:a5&53 ==:a553553 ==:a55553 ==: a5555
8ight 7ost:
==: a&3 ==:a& ==: a535 ==:a5&5 ==:a55355 ==: a5555
erivation /ree
; Ordered tree
; /odes la5eled with left side of productions
- $hildren of a node represent corresponding production right sides.
$*+* %all ,--* $hapter . /. Guydosh !age ,
- erivation trees are associated 0ith a particular 0ord in the
language defined b( the given grammar.
efinition 5#3 erivation /ree ,1rdered /ree-
Given G = (V, T, , !", a conte#t free grammar,
an ordered tree is a derivation tree for G if and only if, it has the properties:
0. 8oot la5eled
,. 1very leaf has la5el from T 24
*. 1very non;leaf verte# has a la5el from V
6. <f verte# has la5el & V and has children la5eled from left to right a
0
,
a
,
, ..., a
n
, then production & a
0
a
,
...a
n
.. & leaf la5eled has no si5lings (sole offshoot from verte#".
<f it had si5lings, say a and 5, would drop out of the derivation
anyway: a5 = a5, thus si5lings for leaf are not used.
<f a tree having properties *, 6, . 5ut not necessarily 0, and item , is modified
to 5e: =1very leaf has la5el from VT24>, then this tree is called a partial
derivation tree.
/heorem 5#1
&elationship bet0een sentential forms and derivations trees#
)et G = (V, T, , !" 5e a conte#t free grammar.
Then w )(G",
derivation tree whose yield is w.
Conversel('
The yield of any derivation tree is in )(G".
&lso: <f t
G
is any partial derivation tree for G whose root is la5eled , then
the yield of t
G
is a sentential form of G.
$*+* %all ,--* $hapter . /. Guydosh !age *
1bservation. ?erivation trees are associated with a particular word in the
language defined 5y the given grammar.
- 8epresents a set of derivations for a particular word for a given
grammar. Various paths through the tree will generate the same word,
hence there is an element of non;determinism in grammars.@
- This is distinct from having more than one derivation tree for the same
word: ambiguity ; see later.
$ 'e can remove non;determinism 5y restricting the way you can
transverse the graph, e#ample )eft 7ost ?erivations.
2o0 to read a derivation tree
1#ample ..A: %rom )inB, *
rd
ed., p. 0*,
a&3, & 535, 3 & 9 . ame as e#ample as in e#. ...,
<f the tree is =layed out> properly, the string of sym5ols o5tained 5y reading the
leafs of the tree from )eft to 8ight, omitting any Cs is said to 5e the yield of the
tree. 7ore formally:
The yield is a string of terminals in the order they are encountered when the
tree is transversed in a Depth-First manner.
$*+* %all ,--* $hapter . /. Guydosh !age 6
!artial tree
yield = a5353
a sentential form
%ull tree:
yield = a5555
a word
5#3 4arsing and 5mbiguit(
/0o uses of grammars#
; Generates a language )(G"
; Dsed to determine if a word w is or is not in )(G".
==: parsing
67rute force8 approach to parsing:w )E
ystematically construct all possi5le (say leftmost" derivation and see if any
matches w.
!rocedure:
# = start sym5ol
<f no match,
&pply all =applica5le> productions to leftmost varia5le in #.
<f no match, apply all productions to sentential forms in second pass
(weed out o5vious dead ends".
isadvantage.
; 9nefficient' tedious
; 7ay not terminate if w ).
!ee example 5#:# ! $+ !! ; a!b ; b!a ; ' parse out aabb
&ound 1. &ound 3.
! *+ !! ! *+ !! *+ !!! ! *+ a!b *+a!!b
! *+ a!a ! *+ !! *+ a!b! ! *+ a!b *+ aa!bb
! *+ b!a dead end ! *+ !! *+ b!a! ! *+ a!b *+ ab!ab dead end
! *+ dead end ! *+ !! *+ ! ! *+ a!b *+ ab dead end
&ound 3.
F ! *+ a!b *+ aa!bb *+ aabb F (used !$+ production-' other =trials> not shown
/ermination problem
& can cause strings to shrinG
& 3 does not increase resulting string
Goal: Dse productions which increase the length of sentential form each step.
Then, if after 9w9 rounds of e#haustive parsing, we have no match, then
we Gnow
w ).
/heorem 5#3
<f conte#t free grammar does not allow:
&
& 3 &, 3 V
then any w ( either gets parsed successfully or we will Gnow (after 9w9
steps" that no parsing is possi5le.
6<or=8 for e#haustive search parsing grows e#ponentially with length of string.
k
w
k
p M 9 9
9 9 ,
0

=
=
7a#imum of ,9w9 rounds>, where 9 p9 = H productions
'orst case is if a production increases string 5y first adding only varia5les
and then converting to terminators ==: ,9w9
/heorem 5#3 parsing algorithm which grows as 9w9
*
!imple grammar. efinition 5#%
G = (V, T, , !" is an s;grammar if, and only if,
&ll productions of the form
& a#
where & V, a T, # V(
and any pair (&, a" (as a5ove"
can occur at most once in !.
That if we were to rela# the =(&, a"> constraint and only retain the format
constraint (& a#, & V, a T, # V(", then the resulting grammar would
still 5e a full 5lown $%G, which later we shall call the Griebach Normal Form
I a canonical form for a $%G. ee later.
"xample 5#> ? illustrates the 6,5' a- constraint8
a 95 9c is an s;grammar.
a 95 9a 9c is not an s;grammar ... whyE
Note. if we drop the =(&,a"> constraint, we have an ar5itrary $%G (in Grei5ach
/ormal %orm I see chapter A".
<f G is an s;grammar, then w )(G" can 5e parsed in no more than 9w9 steps.
==: 1ach su5stitution adds one terminal and cannot 5e repeated.
'hy do we retain the 6,5' a- constraint8 which is not the most general $%GE
'hy not use Grie5ach /ormal %orm which is a full 5lown $%G and still adds
only one terminal sym5ol at a timeE
; 3ecause if we had a choice 5etween two productions:
& a#, and & ay, where #, y V(, and a T, then if one choice lead to a dead end
(no derivation", we would still have to go 5acG and retry the other choice. This would
then e#ceed the 9w9 5ound on the num5er of steps. ;grammar maGes choice uniJue.
5mbiguous Grammars ### 6@ac= 0as given a boo= b( 2emming0a(8
In general, the derivation tree for w )(G" is not uniJue.
efinition 5#5
& $% grammar G is am5iguous if some w )(G" which has at least two
distinct derivation trees
or
&m5iguous ==: two or more leftmost or right most derivations.
; 8esulting from more than one way to start
or
; 8esults from intermediate varia5le if there are more than one
su5stitution for a varia5le verte#.
!ee "xample 5#10 Figure 5#%' p# 1%1 ,1%A 3
nd
ed-' !$+a!b ; !! ;
Non$ambigous version. ! $+ !/ ; ' / $+ a/b ;
"xample 5#11. /he aBbc **+ aB,bc- vs ,aBb-c problem
!ee fig# 5#5 for example ,see belo0-
&esolution
; <mpose precedence rules (outside of grammar"
; 8e;write grammar to 5e non;am5iguous (adds varia5les"
!ee "xample 5#13 and fig 5#A for resolution ,belo0-
"xample 5#11.
& grammar for parsing alge5raic e#pressions:
G * ,C' /' "' 4-' C * D"' 9E' / * Da' b' c' B' ' ,' - E
" 9 ; "B" ; "F" ; ,"-
9 a;b;c
Two possi5le derivation trees for the sting aK5c
see %ig. ...
$aused 5y having too many choices in su5stituting for 1 I may lead to different
derivation trees
Ludiciously get rid of of some choices 5y introducing dummy =isolation>
varia5les:
No0.
C * D"' /' F' 9 E
" /
/ F
F 9
" "B/
/ /FF
F ,F-
9 a;b;c
ee fig 5#A for uniJue derivation.
'e omitted choices that got =K> confused with =(> .
5mbiguit( in the language ,as opposed to the grammar-
<f every grammar that generates ) ($%" is am5iguous, then ) is inherentl(
ambiguous# !ee example 5#13
) * Da
n
b
n
c
m
E Da
n
b
m
c
m
E' n' m 0 * )
1
)
3
For )
1
. !
1
$+ !
1
c 5' 5 $+ a5b ;
For )
3
. !
3
$+ a!
3
7' 7 $+ b7c ;
For ). ! $+ !
1
; !
3

grammar is ambiguous since 0 * a
n
b
n
c
n
has t0o distinct derivations#
No other non$ambiguous grammar is possible# &igorous proof that ),G-
is inherentl( ambiguous is be(ond scope of boo= ### according to )inz#
Context Free grammars and 4rogramming )anguages
7ac=us$Naur Form or 7NF used for productions.
a simple change of notation.
& a&a 9 a ==: & ::= a&a 9 a
<n The real world varia5les and terminal sym5ols are represented 5y strings of
sym5ols.
'e distinguish 5etween them as follows:
M...: around a string means a varia5le
"xample. Me#pression:
5ll other strings are terminal s(mbols#
"xample.
Me#pression: ::= Mterm: 9 Me#pression: K Mterm:
The 8N is a sentential form, =K> is a terminal sym5ol
9ncomplete example ? a subset of C language.
V = 2Mstmt:, MifOst:, MassgnOst:, Mvar:, F4
T = 2if, else, =, F4
tart varia5le is Mstmt:
Mstmt: ::= MifOst: 9 MassgnOst:
MifOst: ::= if (M5oole#p:" MstmtOlist: 9
if (M5oole#p:" MstmtOlist: else MstmtOlist:
MassgnOst: ::= Mvar: = MarithOe#p:P
...
Mvar: ::= a959c9...9#9y9B .... /OQ
hould generate recursive strings starting with an alpha sym5ol:
Mvar: ::= aMid: 9 5Mid: 9 ... 9 BMid:
Mid: ::= aMid: 9 5Mid: 9 ... 9 BMid: 9 -Mid: 9 ... 9 RMid: 9a959 ... 9B9-909 ... 9 R

You might also like