
    Ngy                         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dS )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                   8   e 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d5d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#d5d$Z$d% Z%d& Z&d' Z'd( Z(d) Z)d* Z*d+ Z+d, Z,d- Z-d. Z.d/ Z/d0 Z0d1 Z1d2 Z2d3 Z3dS )6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.
    r   c                 ^   || _         t          ||          | _        t                      | _        | j                            d           |                                  |                     | j                   t          | j                  | _	        | j	        
                    d           d| _        d| _        t          | j                  | _        | j        
                    d           |                     | j                   |                     | j                   |                     | j                   |                     | j                   |                     | j                   | j                            | j                    | j                            d| j                   d S )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       Q/var/www/html/ai-engine/env/lib/python3.11/site-packages/nltk/app/rdparser_app.py__init__zRecursiveDescentApp.__init__X   sy   
5guEE DD		>??? 	 	### "(	!2!2""1%%%  $DI..q!!! 	49%%%49%%%DI&&&49%%%$)$$$ 	
+++ 	-99999    c                    t          t                      d                   | _        |                    d| j                   t	          |          | _        | j                            | j                            d                     t          dd| j                                                  | _	        t          d| j                                                  | _
        | j                                        d	k     r| j                                        d
z
  }n| j                                        d
z   }t          dd|          | _        d S )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33/// D\\

t}))&11222[djnnFVFVWWW4:>>3C3CDDD
:>>a*..""Q&CC*..""Q&CKSIIIr5   c           
         t          |          x| _        }| j                            ddd           t          | j        | j        d          | _        | j                                         t          | j        ddd	d
| j        dd          | _        | j                            ddd           t          | j
                                                                                  | _        | j        D ] }| j                            dd|z             !| j                            t!          t#          | j                  d                     t#          | j                  dk    rmt%          | j        d          }| j                            |j                   |                    | j        j                   |                    dd           | j                            d| j                   d S )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   &+Fmm3)&vA>>>$O$.7M 
  
  
 	!!### O &&	
 	
 	
 	va@@@ !5!5!7!7!C!C!E!EFF+ 	@ 	@JN!!%&:*=????ST->)?)?%D%DEEE t !!B&&"4?:FFFJN!!!@@@dn&:;;;OOcO222 	/1FGGGGGr5   c                    | j                             d| j                   | j                             d| j                   | j                             d| j                   | j                             d| j                   | j                             d| j                   | j                             d| j                   | j                             d| j                   | j                             d| j                   | j                             d	| j                   | j                             d
| j                   | j                             d| j                   | j                             d| j                   | j                             d| j                   | j                             d| j                   | j                             d| j                   | j                             d| j                   | j                             d| j	                   | j                             d| j
                   | j                             d| j                   | j                             d| j                   | j                             d| j                   | j                             d| j                   d S )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"   	}dl333	}dl333	z4<000	sDK((( 		sDJ'''	y$*---	}dj111	sDN+++	y$.111	}dn555	}dn555	}dn555	sDM***	($-888	}d&:;;;	y$),,,	z4:...	}do666 		}di000	vty))) 		}d&7888	}d&899999r5   c                 ,   t          |          x| _        }|                    dddd           t          |ddd| j        	                              d
           t          |ddd| j        	                              d
           t          |dddd| j                                      d
           t          |dddd| j                                      d
           t          |dddd| j                                      d
           d S )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   sT   */--7Kf8!!DDD I	
 	
 	
 $F$


 M	
 	
 	
 $F$


 K	
 	
 	
 $F$


 J	
 	
 	
 $F$


 N	
 	
 	
 $F$




r5   c                     d| _         | j                                        \  }}}}|j        dz
  }d||||fz  | j        d<   |                                  d S )Nr      z%d %d %d %dscrollregion)r$   _cframer   r_   r,   _redraw)r/   eventx1y1x2y2s         r3   r.   zRecursiveDescentApp._configure  s\    <4466RR\A'4BB7G'G^$r5   c                    t          |          x| _        }|                    dddd           t          |d| j                  | _        | j                            d           t          |d	d
          }|                    ddd
d           t          |dd| j                  | _        t          |dddd| j                  | _        | j                            d           | j                            ddd
           d S )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+   .3Fmm;m(CCC$ 1
 
  
  
 	!!v!...mHQGGG'!!DDDiIDJ
 
 
    
 
 
 	&)))&s1=====r5   c                     t          |dddd          | _        | j                            dddd	           | j                                        x}| _        d | _        g | _        d | _        d S )
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&  s~    "
 
 
 	e!DDD $ 3 3 5 55 
