o
    rZŽh¸‚  ã                   @   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rWeƒ  dgZdS )ad  
A graphical tool for exploring the shift-reduce parser.

The shift-reduce parser maintains a stack, which records the structure
of the portion of the text that has been parsed.  The stack is
initially empty.  Its contents are shown on the left side of the main
canvas.

On the right side of the main canvas is the remaining text.  This is
the portion of the text which has not yet been considered by the
parser.

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

  - "shift" moves the first token from the remaining text to the top
    of the stack.  In the demo, the top of the stack is its right-hand
    side.
  - "reduce" uses a grammar production to combine the rightmost stack
    elements into a single tree token.

You can control the parser's operation by using the "shift" and
"reduce" 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:

  - Only shift if no reductions are available.
  - If multiple reductions are available, then apply the reduction
    whose CFG production is listed earliest in the grammar.

The "reduce" button applies the reduction whose CFG production is
listed earliest in the grammar.  There are two ways to manually choose
which reduction to apply:

  - Click on a CFG production from the list of available reductions,
    on the left side of the main window.  The reduction based on that
    production will be applied to the top of the stack.
  - Click on one of the stack elements.  A popup window will appear,
    containing all available reductions.  Select one, and it will be
    applied to the top of the stack.

Note that reductions can only be applied to the top of the stack.

Keyboard Shortcuts::
      [Space]	 Perform the next shift or reduce operation
      [s]	 Perform a shift operation
      [r]	 Perform a reduction operation
      [Ctrl-z]	 Undo most recent operation
      [Delete]	 Reset the parser
      [g]	 Show/hide available production list
      [Ctrl-a]	 Toggle animations
      [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)ÚSteppingShiftReduceParser)ÚTree)Úin_idlec                   @   s,  e Zd ZdZdJd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d!d"„ Zd#d$„ Zd%d&„ Zd'd(„ ZdKd*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#dDdE„ Z$dFdG„ Z%dHdI„ Z&d)S )LÚShiftReduceAppaË  
    A graphical tool for exploring the shift-reduce parser.  The tool
    displays the parser's stack and the remaining text, and allows the
    user to control the parser's operation.  In particular, the user
    can shift tokens onto the stack, and can perform reductions on the
    top elements of the stack.  A "step" button simply steps through
    the parsing process, performing the operations that
    ``nltk.parse.ShiftReduceParser`` would use.
    r   c                 C   sÒ   || _ t||ƒ| _tƒ | _| j d¡ d| _t| jƒ| _| j 	d¡ t| jƒ| _
| j
 	d¡ |  | j¡ |  ¡  |  | j¡ |  | j¡ |  | j¡ |  | j¡ |  | j¡ t| jdd| _|  ¡  d| jd< d S )NzShift Reduce Parser Applicationr   é
   é   ©ZtearoffÚ Útext)Ú_sentr   Ú_parserr	   Ú_topÚtitleÚ_animating_lockr   Ú_animateÚsetÚ_show_grammarÚ_init_fontsÚ_init_bindingsÚ_init_menubarÚ_init_buttonsÚ_init_feedbackÚ_init_grammarÚ_init_canvasr   Ú_canvasÚ_reduce_menuÚresetÚ
_lastoper1)ÚselfÚgrammarÚsentÚtrace© r2   úD/var/www/auris/lib/python3.10/site-packages/nltk/app/srparser_app.pyÚ__init__b   s&   zShiftReduceApp.__init__c                 C   sl   t tƒ d d| _| d| j¡ t|ƒ| _| j | j d¡¡ t dd| j ¡ d| _	t d| j ¡ d| _
d S )	NÚfont)r5   z*FontÚsizeZ	helveticaÚbold)ÚfamilyÚweightr6   )r8   r6   )r
   r   Ú_sysfontZ
option_addr   Ú_sizer!   ZcgetÚgetÚ	_boldfontÚ_font)r.   Úrootr2   r2   r3   r#   Œ   s   
zShiftReduceApp._init_fontsc              
   C   s2  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d}| jj|jd |j| jjd |jddd | j d| j¡ d| _| j d| j¡ | j d| j¡ d S )NÚbothÚlefté   )ÚfillÚsideÚpadxzAvailable Reductions)r5   r   ÚsingleZgrooveÚwhitez#909090ú#004040z#c0f0c0)Z
