
    gy                         d Z ddlmZmZmZmZmZmZmZm	Z	 ddl
mZ ddlmZmZmZ ddlmZmZmZmZ ddlmZ ddlmZ ddlmZ  G d	 d
      Zd Zedk(  r e        dgZy)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                   6   e Zd ZdZd3dZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd4dZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d  Z!d! Z"d" Z#d4d#Z$d$ Z%d% Z&d& Z'd' Z(d( Z)d) Z*d* Z+d+ Z,d, Z-d- Z.d. Z/d/ Z0d0 Z1d1 Z2d2 Z3y)5RecursiveDescentAppa  
    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                 r   || _         t        ||      | _        t               | _        | j                  j                  d       | j                          | j                  | j                         t        | j                        | _	        | j                  j                  d       d| _        d| _        t        | j                        | _        | j                  j                  d       | j                  | j                         | j                  | j                         | j!                  | j                         | j#                  | j                         | j%                  | j                         | j                  j'                  | j                          | j(                  j+                  d| j,                         y )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       J/var/www/openai/venv/lib/python3.12/site-packages/nltk/app/rdparser_app.py__init__zRecursiveDescentApp.__init__X   s@   
5guE D			>? 	 	# "(		!2""1%  $DII.q! 	499%499%DII&499%$))$ 	

+ 	-9    c                 |   t        t               d         | _        |j                  d| j                         t	        |      | _        | j
                  j                  | j                  j                  d             t        dd| j
                  j                               | _	        t        d| j
                  j                               | _
        | j
                  j                         d	k  r| j
                  j                         d
z
  }n| j
                  j                         d
z   }t        dd|      | _        y )Nfontr7   z*Fontsize	helveticabold)familyweightr9   )r<   r9   r      )r   r   _sysfont
option_addr   _sizer"   cgetget	_boldfont_font_bigfont)r/   rootbigs      r3   r    zRecursiveDescentApp._init_fonts   s    &(6"23/ D\


t}}))&12[djjnnFVW4::>>3CD
::>>a**.."Q&C**.."Q&CKSIr5   c           
          t        |      x| _        }| j                  j                  ddd       t        | j                  | j                  d      | _        | j
                  j                          t        | j                  ddd	d
| j                  dd      | _        | j                  j                  ddd       t        | j                  j                         j                               | _        | j                  D ]!  }| j                  j                  dd|z         # | j                  j                  t!        t#        | j                        d             t#        | j                        dkD  rvt%        | j                  d      }| j                  j                  |j&                         |j                  | j                  j(                         |j                  dd       | j                  j+                  d| j,                         y )Nbothleftr>   )fillsidepadxzAvailable Expansions)r7   textsinglegroovewhitez#909090#004040z#c0f0c0)
selectmoderelief
background
foregroundr7   selectforegroundselectbackgroundrightr   rM   rL   expandendz  %s   )heightvertical)orient)yscrollcommand)commandy)rM   rL   z<<ListboxSelect>>)r   
_prodframepackr   rD   _prodlist_labelr   rE   	_prodlistlistr   r0   productions_productionsinsertconfigminlenr	   r"   yviewr-   _prodlist_select)r/   parent	listframe
production
listscrolls        r3   r)   z!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Gr5   c                    | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j
                         | j                   j                  d	| j
                         | j                   j                  d
| j
                         | j                   j                  d| j
                         | j                   j                  d| j
                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         y )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r/   s    r3   r   z"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9r5   c                    t        |      x| _        }|j                  dddd       t        |ddd| j                  	      j                  d
       t        |ddd| j
                  	      j                  d
       t        |dddd| j                        j                  d
       t        |dddd| j                        j                  d
       t        |dddd| j                        j                  d
       y )Nnonebottom   r>   rL   rM   rN   padyStepz#90c0d0black)rO   rV   rW   rc   rK   rM   AutostepExpandr   z#90f090)rO   	underlinerV   rW   rc   Match	Backtrackz#f0a0a0)	r   _buttonframerf   r   r   r~   r\   r|   r}   )r/   rr   buttonframes      r3   r'   z!RecursiveDescentApp._init_buttons   s    */-7Kf8!!D II	
 $F$
 MM	
 $F$
 KK	
 $F$
 JJ	
 $F$
 NN	
 $F$