r5   c                    t          |          }t          |d          }|                    dd| j        d           |                    dd| j        d           |                    dd	| j        d
           |                    dd|           t          |d          }|                    dd| j        d           |                    dd| j        d           |                    dd|           t          |d          }|                    dd	| j        d           |	                                 |                    dd| j
        d           |                    dd| j        d           |	                                 |                    dd| j        d           |                    dd|           t          |d          }|                    dd| j        | j                   |	                                 |                    d| j        dd| j                    |                    d!| j        dd"| j                    |                    d#| j        dd$| j                    |                    d%| j        dd&| j                    |                    d'| j        dd(| j                    |                    d)d|           t          |d          }|                    d*d| j        d+           |                    d,d| j        dd-.           |                    d/d| j        dd0.           |                    d1d| j        d2d3.           |                    d4d	|           t          |d          }|                    d5d| j        6           |                    d7d| j        d8           |                    d9d|           |                    |:           d S );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,,+++ Atzu 	 	
 	
 	
 	'O 	 	 	
 	
 	
 	At| 	 	
 	
 	
 	&AHEEE+++ % 	 	 	
 	
 	
 	& 	 	 	
 	
 	
 	&AHEEE+++Atyg 	 	
 	
 	
 	   Q
 	 	
 	
 	
 	a( 	 	
 	
 	
 	   DNPX 	 	
 	
 	
 	'QXFFF+++   '(	 	! 	
 	
 	
 	     ZK 	! 	
 	
 	
 	  ZK 	! 	
 	
 	
 	  ZK 	! 	
 	
 	
 	  ZK 	! 	
 	
 	
 	  ZK 	! 	
 	
 	
 	&AHEEE7A...## A8NVW 	$ 	
 	
 	
 	##"+ 	$ 	
 	
 	
 	##$+ 	$ 	
 	
 	
 	##"+ 	$ 	
 	
 	
 	)q{KKK+++7aLLL Atyd 	 	
 	
 	
 	&AHEEE7#####r5   c                     |D ]}|                                 |         }t          |t                    r|                                }|S N)subtrees
isinstancer   r   )r/   widgettreelocis       r3   _getzRecursiveDescentApp._get  sL     	* 	*A__&&q)FFf/00 	$\\^^Fr5   c                      j          j         j                             j                    j        D ]} j                            |            j         j                              j                   d j                                         f}d j                                         df}dd||d} j	        
                                }t          |fi | _         j                             j        dd           d j                                         f} j                                        d         x}} fd	 j        D              _         j        D ]z} j                            |d
d
           |                    d
||                                d         z
  dz
             t#          ||                                d                   }{                    d|dz
  d|dz
  d           _                                                                                                              d S )Nr:   r;   #000000r>   )