selectmodeÚreliefÚ
backgroundÚ
foregroundr5   ZselectforegroundZselectbackgroundÚrightr   ©rD   rC   ÚexpandÚendú %sé   )ÚheightÚvertical)Zorient)Zyscrollcommand)ÚcommandÚy)rD   rC   z<<ListboxSelect>>éÿÿÿÿz<Motion>z<Leave>)r   Ú
_prodframeÚpackr   r=   Z_prodlist_labelr   r>   Ú	_prodlistÚlistr   r/   ÚproductionsÚ_productionsÚinsertÚconfigÚminÚlenr   r!   ZyviewÚbindÚ_prodlist_selectÚ_hoverÚ_highlight_hoverÚ_clear_hover)r.   ÚparentZ	listframeÚ
productionZ
listscrollr2   r2   r3   r(   ˜   s<   
ÿ
ø
zShiftReduceApp._init_grammarc                 C   s¬  | 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¡ | j  d| jfdd„¡ | j  d| jfdd„¡ | j  d| jfdd„¡ d S )Nz<Control-q>z<Control-x>z<Alt-q>z<Alt-x>z<space>z<s>z<Alt-s>z<Control-s>z<r>z<Alt-r>z<Control-r>z<Delete>z<u>z<Alt-u>z<Control-u>z<Control-z>z<BackSpace>z<Control-p>z<Control-h>z<F1>z<Control-g>z<Control-t>ú-c                 S   ó
   |  d¡S )Né   ©r!   ©ÚeÚar2   r2   r3   Ú<lambda>ß   ó   
 z/ShiftReduceApp._init_bindings.<locals>.<lambda>ú=c                 S   ri   )Nr   rk   rl   r2   r2   r3   ro   à   rp   ú+c                 S   ri   )Né   rk   rl   r2   r2   r3   ro   á   rp   )r   ra   ÚdestroyÚstepÚshiftÚreducer,   ÚundoÚ
postscriptÚhelpÚedit_grammarÚedit_sentencer    )r.   r2   r2   r3   r$   Á   s2   zShiftReduceApp._init_bindingsc                 C   s–   t |ƒ | _}|jd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)rC   rD   ÚStepz#90c0d0Úblack)r   rJ   rK   rT   rA   ©rD   ÚShiftr   z#90f090)r   Ú	underlinerJ   rK   rT   ÚReduceÚUndoz#f0a0a0)r   Z_buttonframerX   r   ru   rv   rw   rx   )r.   rf   Zbuttonframer2   r2   r3   r&   ã   sR   ûúúùúùú
ùzShiftReduceApp._init_buttonsc           	      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d0d| jdd1d/ |jd2d| jd3d4d/ |jd5d	|d t |dd}|jd6d| jd7 |jd8d| jd9d |jd:d|d |j|d; d S )<Nr   r   zReset ParserÚDel)Úlabelrƒ   rT   ÚacceleratorzPrint to PostscriptzCtrl-pZExitr   zCtrl-xÚFile)r‡   rƒ   ÚmenuzEdit Grammaré   zCtrl-gú	Edit TextzCtrl-tZEditr   ZSpacer‚   zCtrl-sr„   zCtrl-rr…   zCtrl-uZApplyúShow Grammar)r‡   rƒ   ÚvariablerT   ZTinyr   )r‡   rŽ   rƒ   ÚvaluerT   ZSmallé   ZMediumé   ZLargeé   ZHugeé   ZViewzNo Animation)r‡   rƒ   rŽ   r   zSlow Animationrj   rh   )r‡   rƒ   rŽ   r   rˆ   zNormal Animationrq   zFast Animationrs   rr   ZAnimateZAbout)r‡   rƒ   rT   ZInstructionsZF1ZHelp)rŠ   )r   Úadd_commandr,   ry   rt   Zadd_cascader{   r|   ru   Zadd_separatorrv   rw   rx   Zadd_checkbuttonr"   Ú_toggle_grammarZadd_radiobuttonr;   Úresizer    Úaboutrz   r^   )	r.   rf   ZmenubarZfilemenuZeditmenuZrulemenuZviewmenuZanimatemenuZhelpmenur2   r2   r3   r%     sô   
ÿü
ÿüü
ÿ
ÿ
ÿ
ÿüûûûûû
ÿûûû
ÿzShiftReduceApp._init_menubarc                 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~   é   )rC   rD   rE   ÚpadyzLast Operation:)r   r5   rA   r   Úsunkenr   )rI   ÚborderrL   r‹   )rC   rD   rN   rE   z#007070z#f0f0f0)rK   rJ   r5   Úwé   rH   )ÚanchorÚwidthrK   rJ   r5   rM   )r   Ú_feedbackframerX   r   r>   Z_lastoper_labelr-   Ú
_lastoper2)r.   rf   ZfeedbackframeZlastoperframer2   r2   r3   r'   Š  s*   ÿ
ÿúzShiftReduceApp._init_feedbackc                 C   sà   t |dddddd| _| jjddd	dd
 | j ¡  }| _g | _g | _|jddddddd| _|j	dddddd| _
