o
    ¹iy  ã                   @   s¶   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
„ d
ƒZdd„ ZedkrVeƒ  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                   @   s–  e Zd ZdZdddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zded"d#„Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Zd<d=„ Z d>d?„ Z!d@dA„ Z"dBdC„ Z#dedDdE„Z$dFdG„ Z%dHdI„ Z&dJdK„ Z'dLdM„ Z(dNdO„ Z)dPdQ„ Z*dRdS„ Z+dTdU„ Z,dVdW„ Z-dXdY„ Z.dZd[„ Z/d\d]„ Z0d^d_„ Z1d`da„ Z2dbdc„ Z3d!S )fÚ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                 C   sÔ   || _ 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Útrace© r0   úP/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/nltk/app/rdparser_app.pyÚ__init__X   s&   zRecursiveDescentApp.__init__c                 C   s¨   t tƒ d d| _| d| j¡ t|ƒ| _| j | j d¡¡ t dd| j ¡ d| _	t d| j ¡ d| _
| j ¡ d	k rC| j ¡ d
 }n| j ¡ d
 }t dd|d| _d S )NÚfont©r3   z*FontÚsizeÚ	helveticaÚbold)ÚfamilyÚweightr5   )r8   r5   r   é   )r
   r   Ú_sysfontÚ
option_addr   Ú_sizer   ÚcgetÚgetÚ	_boldfontÚ_fontÚ_bigfont)r,   ÚrootÚbigr0   r0   r1   r   ‚   s   
zRecursiveDescentApp._init_fontsc              
   C   s  t |ƒ | _}| jjdddd t| j| jdd| _| j ¡  t| jddd	d
| jddd| _| jjdddd t	| j
 ¡  ¡ ƒ| _| jD ]}| j dd| ¡ qD| jjtt| jƒdƒd t| jƒdkr‚t| jdd}| jj|jd |j| jjd |jddd | j d| j¡ d S )NÚbothÚleftr:   )ÚfillÚsideÚpadxzAvailable Expansions)r3   ÚtextÚsingleÚgrooveÚwhitez#909090ú#004040z#c0f0c0)Ú
selectmodeÚreliefÚ
backgroundÚ
foregroundr3   ÚselectforegroundÚselectbackgroundÚrightr   ©rH   rG   ÚexpandÚendz  %sé   )ÚheightÚvertical)Úorient)Úyscrollcommand)ÚcommandÚy)rH   rG   z<<ListboxSelect>>)r   Ú
_prodframeÚpackr   r@   Ú_prodlist_labelr   rA   Ú	_prodlistÚlistr   r-   ÚproductionsÚ_productionsÚinsertÚconfigÚminÚlenr   r   Úyviewr*   Ú_prodlist_select)r,   ÚparentÚ	listframeÚ
productionÚ
listscrollr0   r0   r1   r&   “   s6   
ÿ
ø
z!RecursiveDescentApp._init_grammarc                 C   sd  | 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*   ÚdestroyrW   ÚmatchÚ	backtrackÚautostepÚcancel_autostepÚstepÚresetÚ
postscriptÚhelpÚedit_grammarÚedit_sentence©r,   r0   r0   r1   r   ·   s,   z"RecursiveDescentApp._init_bindingsc                 C   s¶   t |ƒ | _}|jddddd t|ddd| jd	jd
d t|ddd| jd	jd
d t|dddd| jdjd
d t|dddd| jdjd
d t|dddd| jdjd
d d S )NÚnoneÚbottomé   r:   ©rG   rH   rI   ÚpadyÚStepz#90c0d0Úblack)rJ   rQ   rR   r^   rF   ©rH   ÚAutostepÚExpandr   z#90f090)rJ   Ú	underlinerQ   rR   r^   ÚMatchÚ	Backtrackz#f0a0a0)	r   Ú_buttonframera   r   rz   rx   rW   rv   rw   )r,   rm   Úbuttonframer0   r0   r1   r$   Ø   sd   ûúûúúùúùú
ùz!RecursiveDescentApp._init_buttonsc                 C   sD   d| _ | j ¡ \}}}}|jd }d||||f | jd< |  ¡  d S )Nr   é   z%d %d %d %dÚscrollregion)r!   Ú_cframer‘   rZ   r)   Ú_redraw)r,   ÚeventÚx1Úy1Úx2Úy2r0   r0   r1   r+     s
   
