
    g                         d Z ddlmZmZmZmZmZmZmZm	Z	 ddl
mZ ddlmZmZmZ ddlmZmZmZmZ ddlmZ ddlmZ ddlmZ 	  G d	 d
      Zd Zedk(  r e        dgZy)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                       e Zd ZdZd&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d Zd Zd Zd'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&y)(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.
    c                 D   || _         t        ||      | _        t               | _        | j                  j                  d       d| _        t        | j                        | _        | j                  j                  d       t        | j                        | _
        | j                  j                  d       | j                  | j                         | j                          | j                  | j                         | j                  | j                         | j                  | j                         | j!                  | j                         | j#                  | j                         t%        | j&                  d      | _        | j+                          d| j,                  d<   y )NzShift Reduce Parser Applicationr   
      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traces       J/var/www/openai/venv/lib/python3.12/site-packages/nltk/app/srparser_app.py__init__zShiftReduceApp.__init__b   s2   
0%@ D			9:  !tyy)" $DII.q! 	# 	 	499%499%DII&499%$))$ !q9 	

"$    c                    t        t               d         | _        |j                  d| j                         t	        |      | _        | j
                  j                  | j                  j                  d             t        dd| j
                  j                               | _	        t        d| j
                  j                               | _
        y )	Nfont)r:   z*Fontsize	helveticabold)familyweightr;   )r>   r;   )r   r   _sysfont
option_addr   _sizer%   cgetget	_boldfont_font)r2   roots     r6   r'   zShiftReduceApp._init_fonts   s    &(6"23/ D\


t}}))&12[djjnnFVW4::>>3CD
r8   c           
         t        |      x| _        }| j                  j                  ddd       t        | j                  | j                  d      | _        | j
                  j                          t        | j                  ddd	d
| j                  dd      | _        | j                  j                  ddd       t        | j                  j                         j                               | _        | j                  D ]!  }| j                  j                  dd|z         # | j                  j                  t!        t#        | j                        d             	 t%        | j                  d      }| j                  j                  |j&                         |j                  | j                  j(                         |j                  dd       | j                  j+                  d| j,                         d| _        | j                  j+                  d| j0                         | j                  j+                  d| j2                         y )Nbothleft   )fillsidepadxzAvailable Reductions)r:   r   singlegroovewhitez#909090#004040z#c0f0c0)
selectmoderelief
background
foregroundr:   selectforegroundselectbackgroundrightr   rM   rL   expandend %s   )heightvertical)orient)yscrollcommand)commandy)rM   rL   z<<ListboxSelect>>z<Motion>z<Leave>)r   
_prodframepackr   rE   _prodlist_labelr   rF   	_prodlistlistr    r3   productions_productionsinsertconfigminlenr	   r%   yviewbind_prodlist_select_hover_highlight_hover_clear_hover)r2   parent	listframe
production
listscrolls        r6   r,   zShiftReduceApp._init_grammar   s   &+Fm3)&vA>$OO$..7M 
 	!!# OO &&	
 	va@ !5!5!7!C!C!EF++JNN!!%%**<> ,ST->->)?%DE "4??:FJNN!!!@dnn&:&:;OOcO2 	/1F1FG J(=(=>It'8'89r8   c                    | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d	| j
                         | j                   j                  d
| j
                         | j                   j                  d| j
                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                  fd       | j                   j                  d| j                  fd       | j                   j                  d| j                  fd       y )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                 $    |j                  d      S )N   r%   eas     r6   <lambda>z/ShiftReduceApp._init_bindings.<locals>.<lambda>       quuRyr8   =c                 $    |j                  d      S )Nr   r   r   s     r6   r   z/ShiftReduceApp._init_bindings.<locals>.<lambda>   r   r8   +c                 $    |j                  d      S )N   r   r   s     r6   r   z/ShiftReduceApp._init_bindings.<locals>.<lambda>   s    quuQxr8   )r!   rr   destroystepshiftreducer0   undo