|j	dddddd| _| j ¡ d }t|dd| jd| _t|dd| jd| _| j | j¡ | j | j¡ d S )NrG   i  r   rB   r›   )rJ   r    Zcloseenoughrœ   rI   r   r@   Útop)rN   rC   rD   rš   r   z#c0f0f0r€   )rC   ÚoutlineÚ.)Údashz#408080)rC   rs   ÚStackrH   ©Úcolorr5   zRemaining Text)r   Ú_cframerX   Úcanvasr*   Ú_stackwidgetsÚ_rtextwidgetsZcreate_rectangleÚ	_titlebarZcreate_lineÚ	_exprlineÚ	_stacktopr;   r<   r   r=   Ú_stacklabelÚ_rtextlabelÚ
add_widget)r.   rf   r«   r6   r2   r2   r3   r)   ¡  s4   úÿ
ÿ
ÿzShiftReduceApp._init_canvasc                 C   s:  | j d  ¡ }dd„ |D ƒ\}}}}| jD ]}| j |¡ qg | _| jD ]}| j |¡ q$g | _| j ¡ \}}	}
}||	 d }| j  | j	ddd|d ¡ | j  | j
d|d	 d d|d	 d ¡ | j ¡ \}}	}
}| j d
| d|	 ¡ | j ¡ \}}	}
}| j ||
 d
 d|	 ¡ d
}| j ¡ D ]I}t|tƒr³dd	| jdd| jdœ}t| j |fi |¤Ž}d| ¡ d< n
t| j |d| jd}| | j¡ | j |¡ | j |||¡ | ¡ d	 d }qd}| j ¡ D ]"}t| j |d| jd}| j |¡ | j |||¡ | ¡ d	 d }qát| jƒdkr|| jd  ¡ 7 }t|| j ¡ d ƒ}| j ¡ d }||t||ƒ kr7|t||ƒ }| jD ]}| d| | d¡ q:| j || j ¡ d	  d
 d¡ || t||ƒ d	 }| j  | j|d|d¡ | j ¡ \}}	}
}t| jƒdkr—|| fdd„}| jd  |¡ | jd  | j ¡ |  !¡  d S )NÚscrollregionc                 s   s    | ]}t |ƒV  qd S ©N©Úint)Ú.0Úcr2   r2   r3   Ú	<genexpr>Ä  ó   € z)ShiftReduceApp._redraw.<locals>.<genexpr>r   ixìÿÿr   éˆ  rs   rB   r‹   r™   z#4080a0ú#006060)Z
tree_colorZ
tree_widthZ	node_fontZ
node_colorZ
leaf_colorZ	leaf_fontz#000000r©   r¨   rQ   c                 S   s(   |   ¡ d |k r| ¡  d S | ¡  d S )Nr   )Úbboxrv   Ú_redraw)ÚwidgetÚmidxr.   r2   r2   r3   Ú
drag_shift  s   z*ShiftReduceApp._redraw.<locals>.drag_shift)"r*   Úsplitr¬   rª   Zdestroy_widgetr­   r±   r¾   Úcoordsr®   r¯   Úmover²   r   ÚstackÚ
isinstancer   r=   r>   r   r‡   r   Z
bind_clickÚ_popup_reduceÚappendr³   Zremaining_textr`   r    Úmaxr°   Z	bind_dragrv   Ú_highlight_productions)r.   r´   Zcx1Zcy1Zcx2Zcy2ÚstackwidgetZrtextwidgetÚx1Úy1Úx2Úy2rU   ZstackxÚtokÚattribsrÀ   Z
rtextwidthZrlabelwidthrÁ   rÂ   r2   r2   r3   r¿   Â  sp   

&
ú
 zShiftReduceApp._redrawc                 C   s*   |  ¡ d d }| j | j|d|d¡ d S )NrB   é2   r   r¼   )r¾   r*   rÄ   r°   )r.   rÀ   rÁ   r2   r2   r3   Ú_draw_stack_top  s   zShiftReduceApp._draw_stack_topc                 C   s:   | j  dd¡ | j ¡ D ]}| j |¡}| j  |¡ qd S )Nr   rO   )rY   Úselection_clearr   Úreducible_productionsr\   ÚindexÚselection_set)r.   Úprodr×   r2   r2   r3   rË     s
   þz%ShiftReduceApp._highlight_productionsc                 G   s"   | j d u rd S | j  ¡  d | _ d S rµ   )r   rt   ©r.   rm   r2   r2   r3   rt   )  s   


zShiftReduceApp.destroyc                 G   s.   | j  | j¡ d| jd< d| jd< |  ¡  d S )Nz	Reset Appr   r   )r   Z
initializer   r-   r¢   r¿   rÚ   r2   r2   r3   r,   /  s   

zShiftReduceApp.resetc                 G   sV   |   ¡ rdS |  ¡ rdS t| j ¡ ƒrd| jd< d| jd< d S d| jd< d| jd< d S )NTz	Finished:r   ZSuccessZFailure)rw   rv   rZ   r   Zparsesr-   r¢   rÚ   r2   r2   r3   ru   5  s   

zShiftReduceApp.stepc                 G   s`   | j rd S | j ¡ r.| j ¡ d }d| jd< d| | jd< | j ¡ r(|  ¡  dS |  	¡  dS dS )NrV   zShift:r   z%rTF)
r   r   rv   rÆ   r-   r¢   r    r<   Ú_animate_shiftr¿   )r.   rm   rÑ   r2   r2   r3   rv   B  s   