tree_color
tree_width	node_font	leaf_fontr   r   r   c                 >    g | ]}t          |j                   S )r8   )r   rE   ).0wordr   r/   s     r3   
<listcomp>z/RecursiveDescentApp._redraw.<locals>.<listcomp>  s7     
 
 
:>Jvt$*555
 
 
r5   r   r   ixi  .)dash)r,   r   r   destroy_widgetr   r   deleterA   rC   r   treer   
add_widgetr   r   movebboxrn   create_line_highlight_nodes_highlight_prodlist_position_text)	r/   twidgethelvr;   attribsr   r   rd   r   s	   `       @r3   r   zRecursiveDescentApp._redraw  s[    :!L''
333( 	1 	1GL''0000>%L/// djnn.../djnn...7#	
 
 |  ""(AAAA

B222 djnn.../\..0033
 
 
 
 
BF*
 
 
 ( 	* 	*GL##GQ222LLFW\\^^A%66:;;;Aw||~~a())AA  ++E1q5$AC+PP 	  """ 	r5   c                 ~    |                                   |                                  |                                  d S r   )r  r  r  r   s    r3   _redraw_quickz!RecursiveDescentApp._redraw_quick  s>      """r5   c                    d| j                                          df}| j                                        d d         D ]>}d|                     | j        |          d<   ||                     | j        |          d<   ?| j                                        dd          D ] }d|                     | j        |          d<   !d S )Nr:   r;   r   #20a050colorr7   z#008080)rA   rC   r   frontierr   r   )r/   r;   r   s      r3   r  z$RecursiveDescentApp._highlight_nodes  s    djnn...7|,,..rr2 	: 	:G6?DIIdj'**7359DIIdj'**622|,,..qrr2 	@ 	@G6?DIIdj'**733	@ 	@r5   c                 $   | j                             dd           | j                                        }| j                                        }| j        }t          t          |                    D ]}||         |v rn||         |v r%| j                             |d||         z             n$| j                             |d||         z             | j         	                    |           z| j                             |d||         z             d S )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{++,, 	I 	IE5!Z//u%00N))%U9K1KLLLLN))%UAS1STTT,,U3333%%eU[5G-GHHHH	I 	Ir5   c                 j   t          | j                  }|t          | j                                                  z
  }|                                 d |         }| j                                        d         }t          dt          |                    D ]}| j        |         }||         }d|d<   d|d<   |	                    |                                d         |                                d         z
  d           |                                d         dz   }t          t          |          |          D ]b}| j        |         }d|d<   |	                    ||                                d         z
  d           |                                d         dz   }c| j        
                                r| j        D ]}d|d<   t          dt          |                    D ]}| j        |         }||         }|                                d         |                                d	         z
  d
z
  }	t          |	|                                                                                                d	         |                                d	         z
  dz             }	|	                    d|	           d S )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  sh   tz??T\%@%@%B%B!C!CC""$$\k\2z  #q#f++&& 	) 	)A&q)F!9D'F7O%DMKK		Aq)991===;;==#b(DD s6{{H-- 	) 	)A&q)F'F7OKKv{{}}Q//333;;==#b(DD <**,, 	-, - -#,   q#f++&& 	 	A&q)F!9Dq!DIIKKN2T9BR,,..3355a8499;;q>IBNOOBIIa	 	r5   Nc                     || j         }t          |t                    r3g }|                                D ]}||                     |          z  }|S |gS r   )r   r   r   r   r  )r/   r   r   childs       r3   r  z RecursiveDescentApp._tree_leaves9  sc    <:Dd-.. 	F 3 3$++E222M6Mr5   c                 f    d| _         | j        d S | j                                         d | _        d S Nr   )r$   r   r{   r/   rw   s     r3   r{   zRecursiveDescentApp.destroyH  s5    9F				r5   c                     d| _         | j                            | j                   d| j        d<   d| j        d<   |                                  d S )Nr   zReset ApplicationrO    )r$   r   r+   r   r   r   r   r(  s     r3   r   zRecursiveDescentApp.resetO  sJ    
+++"5"$r5   c                     | j                                         dk    r| j                             d           | j        r	d| _        d S d| _        |                                  d S )Nr   r>   r   )r!   rC   r"   r$   _stepr(  s     r3   r~   zRecursiveDescentApp.autostepV  s`    !%%''1,,"&&q)))> 	DNNNDNJJLLLLLr5   c                     d| _         d S r'  )r$   r(  s     r3   r   z#RecursiveDescentApp.cancel_autostep_  s    r5   c                 <    d| _         |                                  d S r'  )r$   r,  r(  s     r3   r   zRecursiveDescentApp.stepd  s    

