in sensible gramma rs these strings share some common c
play

In sensible gramma rs, these strings share some common c - PDF document

Con text-F ree Grammars Notation for recursiv e description of languages. Example: Rol l ! < RO LL > C l ass S tuds < =RO LL > C l ass ! < C LAS S > T ext < =C LAS S > T ext ! C


  1. Con text-F ree Grammars Notation for recursiv e description of languages. Example: Rol l ! < RO LL > C l ass S tuds < =RO LL > C l ass ! < C LAS S > T ext < =C LAS S > T ext ! C har T ext T ext ! C har C har ! a � � � (other c hars) S tuds ! S tud S tuds S tuds ! � S tud ! < S T U D > T ext < =S T U D > � Generates \do cumen ts" suc h as: <ROLL><CLASS>cs 154</ CLASS > <STUD>Sally</ST UD> <STUD>Fred</STU D> � � � </ROLL> � V ariables (e.g., S tuds ) represen t sets of strings (i.e., languages). ✦ In sensible gramma rs, these strings share some common c haracteristic or roll. � T erminals (e.g., a or < RO LL > ) = sym b ols of whic h strings are comp osed. ✦ \T ags" lik e < RO LL > could b e considered either a single terminal or the concatenation of 6 terminals. � = rules of the form H ead ! Pr o ductions B ody . ✦ H ead is a v ariable. ✦ B ody is a string of zero or more v ariables and/or terminals. � = v ariable that represen ts \the Start Symb ol language." � Notation: G = ( V ; � ; P ; S ) = (v ariables, terminals, pro ductions, start sym b ol). Example A simpler example generates strings of 0's and 1's suc h that eac h blo c k of 0's is follo w ed b y at least as man y 1's. S ! AS j � A ! 0 A 1 j A 1 j 01 � Note v ertical bar separates di�eren t b o dies for the same head. 1

  2. Deriv ations � �A� ) �� � whenev er there is a pro duction A ! � . ✦ Subscript with name of grammar, e.g., ) , if necessary . G ✦ Example: 011 AS ) 0110 A 1 S . * � � ) � means string � can b ecome � in zero or more deriv ation steps. * ✦ Examples: 011 AS ) 011 AS (zero steps); * * 011 AS ) 0110 A 1 S (one step); 011 AS ) 0110011 (three steps). Language of a CF G L ( G ) = set of terminal strings w suc h that * S ) w , where S is the start sym b ol. G Aside: Notation � a; b; : : : are terminals; : : : ; y ; z are strings of terminals. � Greek letters are strings of v ariables and/or terminals, often called sentential forms . � A; B ; : : : are v ariables. � : : : ; Y ; Z are v ariables or terminals. � S is t ypically the start sym b ol. Leftmost/Righ t most Deriv ations � W e ha v e a c hoice of v ariable to replace at eac h step. ✦ Deriv ations ma y app ear di�eren t only b ecause w e mak e the replacemen ts same in a di�eren t order. ✦ T o a v oid suc h di�erences, w e ma y restrict the c hoice. � A deriv ation alw a ys replaces the leftmost leftmost v ariable in a sen ten tial form. ✦ Yields left-sentential forms . � R ightmost de�ned analogously . � ) , ) , etc., used to indicate deriv ations are lm rm leftmost or righ tmost. 2

  3. Example � S ) AS ) A 1 S ) 011 S ) 011 AS ) lm lm lm lm lm 0110 A 1 S ) 0110011 S ) 0110011 lm lm � S ) AS ) AAS ) AA ) A 0 A 1 ) rm rm rm rm rm A 0011 ) A 10011 ) 0110011 rm rm Deriv ation T rees � No des = v ariables, terminals, or � . ✦ V ariables at in terior no des; terminals and � at lea v es. ✦ A leaf can b e � only if it is the only c hild of its paren t. � A no de and its c hildren from the left m ust form the head and b o dy of a pro duction. Example S A S A 1 A S 0 1 0 A 1 � 0 1 Equiv alence of P arse T rees, Leftmost, and Righ tmost Deriv ations The follo wing ab out a grammar G = ( V ; � ; P ; S ) and a terminal string w are all equiv alen t: * 1. S ) w (i.e., w is in L ( G )). * 2. S ) w lm * 3. S ) w rm 4. There is a parse tree for G with ro ot S and (lab els of lea v es, from the left) w . yield � Ob viously (2) and (3) eac h imply (1). 3

  4. P arse T ree Implies LM/RM Deriv ations � Generalize all statemen ts to talk ab out an arbitrary v ariable A in place of S . ✦ Except no w (1) no longer means w is in L ( G ). � Induction on the heigh t of the parse tree. Basis : Heigh t 1: T ree is ro ot A and lea v es w = a ; a ; : : : ; a . 1 2 k � A ! w m ust b e a pro duction, so A ) w and lm A ) w . rm Induction : Heigh t > 1. T ree is ro ot A with c hildren X ; X ; : : : ; X . 1 2 k � Those X 's that are v ariables are ro ots of i shorter trees. ✦ Th us, the IH sa ys that they ha v e LM deriv ations of their yields. � Construct a LM deriv ation of w from A b y starting with A ) X X � � � X , then 1 2 k lm using LM deriv ations from eac h X that is a i v ariable, in order from the left. � RM deriv ation analogous. Deriv ations to P arse T rees Induction on length of the deriv ation. : One step. There is an ob vious parse tree. Basis : More than one step. Induction � Let the �rst step b e A ) X X � � � X . k 1 2 � Subsequen t c hanges can b e reordered so that all c hanges to X and the sen ten tial forms 1 that replace it are done �rst, then those for X , and so on (i.e., w e can rewrite the 2 deriviation as a LM deriv ation). � The deriv ations from those X 's that are i v ariables are all shorter than the giv en deriviation, so the IH applies. � By the IH, there are parse trees for eac h of these deriv ations. � Mak e the ro ots of these trees b e c hildren of a new ro ot lab eled A . Example Consider deriv ation S ) AS ) AAS ) AA ) A 1 A ) A 10 A 1 ) 0110 A 1 ) 0110011 4

  5. � Sub deriv ation from A is: A ) A 1 ) 011 � Sub deriv ation from S is: S ) AS ) A ) 0 A 1 ) 0011 � Eac h has a parse tree; put them together with new ro ot S . 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend