Professional Documents
Culture Documents
=
=
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