postscripthelpedit_grammaredit_sentencer$   )r2   s    r6   r(   zShiftReduceApp._init_bindings   s;   		}dll3		}dll3		y$,,/		y$,,/ 			y$)),		udjj)		y$**-		}djj1		udkk*		y$++.		}dkk2		z4::.		udii(		y$)),		}dii0		}dii0		}dii0 			}doo6		}dii0		vtyy)		}d&7&78		}d&8&89 			s@A		s@A		s?@r8   c                    t        |      x| _        }|j                  dd       t        |ddd| j                        j                  d	       t        |d
ddd| j
                        j                  d	       t        |dddd| j                        j                  d	       t        |dddd| j                        j                  d	       y )Nnonebottom)rL   rM   Stepz#90c0d0black)r   rU   rV   rc   rJ   rM   Shiftr   z#90f090)r   	underlinerU   rV   rc   ReduceUndoz#f0a0a0)r   _buttonframerg   r   r   r   r   r   )r2   rw   buttonframes      r6   r*   zShiftReduceApp._init_buttons   s    */-7Kf84 II	
 $F$
 JJ	
 $F$
 KK	
 $F$
 II	
 $F$
r8   c                    t        |      }t        |d      }|j                  dd| j                  d       |j                  dd| j                  d       |j                  dd	| j                  d
       |j                  dd|       t        |d      }|j                  dd| j                  d       |j                  dd| j                  d       |j                  dd|       t        |d      }|j                  dd	| j                  d       |j                          |j                  dd| j                  d       |j                  dd| j                  d       |j                          |j                  dd| j                  d       |j                  dd|       t        |d      }|j                  dd| j                  | j                         |j                          |j!                  d| j"                  dd| j$                          |j!                  d!| j"                  dd"| j$                          |j!                  d#| j"                  dd$| j$                          |j!                  d%| j"                  dd&| j$                          |j!                  d'| j"                  dd(| j$                          |j                  d)d|       t        |d      }|j!                  d*d| j&                  d+       |j!                  d,d| j&                  d-d./       |j!                  d0d| j&                  dd1/       |j!                  d2d| j&                  d3d4/       |j                  d5d	|       t        |d      }|j                  d6d| j(                  7       |j                  d8d| j*                  d9       |j                  d:d|       |j-                  |;       y )<Nr   r   zReset ParserDel)labelr   rc   acceleratorzPrint to PostscriptzCtrl-pExitr   zCtrl-xFile)r   r   menuzEdit Grammar   zCtrl-g	Edit TextzCtrl-tEditr   Spacer   zCtrl-sr   zCtrl-rr   zCtrl-uApplyShow Grammar)r   r   variablerc   Tinyr   )r   r   r   valuerc   Small   Medium   Large   Huge   ViewzNo Animation)r   r   r   r   zSlow Animationr~   r|   )r   r   r   r   r   zNormal Animationr   zFast Animationr   r   AnimateAbout)r   r   rc   InstructionsF1Help)r   )r   add_commandr0   r   r   add_cascader   r   r   add_separatorr   r   r   add_checkbuttonr&   _toggle_grammaradd_radiobuttonrB   resizer$   aboutr   rn   )	r2   rw   menubarfilemenueditmenurulemenuviewmenuanimatemenuhelpmenus	            r6   r)   zShiftReduceApp._init_menubar  s    v,+ Atzzu 	 	
 	'OO 	 	 	
 	At|| 	 	
 	&AHE+ %% 	 	 	
 	&& 	 	 	
 	&AHE+Atyyg 	 	
 	 Q

 	 	
 	a( 	 	
 	 Atyyh 	 	
 	'QXF+   ''((	 	! 	
 	   ZZKK 	! 	
 	  ZZKK 	! 	
 	  ZZKK 	! 	
 	  ZZKK 	! 	
 	  ZZKK 	! 	
 	&AHE7A.## AQ 	$ 	
 	##"]] 	$ 	
 	##$]] 	$ 	
 	##"]] 	$ 	
 	)q{K+7aL Atyyd 	 	
 	&AHE7#r8   c                    t        |      x| _        }|j                  dddd       t        |d| j                        | _        | j
                  j                  d       t        |d	d
      }|j                  ddd
