JL itdZddlZddlmZmZmZmZmZmZm Z m Z m Z m Z ddl mZmZddlmZmZmZmZmZddlmZmZmZmZddlmZGdd eZd ZGd d ZGd dZ dZ!dZ"dZ#e$dk(re"yy)z Visualization tools for CFGs. N) ButtonCanvasEntryFrameIntVarLabel ScrollbarTextTkToplevel)TreeSegmentWidgettree_to_treesegment) CanvasFrame ColorizedListShowText SymbolWidget TextWidget)CFG Nonterminal_read_cfg_production nonterminals)Treec6eZdZejdZdZdZy)ProductionList rightarrowcz|jdd|jddd|jdd d y) Nterminal#006000 foregroundarrowsymbol0)font underline nonterminalblue helveticaiboldr r$ tag_config)self textwidgetoptionss S/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/draw/cfg.py_init_colortagszProductionList._init_colortagsRsEjY?gHD f3M  cLg}|jd|jzdf|j|jdf|jD]M}t |t r%|jd|j zdf8|jd|zdfO|S)Nz%s r&r! %s %rr)appendlhsARROWrhs isinstancerr")r.itemcontentselts r1 _item_reprzProductionList._item_reprYs$((*,m<=W-.88: ;C#{+!5} EFj 9:  ; r3N)__name__ __module__ __qualname__rSYMBOLSr9r2r?r3r1rrOs   .E  r3raM The CFG Editor can be used to create or modify context free grammars. A context free grammar consists of a start symbol and a list of productions. The start symbol is specified by the text entry field in the upper right hand corner of the editor; and the list of productions are specified in the main text editing box. Every non-blank line specifies a single production. Each production has the form "LHS -> RHS," where LHS is a single nonterminal, and RHS is a list of nonterminals and terminals. Nonterminals must be a single word, such as S or NP or NP_subj. Currently, nonterminals must consists of alphanumeric characters and underscores (_). Nonterminals are colored blue. If you place the mouse over any nonterminal, then all occurrences of that nonterminal will be highlighted. Terminals must be surrounded by single quotes (') or double quotes("). For example, "dog" and "New York" are terminals. Currently, the string within the quotes must consist of alphanumeric characters, underscores, and spaces. To enter a new production, go to a blank line, and type a nonterminal, followed by an arrow (->), followed by a sequence of terminals and nonterminals. Note that "->" (dash + greater-than) is automatically converted to an arrow symbol. When you move your cursor to a different line, your production will automatically be colorized. If there are any errors, they will be highlighted in red. Note that the order of the productions is significant for some algorithms. To re-order the productions, use cut and paste to move them. Use the buttons at the bottom of the window when you are done editing the CFG: - Ok: apply the new CFG, and exit the editor. - Apply: apply the new CFG, and do not exit the editor. - Reset: revert to the original CFG, and do not exit the editor. - Cancel: revert to the original CFG, and exit the editor. cteZdZdZej dZejdezdzZ ejdezdzZ ejdezdzdzZ ejdezd zZ d Z d d Zd ZdZdZdZdZdZdZdZd!dZdZdZdZdZdZdZdZdZdZ dZ!y )" CFGEditora* A dialog window for creating and editing context free grammars. ``CFGEditor`` imposes the following restrictions: - All nonterminals must be strings consisting of word characters. - All terminals must be strings consisting of word characters and space characters. rz(^\s*\w+\s*)(->|(z))z\s*(->|(z))\s*z#((\w+|'[\w ]*'|\"[\w ]*\"|\|)\s*)*$z\w+|->|'[\w ]+'|"[\w ]+"|()r(Nc||_|||_nttdg|_||_d|_t ||_|j|j|jjddd|j|jjddd|j|jjddd|j j#y) NStopxrsidefillexpandbothbottom)_parent_cfgrr_set_cfg_callback _highlight_matching_nonterminalsr _top_init_bindings_init_startframe _startframepack_init_prodframe _prodframe _init_buttons _buttonframe _textwidgetfocus)r.parentcfgset_cfg_callbacks r1__init__zCFGEditor.__init__s ?DIK,b1DI!101-V$    5s1=  %fQ?  H3qA  r3ct|jx}|_t||_|jj dt |dj dt |dj d|jjd|jjjy)NrightrNz Start Symbol:textz Productions:leftr) rrWrZr_startr[rinsertrTstartr"r.frames r1rYzCFGEditor._init_startframes#(#33 El  g& e/*//W/= e.)..F.; 1diioo/6689r3ct|jx}|_t|d|jddj dt|d|j ddj dt|d|jddj dt|d|jddj dt|d |jddj d y) NOkr)rjcommandr% takefocusrkrhApplyResetCancelHelprg) rrWr_r_okr[_apply_reset_cancel_helpros r1r^zCFGEditor._init_buttonss$)$))$44!u4Q!LQQ R  u7DKK1PQRWW X  u7DKK1PQRWW X   $,,!q $F$ u64::aPUU V r3c|jjd|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|jy)Nz CFG Editor zz zzzz zz zz zz zz)rWtitlebindr|ryrzr{r})r.s r1rXzCFGEditor._init_bindingss\  % }dll3 y$,,/ }dll3 y$,,/ z4<<0 y$,,/ }dhh/ y$((+ }dkk2 y$++. }dkk2 y$++. }djj1 y$**- vtzz*r3ct|j|_t|jdd|_t |jdd|_|jj|j j|j j|jj|j jd d |jjdd d |jjdd|jjdd|jjddd|_ |jjd|j|jjd|j|jjd|j |jjd|j |jfd}|jjd||j"j%Dcgc]#}|j'|j)gf%}}t+t-|dz ddD]x}||d||dz dk(sd||dvr#d||dz dvr1t/||dz dt/||d||dz dj1||d||=z|D]n\}}t/||d |z}|D]/}|D]#} t3| t4r |d!| zz }|d"| zz }%|d#z }1|dd$d%z}|jj7d&|p|jycc}w)'Nz#e0e0e0rJ) backgroundexportselectionrvertical)rtorient)yscrollcommand)rsrgyrNrOrQrk)rPrOrNrrrr!r")r$errorredr>z <>z z c@|jjyN) tk_focusNextra)er/s r1cyclez(CFGEditor._init_prodframe..cycles  # # % + + -r3zrDz%s ->r5r6z | end)rrWr]r r`r _textscrollconfigsetyviewr[r-_linenumr_replace_arrows_analyze_check_analyzerT productionsr8r:rangelenprintextendr;rrm) r.rp prod_tuplesir8rhsssr:r>s r1r\zCFGEditor._init_prodframes@ * OO 1 %T__*U t/?/?/C/CD (8(8(>(>? 75 QV&A ##J9#E ##G(#; ##G#>  sD001 {DMM2 |T%8%89 (;(;<!% 0 0 . gu-59YY5J5J5LM!%%'+M Ms;'!+Q3 #A1~a KA$6q$99Q**QU+A..k!a%(+,k!nQ'(AE"1%,,[^A->?N #% .IC #t # A )C!#{3US[(US[( ) T   #2 A    # #E1 - . 3Ns-(M/cd|z}d|z}|jjD]$}|dvs|jj|||&y)z Remove all tags (except ``arrow`` and ``sel``) from the given line of the text widget used for editing the productions. %d.0%d.end)r!selN)r` tag_names tag_remove)r.linenumrnrtags r1 _clear_tagszCFGEditor._clear_tagsNsW   ##--/ =C**  ++C< =r3ct|jjdjdd}||jk7r4|j ||j |j||_yy)z Check if we've moved to a new line. If we have, then remove all colorization from the line we moved to, and re-colorize the line that we moved from. rm.rN)intr`indexsplitrr _analyze_line)r.rrs r1rzCFGEditor._check_analyzeYsg d&&,,X6<'`` text strings with arrows (char \256, in symbol font). This searches the whole buffer, but is fast enough to be done anytime they press '>'. 1.0->z end+1charz+2charr! z+1charN)r`searchdeletermr9tag_add)r.rr!s r1rzCFGEditor._replace_arrowses $$++D%EE{    # #E58+; <    # #E4::w ?    # #E4 0 $$++DJJ8H+VE{    $ $WeUX5E F r3c|jddvrd}na|jd|jfvrd}n@d|jz}||jjvr|j |d||j fz}d||j fz}|jj|||y) z Given a line number and a regexp match for a token on that line, colorize the token. Note that the regexp match gives us the token's text, start index (on the line), and end index (on the line). rz'"rrr! nonterminal_%d.%dN)groupr9r`r_init_nonterminal_tagrnrr)r.matchrrrnrs r1_analyze_tokenzCFGEditor._analyze_token{s ;;= u $C [[]tTZZ0 0C !5;;=0C$**4466**3/7EKKM22%))+..   eS1r3c*|jj||tj|jsy|j|fd}|j|fd}|jj |d||jj |d|y)Nr+c*|j|dy)Nz#80ff80rr,rr/rs r1enterz.CFGEditor._init_nonterminal_tag..enters  ! !#) ! .leaves  ! !#" ! 5r3zz)r`r-rF_BOLDrVtag_bind)r.rr rrs r1rzCFGEditor._init_nonterminal_tags ##CJY__#U44  $ 0 0c =!% 0 0c 6 !!#y%8 !!#y%8r3cf|j||jjt|dzt|dz}tj j |r'||fd}tjj||y|jdk7r|j||yy)z( Colorize a given line. z.0z.endc(|j||y)Nr)r)rr.rs r1 analyze_tokenz.CFGEditor._analyze_line..analyze_tokens##E73r3rN) rr`getreprrF_PRODUCTION_REr _TOKEN_REsubstrip _mark_error)r.rliners r1rzCFGEditor._analyze_lines !##DMD$8$w-&:PQ  # # ) )$ /+/     # #M4 8 ZZ\R    Wd + r3ctjj|}|s d|z}d|z}nTtjj |sd|z}d||j fz}nd||j fz}d|z}|jj|d|r d|z}d|z}|jjd||y)z: Mark the location of an error in a line. rrrz==rN) rF _ARROW_REr_LHS_RErrnrr`comparer)r.rr arrowmatchrnrs r1rzCFGEditor._mark_errors((//5 W$EW$C""((.W$EWj&6&6&899Cw (899EW$C    # #E4 5W$EW$C   %5r3c|jt|jjdj dd}t d|dzD]}|j |y)zM Replace ``->`` with arrows, and colorize the entire buffer. rrrrJN)rrr`rrrr)r.rnumlinesrs r1rzCFGEditor._analyzesc t''--e4::3?BCQ1 - (G   w ' (r3c*g}|jjdd}tj|jd|}tjdd|}|j d}|D]&}|j }|dk(r|t|z }(|S)zo Parse the current contents of the textwidget buffer, to create a list of productions. rrrr rr)r`rrerr9rrr)r.rrjlinesrs r1_parse_productionszCFGEditor._parse_productionss  ##E51vvdjj$-vvdC& 4  6D:: ? @   ! ! -  " "499 - .r3cT |j|jy#YxYwr)r{rrs r1r|zCFGEditor._cancels%  KKM   s#'c t|jdtjddy#t|jdtjdYyxYw)NzHelp: Chart Parser DemoKfixed)widthr$r)rrS_CFGEditor_HELPrrs r1r}zCFGEditor._helpsS   ) '')     ) '')  s ,/-A)NN)r')"r@rArB__doc__rrCr9rcompilerrrrrrerYr^rXr\rrrrrrrrrrryrzr{r|r}rDr3r1rFrFs   .Ebjj-5<=G ;.9:IRZZ      1  1N r = $G,20 9,.60(#J (.r3rFc\eZdZdZdZdZdZdZdZdZ dZ d Z dd Z d Z d ZdZy )CFGDemocL||_||_t|_|jj dt |j|_|j jd|j|jt|j}|jddd|j|j|j|j|j||j||j|jy)NzContext Free Grammar Demo rkrrrM)_grammar_textr rWrr_sizerrXrr[ _init_menubarr^ _init_grammar _init_treelet_init_workspace)r.grammarrjframe1s r1rezCFGDemo.__init__7s  D  34DII&  r DII&tyy! c! 4 499% 499% 6" 6" TYY'r3c<|jd|jy)Nr)rr)r.rKs r1rXzCFGDemo._init_bindingsSs  -r3cyrrDr.rbs r1rzCFGDemo._init_menubarV r3cyrrDrs r1r^zCFGDemo._init_buttonsYrr3cFt||jd|_|jjddd|jj |jj d|j |jj d|j y) NrrKrQrJrMselectmove)rr _prodlistr[ra add_callback_selectprod_cbrs r1rzCFGDemo._init_grammar\ss' RH VA>  ##Hd.A.AB ##FD,?,?@r3cpt|d|_|jjddd|_y)NwhiterrRrLr)r_treelet_canvasr[_treeletrs r1rzCFGDemo._init_treeletcs1%fA !!xc!: r3ct|d|_|jjdddd|_|j y)NrrrgrQrJrM)r _workspacer[_treereset_workspacers r1rzCFGDemo._init_workspacehs<%fA 'qA  r3c v|jj}t|jj }d|dz df}d|dz f}|j %|jj |j |jjj}t|||d}g}|jD] }|jt|||d"t|||d|_|jj|j |D]} | jd d y) Nr)r*rJ)r$ draggabler)colorrd)rcanvasrrrr remove_widgetrrnr"rrr7r add_widgetr ) r.cfontsize node_font leaf_fontrnrootnodeleaveswordleafs r1rzCFGDemo.reset_workspacers OO " " $tzz~~'( HqL/6:  HqL/2  :: ! OO ) )$** 5 ##%,,.aY!DJJ LD MM*Q9J K L'q(F'J  ""4::. D IIa  r3cyrrD)r.rs r1workspace_markprodzCFGDemo.workspace_markprodrr3NcV| |j}tt|jt|j z D]}|d|fdk(r |j t |j D]\}}|j||z}t|tr %AGQJ7*$$$TXXZ0 %4Q/t[1"6+<= v||~':':'<<tS)"6:6 - %$j!$- %r3c|j}|jj||j|jj |j }t |D]!\}}t|tst|}#t|jjg|}t|jj}d|dz df}d|dz f} t|||| |_d|jd<|jj!\} } } } t|dt|d }}|jj#|| z | z dz || z | z dz |j%|y) Nr)rr*r)r$r%rJrrheight)rr highlightrrr:r/r;rrr8r"rrrrbboxr r.)r.rrr:rr>r3r#r$r%x1y1x2y2whs r1rzCFGDemo._selectprod_cbsg%%   , == $ MM ! ! #nnn FAs#{+3i JNN$++-44tzz~~'( HqL/6:  HqL/2 + DI  &' k" ==--/RR6'?#S)9%:1 AFRK1,q2v{a.?@ Z(r3c8|jjyrr)r.argss r1rzCFGDemo.destroys r3c<|jj|i|yr)rWmainloop)r.rBkwargss r1rDzCFGDemo.mainloops D+F+r3r)r@rArBrerXrr^rrrrr+r.rrrDrDr3r1rr6sE(8.  A @ %>)>,r3rcNddlm}mm}d}fd|jD\ }}}}}}} } } ||||g||| |g||||g||||g||| ||g||| |g||||g||g||dd|g||dg|| dg|| d g||d g|| d g||d g||d g||dg||dg||dg|| dgf} ||| } dj }t | |}|j y)Nr)rr ProductionS VP NP PP P N Name V Detc3.K|] }|ywrrD.0rrs r1 zdemo2..*Xa;q>*XupoverItheamansawinwithparkdogstatuemyzI saw a man in the park)nltkrrrGrrrD)rrGrrIVPNPPPPNNameVDetrrrjdrs @r1demo2rfst11.L*XCU.rMrNa+ S -> NP VP PP -> P NP NP -> Det N NP -> NP PP VP -> V NP VP -> VP PP Det -> 'a' Det -> 'the' Det -> 'my' NP -> 'I' N -> 'dog' N -> 'man' N -> 'park' N -> 'statue' V -> 'saw' P -> 'in' P -> 'up' P -> 'over' P -> 'with' ct|yr)r)rs r1cbzdemo..cb!s  gr3z Testing CFG Editor riQuit)rjrs) r\rrr fromstringr rFrr[rrrD)rrrIr]r^r_r`rarbrcrdrrjrKeditorrs @r1demorns%.L*XCU.destroyMs  r3qrJrQ)rPrOr r r) r\rGrr rrr[rmarkonlyramark)rGrIr]r^r_r`rarbrcrdrrqrrs r1demo3rv+s*6++'QBAq$3 1r2h2Qx 2Bx 2Bx 22r{#22w22w2r2fb)*2u3 31ug1ug1tf1vh1vh1ug1xj!3-K2 AFF3q+&AFF!&F!NN8QZZ(NN61::&GGIFF;q>FF;q>r3__main__)%rrtkinterrrrrrrr r r r nltk.draw.treer rnltk.draw.utilrrrrr nltk.grammarrrrr nltk.treerrrrFrrfrnrvr@rDr3r1r}sR    BNM]4)XYYB a,a,H!R%P,^ zFr3