zRecursiveDescentApp._configurec                 C   s°   t |ƒ | _}|jddddd t|d| jd| _| jjdd t |d	d
d}|jddd
dd t|dd| jd| _t|dddd| jd| _| jjdd | jjddd
d d S )NÚxr‚   rƒ   r„   zLast Operation:)rJ   r3   rF   rˆ   Úsunkenr   )rP   ÚborderrU   r   )rG   rH   rW   rI   z#007070z#f0f0f0)rR   rQ   r3   Úwé   rN   )ÚanchorÚwidthrR   rQ   r3   rV   )r   Ú_feedbackframera   r   rA   Ú_lastoper_labelÚ
_lastoper1Ú
_lastoper2)r,   rm   ÚfeedbackframeÚlastoperframer0   r0   r1   r%     s*   ÿ
ÿúz"RecursiveDescentApp._init_feedbackc                 C   sN   t |ddddd| _| jjddddd	 | j ¡  }| _d | _g | _d | _d S )
NrM   é
   r:   rš   )rQ   Úcloseenoughr›   rP   r   rE   Útop)rW   rG   rH   r…   )r   r’   ra   Úcanvasr)   Ú_treeÚ_textwidgetsÚ	_textline)r,   rm   r©   r0   r0   r1   r'   &  s   ú
z RecursiveDescentApp._init_canvasc           	      C   s–  t |ƒ}t |dd}|jdd| jdd |jdd| jdd |jdd	| jd
d |jdd|d t |dd}|jdd| jdd |jdd| jdd |jdd|d t |dd}|jdd	| jdd | 	¡  |jdd| j
dd |jdd| jdd | 	¡  |jdd| jdd |jdd|d t |dd}|jdd| j| jd | 	¡  |jd| jdd| jd  |jd!| jdd"| jd  |jd#| jdd$| jd  |jd%| jdd&| jd  |jd'| jdd(| jd  |jd)d|d t |dd}|jd*d| jdd+ |jd,d| jdd-d. |jd/d| jdd0d. |jd1d| jd2d3d. |jd4d	|d t |dd}|jd5d| jd6 |jd7d| jd8d |jd9d|d |j|d: d S );Nr   )ÚtearoffzReset ParserÚDel)Úlabelr‹   r^   Ú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‹   Úvariabler^   ÚTinyr¦   )r¯   r¹   r‹   Úvaluer^   Ú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‹   r^   ÚInstructionsÚF1ÚHelp)r³   )r   Úadd_commandr{   r|   ru   Úadd_cascader~   r   rz   Úadd_separatorrv   rW   rw   Úadd_checkbuttonr"   Ú_toggle_grammarÚadd_radiobuttonr=   Úresizer   Úaboutr}   rh   )	r,   rm   ÚmenubarÚfilemenuÚeditmenuÚrulemenuÚviewmenuÚanimatemenuÚhelpmenur0   r0   r1   r#   7  sô   
ÿü
ÿüü
ÿ
ÿ
ÿ
ÿüûûûûû
ÿûûû
ÿz!RecursiveDescentApp._init_menubarc                 C   s,   |D ]}|  ¡ | }qt|tƒr| ¡ }|S ©N)ÚsubtreesÚ
isinstancer   r¯   )r,   ÚwidgetÚtreelocÚir0   r0   r1   Ú_get¾  s
   
zRecursiveDescentApp._getc                    sl  ˆj ‰ ˆjd urˆj ˆj¡ ˆjD ]}ˆj |¡ qˆjd ur'ˆ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  }}‡ ‡fd	d
„ˆjD ƒˆ_ˆjD ]!}ˆj |dd¡ | d|| ¡ d  d ¡ t|| ¡ d ƒ}qwˆ jd|d d|d ddˆ_ˆ ¡  ˆ ¡  ˆ ¡  d S )Nr6   r7   ú#000000r:   )Ú
tree_colorÚ
tree_widthÚ	node_fontÚ	leaf_fontr   r   rƒ   c                    s   g | ]
}t ˆ |ˆjd ‘qS )r4   )r   rA   )Ú.0Úword©r©   r,   r0   r1   Ú
<listcomp>ä  s    ÿz/RecursiveDescentApp._redraw.<locals>.<listcomp>r   r   ixìÿÿiˆ  Ú.)Údash)r)   rª   r’   Údestroy_widgetr«   r¬   Údeleter=   r?   r   Útreer   Ú
add_widgetr‘   r   ÚmoveÚbboxri   Úcreate_lineÚ_highlight_nodesÚ_highlight_prodlistÚ_position_text)r,   ÚtwidgetÚhelvr7   Úattribsrð   r‚   r_   r0   rê   r1   r“   É  s<   