d       t        |dd| j                        | _        t        |dddd| j                        | _        | j                  j                  d       | j                  j                  ddd
       y )Nxr      )rL   rM   rN   padyzLast Operation:)r   r:   rJ   r   sunkenr   )rT   borderrY   r   )rL   rM   r[   rN   z#007070z#f0f0f0)rV   rU   r:   w   rR   )anchorwidthrV   rU   r:   rZ   )r   _feedbackframerg   r   rF   _lastoper_labelr1   
_lastoper2)r2   rw   feedbackframelastoperframes       r6   r+   zShiftReduceApp._init_feedback  s    .3Fm;m(C$ 1

 
 	!!v!.mHQG'!!DiIDJJ
    
 	&)&s1=r8   c                    t        |ddddd      | _        | j                  j                  ddd	d
       | j                  j                         x}| _        g | _        g | _        |j                  dddddd      | _        |j                  ddddd      | _
        |j                  ddddd      | _        | j                  j                         dz   }t        |dd| j                        | _        t        |dd| j                        | _        | j                  j%                  | j                          | j                  j%                  | j"                         y )NrQ   i  r   rK   r   )rU   r   closeenoughr   rT   r   rI   top)r[   rL   rM   r   r   z#c0f0f0r   )rL   outline.)dashz#408080)rL   r   StackrR   colorr:   zRemaining Text)r   _cframerg   canvasr.   _stackwidgets_rtextwidgetscreate_rectangle	_titlebarcreate_line	_exprline	_stacktoprB   rD   r   rE   _stacklabel_rtextlabel
add_widget)r2   rw   r   r;   s       r6   r-   zShiftReduceApp._init_canvas  sD   "
 	e!D $ 3 3 5500q!QY 1 
  ++Aq!QS+A++Aq!QY+Gzz~~!#%G94>>
 &$IDNN
 	 0 01 0 01r8   c                 
   | j                   d   j                         }d |D        \  }}}}| j                  D ]  }| j                  j	                  |        g | _        | j
                  D ]  }| j                  j	                  |        g | _        | j                  j                         \  }}	}
}||	z
  dz   }| j                   j                  | j                  ddd|dz
         | j                   j                  | j                  d|dz  dz
  d|dz  dz
         | j                  j                         \  }}	}
}| j                  j                  d	|z
  d
|	z
         | j                  j                         \  }}	}
}| j                  j                  ||
z
  d	z
  d
|	z
         d	}| j                  j                         D ]  }t        |t               rHdd| j"                  dd| j$                  d}t'        | j                   |fi |}d|j)                         d<   n#t+        | j                   |d| j$                        }|j-                  | j.                         | j                  j1                  |       | j                  j3                  |||       |j                         d   dz   } d}| j                  j5                         D ]s  }t+        | j                   |d| j$                        }| j
                  j1                  |       | j                  j3                  |||       |j                         d   dz   }u t7        | j
                        dkD  r || j
                  d   j9                         z  }t;        || j                  j9                         dz         }| j                  j9                         dz   }||t;        ||      z
  k\  r|t;        ||      z   }| j
                  D ]  }|j                  d|z   |z
  d        | j                  j                  || j                  j                         d   z
  d	z
  d       ||z   t;        ||      z
  dz  }| j                   j                  | j<                  |d|d       | j                  j                         \  }}	}
}t7        | j
                        dkD  rL|| fd}| j
                  d   j?                  |       | j
                  d   j-                  | j@                         | jC                          y )Nscrollregionc              3   2   K   | ]  }t        |        y wN)int).0cs     r6   	<genexpr>z)ShiftReduceApp._redraw.<locals>.<genexpr>  s     =1As   r   ixr     r   rK   r   r   z#4080a0#006060)
