
    /hy                         S r SSKJrJrJrJrJrJrJrJ	r	  SSK
Jr  SSKJrJrJr  SSKJrJrJrJr  SSKJr  SSKJr  SSKJr   " S	 S
5      rS r\S:X  a  \" 5         S/rg)a	  
A graphical tool for exploring the recursive descent parser.

The recursive descent parser maintains a tree, which records the
structure of the portion of the text that has been parsed.  It uses
CFG productions to expand the fringe of the tree, and matches its
leaves against the text.  Initially, the tree contains the start
symbol ("S").  It is shown in the main canvas, to the right of the
list of available expansions.

The parser builds up a tree structure for the text using three
operations:

  - "expand" uses a CFG production to add children to a node on the
    fringe of the tree.
  - "match" compares a leaf in the tree to a text token.
  - "backtrack" returns the tree to its state before the most recent
    expand or match operation.

The parser maintains a list of tree locations called a "frontier" to
remember which nodes have not yet been expanded and which leaves have
not yet been matched against the text.  The leftmost frontier node is
shown in green, and the other frontier nodes are shown in blue.  The
parser always performs expand and match operations on the leftmost
element of the frontier.

You can control the parser's operation by using the "expand," "match,"
and "backtrack" buttons; or you can use the "step" button to let the
parser automatically decide which operation to apply.  The parser uses
the following rules to decide which operation to apply:

  - If the leftmost frontier element is a token, try matching it.
  - If the leftmost frontier element is a node, try expanding it with
    the first untried expansion.
  - Otherwise, backtrack.

The "expand" button applies the untried expansion whose CFG production
is listed earliest in the grammar.  To manually choose which expansion
to apply, click on a CFG production from the list of available
expansions, on the left side of the main window.

The "autostep" button will let the parser continue applying
applications to the tree until it reaches a complete parse.  You can
cancel an autostep in progress at any time by clicking on the
"autostep" button again.

Keyboard Shortcuts::
      [Space]    Perform the next expand, match, or backtrack operation
      [a]        Step through operations until the next complete parse
      [e]        Perform an expand operation
      [m]        Perform a match operation
      [b]        Perform a backtrack operation
      [Delete]   Reset the parser
      [g]        Show/hide available expansions list
      [h]        Help
      [Ctrl-p]   Print
      [q]        Quit
    )ButtonFrameIntVarLabelListboxMenu	ScrollbarTk)Font)	CFGEditorTreeSegmentWidgettree_to_treesegment)CanvasFrameEntryDialogShowText