ü
ÿ
zRecursiveDescentApp._redrawc                 C   s   |   ¡  |  ¡  |  ¡  d S rÜ   )rõ   rö   r÷   r€   r0   r0   r1   Ú_redraw_quickö  s   z!RecursiveDescentApp._redraw_quickc                 C   s|   d| j  ¡  df}| j ¡ d d… D ]}d|  | j|¡d< ||  | j|¡d< q| j ¡ dd … D ]}d|  | j|¡d< q0d S )Nr6   r7   r   ú#20a050Úcolorr3   z#008080)r=   r?   r   Úfrontierrâ   rª   )r,   r7   rà   r0   r0   r1   rõ   ü  s   ÿz$RecursiveDescentApp._highlight_nodesc                 C   s¨   | j  dd¡ | j ¡ }| j ¡ }| j}tt|ƒƒD ]7}|| |v rF|| |v r4| j  |d||  ¡ n| j  |d||  ¡ | j  	|¡ q| j  |d||  ¡ qd S )Nr   rX   ú %sz %s (TRIED))
rc   rï   r   Úexpandable_productionsÚuntried_expandable_productionsrf   Úrangerj   rg   Úselection_set)r,   Ú
expandableÚuntriedre   Úindexr0   r0   r1   rö     s   

øz'RecursiveDescentApp._highlight_prodlistc           
      C   sˆ  t | jƒ}|t | j ¡ ƒ }|  ¡ d |… }| j ¡ d }tdt |ƒƒD ]+}| j| }|| }d|d< d|d< | 	| ¡ d | ¡ d  d¡ | ¡ d d }q$tt |ƒ|ƒD ]}| j| }d|d< | 	|| ¡ d  d¡ | ¡ d d }qW| j 
¡ r†| jD ]}d|d< qtdt |ƒƒD ]4}| j| }|| }| ¡ d | ¡ d	  d
 }	t|	| ¡  ¡  ¡ d	 | ¡ d	  d ƒ}	| 	d|	¡ qd S )Nr   ú#006040rý   r:   r¦   z#a0a0a0z#00a000r   rƒ   ç      $@)rj   r   r   Úremaining_textÚ_tree_leavesrª   ró   r  r«   rò   Úcurrently_completeÚmaxrm   r¯   )
r,   ÚnumwordsÚnum_matchedÚleavesÚxmaxrá   rß   Úleafrø   Údyr0   r0   r1   r÷     s4   

 




*ûz"RecursiveDescentApp._position_textNc                 C   sB   |d u r| j }t|tƒrg }| ¡ D ]	}||  |¡7 }q|S |gS rÜ   )rª   rÞ   r   rÝ   r
  )r,   rð   r  Úchildr0   r0   r1   r
  9  s   
z RecursiveDescentApp._tree_leavesc                 G   s(   d| _ | jd u r
d S | j ¡  d | _d S ©Nr   )r!   r   ru   ©r,   rq   r0   r0   r1   ru   H  s
   


zRecursiveDescentApp.destroyc                 G   s4   d| _ | j | j¡ d| jd< d| jd< |  ¡  d S )Nr   zReset ApplicationrJ   Ú )r!   r   r(   r   r¢   r£   r“   r  r0   r0   r1   r{   O  s
   

zRecursiveDescentApp.resetc                 G   s<   | j  ¡ dkr| j  d¡ | jrd| _d S d| _|  ¡  d S )Nr   r:   r   )r   r?   r   r!   Ú_stepr  r0   r0   r1   rx   V  s   
zRecursiveDescentApp.autostepc                 G   s
   d| _ d S r  )r!   r  r0   r0   r1   ry   _  s   
