JL idZddlmZmZmZmZmZmZmZm Z ddl m Z ddl m Z mZmZddlmZmZmZmZddlmZddlmZddlmZ Gd d Zd Zed k(red gZy)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 )ButtonFrameIntVarLabelListboxMenu ScrollbarTk)Font) CFGEditorTreeSegmentWidgettree_to_treesegment) CanvasFrame EntryDialogShowText TextWidget)SteppingShiftReduceParser)Tree)in_idleceZdZdZd&dZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZd'dZdZdZdZdZdZdZdZdZdZ 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. cD||_t|||_t|_|jj dd|_t|j|_|jjdt|j|_ |jjd|j|j|j|j|j|j|j|j|j|j!|j|j#|jt%|j&d|_|j+d|j,d<y)NzShift Reduce Parser Applicationr tearofftext)_sentr_parserr _toptitle_animating_lockr_animateset _show_grammar _init_fonts_init_bindings _init_menubar _init_buttons_init_feedback _init_grammar _init_canvasr_canvas _reduce_menureset _lastoper1)selfgrammarsenttraces [/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/app/srparser_app.py__init__zShiftReduceApp.__init__bs2 0%@ D  9: !tyy)  "$DII. q! #  499% 499% DII& 499% $))$!q9 "$cttd|_|jd|jt ||_|j j |jjdtdd|j j|_ td|j j|_ y) Nfont)r:z*Fontsize helveticabold)familyweightr;)r>r;) r r_sysfont option_addr_sizer%cgetget _boldfont_font)r2roots r6r'zShiftReduceApp._init_fontss&(6"23  /D\  t}}))&12[djjnnFVW4::>>3CD r8c t|x|_}|jjdddt|j|jd|_|j jt |jddd d |jd d |_|jjdddt|jjj|_ |jD]!}|jjdd|z#|jjt!t#|jd t%|jd}|jj|j&|j|jj(|jdd|jj+d|j,d|_|jj+d|j0|jj+d|j2y)Nbothleft)fillsidepadxzAvailable Reductions)r:rsinglegroovewhitez#909090#004040z#c0f0c0) selectmoderelief background foregroundr:selectforegroundselectbackgroundrightrrMrLexpandend %s)heightvertical)orient)yscrollcommand)commandy)rMrLz<>zz)r _prodframepackrrE_prodlist_labelrrF _prodlistlistr r3 productions _productionsinsertconfigminlenr r%yviewbind_prodlist_select_hover_highlight_hover _clear_hover)r2parent listframe production listscrolls r6r,zShiftReduceApp._init_grammars&+Fm3) &vA>$ OO$..7M  !!# OO &&   va@ !5!5!7!C!C!EF++ ?J NN ! !%%**< > ? ST->->)?%DE "4??:FJ NN ! ! ! @   dnn&:&:  ; OOcO 2 /1F1FG  J(=(=> It'8'89r8c|jjd|j|jjd|j|jjd|j|jjd|j|jjd|j|jjd|j|jjd|j|jjd|j|jjd |j |jjd |j |jjd |j |jjd |j |jjd |j|jjd|j|jjd|j|jjd|j|jjd|j|jjd|j|jjd|j|jjd|j|jjd|j|jjd|j|jjd|jfd|jjd|jfd|jjd|jfdy)Nz z zzzzzz zzz zzzz z z z z zz z -c$|jdS)Nr%eas r6z/ShiftReduceApp._init_bindings..quuRyr8=c$|jdS)Nrrrs r6rz/ShiftReduceApp._init_bindings..rr8+c$|jdS)Nrrs r6rz/ShiftReduceApp._init_bindings..squuQxr8) r!rrdestroystepshiftreducer0undo postscripthelp edit_grammar edit_sentencer$)r2s r6r(zShiftReduceApp._init_bindingss; }dll3 }dll3 y$,,/ y$,,/ y$)), udjj) y$**- }djj1 udkk* y$++. }dkk2 z4::. udii( y$)), }dii0 }dii0 }dii0 }doo6 }dii0 vtyy) }d&7&78 }d&8&89 s @A s @A s ?@r8ct|x|_}|jddt|ddd|jjd t|d d d d|j jd t|dd d d|j jd t|dd dd|j jd y)Nnonebottom)rLrMStepz#90c0d0black)rrUrVrcrJrMShiftrz#90f090)r underlinerUrVrcReduceUndoz#f0a0a0)r _buttonframergrrrrr)r2rw buttonframes r6r*zShiftReduceApp._init_buttonss*/-7Kf84  II  $F$   JJ   $F$   KK   $F$   II   $F$ r8ct|}t|d}|jdd|jd|jdd|jd|jdd |jd |j d d| t|d}|jd d|j d|jdd|jd|j dd| t|d}|jdd |jd|j|jdd|jd|jdd|jd|j|jdd|jd|j dd| t|d}|jdd|j|j|j|j!d|j"dd|j$ |j!d!|j"dd"|j$ |j!d#|j"dd$|j$ |j!d%|j"dd&|j$ |j!d'|j"dd(|j$ |j d)d| t|d}|j!d*d|j&d+|j!d,d|j&d-d./|j!d0d|j&dd1/|j!d2d|j&d3d4/|j d5d | t|d}|jd6d|j(7|jd8d|j*d9|j d:d| |j-|;y)> & $IDNN   0 01  0 01r8c |jdj}d|D\}}}}|jD]}|jj |g|_|j D]}|jj |g|_|j j\}} } } | | z dz} |jj|jddd| dz |jj|jd| dzdz d| dzdz |j j\}} } } |j jd |z d | z |jj\}} } } |jj|| z d z d | z d } |jjD]}t|t rHd d|j"d d |j$d }t'|j|fi|}d|j)d<n#t+|j|d|j$}|j-|j.|jj1||jj3|| | |jddz} d}|jj5D]s}t+|j|d|j$}|j j1||jj3||| |jddz}ut7|j dkDr | |j dj9z } t;| |j j9dz} |jj9dz}| |t;||z k\r| t;||z}|j D]}|jd|z|z d|jj||jjdz d z d| |zt;||z dz }|jj|j<|d|d|j j\}} } } t7|j dkDrL||fd}|j dj?||j dj-|j@|jCy)N scrollregionc32K|]}t|ywN)int).0cs r6 z)ShiftReduceApp._redraw..s=1A=srixrrrKrrz#4080a0#006060) tree_color tree_width node_font node_color leaf_color leaf_fontz#000000rrr^cr|jd|kr|jy|jy)Nr)bboxr_redraw)widgetmidxr2s r6 drag_shiftz*ShiftReduceApp._redraw..drag_shift s';;=#d*JJLLLNr8)"r.splitrrdestroy_widgetrrrcoordsrrmoverr stack isinstancerrErFrrr bind_click _popup_reduceappendrremaining_textrprmaxr bind_dragr_highlight_productions)r2rcx1cy1cx2cy2 stackwidget rtextwidgetx1y1x2y2rdstackxtokattribsr rtextwidth rlabelwidthrrs r6rzShiftReduceApp._redraws||N399; = =c3 -- 5K LL ' ' 4 5-- 5K LL ' ' 4 5 ++002RR GbL DNNE1dAEB DNNAq1urz4QL ++002RR a"fa"f-++002RR cBhlAF3<<%%' +C#t$"+"#!%"+"+!% -T\\3J'J*3 w'#DLL#YTZZX   d00 1    % %f - LL # #FFA 6[[]1%*F# +( <<..0 .C cTF    % %f - LL # #FJ :q)A-J  . t!! "Q & d((+113 3F VT--335:;&&,,.3 S3z;77 73z;77C-- 6K   QWz11 5 6 cD$4$4$9$9$;A$>>BAF s:{;;q@ DNND!T4@++002RR t!! "Q &(,4 #   q ! + +J 7   q ! , ,TZZ 8 ##%r8c|jddz}|jj|j|d|dy)NrK2rr)rr.rr)r2rrs r6_draw_stack_topzShiftReduceApp._draw_stack_tops5{{}Q"$ DNND!T4@r8c|jjdd|jjD]8}|jj |}|jj |:y)Nrr\)riselection_clearr reducible_productionsrlindex selection_set)r2prodr(s r6rz%ShiftReduceApp._highlight_productionssZ &&q%0LL668 0D%%++D1E NN ( ( / 0r8c`|jy|jjd|_yr)r!rr2rs r6rzShiftReduceApp.destroy)s& 99    r8c|jj|jd|jd<d|jd<|j y)Nz Reset Apprr)r initializerr1rrr,s r6r0zShiftReduceApp.reset/s=  +"-"$ r8c|jry|jryt|jj rd|j d<d|j d<yd|j d<d|j d<y)NTz Finished:rSuccessFailure)rrrjr parsesr1rr,s r6rzShiftReduceApp.step5sg ;;= ZZ\DLL'')**5'*3'*5'*3'r8cF|jry|jjrz|jjd}d|jd<d|z|j d<|j jr|jy|jyy)NrezShift:rz%rTF) r#r rr r1rr$rD_animate_shiftr)r2rrs r6rzShiftReduceApp.shiftBs     <<   ,,$$&r*C&.DOOF #&*SjDOOF #}}  "##% r8c|jry|jj}|r]d|jd<d|z|jd<|j j r|j|S|j|S)NReduce:r%s) r#r rr1rr$rD_animate_reducer)r2rrys r6rzShiftReduceApp.reducePsy    \\((* &/DOOF #&*Z&7DOOF #}}  "$$& r8ct|jry|jjr|jyyr)r#r rrr,s r6rzShiftReduceApp.undo]s,     <<    LLN r8c8|jjyr)r print_to_filer,s r6rzShiftReduceApp.postscriptcs ""$r8cRtry|jj|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)rr!mainloop)r2argskwargss r6r=zShiftReduceApp.mainloopfs% 9  D+F+r8Ncv||jj||jj}|jj t | |j j t | |jj t | |jy)N)r;) rBr%rDrF configureabsrEr@r)r2r;s r6rzShiftReduceApp.resizeus   JJNN4 zz~~ CI,/   D l 3 s4y\2 r8c t|jdtxsdjddy#t|jdtxsdjdYyxYw)Nz%Help: Shift-Reduce Parser ApplicationrKfixed)rr:r)rr!__doc__stripr,s r6rzShiftReduceApp.helps[   7B%%'     7B%%'  s 031A&cd}d} ddlm}|||jy#t|j||YyxYw)NzT8 . 2 E / 4 4 6 . TYYu -s %Acvt|j|jj|jyr)r r!r r3 set_grammarr,s r6rzShiftReduceApp.edit_grammars%$))T\\113T5E5EFr8c|jj|t|j|_|j j dd|jD]!}|j jdd|z#y)Nrr\r])r rQrjrkrlrideleterm)r2r3rys r6rQzShiftReduceApp.set_grammarsl   ) !4!4!67 a'++ ?J NN ! !%%**< > ?r8cdj|j}d}d}t|j|||j|y)N rzEnter a new sentence to parse.)joinrrr! set_sentence)r2rsentencer"instrs r6rzShiftReduceApp.edit_sentences888DJJ'0DIIx0A0A5Ir8cN|j|_|jyr)rrr0)r2r4s r6rWzShiftReduceApp.set_sentencesZZ\  r8c|jjr9|jjddd|jd|j d<n)|jj d|j d<d|jd<y) NrIrJrK)rLrMrNafterrrz Hide Grammarr)r&rDrfrgrr1 pack_forgetrr,s r6rzShiftReduceApp._toggle_grammarsv    ! ! # OO &q8K8K ! '5DOOF # OO ' ' )&4DOOF #"$r8cb|jj}t|dk7ryt|d}|jj |j |}|r]d|jd<d|z|jd<|jjr|jy|jy|jjdd|jjD]8}|j j|}|jj!|:y)Nrrr6rr7r\)ri curselectionrprr rrlr1rr$rDr8rr&r'r(r))r2event selectionr(ryr*s r6rszShiftReduceApp._prodlist_selectsNN//1 y>Q  IaL!\\(():):5)AB &/DOOF #&*Z&7DOOF #}}  "$$&  NN * *1e 4 ::< 4))//5,,U3 4r8c|jj}t|dk(ry|jj dd|D]2}|jj t ||j4|jj|jj|jjy)Nrr\)rrc) r r'rpr/rSrstrrpostr.winfo_pointerxwinfo_pointery)r2rrkrys r6r zShiftReduceApp._popup_reducesll88: { q    E*% VJ    ) )J ) U V  LL ' ' )4<<+F+F+H r8c4|jd}|jd}t|jdk(rd}n#|jdjddz}|jj }||z dz|z }|j |||y)NrrrerKrg?)rrrprr$rD_animate_shift_frame)r2rrYrJdtdxs r6r4zShiftReduceApp._animate_shifts##A& a  t!! "a 'D%%b)..03b8D]]   Ulc !B & !!"fb1r8c:|dkDrFd|_|j|d|jjd|j|dz ||y|j d=|j j|d|_|j||jy)Nrrr) r#r r!r\rhrrrr$r)r2framerrjs r6rhz#ShiftReduceApp._animate_shift_frames 19#$D KKA  IIOOB 9 9519fb Q ""1%    % %f -#$D    (  ' ' )r8c|t|jjd}|j| d}t |dt r%d|dj jz}nd|djz}|jj}|dz|z }|j|dz ||y)Nrerg@rK) rpr r rr r rr_r$rD_animate_reduce_frame)r2 numwidgetswidgetsydistridys r6r8zShiftReduceApp._animate_reduce s++-b12 $$j[\2 gaj"3 4))+2244E**,,E]]    S[2  ""267B7r8c|dkDrMd|_|D]}|jd||jjd|j|dz ||y|j t | d=|D]}|jj||jjd}t|ts tt|jt!|j#d|j$}t'|j||d}|j(j+\}}} } | |z dz} |j sd } n#|j dj+ddz} |jj-|| | |j j/||j1||j3d|_y) NrrrrerrrKrFr)r#r r!r\rorrpr remove_widgetr r r r ValueErrorrr.rcrrEr rrrrr$r) r2rlrqrsrrrrrrrrdrs r6roz$ShiftReduceApp._animate_reduce_frames 19#$D ! # Ar" # IIOOB : :EAIwPR S""CL=?3! 3 **62 3,,$$&r*Cc4( l" c#))+.idnnE't||UG1MF#//446 RRR" A%%&&r*//1!4r9 LL # #FAq 1    % %f -   (  ' ' )8$%D r8c|jj|j}|j|k(ry|j |jj Dcgc] }t |}}||vrdt|j|j}|j| dD]+}t|trd|jd<'d|d<-||_ycc}w)Nz#00a000r)rinearestrdrtrvr_rrprlrhsrr r r)r2r`r(srarhslenrs r6ruzShiftReduceApp._highlight_hoverYs&&uww/ ;;%   &*^^%@%@%BCSVC C I **515578F#116'(; 5 k+<=3.2BU&nA 0  4   % ,"$.G?J  %4(  "2 *"8 8%|. /r8rc"ddlm}mm}d}fd|j D\ }}}}}}} } } ||||g||| |g||||g||||g||| ||g||| |g||||g||dg|| dg|| dg||dg|| d g||d g||d g||d g||d g||dg|| dgf} ||| } dj }t | |j y)zP Create a shift reduce parser app, using a simple grammar and text. r)CFG Nonterminal ProductionzS VP NP PP P N Name V Detc3.K|] }|ywrr)rrzrs r6rzapp..s*Xa;q>*XsIthermansawinwithparkdogstatuemyz*my dog saw a man in the park with a statueN) nltk.grammarrrrrrr=)rr nonterminalsSVPNPPPPNNameVDetrkr3r4rs @r6appr|sX :9.L*Xrs]7rONNGGII0 b /b /J'-T zE 'r8