ÿzShiftReduceApp.shiftc                 G   sR   | j rd S | j ¡ }|r'd| jd< d| | jd< | j ¡ r#|  ¡  |S |  ¡  |S )NúReduce:r   ú%s)	r   r   rw   r-   r¢   r    r<   Ú_animate_reducer¿   )r.   rm   rg   r2   r2   r3   rw   P  s   


ÿzShiftReduceApp.reducec                 G   s$   | j rd S | j ¡ r|  ¡  d S d S rµ   )r   r   rx   r¿   rÚ   r2   r2   r3   rx   ]  s
   
ÿzShiftReduceApp.undoc                 G   s   | j  ¡  d S rµ   )rª   Zprint_to_filerÚ   r2   r2   r3   ry   c  s   zShiftReduceApp.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Úkwargsr2   r2   r3   rß   f  s   zShiftReduceApp.mainloopNc                 C   sf   |d ur
| j  |¡ | j  ¡ }| jjt|ƒ d | jjt|ƒ d | jjt|ƒ d |  ¡  d S )N)r6   )	r;   r!   r<   r>   Ú	configureÚabsr=   r:   r¿   )r.   r6   r2   r2   r3   r–   u  s   
	zShiftReduceApp.resizec              	   G   sJ   zt | jdtpd ¡ ddd W d S    t | jdtpd ¡ dd Y d S )Nz%Help: Shift-Reduce Parser Applicationr   éK   Úfixed)r    r5   ©r    )r   r   Ú__doc__ÚstriprÚ   r2   r2   r3   rz   †  s   