z#RecursiveDescentApp.cancel_autostepc                 G   ó   d| _ |  ¡  d S r  )r!   r  r  r0   r0   r1   rz   d  ó   zRecursiveDescentApp.stepc                 G   r  r  )r!   Ú_matchr  r0   r0   r1   rv   h  r  zRecursiveDescentApp.matchc                 G   r  r  )r!   Ú_expandr  r0   r0   r1   rW   l  r  zRecursiveDescentApp.expandc                 G   r  r  )r!   Ú
_backtrackr  r0   r0   r1   rw   p  r  zRecursiveDescentApp.backtrackc                 C   sv   | j rd S |  ¡ r
n| j ¡ r|  ¡ rn|  ¡ rnd| jd< d| jd< d| _| j 	¡ r9d| _| jd  d7  < d S d S )NÚFinishedrJ   r  r   z    [COMPLETE PARSE])
r    r  r   Úuntried_matchr  r  r¢   r£   r!   r  r€   r0   r0   r1   r  t  s   


þzRecursiveDescentApp._stepc                 G   sŠ   | j rd S | j ¡ }| j ¡ }|d ur9d| jd< || jd< | j dd¡ | j 	|¡}| j 
|¡ |  |d ¡ dS d| jd< d| jd< dS )NúExpand:rJ   r   rX   Tz(all expansions tried)F)r    r   rþ   rW   r¢   r£   rc   Úselection_clearrf   r  r  Ú_animate_expand)r,   rq   Úold_frontierÚrvr  r0   r0   r1   r  ‰  s   





zRecursiveDescentApp._expandc                 G   sd   | j rd S | j ¡ }| j ¡ }|d ur&d| jd< || jd< |  |d ¡ dS d| jd< d| jd< dS )NzMatch:rJ   r   Tz(failed)F)r    r   rþ   rv   r¢   r£   Ú_animate_match)r,   rq   r"  r#  r0   r0   r1   r  ›  s   





zRecursiveDescentApp._matchc                 G   s¦   | j rd S | j ¡ rD| j ¡ }| j ¡ d D ]}|| }qd| jd< d| jd< t|tƒr8|  	| j ¡ d ¡ dS |  
| j ¡ d ¡ dS d| _d| jd< d| jd< dS )Nr   r   rJ   r  Tr  F)r    r   rw   rð   rþ   r¢   r£   rÞ   r   Ú_animate_backtrackÚ_animate_match_backtrackr!   )r,   rq   Úeltrá   r0   r0   r1   r  ª  s"   





ÿ

zRecursiveDescentApp._backtrackc                 G   sF   d}d}zddl m} |||d ¡  W d S    t| j||ƒ Y d S )NzANLTK Recursive Descent Parser Application
Written by Edward Loperz+About: Recursive Descent Parser Applicationr   )ÚMessage)Úmessager   )Útkinter.messageboxr(  Úshowr   r   )r,   rq   ÚABOUTÚTITLEr(  r0   r0   r1   rÔ   ¾  s   ÿzRecursiveDescentApp.aboutc              	   G   sP   d| _ zt| jdtpd ¡ ddd W d S    t| jdtpd ¡ dd Y d S )Nr   z*Help: Recursive Descent Parser Applicationr  éK   Úfixed)rŸ   r3   )rŸ   )r!   r   r   Ú__doc__Ústripr  r0   r0   r1   r}   Ê  s    
û
üzRecursiveDescentApp.helpc                 G   s   d| _ | j ¡  d S r  )r!   r’   Úprint_to_filer  r0   r0   r1   r|   Ý  s   zRecursiveDescentApp.postscriptc                 O   s    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Úkwargsr0   r0   r1   r3  á  s   zRecursiveDescentApp.mainloopc                 C   s~   |d ur
| j  |¡ | j  ¡ }| jjt|ƒ d | jjt|ƒ d | jjt|ƒ d | jjt|d ƒ d |  	¡  d S )N)r5   r:   )
r=   r   r?   rA   Ú	configureÚabsr@   r;   rB   r“   )r,   r5   r0   r0   r1   rÓ   ì  s   
zRecursiveDescentApp.resizec                 G   sN   | j  ¡ r| jjddd| jd d| jd< n
| j ¡  d| jd< d| jd< d S )	NrE   rF   r:   )rG   rH   rI   Úafterr¸   rJ   zHide Grammarr  )r"   r?   r`   ra   r    r¢   Úpack_forgetr£   r  r0   r0   r1   rÑ   ú  s   

ÿ