r5   c                 <    d| _         |                                  d S r'  )r$   _matchr(  s     r3   r|   zRecursiveDescentApp.matchh  s    r5   c                 <    d| _         |                                  d S r'  )r$   _expandr(  s     r3   r\   zRecursiveDescentApp.expandl  s    r5   c                 <    d| _         |                                  d S r'  )r$   
_backtrackr(  s     r3   r}   zRecursiveDescentApp.backtrackp  s    r5   c                 l   | j         rd S |                                 rn^| j                                        r|                                 rn0|                                 rnd| j        d<   d| j        d<   d| _        | j        	                                rd| _        | j        dxx         dz  cc<   d S d S )NFinishedrO   r*  r   z    [COMPLETE PARSE])
r#   r2  r   untried_matchr0  r4  r   r   r$   r  r   s    r3   r,  zRecursiveDescentApp._stept  s     	F <<>> 		\'')) 	dkkmm 	__ 	&0DOF#&(DOF#DN <**,, 	>DNOF###'==#####	> 	>r5   c                    | j         rd S | j                                        }| j                                        }|d| j        d<   || j        d<   | j                            dd           | j        	                    |          }| j        
                    |           |                     |d                    dS d| j        d<   d| j        d<   dS )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   r2  zRecursiveDescentApp._expand  s     	F|,,..\  "">&/DOF#&(DOF#N**1e444%++B//EN((///  a1114&/DOF#&>DOF#5r5   c                 
   | j         rd S | j                                        }| j                                        }|1d| j        d<   || j        d<   |                     |d                    dS d| j        d<   d| j        d<   dS )NzMatch:rO   r   Tz(failed)F)r#   r   r  r|   r   r   _animate_match)r/   rw   r<  r=  s       r3   r0  zRecursiveDescentApp._match  s     	F|,,..\!!>&.DOF#&(DOF#Q0004&.DOF#&0DOF#5r5   c                 &   | j         rd S | j                                        r| j                                        }| j                                        d         D ]
}||         }d| j        d<   d| j        d<   t          |t                    r3| 	                    | j                                        d                    n2| 
                    | j                                        d                    dS d| _        d| j        d<   d| j        d<   dS )Nr   r   rO   r*  Tr6  F)r#   r   r}   r   r  r   r   r   r   _animate_backtrack_animate_match_backtrackr$   )r/   rw   eltr   s       r3   r4  zRecursiveDescentApp._backtrack  s    	F<!!## 	,##%%C\**,,Q/  !f&1DOF#&(DOF##t$$ J''(=(=(?(?(BCCCC--dl.C.C.E.Ea.HIII4DN&0DOF#&(DOF#5r5   c                     d}d}	 ddl m}  |||                                           d S #  t          | j        ||           Y d S xY w)NzANLTK Recursive Descent Parser Application
Written by Edward Loperz+About: Recursive Descent Parser Applicationr   )Message)messager   )tkinter.messageboxrE  showr   r   )r/   rw   ABOUTTITLErE  s        r3   r   zRecursiveDescentApp.about  su    U 	 >	.222222GE///4466666	.TYu------s	   %- Ac                     d| _         	 t          | j        dt          pd                                dd           d S #  t          | j        dt          pd                                d           Y d S 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  s    		<B%%''     		<B%%''	      s	   2= 3A3c                 F    d| _         | j                                         d S r'  )r$   r   print_to_filer(  s     r3   r   zRecursiveDescentApp.postscript  s#    ""$$$$$r5   c                 J    t                      rdS  | j        j        |i | dS )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   rS  zRecursiveDescentApp.mainloop  s5     99 	F	D+F+++++r5   c                    || j                             |           | j                                         }| j                            t          |                      | j                            t          |                      | j                            t          |                      | j                            t          |dz                         | 	                                 d S )N)r9   r>   )
rA   r"   rC   rE   	configureabsrD   r?   rF   r   )r/   r9   s     r3   r   zRecursiveDescentApp.resize  s    JNN4   z~~
CII,///  D		l 333s4yy\222s4!8}}%5666r5   c                     | j                                         r.| j                            ddd| j                   d| j        d<   n#| j                                         d| j        d<   d| j        d<   d S )	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  s    !!## 	5O  &q8K !    '5DOF##O'')))&4DOF#"$r5   c                    | j                                         }t          |          dk    rd S t          |d                   }| j                                        }| j                            | j        |                   }|rfd| j        d<   || j	        d<   | j         
                    dd           | j                             |           |                     |d                    d S | j         
                    dd           | j                                        D ]6}| j                            |          }| j                             |           7d S )Nr   r   r9  rO   r]   )rh   curselectionro   intr   r  r\   rk   r   r   r:  r  r;  r  r  )r/   r   	selectionr  r<  rt   prods          r3   rq   z$RecursiveDescentApp._prodlist_select  sH   N//11	y>>QFIaL!!|,,..\(():5)ABB
 	4&/DOF#&0DOF#N**1e444N((///  a11111 N**1e444;;== 4 4)//55,,U33334 4r5   c           
                                 j        |          }|                                }t          |                                t                     } j                                        }|D ]
}||         }t           j        | j	        dddd j
                  }d|                                d<   |                                                                d d         \  }}	|                                                                d d         \  }
}|                    ||
z
  |	|z
             |rg j                            |dd           |                    d|                                                                d         z
  d           | _        n(|                                                    ||           |                                r||                                                                dz  z   |                                d                                         d         dz  z
  |                                d                                         d         dz  z
  }|                                D ]}|                    |d                                |           |r j                            |           n|                                  fd	t-           j                                        dd
          D             }|                                d         dz    j                             j                  d         z
  }|dk    rA j        D ]}|                    d|            j                             j        d|                                ||           d S )NrR   r>   )r   
