Your IP : 3.149.254.35


Current Path : /opt/alt/python34/lib64/python3.4/lib2to3/pgen2/__pycache__/
Upload File :
Current File : //opt/alt/python34/lib64/python3.4/lib2to3/pgen2/__pycache__/pgen.cpython-34.pyc

�
j f�5�@s�ddlmZmZmZGdd�dej�ZGdd�de�ZGdd�de�ZGdd	�d	e�Z	d
dd�Z
d
S)�)�grammar�token�tokenizec@seZdZdS)�PgenGrammarN)�__name__�
__module__�__qualname__�r	r	�7/opt/alt/python34/lib64/python3.4/lib2to3/pgen2/pgen.pyrsrc@s�eZdZddd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd d!�Zd"d#�Zd$d%�ZdS)&�ParserGeneratorNcCs�d}|dkr*t|�}|j}n||_||_tj|j�|_|j�|j	�\|_
|_|dk	r�|�ni|_|j
�dS)N)�open�close�filename�streamr�generate_tokens�readline�	generator�gettoken�parse�dfas�startsymbol�first�addfirstsets)�selfrrZclose_streamr	r	r
�__init__s		

	zParserGenerator.__init__cCs�t�}t|jj��}|j�|j|j�|jd|j�x;|D]3}dt|j	�}||j	|<||j
|<qRWx�|D]�}|j|}g}x�|D]�}g}xE|jj�D]4\}	}
|j
|j||	�|j|
�f�q�W|jr,|j
d|j|�f�n|j
|�q�W|jj
|�||j||�f|j|j	|<q�W|j	|j|_|S)N��)r�listr�keys�sort�remover�insert�len�
symbol2numberZ
number2symbol�arcs�items�append�
make_label�index�isfinal�states�
make_first�start)r�c�names�name�i�dfar*�stater$�label�nextr	r	r
�make_grammars.	





,	*zParserGenerator.make_grammarcCsD|j|}i}x*|D]"}|j||�}d||<qW|S)Nr)rr')rr-r/Zrawfirstrr3�ilabelr	r	r
r+4s

zParserGenerator.make_firstcCs�t|j�}|dj�r||jkry||jkrH|j|S|jj|j|df�||j|<|Sq�tt|d�}t|t	�s�t
|��|tjks�t
|��||jkr�|j|S|jj|df�||j|<|Sn�|ddks!t
|��t
|�}|dj�r�||jkrW|j|S|jjtj|f�||j|<|SnNtj|}||jkr�|j|S|jj|df�||j|<|SdS)Nr�"�')r7r8)r"Zlabels�isalphar#Zsymbol2labelr&�getattrr�
isinstance�int�AssertionError�tok_name�tokens�eval�keywords�NAMErZopmap)rr-r3r6Zitoken�valuer	r	r
r'=s<




zParserGenerator.make_labelcCsSt|jj��}|j�x-|D]%}||jkr&|j|�q&q&WdS)N)rrrrr�	calcfirst)rr.r/r	r	r
rks


zParserGenerator.addfirstsetsc	Csj|j|}d|j|<|d}i}i}x�|jj�D]�\}}||jkr�||jkr�|j|}|dkr�td|��q�n|j|�|j|}|j|�|||<q@d||<id|6||<q@Wi}	xd|j�D]V\}}
xG|
D]?}||	krGtd||||	|f��n||	|<qWq�W||j|<dS)Nrzrecursion for rule %rrzArule %s is ambiguous; %s is in the first sets of %s as well as %s)rrr$r%�
ValueErrorrD�update)rr/r1r2ZtotalsetZoverlapcheckr3r4�fsetZinverseZitsfirstZsymbolr	r	r
rDss2









zParserGenerator.calcfirstc	Cs�i}d}x�|jtjkr�x |jtjkrC|j�q$W|jtj�}|jtjd�|j�\}}|jtj�|j	||�}t
|�}|j|�t
|�}|||<|dkr|}qqW||fS)N�:)�typer�	ENDMARKER�NEWLINEr�expectrB�OP�	parse_rhs�make_dfar"�simplify_dfa)	rrrr/�a�zr1ZoldlenZnewlenr	r	r
r�s"


zParserGenerator.parsecsDt|t�st�t|t�s*t��fdd�}�fdd��t||�|�g}x�|D]�}i}xS|jD]H}x?|jD]4\}}	|dk	r��|	|j|i��q�q�Wq�Wxj|j�D]\\}}
x=|D]}|j|
kr�Pq�q�Wt|
|�}|j|�|j	||�q�WqmW|S)Ncsi}�||�|S)Nr	)r2�base)�
addclosurer	r
�closure�s
z)ParserGenerator.make_dfa.<locals>.closurecsit|t�st�||kr%dSd||<x3|jD](\}}|dkr9�||�q9q9WdS)Nr)r;�NFAStater=r$)r2rSr3r4)rTr	r
rT�s
z,ParserGenerator.make_dfa.<locals>.addclosure)
r;rVr=�DFAState�nfasetr$�
setdefaultr%r&�addarc)rr,�finishrUr*r2r$Znfastater3r4rX�str	)rTr
rO�s&
$