TextWidget)SteppingRecursiveDescentParser)Tree)in_idlec                   D   \ rS rSrSrS5S jrS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS6S jrS rS rS rS rS rS rS rS rS rS rS rS r S  r!S! r"S" r#S# r$S6S$ jr%S% r&S& r'S' r(S( r)S) r*S* r+S+ r,S, r-S- r.S. r/S/ r0S0 r1S1 r2S2 r3S3 r4S4r5g)7RecursiveDescentAppM   a  
A graphical tool for exploring the recursive descent parser.  The tool
displays the parser's tree and the remaining text, and allows the
user to control the parser's operation.  In particular, the user
can expand subtrees on the frontier, match tokens on the frontier
against the text, and backtrack.  A "step" button simply steps
through the parsing process, performing the operations that
``RecursiveDescentParser`` would use.
c                 n   X l         [        X5      U l        [        5       U l        U R                  R                  S5        U R                  5         U R                  U R                  5        [        U R                  5      U l	        U R                  R                  S5        SU l        SU l        [        U R                  5      U l        U R                  R                  S5        U R                  U R                  5        U R                  U R                  5        U R!                  U R                  5        U R#                  U R                  5        U R%                  U R                  5        U R                  R'                  U R                   5        U R(                  R+                  SU R,                  5        g )Nz$Recursive Descent Parser Application   r      z<Configure>)_sentr   _parserr
   _toptitle_init_bindings_init_fontsr   _animation_framesset_animating_lock	_autostep_show_grammar_init_menubar_init_buttons_init_feedback_init_grammar_init_canvas
initialize_canvasbind
_configure)selfgrammarsenttraces       M/var/www/auris/envauris/lib/python3.13/site-packages/nltk/app/rdparser_app.py__init__RecursiveDescentApp.__init__X   s<   
5gE D			>? 	 	# "(		!2""1%  $DII.q! 	499%499%DII&499%$))$ 	

+ 	-9    c                 f   [        [        5       S   S9U l        UR                  SU R                  5        [	        U5      U l        U R
                  R                  U R                  R                  S5      5        [        SSU R
                  R                  5       S9U l	        [        SU R
                  R                  5       S9U l
        U R
                  R                  5       S	:  a  U R
                  R                  5       S
-
  nOU R
                  R                  5       S
-   n[        SSUS9U l        g )Nfontr9   z*Fontsize	helveticabold)familyweightr;   )r>   r;   r      )r   r   _sysfont
option_addr   _sizer#   cgetget	_boldfont_font_bigfont)r0   rootbigs      r4   r!   RecursiveDescentApp._init_fonts   s    &(6"23/ D\


t}}))&12[djjnnFVW4::>>3CD
::>>a**.."Q&C**.."Q&CKSIr7   c                    [        U5      =U l        nU R                  R                  SSSS9  [        U R                  U R                  SS9U l        U R
                  R                  5         [        U R                  SSS	S
U R                  SSS9U l        U R                  R                  SSSS9  [        U R                  R                  5       R                  5       5      U l        U R                   H"  nU R                  R                  SSU-  5        M$     U R                  R                  [!        [#        U R                  5      S5      S9  [#        U R                  5      S:  aj  [%        U R                  SS9nU R                  R                  UR&                  S9  UR                  U R                  R(                  S9  UR                  SSS9  U R                  R+                  SU R,                  5        g )Nbothleftr@   )fillsidepadxzAvailable Expansions)r9   textsinglegroovewhitez#909090#004040z#c0f0c0)
selectmoderelief
background
foregroundr9   selectforegroundselectbackgroundrightr   rP   rO   expandendz  %s   )heightvertical)orient)yscrollcommand)commandy)rP   rO   z<<ListboxSelect>>)r   
_prodframepackr   rF   _prodlist_labelr   rG   	_prodlistlistr   r1   productions_productionsinsertconfigminlenr	   r#   yviewr.   _prodlist_select)r0   parent	listframe
production
listscrolls        r4   r*   !RecursiveDescentApp._init_grammar   s   &+Fm3)&vA>$OO$..7M 
 	!!# OO &&	
 	va@ !5!5!7!C!C!EF++JNN!!%&:*=? ,ST->->)?%DE t  !B&"4??:FJNN!!!@dnn&:&:;OOcO2 	/1F1FGr7   c                    U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R
                  5        U R                   R                  S	U R
                  5        U R                   R                  S
U R
                  5        U R                   R                  SU R
                  5        U R                   R                  SU R
                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        g )Nz<Control-q>z<Control-x>z<Escape>emz<Alt-m>z<Control-m>bz<Alt-b>z<Control-b>z<Control-z>z<BackSpace>az<Control-space>z<Control-c>z<space>z<Delete>z<Control-p>z<Control-h>z<F1>z<Control-g>z<Control-t>)r   r.   destroyr_   match	backtrackautostepcancel_autostepstepreset
postscripthelpedit_grammaredit_sentencer0   s    r4   r    "RecursiveDescentApp._init_bindings   s   		}dll3		}dll3		z4<<0		sDKK( 			sDJJ'		y$**-		}djj1		sDNN+		y$..1		}dnn5		}dnn5		}dnn5		sDMM*		($--8		}d&:&:;		y$)),		z4::.		}doo6 			}dii0		vtyy) 			}d&7&78		}d&8&89r7   c           	         [        U5      =U l        nUR                  SSSSS9  [        USSSU R                  S	9R                  S
S9  [        USSSU R
                  S	9R                  S
S9  [        USSSSU R                  S9R                  S
S9  [        USSSSU R                  S9R                  S
S9  [        USSSSU R                  S9R                  S
S9  g )Nnonebottom   r@   rO   rP   rQ   padyStepz#90c0d0black)rR   rY   rZ   rf   rN   rP   AutostepExpandr   z#90f090)rR   	underlinerY   rZ   rf   Match	Backtrackz#f0a0a0)	r   _buttonframeri   r   r   r   r_   r   r   )r0   ru   buttonframes      r4   r(   !RecursiveDescentApp._init_buttons   s    */-7Kf8!!D II	
 $F$
 MM	
 $F$
 KK	
 $F$
 JJ	
 $F$
 NN	
 $F$
r7   c                     SU l         U R                  R                  5       u  p#pEUR                  S-
  nSX#XE4-  U R                  S'   U R                  5         g )Nr      z%d %d %d %dscrollregion)r%   _cframer   rb   r-   _redraw)r0   eventx1y1x2y2s         r4   r/   RecursiveDescentApp._configure  sO    <<446\\A'47G'G^$r7   c           	         [        U5      =U l        nUR                  SSSSS9  [        USU R                  S9U l        U R
                  R                  SS9  [        US	S
S9nUR                  SSS
SS9  [        USSU R                  S9U l        [        USSSSU R                  S9U l        U R                  R                  SS9  U R                  R                  SSS
S9  g )Nxr   r   r   zLast Operation:)rR   r9   rN   r   sunkenr   )rX   borderr]   r   )rO   rP   r_   rQ   z#007070z#f0f0f0)rZ   rY   r9   w   rV   )anchorwidthrZ   rY   r9   r^   )r   _feedbackframeri   r   rG   _lastoper_label
_lastoper1
_lastoper2)r0   ru   feedbackframelastoperframes       r4   r)   "RecursiveDescentApp._init_feedback  s    .3Fm;m(C$ 1

 
 	!!v!.mHQG'!!DiIDJJ
    
 	&)&s1=r7   c                     [        USSSSS9U l        U R                  R                  SSSSS	9  U R                  R                  5       =o l        S U l        / U l        S U l        g )
