Th us, suc h a grammar is unlik ely to b e useful for - - PDF document

th us suc h a grammar is unlik ely to b e useful for a
SMART_READER_LITE
LIVE PREVIEW

Th us, suc h a grammar is unlik ely to b e useful for - - PDF document

Am biguous Grammars A CF G is if one or more terminal ambiguous strings ha v e m ultiple leftmost deriv ations from the start sym b ol. Equiv alen tly: m ultiple righ tmost deriv ations, or m ultiple


slide-1
SLIDE 1 Am biguous Grammars A CF G is ambiguous if
  • ne
  • r
more terminal strings ha v e m ultiple leftmost deriv ations from the start sym b
  • l.
  • Equiv
alen tly: m ultiple righ tmost deriv ations,
  • r
m ultiple parse trees. Example Consider S ! AS j ; A ! A1 j 0A1 j 01. The string 00111 has the follo wing t w
  • leftmost
deriviations from S : 1. S ) lm AS ) lm 0A1S ) lm 0A11S ) lm 00111S ) lm 00111 2. S ) lm AS ) lm A1S ) lm 0A11S ) lm 00111S ) lm 00111
  • In
tuitiv ely , w e can use A ! A1 rst
  • r
second to generate the extra 1. Inheren tly Am biguous Languages A CFL L is inher ently ambiguous if every CF G for L is am biguous.
  • Suc
h things exist; see course reader. Example The language
  • f
  • ur
example grammar is not inheren tly am biguous, ev en though the grammar is am biguous.
  • Change
the grammar to force the extra 1's to b e generated last. S ! AS j
  • A
! 0A1 j B B ! B 1 j 01 Wh y Care?
  • Am
biguit y
  • f
the grammar implies that at least some strings in its language ha v e dieren t structures (parse trees).

Th us, suc h a grammar is unlik ely to b e useful for a programmi ng language, b ecause t w
  • structures
for the same string (program) implies t w
  • dieren
t meanings (executable equiv alen t programs) for this program.

Common example: the easiest grammars for arithmetic expressions are am biguous and need to b e replaced b y more complex, 1
slide-2
SLIDE 2 unam biguous grammars (see course reader).
  • An
inheren tly am biguous language w
  • uld
b e absolutely unsuitable as a programming language, b ecause w e w
  • uld
not ha v e an y w a y
  • f
xing a unique structure for all its programs. Pushdo wn Automata
  • Add
a stac k to a F A.
  • T
ypically nondeterministic.
  • An
automaton equiv alen t to CF G's. Example Notation for \transition diagrams": a; Z =X 1 X + 2
  • X
k = \on input a, with Z
  • n
top
  • f
the stac k, consume the a, mak e this state transition, and replace the Z
  • n
top
  • f
the stac k b y X 1 X 2
  • X
k (with X 1 at the top). p q r Start 0; Z =X Z 0; X=X X 1; X= 1; X= 1; Z = Z ; Z = Z
  • p
= starting to see a group
  • f
0's and 1's; q = reading 0's and pushing X 's
  • n
to the stac k; r = reading 1's and p
  • pping
X 's un til the X 's are all p
  • pp
ed.
  • W
e can start a new group (transition from r to p)
  • nly
when all X 's (whic h coun t the 0's) ha v e b een matc hed against 1's. F
  • rmal
PD A P = (Q; ; ;
  • ;
q ; Z ; F ), where Q, , q , and F ha v e their meanings from F A.
  • =
stac k alphab et.
  • Z
in
  • =
start sym b
  • l
= the
  • ne
sym b
  • l
  • n
the stac k initially .
  • =
transition function tak es a state, an input sym b
  • l
(or ), and a stac k sym b
  • l
and giv es y
  • u
a nite n um b er
  • f
c hoices
  • f:
2
slide-3
SLIDE 3 1. A new state (p
  • ssibly
the same). 2. A string
  • f