tree_color
tree_width	node_font
node_color
leaf_color	leaf_fontz#000000r   r   r^   c                 r    | j                         d   |k  r|j                          y |j                          y )Nr   )bboxr   _redraw)widgetmidxr2   s      r6   
drag_shiftz*ShiftReduceApp._redraw.<locals>.drag_shift  s'    ;;=#d*JJLLLNr8   )"r.   splitr   r   destroy_widgetr   r   r  coordsr   r   mover   r    stack
isinstancer   rE   rF   r   r   r   
bind_click_popup_reduceappendr   remaining_textrp   r   maxr   	bind_dragr   _highlight_productions)r2   r   cx1cy1cx2cy2stackwidgetrtextwidgetx1y1x2y2rd   stackxtokattribsr  
rtextwidthrlabelwidthr  r  s                        r6   r  zShiftReduceApp._redraw  st   ||N399;==c3  --KLL''4 .--KLL''4 .  ++002RRGbLDNNE1dAEBDNNAq1urz4QL  ++002RRa"fa"f-++002RRcBhlAF3 <<%%'C#t$"+"#!%"+"+!% -T\\3J'J*3w'#DLL#YTZZXd001%%f-LL##FFA6[[]1%*F# (( 
<<..0CcTF%%f-LL##FJ:q)A-J	 1 t!!"Q&d((+1133F
 VT--335:;&&,,.3S3z;7773z;77C--KQWz115 .cD$4$4$9$9$;A$>>BAFs:{;;q@DNND!T4@++002RR t!!"Q&(,4 # q!++J7q!,,TZZ8 	##%r8   c                     |j                         d   dz   }| j                  j                  | j                  |d|d       y )NrK   2   r   r   )r  r.   r  r   )r2   r  r  s      r6   _draw_stack_topzShiftReduceApp._draw_stack_top  s5    {{}Q"$DNND!T4@r8   c                     | j                   j                  dd       | j                  j                         D ]8  }| j                  j                  |      }| j                   j                  |       : y )Nr   r\   )ri   selection_clearr    reducible_productionsrl   indexselection_set)r2   prodr(  s      r6   r  z%ShiftReduceApp._highlight_productions  sU    &&q%0LL668D%%++D1ENN((/ 9r8   c                 `    | j                   y | j                   j                          d | _         y r   )r!   r   r2   r   s     r6   r   zShiftReduceApp.destroy)  s&    99			r8   c                     | j                   j                  | j                         d| j                  d<   d| j                  d<   | j                          y )Nz	Reset Appr   r   )r    
initializer   r1   r   r  r,  s     r6   r0   zShiftReduceApp.reset/  s=    

+"-"$r8   c                    | j                         ry| j                         ryt        | j                  j	                               rd| j
                  d<   d| j                  d<   y d| j
                  d<   d| j                  d<   y )NTz	Finished:r   SuccessFailure)r   r   rj   r    parsesr1   r   r,  s     r6   r   zShiftReduceApp.step5  sg    ;;=ZZ\DLL'')**5'*3'*5'*3'r8   c                 F   | j                   ry | j                  j                         rz| j                  j                         d   }d| j                  d<   d|z  | j
                  d<   | j                  j                         r| j                          y| j                          yy)Nre   zShift:r   z%rTF)
r#   r    r   r
  r1   r   r$   rD   _animate_shiftr  )r2   r   r  s      r6   r   zShiftReduceApp.shiftB  s    <<,,$$&r*C&.DOOF#&*SjDOOF#}}  "##%  r8   c                    | j                   ry | j                  j                         }|r]d| j                  d<   d|z  | j                  d<   | j
                  j                         r| j                          |S | j                          |S )NReduce:r   %s)	r#   r    r   r1   r   r$   rD   _animate_reducer  )r2   r   ry   s      r6   r   zShiftReduceApp.reduceP  sy    \\((*
&/DOOF#&*Z&7DOOF#}}  "$$&  r8   c                 t    | j                   ry | j                  j                         r| j                          y y r   )r#   r    r   r  r,  s     r6   r   zShiftReduceApp.undo]  s,    <<LLN r8   c                 8    | j                   j                          y r   )r   print_to_filer,  s     r6   r   zShiftReduceApp.postscriptc  s    ""$r8   c                 R    t               ry | j                  j                  |i | y)z
        Enter the Tkinter mainloop.  This function must be called if
        this demo is created from a non-interactive program (e.g.
        from a secript); otherwise, the demo will close as soon as
        the script completes.
        N)r   r!   mainloop)r2   argskwargss      r6   r=  zShiftReduceApp.mainloopf  s%     9		D+F+r8   Nc                 v   || j                   j                  |       | j                   j                         }| j                  j	                  t        |              | j                  j	                  t        |              | j                  j	                  t        |              | j                          y )N)r;   )	rB   r%   rD   rF   	configureabsrE   r@   r  )r2   r;   s     r6   r   zShiftReduceApp.resizeu  s    JJNN4 zz~~

CI,/  D	l 3s4y\2 	r8   c                     	 t        | j                  dt        xs dj                         dd       y #  t        | j                  dt        xs dj                         d       Y y xY w)Nz%Help: Shift-Reduce Parser Applicationr   K   fixed)r   r:   r   )r   r!   __doc__stripr,  s     r6   r   zShiftReduceApp.help  s[    			7B%%'			7B%%'	s	   03 1A&c                     d}d}	 ddl m}  |||      j                          y #  t        | j                  ||       Y y xY w)Nz<NLTK Shift-Reduce Parser Application
Written by Edward Loperz&About: Shift-Reduce Parser Applicationr   )Message)messager"   )tkinter.messageboxrJ  showr   r!   )r2   r   ABOUTTITLErJ  s        r6   r   zShiftReduceApp.about  s>    T8	.2E/446	.TYYu-s	   % A c                 v    t        | j                  | j                  j                         | j                         y r   )r   r!   r    r3   set_grammarr,  s     r6   r   zShiftReduceApp.edit_grammar  s%    $))T\\113T5E5EFr8   c                    | j                   j                  |       t        |j                               | _        | j
                  j                  dd       | j                  D ]!  }| j
                  j                  dd|z         # y )Nr   r\   r]   )r    rQ  rj   rk   rl   ri   deleterm   )r2   r3   ry   s      r6   rQ  zShiftReduceApp.set_grammar  sg      ) !4!4!67a'++JNN!!%%**<> ,r8   c                     dj                  | j                        }d}d}t        | j                  ||| j                  |       y )N r   zEnter a new sentence to parse.)joinr   r   r!   set_sentence)r2   r   sentencer"   instrs        r6   r   zShiftReduceApp.edit_sentence  s8    88DJJ'0DIIx0A0A5Ir8   c                 N    |j                         | _        | j                          y r   )r  r   r0   )r2   r4   s     r6   rW  zShiftReduceApp.set_sentence  s    ZZ\


r8   c                    | j                   j                         r9| j                  j                  ddd| j                         d| j
                  d<   n)| j                  j                          d| j
                  d<   d| j                  d<   y )	NrI   rJ   rK   )rL   rM   rN   afterr   r   zHide Grammarr   )r&   rD   rf   rg   r   r1   pack_forgetr   r,  s     r6   r   zShiftReduceApp._toggle_grammar  sv    !!#OO  &q8K8K !  '5DOOF#OO'')&4DOOF#"$r8   c                 b   | j                   j                         }t        |      dk7  ry t        |d         }| j                  j                  | j                  |         }|r]d| j                  d<   d|z  | j                  d<   | j                  j                         r| j                          y | j                          y | j                   j                  dd       | j                  j                         D ]8  }| j                  j                  |      }| j                   j!                  |       : y )Nr   r   r6  r   r7  r\   )ri   curselectionrp   r   r    r   rl   r1   r   r$   rD   r8  r  r&  r'  r(  r)  )r2   event	selectionr(  ry   r*  s         r6   rs   zShiftReduceApp._prodlist_select  s    NN//1	y>QIaL!\\(():):5)AB
&/DOOF#&*Z&7DOOF#}}  "$$& NN**1e4::<))//5,,U3 =r8   c                    | j                   j                         }t        |      dk(  ry | j                  j	                  dd       |D ]2  }| j                  j                  t        |      | j                         4 | j                  j                  | j                  j                         | j                  j                                y )Nr   r\   )r   rc   )r    r'  rp   r/   rS  r   strr   postr.   winfo_pointerxwinfo_pointery)r2   r  rk   ry   s       r6   r  zShiftReduceApp._popup_reduce  s    ll88:{q   E*%J))J)U &LL'')4<<+F+F+H	
r8   c                 4   | j                   d   }|j                         d   }t        | j                        dk(  rd}n#| j                  d   j                         d   dz   }| j                  j                         }||z
  dz  |z  }| j                  |||       y )Nr   r   re   rK   r   g      ?)r   r  rp   r   r$   rD   _animate_shift_frame)r2   r  rY   rJ   dtdxs         r6   r4  zShiftReduceApp._animate_shift  s    ##A& a t!!"a'D%%b)..03b8D ]] Ulc!B&!!"fb1r8   c                 :   |dkD  rFd| _         |j                  |d       | j                  j                  d| j                  |dz
  ||       y | j
                  d= | j                  j                  |       d| _         | j                  |       | j                          y )Nr   r   r   )
r#   r	  r!   r\  rh  r   r   r  r$  r  )r2   framer  rj  s       r6   rh  z#ShiftReduceApp._animate_shift_frame  s    19#$D KKAIIOOB 9 9519fbQ
 ""1%%%f-#$D    ('')r8   c                 |   t        | j                  j                         d         }| j                  | d  }t	        |d   t
              r%d|d   j                         j                         z   }nd|d   j                         z   }| j                  j                         }|dz  |z  }| j                  |dz  ||       y )Nre   r      g       @rK   )rp   r    r
  r   r  r   r   r_   r$   rD   _animate_reduce_frame)r2   
numwidgetswidgetsydistri  dys         r6   r8  zShiftReduceApp._animate_reduce  s    ++-b12
$$j[\2 gaj"34))+2244E**,,E ]] S[2""267B7r8   c                    |dkD  rMd| _         |D ]  }|j                  d|        | j                  j                  d| j                  |dz
  ||       y | j
                  t        |       d = |D ]  }| j                  j                  |        | j                  j                         d   }t        |t              s
t               t        | j                  t!        |j#                               d| j$                        }t'        | j                  ||d      }| j(                  j+                         \  }}}	}
|
|z
  dz   }| j
                  sd	}n#| j
                  d   j+                         d   dz   }| j                  j-                  |||       | j
                  j/                  |       | j1                  |       | j3                          d| _         y )
Nr   r   r   re   r   r   rK   rF  r   )r#   r	  r!   r\  ro  r   rp   r   remove_widgetr    r
  r  r   
ValueErrorr   r.   rc  r   rE   r   r   r  r   r  r$  r  )r2   rl  rq  rs  r  r  r   r  r  r  r  rd   r   s                r6   ro  z$ShiftReduceApp._animate_reduce_frame  s   19#$D !Ar" "IIOOB : :EAIwPRS""CL=?3!**62 ",,$$&r*Cc4( l"c#))+.idnnE 't||UG1MF#//446RRR"A%%&&r*//1!4r9LL##FAq1%%f-   ('')8 $%D r8   c                    | j                   j                  |j                        }| j                  |k(  ry | j	                          | j                   j                         D cg c]  }t        |       }}||v rdt        | j                  |   j                               }| j                  | d  D ]+  }t        |t              rd|j                         d<   'd|d<   - || _        y c c}w )Nz#00a000r   )ri   nearestrd   rt   rv   r_  r   rp   rl   rhsr   r  r   r   )r2   r`  r(  sra  rhslenr  s          r6   ru   zShiftReduceApp._highlight_hoverY  s    &&uww/;;% 	 &*^^%@%@%BC%BSV%B	CI**515578F#116'(;k+<=3<K%%'0+4K(	  <  Ds   "C&c                     | j                   dk(  ry d| _         | j                  D ]+  }t        |t              rd|j	                         d<   'd|d<   - y )Nre   r   r   )rt   r   r  r   r   )r2   r`  r  s      r6   rv   zShiftReduceApp._clear_hoverp  sP    ;;"--K+'89/6!!#G,'.G$	 .r8   )r   r   )'__name__
__module____qualname__rG  r7   r'   r,   r(   r*   r)   r+   r-   r  r$  r  r   r0   r   r   r   r   r   r=  r   r   r   r   rQ  r   rW  r   rs   r  r4  rh  r8  ro  ru   rv    r8   r6   r   r   W   s    $%T
E':R AD"HA$F>.2BU&nA
04%	,"$.G?J	%4(
"2 *"8 8%|.	/r8   r   c                  "   ddl m} mm} d}fd|j	                         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j	                         }t        ||      j                          y)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   .   K   | ]  } |        y wr   r  )r   rz  r  s     r6   r   zapp.<locals>.<genexpr>  s     *XCWa;q>CWs   Ither   mansawinwithparkdogstatuemyz*my dog saw a man in the park with a statueN)nltk.grammarr  r  r  r  r   r=  )r  r  nonterminalsSVPNPPPPNNameVDetrk   r3   r4   r  s                  @r6   appr  |  sX    :9.L*X<CUCUCW*X'QBAq$3 	1r2h2Qx 2Bx 2Bx 22r{#22w22w2u3 31ug1ug1tf1vh1vh1ug1xj!3)K. ![!G 8==?D7D!**,r8   __main__r  N)rG  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  r8   r6   <module>r     s]   7r O N N  G G I I 0  
b/ b/J'-T zE'r8   