r5   c                     d| _         | j                  j                         \  }}}}|j                  dz
  }d||||fz  | j                  d<   | j                          y )Nr      z%d %d %d %dscrollregion)r$   _cframer   r_   r,   _redraw)r/   eventx1y1x2y2s         r3   r.   zRecursiveDescentApp._configure  sW    <<446RR\\A'4BB7G'G^$r5   c                    t        |      x| _        }|j                  dddd       t        |d| j                        | _        | j
                  j                  d       t        |d	d
      }|j                  ddd
d       t        |dd| j                        | _        t        |dddd| j                        | _        | j                  j                  d       | j                  j                  ddd
       y )Nxr   r   r   zLast Operation:)rO   r7   rK   r   sunkenr   )rU   borderrZ   r   )rL   rM   r\   rN   z#007070z#f0f0f0)rW   rV   r7   w   rS   )anchorwidthrW   rV   r7   r[   )r   _feedbackframerf   r   rE   _lastoper_label
_lastoper1
_lastoper2)r/   rr   feedbackframelastoperframes       r3   r(   z"RecursiveDescentApp._init_feedback  s    .3Fm;m(C$ 1

 
 	!!v!.mHQG'!!DiIDJJ
    
 	&)&s1=r5   c                     t        |dddd      | _        | j                  j                  dddd	       | j                  j                         x}| _        d | _        g | _        d | _        y )
NrR   
   r>   r   )rV   closeenoughr   rU   r   rJ   top)r\   rL   rM   r   )r   r   rf   canvasr,   _tree_textwidgets	_textline)r/   rr   r   s      r3   r*   z RecursiveDescentApp._init_canvas&  sk    "
 	e!D $ 3 3 55 
r5   c                    t        |      }t        |d      }|j                  dd| j                  d       |j                  dd| j                  d       |j                  dd	| j                  d
       |j                  dd|       t        |d      }|j                  dd| j                  d       |j                  dd| j                  d       |j                  dd|       t        |d      }|j                  dd	| j                  d       |j                          |j                  dd| j                  d       |j                  dd| j                  d       |j                          |j                  dd| j                  d       |j                  dd|       t        |d      }|j                  dd| j                  | j                         |j                          |j!                  d| j"                  dd| j$                          |j!                  d!| j"                  dd"| j$                          |j!                  d#| j"                  dd$| j$                          |j!                  d%| j"                  dd&| j$                          |j!                  d'| j"                  dd(| j$                          |j                  d)d|       t        |d      }|j!                  d*d| j&                  d+       |j!                  d,d| j&                  dd-.       |j!                  d/d| j&                  dd0.       |j!                  d1d| j&                  d2d3.       |j                  d4d	|       t        |d      }|j                  d5d| j(                  6       |j                  d7d| j*                  d8       |j                  d9d|       |j-                  |:       y );Nr   )tearoffzReset ParserDel)labelr   rc   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   variablerc   Tinyr   )r   r   r   valuerc   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   rc   InstructionsF1Help)r   )r   add_commandr   r   r{   add_cascader   r   r   add_separatorr|   r\   r}   add_checkbuttonr%   _toggle_grammaradd_radiobuttonrA   resizer!   aboutr   rm   )	r/   rr   menubarfilemenueditmenurulemenuviewmenuanimatemenuhelpmenus	            r3   r&   z!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#r5   c                 z    |D ]  }|j                         |   } t        |t              r|j                         }|S N)subtrees