û
üzShiftReduceApp.helpc                 G   sF   d}d}zddl m} |||d ¡  W d S    t| j||ƒ Y d S )Nz<NLTK Shift-Reduce Parser Application
Written by Edward Loperz&About: Shift-Reduce Parser Applicationr   )ÚMessage)Úmessager   )Ztkinter.messageboxré   Úshowr   r   )r.   rm   ZABOUTZTITLEré   r2   r2   r3   r—   ˜  s   zShiftReduceApp.aboutc                 G   s   t | j| j ¡ | jƒ d S rµ   )r   r   r   r/   Úset_grammarrÚ   r2   r2   r3   r{   ¢  s   zShiftReduceApp.edit_grammarc                 C   sJ   | j  |¡ t| ¡ ƒ| _| j dd¡ | jD ]}| j dd| ¡ qd S )Nr   rO   rP   )r   rì   rZ   r[   r\   rY   Údeleter]   )r.   r/   rg   r2   r2   r3   rì   ¥  s   
ÿzShiftReduceApp.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.   rm   Zsentencer   Úinstrr2   r2   r3   r|   ¬  s   zShiftReduceApp.edit_sentencec                 C   s   |  ¡ | _|  ¡  d S rµ   )rÃ   r   r,   )r.   r0   r2   r2   r3   rð   ²  s   
zShiftReduceApp.set_sentencec                 G   sN   | j  ¡ r| jjddd| jd d| jd< n
| j ¡  d| jd< d| jd< d S )	Nr@   rA   rB   )rC   rD   rE   Úafterr   r   zHide Grammarr   )r"   r<   rW   rX   r¡   r-   Zpack_forgetr¢   rÚ   r2   r2   r3   r•   º  s   

ÿ

zShiftReduceApp._toggle_grammarc                 C   s°   | j  ¡ }t|ƒdkrd S t|d ƒ}| j | j| ¡}|r;d| jd< d| | jd< | j	 
¡ r5|  ¡  d S |  ¡  d S | j  dd¡ | j ¡ D ]}| j |¡}| j  |¡ qGd S )Nr   r   rÜ   r   rÝ   rO   )rY   Úcurselectionr`   r·   r   rw   r\   r-   r¢   r    r<   rÞ   r¿   rÕ   rÖ   r×   rØ   )r.   ÚeventÚ	selectionr×   rg   rÙ   r2   r2   r3   rb   Å  s    


þzShiftReduceApp._prodlist_selectc                 C   sf   | j  ¡ }t|ƒdkrd S | j dd¡ |D ]}| jjt|ƒ| jd q| j | j	 
¡ | j	 ¡ ¡ d S )Nr   rO   )r‡   rT   )r   rÖ   r`   r+   rí   r”   Ústrrw   Úpostr*   Zwinfo_pointerxZwinfo_pointery)r.   rÀ   r[   rg   r2   r2   r3   rÈ   Ù  s   
ÿzShiftReduceApp._popup_reducec                 C   sl   | j d }| ¡ d }t| jƒdkrd}n| jd  ¡ d d }| j ¡ }|| d | }|  |||¡ d S )Nr   r‹   rV   rB   r   g      ð?)r­   r¾   r`   r¬   r    r<   Ú_animate_shift_frame)r.   rÀ   rL   rA   ÚdtÚdxr2   r2   r3   rÛ   ê  s   

zShiftReduceApp._animate_shiftc                 C   sh   |dkrd| _ | |d¡ | j d| j|d ||¡ d S | jd= | j |¡ d| _ |  |¡ |  	¡  d S )Nr   r   r   )
r   rÅ   r   rò   rø   r­   r¬   rÉ   rÔ   rË   )r.   ÚframerÀ   rú   r2   r2   r3   rø   ú  s   
z#ShiftReduceApp._animate_shift_framec                 C   s‚   t | j ¡ d ƒ}| j| d … }t|d tƒr#d|d  ¡  ¡  }nd|d  ¡  }| j 	¡ }|d | }|  
|d ||¡ d S )NrV   r   é   g       @rB   )r`   r   rÆ   r¬   rÇ   r   r‡   rR   r    r<   Ú_animate_reduce_frame)r.   Z
numwidgetsÚwidgetsZydistrù   Údyr2   r2   r3   rÞ     s   
zShiftReduceApp._animate_reducec                 C   s.  |dkr!d| _ |D ]}| d|¡ q	| j d| j|d ||¡ d S | jt|ƒ d …= |D ]}| j |¡ q,| j	 
¡ d }t|tƒsDtƒ ‚t| jt| ¡ ƒd| jd}t| j||dd}| j ¡ \}}}	}
|
| d }| jspd	}n| jd  ¡ d d }| j |||¡ | j |¡ |  |¡ |  ¡  d| _ d S )
Nr   r   r   rV   r½   r¨   rB   ræ   r‹   )r   rÅ   r   rò   rý   r¬   r`   rª   Zremove_widgetr   rÆ   rÇ   r   Ú
ValueErrorr   r*   rö   r‡   r=   r   r±   r¾   r³   rÉ   rÔ   rË   )r.   rû   rþ   rÿ   rÀ   rÑ   r‡   rÍ   rÎ   rÏ   rÐ   rU   r˜   r2   r2   r3   rý     s2   
ÿ

z$ShiftReduceApp._animate_reduce_framec                 C   s’   | j  |j¡}| j|krd S |  ¡  dd„ | j  ¡ D ƒ}||v rDt| j|  ¡ ƒ}| j	| d … D ]}t
|tƒr?d| ¡ d< q1d|d< q1|| _d S )Nc                 S   s   g | ]}t |ƒ‘qS r2   r¶   ©r¸   Úsr2   r2   r3   Ú
<listcomp>d  s    z3ShiftReduceApp._highlight_hover.<locals>.<listcomp>z#00a000r©   )rY   ZnearestrU   rc   re   ró   r`   r\   Úrhsr¬   rÇ   r   r‡   )r.   rô   r×   rõ   ZrhslenrÌ   r2   r2   r3   rd   Y  s   