NrU   
   r@   r   )rY   closeenoughr   rX   r   rM   top)r_   rO   rP   r   )r   r   ri   canvasr-   _tree_textwidgets	_textline)r0   ru   r   s      r4   r+    RecursiveDescentApp._init_canvas&  si    "
 	e!D $ 3 3 55 
r7   c                    [        U5      n[        USS9nUR                  SSU R                  SS9  UR                  SSU R                  SS9  UR                  SS	U R                  S
S9  UR                  SSUS9  [        USS9nUR                  SSU R                  SS9  UR                  SSU R                  SS9  UR                  SSUS9  [        USS9nUR                  SS	U R                  SS9  UR                  5         UR                  SSU R                  SS9  UR                  SSU R                  SS9  UR                  5         UR                  SSU R                  SS9  UR                  SSUS9  [        USS9nUR                  SSU R                  U R                  S9  UR                  5         UR!                  SU R"                  SSU R$                  S 9  UR!                  S!U R"                  SS"U R$                  S 9  UR!                  S#U R"                  SS$U R$                  S 9  UR!                  S%U R"                  SS&U R$                  S 9  UR!                  S'U R"                  SS(U R$                  S 9  UR                  S)SUS9  [        USS9nUR!                  S*SU R&                  SS+9  UR!                  S,SU R&                  SS-S.9  UR!                  S/SU R&                  SS0S.9  UR!                  S1SU R&                  S2S3S.9  UR                  S4S	US9  [        USS9nUR                  S5SU R(                  S69  UR                  S7SU R*                  S8S9  UR                  S9SUS9  UR-                  US:9  g );Nr   )tearoffzReset ParserDel)labelr   rf   acceleratorzPrint to PostscriptzCtrl-pExitr   zCtrl-xFile)r   r   menuzEdit Grammarr   zCtrl-g	Edit TextzCtrl-tEditr   Spacer   zCtrl-mr   zCtrl-er   zCtrl-bApplyShow Grammar)r   r   variablerf   Tinyr   )r   r   r   valuerf   Small   Medium   Large   Huge   ViewzNo Animation)r   r   r   r   zSlow Animation-)r   r   r   r   r   zNormal Animation=zFast Animationr@   +AnimateAbout)r   r   rf   InstructionsF1Help)r   )r   add_commandr   r   r   add_cascader   r   r   add_separatorr   r_   r   add_checkbuttonr&   _toggle_grammaradd_radiobuttonrC   resizer"   aboutr   rp   )	r0   ru   menubarfilemenueditmenurulemenuviewmenuanimatemenuhelpmenus	            r4   r'   !RecursiveDescentApp._init_menubar7  s*   v,+ Atzzu 	 	
 	'OO 	 	 	
 	At|| 	 	
 	&AHE+ %% 	 	 	
 	&& 	 	 	
 	&AHE+Atyyg 	 	
 	 Q

 	 	
 	a( 	 	
 	 DNNPX 	 	
 	'QXF+   ''((	 	! 	
 	   ZZKK 	! 	
 	  ZZKK 	! 	
 	  ZZKK 	! 	
 	  ZZKK 	! 	
 	  ZZKK 	! 	
 	&AHE7A.## A8N8NVW 	$ 	
 	##"++ 	$ 	
 	##$++ 	$ 	
 	##"++ 	$ 	
 	)q{K+7aL Atyyd 	 	
 	&AHE7#r7   c                     U H  nUR                  5       U   nM     [        U[        5      (       a  UR                  5       nU$ N)subtrees
isinstancer   r   )r0   widgettreelocis       r4   _getRecursiveDescentApp._get  s;    A__&q)F f/00\\^Fr7   c           	         U R                   nU R                  b%  U R                  R                  U R                  5        U R                   H  nU R                  R                  U5        M      U R
                  b%  U R                   R                  U R
                  5        SU R                  R                  5       * 4nSU R                  R                  5       * S4nSSUUS.nU R                  R                  5       n[        X40 UD6U l        U R                  R                  U R                  SS5        SU R                  R                  5       * 4nU R                  R                  5       S   =pxU R                   V	s/ s H  n	[        XU R                   S	9PM     sn	U l        U R                   Hd  nU R                  R                  US
S
5        UR#                  S
XrR%                  5       S   -
  S-
  5        ['        XR%                  5       S   5      nMf     UR)                  SUS-
  SUS-
  SS9U l        U R+                  5         U R-                  5         U R/                  5         g s  sn	f )Nr<   r=   #000000r@   )
tree_color
tree_width	node_font	leaf_fontr   r   r   r:   r   r   ixi  .)dash)r-   r   r   destroy_widgetr   r   deleterC   rE   r   treer   
add_widgetr   r   r   rG   movebboxrq   create_line_highlight_nodes_highlight_prodlist_position_text)
r0   r   twidgethelvr=   attribsr  r   rg   words
             r4   r   RecursiveDescentApp._redraw  s    ::!LL''