isinstancer   r   )r/   widgettreelocis       r3   _getzRecursiveDescentApp._get  s8    A__&q)F f/0\\^Fr5   c                    | j                   }| j                  %| j                  j                  | j                         | j                  D ]  }| j                  j                  |        | j
                  %| j                   j                  | j
                         d| j                  j                          f}d| j                  j                          df}dd||d}| j                  j                         }t        ||fi || _        | j                  j                  | j                  dd       d| j                  j                          f}| j                  j                         d   x}}| j                  D 	cg c]  }	t        ||	| j                   	       c}	| _        | j                  D ]e  }| j                  j                  |d
d
       |j#                  d
||j%                         d   z
  dz
         t'        ||j%                         d         }g |j)                  d|dz
  d|dz
  d      | _        | j+                          | j-                          | j/                          y c c}	w )Nr:   r;   #000000r>   )
tree_color
tree_width	node_font	leaf_fontr   r   r   r8   r   r   ixi  .)dash)r,   r   r   destroy_widgetr   r   deleterA   rC   r   treer   
add_widgetr   r   r   rE   movebboxrn   create_line_highlight_nodes_highlight_prodlist_position_text)
r/   r   twidgethelvr;   attribsr   r   rd   words
             r3   r   zRecursiveDescentApp._redraw  s    ::!LL''

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

B2 djjnn../\\..033BF**
BL$Jvt$**5*
 ((GLL##GQ2LLFW\\^A%66:;Aw||~a()A )  ++E1q5$AC+P 	  " 	!
s   :I'c                 d    | j                          | j                          | j                          y r   )r   r   r   r   s    r3   _redraw_quickz!RecursiveDescentApp._redraw_quick  s&      "r5   c                    d| j                   j                          df}| j                  j                         d d D ]@  }d| j	                  | j
                  |      d<   || j	                  | j
                  |      d<   B | j                  j                         dd  D ]!  }d| j	                  | j
                  |      d<   # y )Nr:   r;   r   #20a050colorr7   z#008080)rA   rC   r   frontierr   r   )r/   r;   r   s      r3   r   z$RecursiveDescentApp._highlight_nodes  s    djjnn..7||,,.r2G6?DIIdjj'*7359DIIdjj'*62 3 ||,,.qr2G6?DIIdjj'*73 3r5   c                    | j                   j                  dd       | j                  j                         }| j                  j	                         }| j
                  }t        t        |            D ]  }||   |v rh||   |v r#| j                   j                  |d||   z         n"| j                   j                  |d||   z         | j                   j                  |       r| j                   j                  |d||   z          y )Nr   r]    %sz %s (TRIED))
rh   r   r   expandable_productionsuntried_expandable_productionsrk   rangero   rl   selection_set)r/   
expandableuntriedrj   indexs        r3   r   z'RecursiveDescentApp._highlight_prodlist  s     	a'\\88:
,,==?''3{+,E5!Z/u%0NN))%U9K1KLNN))%UAS1ST,,U3%%eU[5G-GH -r5   c                    t        | j                        }|t        | j                  j                               z
  }| j	                         d | }| j
                  j                         d   }t        dt        |            D ]m  }| j                  |   }||   }d|d<   d|d<   |j                  |j                         d   |j                         d   z
  d       |j                         d   dz   }o t        t        |      |      D ]R  }| j                  |   }d|d<   |j                  ||j                         d   z
  d       |j                         d   dz   }T | j                  j                         r| j                  D ]  }d|d<   	 t        dt        |            D ]  }| j                  |   }||   }|j                         d   |j                         d	   z
  d
z
  }	t        |	|j                         j                         j                         d	   |j                         d	   z
  dz         }	|j                  d|	        y )Nr   #006040r	  r>   r   z#a0a0a0z#00a000r   r         $@)ro   r   r   remaining_text_tree_leavesr   r   r  r   r   currently_completemaxrr   r   )