z#RecursiveDescentApp._toggle_grammarc                 C   sÀ   | j  ¡ }t|ƒdkrd S t|d ƒ}| j ¡ }| j | j| ¡}|rCd| jd< || j	d< | j  
dd¡ | j  |¡ |  |d ¡ d S | j  
dd¡ | j ¡ D ]}| j |¡}| j  |¡ qOd S )Nr   r   r  rJ   rX   )rc   Úcurselectionrj   Úintr   rþ   rW   rf   r¢   r£   r   r  r!  r   r  )r,   r”   Ú	selectionr  r"  ro   Úprodr0   r0   r1   rl     s"   



þz$RecursiveDescentApp._prodlist_selectc              
      s  ˆ   ˆ j|¡}| ¡ }t| ¡ tƒ }ˆ j ¡ }|D ]}|| }qtˆ j|ˆ j	ddddˆ j
d}d| ¡ d< | ¡  ¡ d d… \}}	| ¡  ¡ d d… \}
}| ||
 |	| ¡ |rtˆ j |dd¡ | d| ¡  ¡ d  d¡ |ˆ _n| ¡  ||¡ | ¡ r¯|| ¡  ¡ d  | ¡ d  ¡ d d  | ¡ d  ¡ d d  }| ¡ D ]}| |d¡ q¦ˆ  |¡ |r½ˆ j |¡ n| ¡  ‡ fd	d
„tˆ j ¡ ddƒD ƒ}| ¡ d d ˆ j ˆ j¡d  }|dkrûˆ jD ]}| d|¡ qéˆ j ˆ jd|¡ ˆ  ||¡ d S )NrM   r:   )ræ   Ú
leaf_colorrå   rä   Ú
node_colorrç   rü   rý   r   r   r   c                    ó*   g | ]}d dt d| ˆ j ¡  ƒ  ‘qS ©zgray%dr¦   ©r;  r   r?   ©rè   r™   r€   r0   r1   rë   \  ó    ÿÿz7RecursiveDescentApp._animate_expand.<locals>.<listcomp>éÿÿÿÿrƒ   r   )râ   rª   rm   rÞ   r   r   rð   r   r)   r@   rA   r¯   ró   rò   r’   rñ   Úreplace_childrÝ   rŸ   Ú	_makeroomrî   ru   r  r   r?   Úcoordsr¬   r«   Ú_animate_expand_frame)r,   rà   Ú	oldwidgetÚoldtreer¨   rð   rá   rß   ÚoldxÚoldyÚnewxÚnewyÚdxÚsubtreeÚcolorsr  rø   r0   r€   r1   r!  )  sb   

ø
ÿþýÿ

þ"
z#RecursiveDescentApp._animate_expandc                 C   sÆ   |  ¡ }t|tƒsdS | ¡  |¡}| ¡ |d d… }|r9| ¡ d |d  ¡ d  d }|D ]}| |d¡ q0|dkr\| ¡ |d  }td| ¡ d | ¡ d  d ƒ}| |d¡ |  |¡ dS )z@
        Make sure that no sibling tree bbox's overlap.
        Nr   r:   r   r¦   )	rm   rÞ   r   rÝ   r  ró   rò   r  rG  )r,   Útreesegrm   r  Ú	rsiblingsrP  ÚsiblingÚlsiblingr0   r0   r1   rG  j  s   
 "zRecursiveDescentApp._makeroomc                 C   sÚ   t |ƒdkr:d| _|d |d< | ¡ D ]}t|tƒr#|d | ¡ d< q|d |d< q| j d| j||dd … ¡ d S d|d< | ¡ D ]}t|tƒrPd| ¡ d< qBd|d< qB|  	¡  d| ¡ d< d| _| j
rk|  ¡  d S d S )Nr   r   rý   é2   r‡   )rj   r    rÝ   rÞ   r   r¯   r   r8  rI  rû   r!   r  )r,   rß   rR  rQ  r0   r0   r1   rI  „  s&   
 