zShiftReduceApp._highlight_hoverc                 G   sD   | j dkrd S d| _ | jD ]}t|tƒrd| ¡ d< qd|d< qd S )NrV   r€   r©   )rc   r¬   rÇ   r   r‡   )r.   rô   rÌ   r2   r2   r3   re   p  s   



üzShiftReduceApp._clear_hover)r   rµ   )'Ú__name__Ú
__module__Ú__qualname__rç   r4   r#   r(   r$   r&   r%   r'   r)   r¿   rÔ   rË   rt   r,   ru   rv   rw   rx   ry   rß   r–   rz   r—   r{   rì   r|   rð   r•   rb   rÈ   rÛ   rø   rÞ   rý   rd   re   r2   r2   r2   r3   r   W   sL    

*)"$ !W

>r   c                     s,  ddl m} m‰ m} d}‡ fdd„| ¡ D ƒ\	}}}}}}}	}
}||||gƒ||||gƒ||||gƒ||||gƒ|||
||gƒ|||
|gƒ||||gƒ||dgƒ||dgƒ||dgƒ||d	gƒ||
d
gƒ||dgƒ||dgƒ||dgƒ||dgƒ||dgƒ||dgƒf}| ||ƒ}d ¡ }t||ƒ ¡  dS )zP
    Create a shift reduce parser app, using a simple grammar and
    text.
    r   )ÚCFGÚNonterminalÚ
ProductionzS VP NP PP P N Name V Detc                 3   s    | ]}ˆ |ƒV  qd S rµ   r2   r  ©r	  r2   r3   rº   …  r»   zapp.<locals>.<genexpr>ÚIZthern   ÚmanZsawÚinÚwithZparkÚdogZstatueZmyz*my dog saw a man in the park with a statueN)Znltk.grammarr  r	  r
  rÃ   r   rß   )r  r
  ZnonterminalsÚSZVPZNPZPPÚPÚNÚNameÚVZDetr[   r/   r0   r2   r  r3   Úapp|  s2   (










ì
r  Ú__main__N)rç   Útkinterr   r   r   r   r   r   r   r	   Ztkinter.fontr
   Z	nltk.drawr   r   r   Znltk.draw.utilr   r   r   r   Z
nltk.parser   Z	nltk.treer   Z	nltk.utilr   r   r  r  Ú__all__r2   r2   r2   r3   Ú<module>   s(   (9      +*