r/   numwordsnum_matchedleavesxmaxr   r   leafr  dys
             r3   r   z"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v{{}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 'r5   Nc                     || j                   }t        |t              r-g }|j                         D ]  }|| j	                  |      z  } |S |gS r   )r   r   r   r   r  )r/   r   r  childs       r3   r  z RecursiveDescentApp._tree_leaves9  sQ    <::Dd-.F$++E22 )M6Mr5   c                 n    d| _         | j                  y | j                  j                          d | _        y Nr   )r$   r   r{   r/   rw   s     r3   r{   zRecursiveDescentApp.destroyH  s-    99			r5   c                     d| _         | j                  j                  | j                         d| j                  d<   d| j
                  d<   | j                          y )Nr   zReset ApplicationrO    )r$   r   r+   r   r   r   r   r%  s     r3   r   zRecursiveDescentApp.resetO  sD    

+"5"$r5   c                     | j                   j                         dk(  r| j                   j                  d       | j                  rd| _        y d| _        | j	                          y )Nr   r>   r   )r!   rC   r"   r$   _stepr%  s     r3   r~   zRecursiveDescentApp.autostepV  sK    !!%%'1,""&&q)>>DNDNJJLr5   c                     d| _         y r$  )r$   r%  s     r3   r   z#RecursiveDescentApp.cancel_autostep_  s	    r5   c                 2    d| _         | j                          y r$  )r$   r)  r%  s     r3   r   zRecursiveDescentApp.stepd  s    

r5   c                 2    d| _         | j                          y r$  )r$   _matchr%  s     r3   r|   zRecursiveDescentApp.matchh  s    r5   c                 2    d| _         | j                          y r$  )r$   _expandr%  s     r3   r\   zRecursiveDescentApp.expandl  s    r5   c                 2    d| _         | j                          y r$  )r$   
_backtrackr%  s     r3   r}   zRecursiveDescentApp.backtrackp  s    r5   c                 t   | j                   ry | j                         rna| j                  j                         r| j	                         rn6| j                         rn%d| j                  d<   d| j                  d<   d| _        | j                  j                         rd| _        | j                  dxx   dz  cc<   y y )NFinishedrO   r'  r   z    [COMPLETE PARSE])
r#   r/  r   untried_matchr-  r1  r   r   r$   r  r   s    r3   r)  zRecursiveDescentApp._stept  s     <<>\\'')dkkm__&0DOOF#&(DOOF#DN <<**,DNOOF#'==# -r5   c                    | j                   ry | j                  j                         }| j                  j                         }|d| j                  d<   || j
                  d<   | j                  j                  dd       | j                  j                  |      }| j                  j                  |       | j                  |d          yd| j                  d<   d| j
                  d<   y)NExpand:rO   r   r]   Tz(all expansions tried)F)r#   r   r
  r\   r   r   rh   selection_clearrk   r  r  _animate_expand)r/   rw   old_frontierrvr  s        r3   r/  zRecursiveDescentApp._expand  s    ||,,.\\  ">&/DOOF#&(DOOF#NN**1e4%%++B/ENN((/  a1&/DOOF#&>DOOF#r5   c                 ,   | j                   ry | j                  j                         }| j                  j                         }|3d| j                  d<   || j
                  d<   | j                  |d          yd| j                  d<   d| j
                  d<   y)NzMatch:rO   r   Tz(failed)F)r#   r   r
  r|   r   r   _animate_match)r/   rw   r9  r:  s       r3   r-  zRecursiveDescentApp._match  s    ||,,.\\!>&.DOOF#&(DOOF#Q0&.DOOF#&0DOOF#r5   c                 .   | j                   ry | j                  j                         r| j                  j                         }| j                  j	                         d   D ]  }||   }	 d| j
                  d<   d| j                  d<   t        |t              r-| j                  | j                  j	                         d          y| j                  | j                  j	                         d          yd| _        d| j
                  d<   d| j                  d<   y)Nr   r   rO   r'  Tr3  F)r#   r   r}   r   r
  r   r   r   r   _animate_backtrack_animate_match_backtrackr$   )r/   rw   eltr   s       r3   r1  zRecursiveDescentApp._backtrack  s    <<!!#,,##%C\\**,Q/!f 0&1DOOF#&(DOOF##t$''(=(=(?(BC  --dll.C.C.Ea.HIDN&0DOOF#&(DOOF#r5   c                     d}d}	 ddl m}  |||      j                          y #  t        | j                  ||       Y y xY w)NzANLTK Recursive Descent Parser Application