leaf_colorr   r   
node_colorr   r  r  r   r   r   c           	      t    g | ]4}d dt          d|z  j                                        z            z  z  5S zgray%dr   r^  r!   rC   r   r   r/   s     r3   r   z7RecursiveDescentApp._animate_expand.<locals>.<listcomp>\  sS     
 
 
 Sa$*@*D*D*F*F!FGGGH
 
 
r5   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   coordsr   r   _animate_expand_frame)r/   r   	oldwidgetoldtreer   r   r   r   oldxoldynewxnewydxsubtreecolorsr#  r  s   `                r3   r;  z#RecursiveDescentApp._animate_expand)  s   IIdj'22	""$$W^^--/@AAA|  "" 	 	A7DD$Lnj	
 	
 	
 #,w}}++--bqb1t||~~**,,RaR0tD4K--- 	<L##FAq111KKV\\^^0022155q999DJJNN**7F;;; ?? 	$,,..&&((1,-//##A&++--a0145 //##A&++--a0145  "??,, $ $R####v 	L''0000OO
 
 
 
415577B??
 
 
 [[]]1"T\%8%8%H%H%KK66, $ $Q####Ldna444""6622222r5   c                    |                                 }t          |t                    sdS |                                                    |          }|                                |dz   d         }|rY|                                d         |d                                         d         z
  dz   }|D ]}|                    |d           |dk    ry|                                |dz
           }t          d|                                d         |                                d         z
  dz             }|                    |d           |                     |           dS )z@
        Make sure that no sibling tree bbox's overlap.
        Nr   r>   r   r   )	rr   r   r   r   r  r   r   r  rj  )r/   treesegrr   r  	rsiblingsrs  siblinglsiblings           r3   rj  zRecursiveDescentApp._makeroomj  sI    !!&"344 	F!!''00 OO%%eaikk2	 	$"Yq\%6%6%8%8%;;b@B$ $ $R#### 199((3HQ*W\\^^A->>CDDBLLQ 	vr5   c                    t          |          dk    rd| _        |d         |d<   |                                D ]@}t          |t                    r|d         |                                d<   5|d         |d<   A| j                            d| j        ||dd                     d S d|d<   |                                D ]4}t          |t                    rd|                                d<   /d|d<   5| 	                                 d|                                d<   d| _        | j
        r|                                  d S d S )Nr   r   r  2   r   )ro   r#   r   r   r   r   r   rZ  rl  r	  r$   r,  )r/   r   ru  rt  s       r3   rl  z)RecursiveDescentApp._animate_expand_frame  sO   v;;??#$D $QiF7O!??,, 1 1g'899 1/5ayGMMOOG,,'-ayGG$$IOOB :FF122JOOOOO%F7O!??,, / /g'899 //6GMMOOG,,'.GG$$   &-FLLNN7##$D ~ 

 r5   c                 (     j                                         dk    rg }ng d}| fdt          d j                                         dz             D             z  }                      j        |                                          g}|d                                         D ]T}t          |t                    r(|	                    |
                                           ?|	                    |           U                     ||           d S )Nr   )#a00000r   r~  c           	      t    g | ]4}d dt          d|z  j                                        z            z  z  5S re  rf  rg  s     r3   r   z:RecursiveDescentApp._animate_backtrack.<locals>.<listcomp>  sS     
 
 
 Sa4+A+E+E+G+G!HIIIJ
 
 
