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 parse trees. Example Consider S ! AS j � ; A ! A 1 j 0 A 1 j 01. The string 00111 has the follo wing t w o leftmost deriviations from S : 1. S ) AS ) 0 A 1 S ) 0 A 11 S ) 00111 S ) lm lm lm lm lm 00111 2. S ) AS ) A 1 S ) 0 A 11 S ) 00111 S ) lm lm lm lm lm 00111 � In tuitiv ely , w e can use A ! A 1 �rst or second to generate the extra 1. Inheren tly Am biguous Languages A CFL L is if CF G for inher ently ambiguous every L is am biguous. � Suc h things exist; see course reader. Example The language of our 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 ! 0 A 1 j B B ! B 1 j 01 Wh y Care? � Am biguit y of the grammar implies that at least some strings in its language ha v e di�eren 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 o structures for the same string (program) implies t w o di�eren 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
unam biguous grammars (see course reader). � An inheren tly am biguous language w ould b e absolutely unsuitable as a programming language, b ecause w e w ould not ha v e an y w a y of �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 X + 1 2 � � � X = \on input a , with Z on top of the stac k, k consume the a , mak e this state transition, and replace the Z on top of the stac k b y X X � � � X 1 2 k (with X at the top). 1 1 ; Z = Z 0 0 0 ; X=X X 1 ; X=� Start 0 ; Z =X Z 1 ; X=� 0 0 p q r �; Z = Z 0 0 � p = starting to see a group of 0's and 1's; q = reading 0's and pushing X 's on to the stac k; r = reading 1's and p opping X 's un til the X 's are all p opp ed. � W e can start a new group (transition from r to p ) only when all X 's (whic h coun t the 0's) ha v e b een matc hed against 1's. F ormal PD A P = ( Q; � ; � ; � ; q ; Z ; F ), where Q , �, q , and F 0 0 0 ha v e their meanings from F A. � � = stac k alphab et. � Z in � = start sym b ol = the one sym b ol on 0 the stac k initially . � � = transition function tak es a state, an input sym b ol (or � ), and a stac k sym b ol and giv es y ou a �nite n um b er of c hoices of: 2
1. A new state (p ossibly the same). 2. A string of stac k sym b ols to replace the top stac k sym b ol. Instan taneous Descripti ons (ID's) F or a F A, the only thing of in terest ab out the F A is its state. F or a PD A, w e w an t to kno w its state and the en tire con ten t of 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 of the PD A If � ( q ; a; X ) con tains ( p; � ), then ( q ; aw ; X � ) ` ( p; w ; �� ). * � Extend to ` to represen t 0, 1, or man y mo v es. � Subscript b y name of the PD A, if necessary . * � Input string w is accepted if ( q ; w ; Z ) ` 0 0 ( p; �; � ) for an y accepting state p and an y stac k string � . � L ( P ) = set of strings accepted b y P . Example ( p; 0110011 ; Z ) ` ( q ; 110011 ; X Z ) ` 0 0 ( r ; 10011 ; Z ) ` ( r ; 0011 ; Z ) ` ( p; 0011 ; Z ) ` 0 0 0 ( q ; 011 ; X Z ) ` ( q ; 11 ; X X Z ) ` ( r ; 1 ; X Z ) ` 0 0 0 ( r ; �; Z ) ` ( p; �; Z ) 0 0 Acceptance b y Empt y Stac k Another one of 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 o ccurs. � N ( P ) = set of strings w suc h that * ( q ; w ; Z ) ` ( p; �; � ) for some state p . 0 0 ✦ Note p need not b e in F . ✦ In fact, if w e talk ab out N ( P ) only , then w e need not ev en sp ecify a set of accepting states. Example F or our previous example, to accept b y empt y stac k: 3
1. Add a new transition � ( p; �; Z ) = f ( p; � ) g . 0 ✦ That is, when starting to lo ok for a new 0-1 blo c k, the PD A has the option to p op the last sym b ol o� the stac k instead. 2. p is no longer an accepting state; in fact, there ar e no accepting states. Equiv alence of Acceptance b y Final State and Empt y Stac k A language is L ( P ) for some PD A P if and only 1 1 if it is N ( P ) for some PD A P . 2 2 � Giv en P = ( Q; � ; � ; � ; q ; Z ; F ), construct P : 1 0 0 2 1. In tro duce new start state p and new 0 b ottom-of-stac k mark er X . 0 2. First mo v e of P : replace X b y Z X 2 0 0 0 and go to state q . The presence of X 0 0 prev en ts P from \acciden tally" empt ying 2 its stac k and accepting when P did not 1 accept. 3. Then, P sim ulates P ; i.e., giv e P all the 2 1 2 transitions of P . 1 4. In tro duce a new state r that k eeps p opping the stac k of P un til it is empt y . 2 5. If (the sim ulated) P is in an accepting 1 state, giv e P the additional c hoice of 2 going to state r on � input, and th us empt ying its stac k without reading an y more input. � Giv en P = ( Q; � ; � ; � ; q ; Z ; F ), construct P : 2 0 0 1 1. In tro duce new start state p and new 0 b ottom-of-stac k mark er X . 0 2. First mo v e of P : replace X b y Z X 1 0 0 0 and go to state q . 0 3. In tro duce new state r for P ; it is the 1 only accepting state. 4. P sim ulates P . 1 2 5. If (the sim ulated) P ev er sees X , it 1 0 kno ws P accepts, so P go es to state r 2 1 on � input. 4
Recommend
More recommend