Written by Edward Loperz+About: Recursive Descent Parser Applicationr   )Message)messager   )tkinter.messageboxrB  showr   r   )r/   rw   ABOUTTITLErB  s        r3   r   zRecursiveDescentApp.about  sC    U 	 >	.2E/446	.TYYu-s	   % A c                     d| _         	 t        | j                  dt        xs dj	                         dd       y #  t        | j                  dt        xs dj	                         d       Y y xY w)Nr   z*Help: Recursive Descent Parser Applicationr'  K   fixed)r   r7   )r   )r$   r   r   __doc__stripr%  s     r3   r   zRecursiveDescentApp.help  sb    			<B%%'			<B%%'	s	   0: 1A-c                 F    d| _         | j                  j                          y r$  )r$   r   print_to_filer%  s     r3   r   zRecursiveDescentApp.postscript  s    ""$r5   c                 R    t               ry | j                  j                  |i | y)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)r/   argskwargss      r3   rP  zRecursiveDescentApp.mainloop  s%     9		D+F+r5   c                    || j                   j                  |       | j                   j                         }| j                  j	                  t        |              | j                  j	                  t        |              | j                  j	                  t        |              | j                  j	                  t        |dz                 | j                          y )N)r9   r>   )
rA   r"   rC   rE   	configureabsrD   r?   rF   r   )r/   r9   s     r3   r   zRecursiveDescentApp.resize  s    JJNN4 zz~~

CI,/  D	l 3s4y\2s4!8}%56r5   c                    | j                   j                         r9| j                  j                  ddd| j                         d| j
                  d<   n)| j                  j                          d| j
                  d<   d| j                  d<   y )	NrJ   rK   r>   )rL   rM   rN   afterr   rO   zHide Grammarr'  )r%   rC   re   rf   r   r   pack_forgetr   r%  s     r3   r   z#RecursiveDescentApp._toggle_grammar  sv    !!#OO  &q8K8K !  '5DOOF#OO'')&4DOOF#"$r5   c                    | j                   j                         }t        |      dk7  ry t        |d         }| j                  j                         }| j                  j                  | j                  |         }|rjd| j                  d<   || j                  d<   | j                   j                  dd       | j                   j                  |       | j                  |d          y | j                   j                  dd       | j                  j                         D ]8  }| j                  j                  |      }| j                   j                  |       : y )Nr   r   r6  rO   r]   )rh   curselectionro   intr   r
  r\   rk   r   r   r7  r  r8  r  r  )r/   r   	selectionr  r9  rt   prods          r3   rq   z$RecursiveDescentApp._prodlist_select  s   NN//1	y>QIaL!||,,.\\(():):5)AB
&/DOOF#&0DOOF#NN**1e4NN((/  a1 NN**1e4;;=))//5,,U3 >r5   c           
          | j                  | j                  |      }|j                         }t        |j                         t               }| j
                  j                         }|D ]  }||   }	 t        | j                  || j                  dddd| j                        }d|j                         d<   |j                         j                         d d \  }}	|j                         j                         d d \  }
}|j                  ||
z
  |	|z
         |rY| j                  j                  |dd       |j                  d|j                         j                         d   z
  d       || _        n |j                         j!                  ||       |j#                         r||j                         j%                         dz  z   |j#                         d   j                         d   dz  z
  |j#                         d   j                         d   dz  z
  }|j#                         D ]  }|j                  |d        | j'                  |       |r| j                  j)                  |       n|j+                          t-        | j.                  j1                         dd	      D cg c]1  }d
