3 \5@sdddlmZmZmZGdddejZGdddeZGdddeZGdd d eZ dd d Z d S))grammartokentokenizec@s eZdZdS) PgenGrammarN)__name__ __module__ __qualname__r r /usr/lib64/python3.6/pgen.pyrsrc@seZdZd&ddZddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZddZddZd'd d!Zd"d#Zd$d%ZdS)(ParserGeneratorNcCsld}|dkrt|}|j}||_||_tj|j|_|j|j \|_ |_ |dk rZ|i|_ |j dS)N)openclosefilenamestreamrgenerate_tokensreadline generatorgettokenparsedfas startsymbolfirst addfirstsets)selfrrZ close_streamr r r __init__ szParserGenerator.__init__c Cs*t}t|jj}|j|j|j|jd|jx.|D]&}dt|j }||j |<||j |<qtj|}||j kr@|j |S|jj|df||j |<|SdS)Nr"')r8r9)r"Zlabelsisalphar#Z symbol2labelr'getattrr isinstanceintAssertionErrortok_nametokensevalkeywordsNAMErZopmap)rr.r4r7Zitokenvaluer r r r(=s<                 zParserGenerator.make_labelcCs<t|jj}|jx |D]}||jkr|j|qWdS)N)rrrrr calcfirst)rr/r0r r r rks   zParserGenerator.addfirstsetsc Cs |j|}d|j|<|d}i}i}x|jjD]x\}}||jkr||jkrl|j|}|dkrtd|n|j||j|}|j||||<q0d||<|di||<q0Wi} xJ|jD]>\}} x4| D],} | | krtd|| || | f|| | <qWqW||j|<dS)Nrzrecursion for rule %rrzArule %s is ambiguous; %s is in the first sets of %s as well as %s)rrr%r& ValueErrorrEupdate) rr0r2r3ZtotalsetZ overlapcheckr4r5fsetZinverseZitsfirstZsymbolr r r rEss2          zParserGenerator.calcfirstc Csi}d}x|jtjkrx|jtjkr.|jqW|jtj}|jtjd|j\}}|jtj|j ||}t |}|j |t |}|||<|dkr |}q W||fS)N:) typer ENDMARKERNEWLINErexpectrCOP parse_rhsmake_dfar" simplify_dfa) rrrr0azr2ZoldlenZnewlenr r r rs"      zParserGenerator.parsec st|tstt|tstfdd}fddt|||g}x|D]}i}x<|jD]2}x,|jD]"\}} |dk rf| |j|iqfWqZWxRt|jD]B\}} x,|D]} | j| krPqWt| |} |j | |j | |qWqJW|S)Ncsi}|||S)Nr )r3base) addclosurer r closures z)ParserGenerator.make_dfa..closurecsLt|tst||krdSd||<x$|jD]\}}|dkr*||q*WdS)Nr)r<NFAStater>r%)r3rTr4r5)rUr r rUsz,ParserGenerator.make_dfa..addclosure) r<rWr>DFAStatenfasetr% setdefaultr$r&r'addarc) rr-finishrVr+r3r%Znfastater4r5rYstr )rUr rPs&        zParserGenerator.make_dfac Cstd||g}xt|D]\}}td|||kr4dp6dx^|jD]T\}}||kr^|j|} nt|} |j||dkrtd| qBtd|| fqBWqWdS)NzDump of NFA forz Statez(final)z -> %dz %s -> %d)print enumerater%r)r"r') rr0r-r\Ztodor1r3r4r5jr r r dump_nfas   zParserGenerator.dump_nfacCsltd|x\t|D]P\}}td||jr,dp.dx0t|jjD]\}}td||j|fqBWqWdS)NzDump of DFA forz Statez(final)r^z %s -> %d)r_r`r*r$r%r&r))rr0r2r1r3r4r5r r r dump_dfas  zParserGenerator.dump_dfacCs~d}xt|rxd}xft|D]Z\}}xPt|dt|D]:}||}||kr4||=x|D]}|j||qTWd}Pq4WqWqWdS)NTFr)r`ranger" unifystate)rr2Zchangesr1Zstate_iraZstate_jr3r r r rQs zParserGenerator.simplify_dfacCs|j\}}|jdkr||fSt}t}|j||j|x6|jdkrt|j|j\}}|j||j|q@W||fSdS)N|) parse_altrDrWr[r)rrRrSZaaZzzr r r rOs       zParserGenerator.parse_rhscCsP|j\}}x:|jdks*|jtjtjfkrF|j\}}|j||}qW||fS)N([)rhri) parse_itemrDrJrrCSTRINGr[)rrRbr.dr r r rg s    zParserGenerator.parse_altcCs|jdkr>|j|j\}}|jtjd|j|||fS|j\}}|j}|dkr`||fS|j|j||dkr||fS||fSdS)Nri]+*)rorp)rDrrOrMrrNr[ parse_atom)rrRrSrDr r r rjs     zParserGenerator.parse_itemcCs|jdkr4|j|j\}}|jtjd||fS|jtjtjfkrpt }t }|j ||j|j||fS|j d|j|jdS)Nrh)z+expected (...) or NAME or STRING, got %s/%s) rDrrOrMrrNrJrCrkrWr[ raise_error)rrRrSr r r rq(s  zParserGenerator.parse_atomcCsD|j|ks|dk r2|j|kr2|jd|||j|j|j}|j|S)Nzexpected %s/%s, got %s/%s)rJrDrsr)rrJrDr r r rM9s zParserGenerator.expectcCsJt|j}x"|dtjtjfkr,t|j}q W|\|_|_|_|_|_ dS)Nr) r5rrCOMMENTNLrJrDZbeginendline)rtupr r r rAs zParserGenerator.gettokenc Gs^|r8y ||}Wn&dj|gttt|}YnXt||j|jd|jd|jfdS)N rr)joinrmapstr SyntaxErrorrrvrw)rmsgargsr r r rsHs  zParserGenerator.raise_error)N)N)rrrrr6r,r(rrErrPrbrcrQrOrgrjrqrMrrsr r r r r s$  .$  r c@seZdZddZdddZdS)rWcCs g|_dS)N)r%)rr r r rSszNFAState.__init__NcCs8|dkst|tstt|ts$t|jj||fdS)N)r<r|r>rWr%r')rr5r4r r r r[VszNFAState.addarc)N)rrrrr[r r r r rWQsrWc@s0eZdZddZddZddZddZd Zd S) rXcCsLt|tstttt|ts$tt|ts2t||_||k|_i|_dS)N) r<dictr>r5iterrWrYr*r%)rrYfinalr r r r]s  zDFAState.__init__cCs8t|tst||jkstt|ts*t||j|<dS)N)r<r|r>r%rX)rr5r4r r r r[eszDFAState.addarccCs.x(|jjD]\}}||kr ||j|<q WdS)N)r%r&)roldnewr4r5r r r rekszDFAState.unifystatecCsft|tst|j|jkrdSt|jt|jkr6dSx*|jjD]\}}||jj|k rBdSqBWdS)NFT)r<rXr>r*r"r%r&get)rotherr4r5r r r __eq__ps zDFAState.__eq__N)rrrrr[rer__hash__r r r r rX[s rX Grammar.txtcCst|}|jS)N)r r6)rpr r r generate_grammarsrN)r) r^rrrZGrammarrobjectr rWrXrr r r r sI %