3((GLL''0 )>>%LL/ djjnn../djjnn..7#	
 ||  "(AA


B2 djjnn../\\..033BF**
BL$Jv$**5*
 ((GLL##GQ2LLF\\^A%66:;A||~a()A )  ++E1q5$AC+P 	  " 	!
s   <I%c                 d    U R                  5         U R                  5         U R                  5         g r   )r	  r
  r  r   s    r4   _redraw_quick!RecursiveDescentApp._redraw_quick  s&      "r7   c                    SU R                   R                  5       * S4nU R                  R                  5       S S  H@  nSU R	                  U R
                  U5      S'   XR	                  U R
                  U5      S'   MB     U R                  R                  5       SS   H"  nSU R	                  U R
                  U5      S'   M$     g )Nr<   r=   r   #20a050colorr9   z#008080)rC   rE   r   frontierr   r   )r0   r=   r   s      r4   r	  $RecursiveDescentApp._highlight_nodes  s    djjnn..7||,,.r2G6?DIIdjj'*7359IIdjj'*62 3 ||,,.qr2G6?DIIdjj'*73 3r7   c                    U R                   R                  SS5        U R                  R                  5       nU R                  R	                  5       nU R
                  n[        [        U5      5       H  nX4   U;   ah  X4   U;   a"  U R                   R                  USX4   -  5        O!U R                   R                  USX4   -  5        U R                   R                  U5        Ms  U R                   R                  USX4   -  5        M     g )Nr   r`    %sz %s (TRIED))
rk   r  r   expandable_productionsuntried_expandable_productionsrn   rangerr   ro   selection_set)r0   
expandableuntriedrm   indexs        r4   r
  'RecursiveDescentApp._highlight_prodlist  s     	a'\\88:
,,==?''3{+,E!Z/%0NN))%9K1KLNN))%AS1ST,,U3%%eU[5G-GH -r7   c                    [        U R                  5      nU[        U R                  R                  5       5      -
  nU R	                  5       S U nU R
                  R                  5       S   n[        S[        U5      5       Hm  nU R                  U   nX5   nSUS'   SUS'   UR                  UR                  5       S   UR                  5       S   -
  S5        UR                  5       S   S-   nMo     [        [        U5      U5       HR  nU R                  U   nSUS'   UR                  XFR                  5       S   -
  S5        UR                  5       S   S-   nMT     U R                  R                  5       (       a  U R                   H  nSUS'   M
     [        S[        U5      5       H  nU R                  U   nX5   nUR                  5       S   UR                  5       S	   -
  S
-
  n	[        XR                  5       R                  5       R                  5       S	   UR                  5       S	   -
  S-   5      n	UR                  SU	5        M     g )Nr   #006040r  r@   r   z#a0a0a0z#00a000r   r         $@)rr   r   r   remaining_text_tree_leavesr   r  r  r   r  currently_completemaxru   r   )
r0   numwordsnum_matchedleavesxmaxr   r   leafr  dys
             r4   r  "RecursiveDescentApp._position_text  s   tzz?T\\%@%@%B!CC""$\k2zz #q#f+&A&&q)F9D'F7O%DMKK		Aq)991=;;=#b(D ' s6{H-A&&q)F'F7OKK{{}Q//3;;=#b(D	 . <<**,,,,#,  - q#f+&A&&q)F9Dq!DIIKN2T9BR,,.335a8499;q>IBNOBIIa 'r7   Nc                     Uc  U R                   n[        U[        5      (       a.  / nUR                  5        H  nX R	                  U5      -  nM     U$ U/$ r   )r   r   r   r   r'  )r0   r  r,  childs       r4   r'   RecursiveDescentApp._tree_leaves9  sR    <::Dd-..F++E22 )M6Mr7   c                 p    SU l         U R                  c  g U R                  R                  5         S U l        g Nr   )r%   r   r   r0   r{   s     r4   r   RecursiveDescentApp.destroyH  s-    99			r7   c                     SU l         U R                  R                  U R                  5        SU R                  S'   SU R
                  S'   U R                  5         g )Nr   zReset ApplicationrR    )r%   r   r,   r   r   r   r   r6  s     r4   r   RecursiveDescentApp.resetO  sD    

+"5"$r7   c                     U R                   R                  5       S:X  a  U R                   R                  S5        U R                  (       a  SU l        g SU l        U R	                  5         g )Nr   r@   r   )r"   rE   r#   r%   _stepr6  s     r4   r   RecursiveDescentApp.autostepV  sK    !!%%'1,""&&q)>>DNDNJJLr7   c                     SU l         g r5  )r%   r6  s     r4   r   #RecursiveDescentApp.cancel_autostep_  s	    r7   c                 2    SU l         U R                  5         g r5  )r%   r<  r6  s     r4   r   RecursiveDescentApp.stepd  s    

r7   c                 2    SU l         U R                  5         g r5  )r%   _matchr6  s     r4   r   RecursiveDescentApp.matchh  s    r7   c                 2    SU l         U R                  5         g r5  )r%   _expandr6  s     r4   r_   RecursiveDescentApp.expandl  s    r7   c                 2    SU l         U R                  5         g r5  )r%   
_backtrackr6  s     r4   r   RecursiveDescentApp.backtrackp  s    r7   c                    U R                   (       a  g U R                  5       (       a  OpU R                  R                  5       (       a  U R	                  5       (       a  O;U R                  5       (       a  O%SU R                  S'   SU R                  S'   SU l        U R                  R                  5       (       a  SU l        U R                  S==   S-  ss'   g g )NFinishedrR   r9  r   z    [COMPLETE PARSE])
r$   rF  r   untried_matchrC  rI  r   r   r%   r(  r   s    r4   r<  RecursiveDescentApp._stept  s     <<>>\\''))dkkmm__&0DOOF#&(DOOF#DN <<**,,DNOOF#'==# -r7   c                    U R                   (       a  g U R                  R                  5       nU R                  R                  5       nUb  SU R                  S'   X0R
                  S'   U R                  R                  SS5        U R                  R                  U5      nU R                  R                  U5        U R                  US   5        gSU R                  S'   SU R
                  S'   g)NExpand:rR   r   r`   Tz(all expansions tried)F)r$   r   r  r_   r   r   rk   selection_clearrn   r!  r  _animate_expand)r0   r{   old_frontierrvr!  s        r4   rF  RecursiveDescentApp._expand  s    ||,,.\\  ">&/DOOF#&(OOF#NN**1e4%%++B/ENN((/  a1&/DOOF#&>DOOF#r7   c                 6   U R                   (       a  g U R                  R                  5       nU R                  R                  5       nUb2  SU R                  S'   X0R
                  S'   U R                  US   5        gSU R                  S'   SU R
                  S'   g)NzMatch:rR   r   Tz(failed)F)r$   r   r  r   r   r   _animate_match)r0   r{   rS  rT  s       r4   rC  RecursiveDescentApp._match  s    ||,,.\\!>&.DOOF#&(OOF#Q0&.DOOF#&0DOOF#r7   c                 N   U R                   (       a  g U R                  R                  5       (       a  U R                  R                  5       nU R                  R	                  5       S    H  nX#   nM	     SU R
                  S'   SU R                  S'   [        U[        5      (       a-  U R                  U R                  R	                  5       S   5        gU R                  U R                  R	                  5       S   5        gSU l        SU R
                  S'   SU R                  S'   g)Nr   r   rR   r9  TrL  F)r$   r   r   r  r  r   r   r   r   _animate_backtrack_animate_match_backtrackr%   )r0   r{   eltr   s       r4   rI  RecursiveDescentApp._backtrack  s    <<!!##,,##%C\\**,Q/f 0&1DOOF#&(DOOF##t$$''(=(=(?(BC  --dll.C.C.Ea.HIDN&0DOOF#&(DOOF#r7   c                 |    SnSn SSK Jn  U" X#S9R                  5         g !   [        U R                  X25         g = f)NzANLTK Recursive Descent Parser Application
Written by Edward Loperz+About: Recursive Descent Parser Applicationr   )Message)messager   )tkinter.messageboxr_  showr   r   )r0   r{   ABOUTTITLEr_  s        r4   r   RecursiveDescentApp.about  s?    U 	 >	.2E/446	.TYY-s   ! ;c                     SU l          [        U R                  S[        =(       d    SR	                  5       SSS9  g !   [        U R                  S[        =(       d    SR	                  5       SS9   g = f)Nr   z*Help: Recursive Descent Parser Applicationr9  K   fixed)r   r9   )r   )r%   r   r   __doc__stripr6  s     r4   r   RecursiveDescentApp.help  sb    			<B%%'			<B%%'	s	   2< 3A1c                 F    SU l         U R                  R                  5         g r5  )r%   r   print_to_filer6  s     r4   r   RecursiveDescentApp.postscript  s    ""$r7   c                 \    [        5       (       a  gU R                  R                  " U0 UD6  g)z
Enter the Tkinter mainloop.  This function must be called if
this demo is created from a non-interactive program (e.g.
from a secript); otherwise, the demo will close as soon as
the script completes.
N)r   r   mainloop)r0   argskwargss      r4   rp  RecursiveDescentApp.mainloop  s%     99		D+F+r7   c                    Ub  U R                   R                  U5        U R                   R                  5       nU R                  R	                  [        U5      * S9  U R                  R	                  [        U5      * S9  U R                  R	                  [        U5      * S9  U R                  R	                  [        US-   5      * S9  U R                  5         g )N)r;   r@   )
rC   r#   rE   rG   	configureabsrF   rA   rH   r   )r0   r;   s     r4   r   RecursiveDescentApp.resize  s    JJNN4 zz~~

CI,/  D	l 3s4y\2s4!8}%56r7   c                    U R                   R                  5       (       a6  U R                  R                  SSSU R                  S9  SU R
                  S'   O)U R                  R                  5         SU R
                  S'   SU R                  S'   g )	NrM   rN   r@   )rO   rP   rQ   afterr   rR   zHide Grammarr9  )r&   rE   rh   ri   r   r   pack_forgetr   r6  s     r4   r   #RecursiveDescentApp._toggle_grammar  sy    !!##OO  &q8K8K !  '5DOOF#OO'')&4DOOF#"$r7   c                    U R                   R                  5       n[        U5      S:w  a  g [        US   5      nU R                  R                  5       nU R                  R                  U R                  U   5      nU(       ai  SU R                  S'   XPR                  S'   U R                   R                  SS5        U R                   R                  U5        U R                  US   5        g U R                   R                  SS5        U R                  R                  5        H9  nU R                  R                  U5      nU R                   R                  U5        M;     g )Nr   r   rP  rR   r`   )rk   curselectionrr   intr   r  r_   rn   r   r   rQ  r  rR  r  r!  )r0   r   	selectionr!  rS  rw   prods          r4   rt   $RecursiveDescentApp._prodlist_select  s   NN//1	y>QIaL!||,,.\\(():):5)AB
&/DOOF#&0OOF#NN**1e4NN((/  a1 NN**1e4;;=))//5,,U3 >r7   c                 $   U R                  U R                  U5      nUR                  5       n[        UR                  5       [        5      (       + nU R
                  R                  5       nU H  nXV   nM	     [        U R                  UU R                  SSSSU R                  S9nSUR                  5       S'   UR                  5       R                  5       S S u  pUR                  5       R                  5       S S u  pUR                  X-
  X-
  5        U(       aX  U R                  R                  USS5        UR                  SUR                  5       R                  5       S   -
  S5        Xpl        OUR                  5       R!                  X75        UR#                  5       (       a  UUR                  5       R%                  5       S-  -   UR#                  5       S   R                  5       S   S-  -
  UR#                  5       S   R                  5       S   S-  -
  nUR#                  5        H  nUR                  US5        M     U R'                  U5        U(       a  U R                  R)                  U5        OUR+                  5         [-        U R.                  R1                  5       SS	5       Vs/ s H2  nS
S[3        SU-  U R.                  R1                  5       -  5      -  -  PM4     nnUR                  5       S   S-   U R                  R5                  U R6                  5      S   -
  nUS:  aL  U R8                   H  nUR                  SU5        M     U R                  R                  U R6                  SU5        U R;                  X5        g s  snf )NrU   r@   )r   
leaf_colorr   r   
node_colorr   r  r  r   r   r   gray%dr   r   r   )r   r   ru   r   r   r   r  r   r-   rF   rG   r   r  r  r   r  replace_childr   r   	_makeroomr  r   r  r"   rE   r~  coordsr   r   _animate_expand_frame)r0   r   	oldwidgetoldtreer   r  r   r   oldxoldynewxnewydxsubtreer   colorsr/  r  s                     r4   rR  #RecursiveDescentApp._animate_expand)  s   IIdjj'2	""$W^^-/@AA||  "A7D  %LLnnjj	
 #,w}}++-bq1||~**,Ra0DK-LL##FAq1KKV\\^002155q9JNN**7; ??,,.&&(1,-//#A&++-a0145 //#A&++-a0145  "??,R# - 	vLL''0OO 411557B?
? Sa$*@*@*D*D*F!FGGH? 	 
 [[]1"T\\%8%8%H%KK6,,Q# -LLdnna4""62
s   /9Nc                 >   UR                  5       n[        U[        5      (       d  gUR                  5       R	                  U5      nUR                  5       US-   S nU(       aH  UR                  5       S   US   R                  5       S   -
  S-   nU H  nUR                  US5        M     US:  a\  UR                  5       US-
     n[        SUR                  5       S   UR                  5       S   -
  S-   5      nUR                  US5        U R                  U5        g)z0
Make sure that no sibling tree bbox's overlap.
Nr   r@   r   r   )	ru   r   r   r   r!  r  r  r)  r  )r0   treesegru   r!  	rsiblingsr  siblinglsiblings           r4   r  RecursiveDescentApp._makeroomj  s     !&"344!''0 OO%eaik2	"Yq\%6%6%8%;;b@B$R# % 19(3HQ*W\\^A->>CDBLLQ 	vr7   c                 \   [        U5      S:  a  SU l        US   US'   UR                  5        H8  n[        U[        5      (       a  US   UR                  5       S'   M0  US   US'   M:     U R                  R                  SU R                  XSS  5        g SUS'   UR                  5        H2  n[        U[        5      (       a  SUR                  5       S'   M-  SUS'   M4     U R                  5         SUR                  5       S'   SU l        U R                  (       a  U R                  5         g g )Nr   r   r  2   r   )rr   r$   r   r   r   r   r   ry  r  r  r%   r<  )r0   r   r  r  s       r4   r  )RecursiveDescentApp._animate_expand_frame  s   v;?#$D $QiF7O!??,g'899/5ayGMMOG,'-ayGG$	 -
 IIOOB : :F12JO%F7O!??,g'899/6GMMOG,'.GG$	 -
  &-FLLN7##$D ~~

 r7   c                 b   U R                   R                  5       S:X  a  / nO/ SQnU[        SU R                   R                  5       S-   5       Vs/ s H2  nSS[        SU-  U R                   R                  5       -  5      -  -  PM4     sn-  nU R	                  U R
                  U5      R                  5       /nUS   R                  5        HJ  n[        U[        5      (       a!  UR                  UR                  5       5        M9  UR                  U5        ML     U R                  XB5        g s  snf )Nr   )#a00000r   r  r   r  r   )r"   rE   r  r~  r   r   ru   r   r   r   appendr   _animate_backtrack_frame)r0   r   r  r   widgetsr  s         r4   rZ  &RecursiveDescentApp._animate_backtrack  s   !!%%'1,F6F1d4488:Q>?
? Sa4+A+A+E+E+G!HIIJ?
 	

 99TZZ188:;qz**,G'#455w}}/w'	 - 	%%g6
s   9D,c                    [        U5      S:  aC  SU l        U H  nUS   US'   M     U R                  R                  SU R                  XSS  5        g US   R                  5        H'  nUS   R                  U5        UR                  5         M)     U R                  5         SU l        U R                  (       a  U R                  5         g g )Nr   r   r  r  )rr   r$   r   ry  r  r   remove_childr   r  r%   r<  )r0   r  r  r   s       r4   r  ,RecursiveDescentApp._animate_backtrack_frame  s    v;?#$D !"()w "IIOOB = =wqr
S!!*--/
''/  0  #$D ~~

 r7   c                 j   U R                  U R                  U5      nUR                  5       R                  5       nUR	                  5       S   UR	                  5       S   -
  S-   [        SU R                  R                  5       5      -  nU R                  U R                  R                  5       X$5        g )Nr   r   r   )	r   r   ru   r   r  r)  r"   rE   _animate_match_backtrack_frame)r0   r   r   noder/  s        r4   r[  ,RecursiveDescentApp._animate_match_backtrack  s    4::w/}}$$&iik!nv{{}Q//"4t%%))+9
 
 	++D,B,B,F,F,H&Ur7   c                 H   U R                  U R                  U5      nU R                  S   R                  5       S   UR                  5       S   -
  S-
  [	        SU R
                  R                  5       5      -  nU R                  U R
                  R                  5       X#5        g )Nr   r   r   r%  )r   r   r   r  r)  r"   rE   _animate_match_frame)r0   r   r   r/  s       r4   rW  "RecursiveDescentApp._animate_match  s    4::w/"'')!,v{{}Q/??$F#t%%))+K
 
 	!!$"8"8"<"<">Kr7   c                    US:  aE  SU l         UR                  SU5        U R                  R                  SU R                  US-
  X#5        g SUS'   U R                  5         SU l         U R                  (       a  U R                  5         g g )Nr   r   r   r$  r  )r$   r  r   ry  r  r  r%   r<  r0   framer   r/  s       r4   r  (RecursiveDescentApp._animate_match_frame  sq    19#$D KK2IIOOB 9 9519fQ'F7O #$D ~~

 r7   c                 J   US:  aE  SU l         UR                  SU5        U R                  R                  SU R                  US-
  X#5        g UR                  5       R                  U5        UR                  5         SU l         U R                  (       a  U R                  5         g g )Nr   r   r   )
r$   r  r   ry  r  ru   r  r   r%   r<  r  s       r4   r  2RecursiveDescentApp._animate_match_backtrack_frame  s    19#$D KK2IIOOD77F MMO((0NN#$D ~~

 r7   c                 v    [        U R                  U R                  R                  5       U R                  5        g r   )r   r   r   r1   set_grammarr6  s     r4   r    RecursiveDescentApp.edit_grammar  s%    $))T\\113T5E5EFr7   c                    U R                   R                  U5        [        UR                  5       5      U l        U R
                  R                  SS5        U R                   H"  nU R
                  R                  SSU-  5        M$     g )Nr   r`   r  )r   r  rl   rm   rn   rk   r  ro   )r0   r1   rw   s      r4   r  RecursiveDescentApp.set_grammar  sg      ) !4!4!67a'++JNN!!%%**<> ,r7   c                     SR                  U R                  5      nSnSn[        U R                  X$U R                  U5        g )N r   zEnter a new sentence to parse.)joinr   r   r   set_sentence)r0   r{   sentencer   instrs        r4   r   !RecursiveDescentApp.edit_sentence  s6    88DJJ'0DIIx0A0A5Ir7   c                 N    UR                  5       U l        U R                  5         g r   )splitr   r   )r0   r  s     r4   r   RecursiveDescentApp.set_sentence  s    ^^%


r7   )r$   r"   r%   rH   rF   r   r-   r   r   rG   r   r   r   r   rh   rk   rj   rn   r   r&   rC   rA   r   r   r   r   )r   r   )6__name__
__module____qualname____firstlineno__ri  r5   r!   r*   r    r(   r/   r)   r+   r'   r   r   r  r	  r
  r  r'  r   r   r   r   r   r   r_   r   r<  rF  rC  rI  r   r   r   rp  r   r   rt   rR  r  r  rZ  r  r[  rW  r  r  r   r  r   r  __static_attributes__ r7   r4   r   r   M   s   $:TJ""HH:B)`>."A$N+Z@I$ D	
>*$(
.&%	,	%,42?3B4.7(VL
G?Jr7   r   c                      SSK Jn   U R                  S5      nSR                  5       n[	        X5      R                  5         g)zJ
Create a recursive descent parser demo, using a simple grammar and
text.
r   )CFGaD  
    # Grammatical productions.
        S -> NP VP
        NP -> Det N PP | Det N
        VP -> V NP PP | V NP | V
        PP -> P NP
    # Lexical productions.
        NP -> 'I'
        Det -> 'the' | 'a'
        N -> 'man' | 'park' | 'dog' | 'telescope'
        V -> 'ate' | 'saw'
        P -> 'in' | 'under' | 'with'
    zthe dog saw a man in the parkN)nltk.grammarr  
fromstringr  r   rp  )r  r1   r2   s      r4   appr    s;    
 !nn	G  +002D&//1r7   __main__r  N)ri  tkinterr   r   r   r   r   r   r	   r
   tkinter.fontr   	nltk.drawr   r   r   nltk.draw.utilr   r   r   r   
nltk.parser   	nltk.treer   	nltk.utilr   r   r  r  __all__r  r7   r4   <module>r     sW   9v O N N  G G I I 5  m m`28 zE'r7   