dt3        d|z  | j.                  j1                         z        z  z  3 }}|j                         d   dz   | j                  j5                  | j6                        d   z
  }|dkD  rJ| j8                  D ]  }|j                  d|        | j                  j                  | j6                  d|       | j;                  ||       y c c}w )NrR   r>   )r   
leaf_colorr   r   
node_colorr   r  r	  r   r   r   gray%dr   r   r   )r   r   rr   r   r   r   r   r   r,   rD   rE   r   r   r   r   r   replace_childr   r   	_makeroomr   r{   r  r!   rC   r[  coordsr   r   _animate_expand_frame)r/   r   	oldwidgetoldtreer   r   r   r   oldxoldynewxnewydxsubtreer   colorsr   r  s                     r3   r8  z#RecursiveDescentApp._animate_expand)  s   IIdjj'2	""$W^^-/@AA||  "A7D  %LLnnjj	
 #,w}}++-bq1t||~**,Ra0tD4K-LL##FAq1KKV\\^002155q9DJNN**7F; ??,,.&&(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""662
s   "6M;c                 $   |j                         }t        |t              sy|j                         j	                  |      }|j                         |dz   d }|rF|j                         d   |d   j                         d   z
  dz   }|D ]  }|j                  |d        |dkD  r\|j                         |dz
     }t        d|j                         d   |j                         d   z
  dz         }|j                  |d       | j                  |       y)z@
        Make sure that no sibling tree bbox's overlap.
        Nr   r>   r   r   )	rr   r   r   r   r  r   r   r  rd  )r/   treesegrr   r  	rsiblingsrm  siblinglsiblings           r3   rd  zRecursiveDescentApp._makeroomj  s     !&"34!''0 OO%eaik2	"Yq\%6%6%8%;;b@B$R# % 19(3HQ*W\\^A->>CDBLLQ 	vr5   c                 2   t        |      dkD  rd| _        |d   |d<   |j                         D ]1  }t        |t              r|d   |j                         d<   *|d   |d<   3 | j                  j                  d| j                  ||dd         y d|d<   |j                         D ]+  }t        |t              rd|j                         d<   'd|d<   - | j                          d|j                         d<   d| _        | j                  r| j                          y y )Nr   r   r	  2   r   )ro   r#   r   r   r   r   r   rW  rf  r  r$   r)  )r/   r   ro  rn  s       r3   rf  z)RecursiveDescentApp._animate_expand_frame  s    v;?#$D $QiF7O!??,g'89/5ayGMMOG,'-ayGG$	 -
 IIOOB : :FF12JO%F7O!??,g'89/6GMMOG,'.GG$	 -
  &-FLLN7##$D ~~

 r5   c                 L   | j                   j                         dk(  rg }ng d}|t        d| j                   j                         dz         D cg c]1  }ddt        d|z  | j                   j                         z        z  z  3 c}z  }| j	                  | j
                  |      j                         g}|d   j                         D ]C  }t        |t              r |j                  |j                                3|j                  |       E | j                  ||       y c c}w )Nr   )#a00000r   rx  r   rb  r   )r!   rC   r  r[  r   r   rr   r   r   r   appendr   _animate_backtrack_frame)r/   r   ro  r   widgetsrn  s         r3   r>  z&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w}}/w'	 - 	%%gv6
s   6D!c                    t        |      dkD  rBd| _        |D ]
  }|d   |d<    | j                  j                  d| j                  ||dd         y |d   j                         D ]&  }|d   j                  |       |j                          ( | j                          d| _        | j                  r| j                          y y )Nr   r   r	  rv  )ro   r#   r   rW  rz  r   remove_childr{   r  r$   r)  )r/   r{  ro  r   s       r3   rz  z,RecursiveDescentApp._animate_backtrack_frame  s    v;?#$D !"()w "IIOOB = =wqr