stac k sym b
  • ls
to replace the top stac k sym b
  • l.
Instan taneous Descripti
  • ns
(ID's) F
  • r
a F A, the
  • nly
thing
  • f
in terest ab
  • ut
the F A is its state. F
  • r
a PD A, w e w an t to kno w its state and the en tire con ten t
  • f
its stac k.
  • It
is also con v enien t to main tain a ction that there is an input string w aiting to b e read.
  • Represen
ted b y an ID (q ; w ; ), where q = state, w = w aiting input, and
  • =
stac k, top left. Mo v es
  • f
the PD A If
  • (q
; a; X ) con tains (p; ), then (q ; aw ; X
  • )
` (p; w ;
  • ).
  • Extend
to ` * to represen t 0, 1,
  • r
man y mo v es.
  • Subscript
b y name
  • f
the PD A, if necessary .
  • Input
string w is accepted if (q ; w ; Z ) ` * (p; ;
  • )
for an y accepting state p and an y stac k string
  • .
  • L(P
) = set
  • f
strings accepted b y P . Example (p; 0110011; Z ) ` (q ; 110011; X Z ) ` (r ; 10011; Z ) ` (r ; 0011; Z ) ` (p; 0011; Z ) ` (q ; 011; X Z ) ` (q ; 11; X X Z ) ` (r ; 1; X Z ) ` (r ; ; Z ) ` (p; ; Z ) Acceptance b y Empt y Stac k Another
  • ne
  • f
those tec hnical con v eniences: when w e pro v e that PD A's and CF G's accept the same languages, it helps to assume that the stac k is empt y whenev er acceptance
  • ccurs.
  • N
(P ) = set
  • f
strings w suc h that (q ; w ; Z ) ` * (p; ; ) for some state p.

Note p need not b e in F .

In fact, if w e talk ab
  • ut
N (P )
  • nly
, then w e need not ev en sp ecify a set
  • f
accepting states. Example F
  • r
  • ur
previous example, to accept b y empt y stac k: 3
slide-4
SLIDE 4 1. Add a new transition
  • (p;
; Z ) = f(p; )g.

That is, when starting to lo
  • k
for a new 0-1 blo c k, the PD A has the
  • ption
to p
  • p
the last sym b
  • l
  • the
stac k instead. 2. p is no longer an accepting state; in fact, there ar e no accepting states. Equiv alence
  • f
Acceptance b y Final State and Empt y Stac k A language is L(P 1 ) for some PD A P 1 if and
  • nly
if it is N (P 2 ) for some PD A P 2 .
  • Giv
en P 1 = (Q; ; ;
  • ;
q ; Z ; F ), construct P 2 : 1. In tro duce new start state p and new b
  • ttom-of-stac
k mark er X . 2. First mo v e
  • f
P 2 : replace X b y Z X and go to state q . The presence
  • f
X prev en ts P 2 from \acciden tally" empt ying its stac k and accepting when P 1 did not accept. 3. Then, P 2 sim ulates P 1 ; i.e., giv e P 2 all the transitions
  • f
P 1 . 4. In tro duce a new state r that k eeps p
  • pping
the stac k
  • f
P 2 un til it is empt y . 5. If (the sim ulated) P 1 is in an accepting state, giv e P 2 the additional c hoice
  • f
going to state r
  • n
  • input,
and th us empt ying its stac k without reading an y more input.
  • Giv
en P 2 = (Q; ; ;
  • ;
q ; Z ; F ), construct P 1 : 1. In tro duce new start state p and new b
  • ttom-of-stac
k mark er X . 2. First mo v e
  • f
P 1 : replace X b y Z X and go to state q . 3. In tro duce new state r for P 1 ; it is the
  • nly
accepting state. 4. P 1 sim ulates P 2 . 5. If (the sim ulated) P 1 ev er sees X , it kno ws P 2 accepts, so P 1 go es to state r
  • n
  • input.
4