o
    i                     @   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   
      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 r3   P/var/www/edux/Edux_v2/venv/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)r6   z*Fontsize	helveticabold)familyweightr7   )r:   r7   )r
   r   _sysfont
option_addr   _sizer"   cgetget	_boldfont_font)r/   rootr3   r3   r4   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)r6   r   singlegroovewhitez#909090#004040z#c0f0c0)
selectmoderelief
background
foregroundr6   selectforegroundselectbackgroundrightr   rH   rG   expandend %s   )heightvertical)orient)yscrollcommand)commandy)rH   rG   z<<ListboxSelect>>z<Motion>z<Leave>)r   
_prodframepackr   rA   _prodlist_labelr   rB   	_prodlistlistr   r0   productions_productionsinsertconfigminlenr   r"   yviewbind_prodlist_select_hover_highlight_hover_clear_hover)r/   parent	listframe
production
listscrollr3   r3   r4   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ar3   r3   r4   <lambda>      
 z/ShiftReduceApp._init_bindings.<locals>.<lambda>=c                 S   rw   )Nr   ry   rz   r3   r3   r4   r}      r~   +c                 S   rw   )N   ry   rz   r3   r3   r4   r}      r~   )r   rm   destroystepshiftreducer-   undo
postscripthelpedit_grammaredit_sentencer!   )r/   r3   r3   r4   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)rG   rH   Stepz#90c0d0black)r   rP   rQ   r^   rE   rH   Shiftr   z#90f090)r   	underlinerP   rQ   r^   ReduceUndoz#f0a0a0)r   _buttonframerb   r   r   r   r   r   )r/   rr   buttonframer3   r3   r4   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   r^   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   variabler^   Tinyr   )r   r   r   valuer^   Small   Medium   Large   Huge   ViewzNo Animation)r   r   r   r   zSlow Animationrx   rv   )r   r   r   r   r   zNormal Animationr   zFast Animationr   r   AnimateAbout)r   r   r^   InstructionsF1Help)r   )r   add_commandr-   r   r   add_cascader   r   r   add_separatorr   r   r   add_checkbuttonr#   _toggle_grammaradd_radiobuttonr>   resizer!   aboutr   ri   )	r/   rr   menubarfilemenueditmenurulemenuviewmenuanimatemenuhelpmenur3   r3   r4   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      )rG   rH   rI   padyzLast Operation:)r   r6   rE   r   sunkenr   )rO   borderrT   r   )rG   rH   rV   rI   z#007070z#f0f0f0)rQ   rP   r6   w   rM   )anchorwidthrQ   rP   r6   rU   )r   _feedbackframerb   r   rB   _lastoper_labelr.   
_lastoper2)r/   rr   feedbackframelastoperframer3   r3   r4   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 )NrL   i  r   rF   r   )rP   r   closeenoughr   rO   r   rD   top)rV   rG   rH   r   r   z#c0f0f0r   )rG   outline.)dashz#408080)rG   r   StackrM   colorr6   zRemaining Text)r   _cframerb   canvasr+   _stackwidgets_rtextwidgetscreate_rectangle	_titlebarcreate_line	_exprline	_stacktopr>   r@   r   rA   _stacklabel_rtextlabel
add_widget)r/   rr   r   r7   r3   r3   r4   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rd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 }qt| 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cr3   r3   r4   	<genexpr>      z)ShiftReduceApp._redraw.<locals>.<genexpr>r   ixr     r   rF   r   r   z#4080a0#006060)
tree_color
tree_width	node_font
node_color
leaf_color	leaf_fontz#000000r   r   rY   c                 S   s(   |   d |k r|  d S |  d S )Nr   )bboxr   _redraw)widgetmidxr/   r3   r3   r4   
drag_shift  s   z*ShiftReduceApp._redraw.<locals>.drag_shift)"r+   splitr   r   destroy_widgetr   r   r   coordsr   r   mover   r   stack
isinstancer   rA   rB   r   r   r   
bind_click_popup_reduceappendr   remaining_textrk   r   maxr   	bind_dragr   _highlight_productions)r/   r   cx1cy1cx2cy2stackwidgetrtextwidgetx1y1x2y2r_   stackxtokattribsr   
rtextwidthrlabelwidthr   r   r3   r3   r4   r     sp   

&

 zShiftReduceApp._redrawc                 C   s*   |  d d }| j| j|d|d d S )NrF   2   r   r   )r   r+   r   r   )r/   r   r   r3   r3   r4   _draw_stack_top  s   zShiftReduceApp._draw_stack_topc                 C   s:   | j dd | j D ]}| j|}| j | qd S )Nr   rW   )rd   selection_clearr   reducible_productionsrg   indexselection_set)r/   prodr  r3   r3   r4   r    s
   z%ShiftReduceApp._highlight_productionsc                 G   s"   | j d u rd S | j   d | _ d S r   )r   r   r/   r{   r3   r3   r4   r   )  s   