S!!*--/
''/  0  #$D ~~

 r5   c                 l   | j                  | j                  |      }|j                         j                         }|j	                         d   |j	                         d   z
  dz   t        d| j                  j                               z  }| j                  | j                  j                         ||       y )Nr   r   r   )	r   r   rr   r   r   r  r!   rC   _animate_match_backtrack_frame)r/   r   r   noder   s        r3   r?  z,RecursiveDescentApp._animate_match_backtrack  s    4::w/}}$$&iik!nv{{}Q//"4t%%))+9
 
 	++D,B,B,F,F,H&RTUr5   c                 J   | j                  | j                  |      }| j                  d   j                         d   |j                         d   z
  dz
  t	        d| j
                  j                               z  }| j                  | j
                  j                         ||       y )Nr   r   r   r  )r   r   r   r   r  r!   rC   _animate_match_frame)r/   r   r   r   s       r3   r<  z"RecursiveDescentApp._animate_match  s    4::w/"'')!,v{{}Q/??$F#t%%))+K
 
 	!!$"8"8"<"<">Kr5   c                    |dkD  rFd| _         |j                  d|       | j                  j                  d| j                  |dz
  ||       y d|d<   | j                          d| _         | j                  r| j                          y y )Nr   r   r   r  r	  )r#   r   r   rW  r  r  r$   r)  r/   framer   r   s       r3   r  z(RecursiveDescentApp._animate_match_frame  ss    19#$D KK2IIOOB 9 9519fbQ'F7O #$D ~~

 r5   c                 @   |dkD  rFd| _         |j                  d|       | j                  j                  d| j                  |dz
  ||       y |j                         j                  |       |j                          d| _         | j                  r| j                          y y )Nr   r   r   )
r#   r   r   rW  r  rr   r}  r{   r$   r)  r  s       r3   r  z2RecursiveDescentApp._animate_match_backtrack_frame  s    19#$D KK2IIOOD77FB MMO((0NN#$D ~~

 r5   c                 v    t        | j                  | j                  j                         | j                         y r   )r   r   r   r0   set_grammarr%  s     r3   r   z RecursiveDescentApp.edit_grammar  s%    $))T\\113T5E5EFr5   c                    | j                   j                  |       t        |j                               | _        | j
                  j                  dd       | j                  D ]!  }| j
                  j                  dd|z         # y )Nr   r]   r  )r   r  ri   rj   rk   rh   r   rl   )r/   r0   rt   s      r3   r  zRecursiveDescentApp.set_grammar  sg      ) !4!4!67a'++JNN!!%%**<> ,r5   c                     dj                  | j                        }d}d}t        | j                  ||| j                  |       y )N r   zEnter a new sentence to parse.)joinr   r   r   set_sentence)r/   rw   sentencer   instrs        r3   r   z!RecursiveDescentApp.edit_sentence  s8    88DJJ'0DIIx0A0A5Ir5   c                 N    |j                         | _        | j                          y r   )splitr   r   )r/   r  s     r3   r  z RecursiveDescentApp.set_sentence  s    ^^%


r5   )r   r   )4__name__
__module____qualname__rK  r4   r    r)   r   r'   r.   r(   r*   r&   r   r   r  r   r   r   r  r{   r   r~   r   r   r|   r\   r}   r)  r/  r-  r1  r   r   r   rP  r   r   rq   r8  rd  rf  r>  rz  r?  r<  r  r  r   r  r   r   r5   r3   r   r   M   s   $:TJ""HH:B)`>."A$N+Z@I$ D	
>*$(
.&%	,	%,42?3B4.7(VL
G?Jr5   r   c                      ddl m}  | j                  d      }dj                         }t	        ||      j                          y)zV
    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  r0   r1   s      r3   appr    s=    
 !nn	G  +002D&//1r5   __main__r  N)rK  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  r5   r3   <module>r     sW   9v O N N  G G I I 5  m m`28 zE'r5   