r5   r   )r!   rC   r  r   r   rr   r   r   r   appendr   _animate_backtrack_frame)r/   r   ru  widgetsrt  s   `    r3   rA  z&RecursiveDescentApp._animate_backtrack  s#   !%%''1,,FF666F 
 
 
 
1d488::Q>??
 
 
 	

 99TZ1188::;qz**,, 	( 	(G'#455 (w}}////w''''%%gv66666r5   c                    t          |          dk    rCd| _        |D ]}|d         |d<   | j                            d| j        ||dd                     d S |d                                         D ]1}|d                             |           |                                 2|                                  d| _        | j	        r| 
                                 d S d S )Nr   r   r  r|  )ro   r#   r   rZ  r  r   remove_childr{   r	  r$   r,  )r/   r  ru  r   s       r3   r  z,RecursiveDescentApp._animate_backtrack_frame  s    v;;??#$D ! , ,"()wIOOB =wqrr
SSSSS!!*--// ! !
''///       #$D ~ 

 r5   c                    |                      | j        |          }|                                                                }|                                d         |                                d         z
  dz   t          d| j                                                  z  }|                     | j                                        ||           d S )Nr   r   r   )	r   r   rr   r   r   r  r!   rC   _animate_match_backtrack_frame)r/   r   r   noder#  s        r3   rB  z,RecursiveDescentApp._animate_match_backtrack  s    4:w//}}$$&&iikk!nv{{}}Q//"4t%))++9
 9
 
 	++D,B,F,F,H,H&RTUUUUUr5   c                 n   |                      | j        |          }| j        d                                         d         |                                d         z
  dz
  t	          d| j                                                  z  }|                     | j                                        ||           d S )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
 K
 
 	!!$"8"<"<">">KKKKKr5   c                    |dk    rEd| _         |                    d|           | j                            d| j        |dz
  ||           d S d|d<   |                                  d| _         | j        r|                                  d S d S )Nr   r   r   r  r  )r#   r   r   rZ  r  r	  r$   r,  r/   framer   r#  s       r3   r  z(RecursiveDescentApp._animate_match_frame  s    199#$D KK2IOOB 9519fbQQQQQ'F7O   #$D ~ 

 r5   c                 Z   |dk    rEd| _         |                    d|           | j                            d| j        |dz
  ||           d S |                                                    |           |                                 d| _         | j        r| 	                                 d S d S )Nr   r   r   )
r#   r   r   rZ  r  rr   r  r{   r$   r,  r  s       r3   r  z2RecursiveDescentApp._animate_match_backtrack_frame  s    199#$D KK2IOOD7FB     MMOO((000NN#$D ~ 

 r5   c                 j    t          | j        | j                                        | j                   d S r   )r   r   r   r0   set_grammarr(  s     r3   r   z RecursiveDescentApp.edit_grammar  s-    $)T\1133T5EFFFFFr5   c                    | j                             |           t          |                                          | _        | j                            dd           | j        D ] }| j                            dd|z             !d S )Nr   r]   r  )r   r  ri   rj   rk   rh   r   rl   )r/   r0   rt   s      r3   r  zRecursiveDescentApp.set_grammar  s      ))) !4!4!6!677a'''+ 	? 	?JN!!%%**<>>>>	? 	?r5   c                 |    d                     | j                  }d}d}t          | j        ||| j        |           d S )N r   zEnter a new sentence to parse.)joinr   r   r   set_sentence)r/   rw   sentencer   instrs        r3   r   z!RecursiveDescentApp.edit_sentence  s@    88DJ''0DIx0A5IIIIIr5   c                 `    |                                 | _        |                                  d S r   )splitr   r   )r/   r  s     r3   r  z RecursiveDescentApp.set_sentence  s$    ^^%%


r5   )r   r   )4__name__
__module____qualname__rN  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,  r2  r0  r4  r   r   r   rS  r   r   rq   r;  rj  rl  rA  r  rB  r?  r  r  r   r  r   r   r5   r3   r   r   M   s         $: $: $: $:TJ J J""H "H "HH: : :B) ) )`  > > >.  "A$ A$ A$N  + + +Z  @ @ @I I I$     D	 	 	 	        
        > > >*  $    (
. 
. 
.  &% % %	, 	, 	,   	% 	% 	%,4 4 42?3 ?3 ?3B  4  .7 7 7(  V V VL L L
 
 
  G G G? ? ?J J J    r5   r   c                      ddl m}  |                     d          }d                                }t	          ||                                           dS )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   rS  )r  r0   r1   s      r3   appr    sc    
 !     nn	 G  +0022D&&//11111r5   __main__r  N)rN  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     s<  9 9v O N N N N N N N N N N N N N N N N N N N       G G G G G G G G G G I I I I I I I I I I I I 5 5 5 5 5 5            m m m m m m m m`2 2 28 zCEEE'r5   