zShiftReduceApp.destroyc                 G   s.   | j | j d| jd< d| jd< |   d S )Nz	Reset Appr   r   )r   
initializer   r.   r   r   r  r3   r3   r4   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   SuccessFailure)r   r   re   r   parsesr.   r   r  r3   r3   r4   r   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 )Nr`   zShift:r   z%rTF)
r    r   r   r   r.   r   r!   r@   _animate_shiftr   )r/   r{   r  r3   r3   r4   r   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   r   r.   r   r!   r@   _animate_reducer   )r/   r{   rt   r3   r3   r4   r   P  s   


zShiftReduceApp.reducec                 G   s$   | j rd S | j r|   d S d S r   )r    r   r   r   r  r3   r3   r4   r   ]  s
   
zShiftReduceApp.undoc                 G   s   | j   d S r   )r   print_to_filer  r3   r3   r4   r   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kwargsr3   r3   r4   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)r7   )	r>   r"   r@   rB   	configureabsrA   r<   r   )r/   r7   r3   r3   r4   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   r6   r   )r   r   __doc__stripr  r3   r3   r4   r     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   )tkinter.messageboxr0  showr   r   )r/   r{   ABOUTTITLEr0  r3   r3   r4   r     s   zShiftReduceApp.aboutc                 G   s   t | j| j | j d S r   )r   r   r   r0   set_grammarr  r3   r3   r4   r     s   zShiftReduceApp.edit_grammarc                 C   sJ   | j | t| | _| jdd | jD ]}| jdd|  qd S )Nr   rW   rX   )r   r6  re   rf   rg   rd   deleterh   )r/   r0   rt   r3   r3   r4   r6    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/   r{   sentencer   instrr3   r3   r4   r     s   zShiftReduceApp.edit_sentencec                 C   s   |  | _|   d S r   )r   r   r-   )r/   r1   r3   r3   r4   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 )	NrD   rE   rF   )rG   rH   rI   afterr   r   zHide Grammarr   )r#   r@   ra   rb   r   r.   pack_forgetr   r  r3   r3   r4   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#  rW   )rd   curselectionrk   r   r   r   rg   r.   r   r!   r@   r$  r   r  r  r  r  )r/   event	selectionr  rt   r  r3   r3   r4   rn     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   rW   )r   r^   )r   r  rk   r,   r7  r   strr   postr+   winfo_pointerxwinfo_pointery)r/   r   rf   rt   r3   r3   r4   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   r`   rF   r   g      ?)r   r   rk   r   r!   r@   _animate_shift_frame)r/   r   rT   rE   dtdxr3   r3   r4   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=  rF  r   r   r  r  r  )r/   framer   rH  r3   r3   r4   rF    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 )Nr`   r      g       @rF   )rk   r   r   r   r   r   r   rZ   r!   r@   _animate_reduce_frame)r/   
numwidgetswidgetsydistrG  dyr3   r3   r4   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   r`   r   r   rF   r-  r   )r    r   r   r=  rK  r   rk   r   remove_widgetr   r   r   r   
ValueErrorr   r+   rB  r   rA   r   r   r   r   r  r  r  )r/   rI  rM  rO  r   r  r   r  r  r  r  r_   r   r3   r3   r4   rK    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 r3   r   r   sr3   r3   r4   
<listcomp>d  s    z3ShiftReduceApp._highlight_hover.<locals>.<listcomp>z#00a000r   )rd   nearestr_   ro   rq   r?  rk   rg   rhsr   r   r   r   )r/   r@  r  rA  rhslenr
  r3   r3   r4   rp   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 )Nr`   r   r   )ro   r   r   r   r   )r/   r@  r
  r3   r3   r4   rq   p  s   



zShiftReduceApp._clear_hover)r   r   )'__name__
__module____qualname__r.  r5   r$   r)   r%   r'   r&   r(   r*   r   r  r  r   r-   r   r   r   r   r   r&  r   r   r   r   r6  r   r:  r   rn   r   r!  rF  r$  rK  rp   rq   r3   r3   r3   r4   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   r3   rR  r\  r3   r4   r     r   zapp.<locals>.<genexpr>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Detrf   r0   r1   r3   r^  r4   app|  s2   (











rt  __main__N)r.  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   rt  rX  __all__r3   r3   r3   r4   <module>   s(   (9      +*