zParserGenerator.make_dfac
Cs�td|�|g}x�t|�D]�\}}td|||krJdpMd�x|jD]t\}}||kr�|j|�}	nt|�}	|j|�|dkr�td|	�q[td||	f�q[Wq#WdS)NzDump of NFA forz  Statez(final)�z	    -> %dz    %s -> %d)�print�	enumerater$r(r"r&)
rr/r,r[Ztodor0r2r3r4�jr	r	r
�dump_nfa�s
	"
zParserGenerator.dump_nfacCs�td|�xvt|�D]h\}}td||jr>dpAd�x:|jj�D])\}}td||j|�f�qUWqWdS)NzDump of DFA forz  Statez(final)r]z    %s -> %d)r^r_r)r$r%r()rr/r1r0r2r3r4r	r	r
�dump_dfa�s

zParserGenerator.dump_dfacCs�d}x�|r�d}x�t|�D]x\}}xit|dt|��D]N}||}||krH||=x|D]}|j||�qrWd}PqHqHWq"Wq	WdS)NTFr)r_�ranger"�
unifystate)rr1Zchangesr0Zstate_ir`Zstate_jr2r	r	r
rP�s	 

zParserGenerator.simplify_dfacCs�|j�\}}|jdkr+||fSt�}t�}|j|�|j|�xI|jdkr�|j�|j�\}}|j|�|j|�qZW||fSdS)N�|)�	parse_altrCrVrZr)rrQrRZaaZzzr	r	r
rN�s
		



zParserGenerator.parse_rhscCsr|j�\}}xS|jdks?|jtjtjfkrg|j�\}}|j|�|}qW||fS)N�(�[)rgrh)�
parse_itemrCrIrrB�STRINGrZ)rrQ�br-�dr	r	r
rf
s

zParserGenerator.parse_altcCs�|jdkrU|j�|j�\}}|jtjd�|j|�||fS|j�\}}|j}|dkr�||fS|j�|j|�|dkr�||fS||fSdS)Nrh�]�+�*)rnro)rCrrNrLrrMrZ�
parse_atom)rrQrRrCr	r	r
ris


	



zParserGenerator.parse_itemcCs�|jdkrH|j�|j�\}}|jtjd�||fS|jtjtjfkr�t	�}t	�}|j
||j�|j�||fS|jd|j|j�dS)Nrg�)z+expected (...) or NAME or STRING, got %s/%s)rCrrNrLrrMrIrBrjrVrZ�raise_error)rrQrRr	r	r
rp(s

		

	zParserGenerator.parse_atomcCsc|j|ks*|dk	rL|j|krL|jd|||j|j�n|j}|j�|S)Nzexpected %s/%s, got %s/%s)rIrCrrr)rrIrCr	r	r
rL9s*		
zParserGenerator.expectcCsit|j�}x/|dtjtjfkr@t|j�}qW|\|_|_|_|_|_	dS)Nr)
r4rr�COMMENT�NLrIrCZbegin�end�line)r�tupr	r	r
rAszParserGenerator.gettokencGsz|rGy||}WqGdj|gttt|���}YqGXnt||j|jd|jd|jf��dS)N� rr)�joinr�map�str�SyntaxErrorrrurv)r�msg�argsr	r	r
rrHs-zParserGenerator.raise_error)rrrrr5r+r'rrDrrOrarbrPrNrfrirprLrrrr	r	r	r
r
s$	.$
rc@s+eZdZdd�Zddd�ZdS)rVcCs
g|_dS)N)r$)rr	r	r
rSszNFAState.__init__NcCsP|dks!t|t�s!t�t|t�s6t�|jj||f�dS)N)r;r{r=rVr$r&)rr4r3r	r	r
rZVs!zNFAState.addarc)rrrrrZr	r	r	r
rVQsrVc@sFeZdZdd�Zdd�Zdd�Zdd�Zd	Zd	S)
rWcCspt|t�st�ttt|��t�s6t�t|t�sKt�||_||k|_i|_dS)N)	r;�dictr=r4�iterrVrXr)r$)rrX�finalr	r	r
r]s!	zDFAState.__init__cCsPt|t�st�||jks*t�t|t�s?t�||j|<dS)N)r;r{r=r$rW)rr4r3r	r	r
rZeszDFAState.addarccCs@x9|jj�D](\}}||kr||j|<qqWdS)N)r$r%)r�old�newr3r4r	r	r
rdkszDFAState.unifystatecCs�t|t�st�|j|jkr+dSt|j�t|j�krMdSx9|jj�D](\}}||jj|�k	r]dSq]WdS)NFT)r;rWr=r)r"r$r%�get)r�otherr3r4r	r	r
�__eq__pszDFAState.__eq__N)rrrrrZrdr��__hash__r	r	r	r
rW[s
rWzGrammar.txtcCst|�}|j�S)N)rr5)r�pr	r	r
�generate_grammar�sr�N)r]rrrZGrammarr�objectrrVrWr�r	r	r	r
�<module>s�H
%

?>