ÿz)RecursiveDescentApp._animate_expand_framec                    sœ   ˆ j  ¡ dkr
g }ng d¢}|‡ fdd„tdˆ j  ¡ d ƒD ƒ7 }ˆ  ˆ j|¡ ¡ g}|d  ¡ D ]}t|tƒr@| 	| 
¡ ¡ q1| 	|¡ q1ˆ  ||¡ d S )Nr   )ú#a00000rã   rX  c                    r@  rA  rB  rC  r€   r0   r1   rë   ¡  rD  z:RecursiveDescentApp._animate_backtrack.<locals>.<listcomp>r   )r   r?   r  râ   rª   rm   rÝ   rÞ   r   Úappendr¯   Ú_animate_backtrack_frame)r,   rà   rR  ÚwidgetsrQ  r0   r€   r1   r%  ›  s   þ
z&RecursiveDescentApp._animate_backtrackc                 C   s”   t |ƒdkr$d| _|D ]}|d |d< q| j d| j||dd … ¡ d S |d  ¡ D ]}|d  |¡ | ¡  q*|  ¡  d| _| j	rH|  
¡  d S d S )Nr   r   rý   rW  )rj   r    r   r8  rZ  rÝ   Úremove_childru   rû   r!   r  )r,   r[  rR  rß   r0   r0   r1   rZ  ¯  s    
ÿz,RecursiveDescentApp._animate_backtrack_framec                 C   s^   |   | j|¡}| ¡  ¡ }| ¡ d | ¡ d  d td| j ¡ ƒ }|  | j ¡ ||¡ d S )Nrƒ   r   r¿   )	râ   rª   rm   r¯   ró   r  r   r?   Ú_animate_match_backtrack_frame)r,   rà   rß   Únoder  r0   r0   r1   r&  ¾  s   
ÿz,RecursiveDescentApp._animate_match_backtrackc                 C   sX   |   | j|¡}| jd  ¡ d | ¡ d  d td| j ¡ ƒ }|  | j ¡ ||¡ d S )Nr   r   rƒ   r  )râ   rª   r«   ró   r  r   r?   Ú_animate_match_frame)r,   rà   rß   r  r0   r0   r1   r$  Æ  s
   "
ÿz"RecursiveDescentApp._animate_matchc                 C   sd   |dkrd| _ | d|¡ | j d| j|d ||¡ d S d|d< |  ¡  d| _ | jr0|  ¡  d S d S )Nr   r   r¦   r  rý   )r    rò   r   r8  r_  rû   r!   r  ©r,   Úframerß   r  r0   r0   r1   r_  Î  s   ÿz(RecursiveDescentApp._animate_match_framec                 C   sj   |dkrd| _ | d|¡ | j d| j|d ||¡ d S | ¡  |¡ | ¡  d| _ | jr3|  	¡  d S d S )Nr   r   r¦   )
r    rò   r   r8  r]  rm   r\  ru   r!   r  r`  r0   r0   r1   r]  Ú  s   ÿÿz2RecursiveDescentApp._animate_match_backtrack_framec                 G   s   t | j| j ¡ | jƒ d S rÜ   )r   r   r   r-   Úset_grammarr  r0   r0   r1   r~   è  s   z RecursiveDescentApp.edit_grammarc                 C   sJ   | j  |¡ t| ¡ ƒ| _| j dd¡ | jD ]}| j dd| ¡ qd S )Nr   rX   rÿ   )r   rb  rd   re   rf   rc   rï   rg   )r,   r-   ro   r0   r0   r1   rb  ë  s   
ÿzRecursiveDescentApp.set_grammarc                 G   s,   d  | j¡}d}d}t| j||| j|ƒ d S )Nú r´   zEnter a new sentence to parse.)Újoinr   r   r   Úset_sentence)r,   rq   Úsentencer   Úinstrr0   r0   r1   r   ò  s   z!RecursiveDescentApp.edit_sentencec                 C   s   |  ¡ | _|  ¡  d S rÜ   )Úsplitr   r{   )r,   rf  r0   r0   r1   re  ø  s   
z RecursiveDescentApp.set_sentence)r   rÜ   )4Ú__name__Ú
__module__Ú__qualname__r0  r2   r   r&   r   r$   r+   r%   r'   r#   râ   r“   rû   rõ   rö   r÷   r
  ru   r{   rx   ry   rz   rv   rW   rw   r  r  r  r  rÔ   r}   r|   r3  rÓ   rÑ   rl   r!  rG  rI  r%  rZ  r&  r$  r_  r]  r~   rb  r   re  r0   r0   r0   r1   r   M   sf    

*$!0 -	
"	
Ar   c                  C   s0   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.grammarrl  Ú
fromstringrh  r   r3  )rl  r-   r.   r0   r0   r1   Úappý  s   ÿro  Ú__main__N)r0  Ú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   ro  ri  Ú__all__r0   r0   r0   r1   Ú<module>   s(   (;       7
