3 ÁÌ]óã@s€ddlZddlmZddlmZddlmZddlmZm Z m Z ddl m Z Gdd „d eƒZ ed kr|ddlZddlZddlZdS) éN)Úyaccé)Úc_ast)ÚCLexer)Ú PLYParserÚCoordÚ ParseError)Úfix_switch_casesc @sDeZdZ�dCdd„Z�dDd d „Zd d „Zd d„Zdd„Zdd„Zdd„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd „Zd!d"„Zd#d$„Z�dEd%d&„Zd'd(„Zd)d*„Z�dPZd>d?„Zd@dA„ZdBdC„ZdDdE„ZdFdG„ZdHdI„ZdJdK„ZdLdM„ZdNdO„ZdPdQ„ZdRdS„Z dTdU„Z!dVdW„Z"dXdY„Z#dZd[„Z$d\d]„Z%d^d_„Z&d`da„Z'dbdc„Z(ddde„Z)dfdg„Z*dhdi„Z+djdk„Z,dldm„Z-dndo„Z.dpdq„Z/drds„Z0dtdu„Z1dvdw„Z2dxdy„Z3dzd{„Z4d|d}„Z5d~d„Z6d€d�„Z7d‚dƒ„Z8d„d…„Z9d†d‡„Z:dˆd‰„Z;dŠd‹„Zd�d‘„Z?d’d“„Z@d”d•„ZAd–d—„ZBd˜d™„ZCdšd›„ZDdœd�„ZEdždŸ„ZFd d¡„ZGd¢d£„ZHd¤d¥„ZId¦d§„ZJd¨d©„ZKdªd«„ZLd¬d­„ZMd®d¯„ZNd°d±„ZOd²d³„ZPd´dµ„ZQd¶d·„ZRd¸d¹„ZSdºd»„ZTd¼d½„ZUd¾d¿„ZVdÀdÁ„ZWdÂdÄZXdÄdÅ„ZYdÆdÇ„ZZdÈdÉ„Z[dÊdË„Z\dÌdÍ„Z]dÎdÏ„Z^dÐdÑ„Z_dÒdÓ„Z`dÔdÕ„ZadÖdׄZbdØdÙ„ZcdÚdÛ„ZddÜdÝ„ZedÞdß„Zfdàdá„Zgdâdã„Zhdädå„Zidædç„Zjdèdé„Zkdêdë„Zldìdí„Zmdîdï„Zndðdñ„Zodòdó„Zpdôdõ„Zqdöd÷„Zrdødù„Zsdúdû„Ztdüdý„Zudþdÿ„Zv�d�d„Zw�d�d„Zx�d�d„Zy�d�d„Zz�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Œ�d,�d-„Z��d.�d/„ZŽ�d0�d1„Z��d2�d3„Z��d4�d5„Z‘�d6�d7„Z’�d8�d9„Z“�d:�d;„Z”�d<�d=„Z•�d>�d?„Z–�d@�dA„Z—�dBS(QÚCParserTúpycparser.lextabúpycparser.yacctabFÚc Csšt|j|j|j|jd�|_|jj|||d�|jj|_ddddddd d d d d dddg}x|D]}|j|ƒq\Wt j |d||||d�|_ t ƒg|_ d|_ dS)aŽ Create a new CParser. Some arguments for controlling the debug/optimization level of the parser are provided. The defaults are tuned for release/performance mode. The simple rules for using them are: *) When tweaking CParser/CLexer, set these to False *) When releasing a stable parser, set to True lex_optimize: Set to False when you're modifying the lexer. Otherwise, changes in the lexer won't be used, if some lextab.py file exists. When releasing with a stable lexer, set to True to save the re-generation of the lexer table on each run. lextab: Points to the lex table that's used for optimized mode. Only if you're modifying the lexer and want some tests to avoid re-generating the table, make this point to a local lex table file (that's been earlier generated with lex_optimize=True) yacc_optimize: Set to False when you're modifying the parser. Otherwise, changes in the parser won't be used, if some parsetab.py file exists. When releasing with a stable parser, set to True to save the re-generation of the parser table on each run. yacctab: Points to the yacc table that's used for optimized mode. Only if you're modifying the parser, make this point to a local yacc table file yacc_debug: Generate a parser.out file that explains how yacc built the parsing table from the grammar. taboutputdir: Set this parameter to control the location of generated lextab and yacctab files. )Z error_funcZon_lbrace_funcZon_rbrace_funcZtype_lookup_func)ÚoptimizeÚlextabÚ outputdirZabstract_declaratorZassignment_expressionZdeclaration_listZdeclaration_specifiersZ designationZ expressionZidentifier_listZinit_declarator_listZinitializer_listZparameter_type_listZspecifier_qualifier_listZblock_item_listZtype_qualifier_listZstruct_declarator_listZtranslation_unit_or_empty)ÚmoduleÚstartÚdebugrZ tabmodulerN)rÚ_lex_error_funcÚ_lex_on_lbrace_funcÚ_lex_on_rbrace_funcÚ_lex_type_lookup_funcÚclexZbuildÚtokensZ_create_opt_rulerÚcparserÚdictÚ _scope_stackÚ_last_yielded_token) ÚselfZ lex_optimizerZ yacc_optimizeZyacctabZ yacc_debugZ taboutputdirZrules_with_optZrule©rú/usr/lib/python3.6/c_parser.pyÚ__init__sF5    zCParser.__init__rcCs6||j_|jjƒtƒg|_d|_|jj||j|d�S)a& Parses C code and returns an AST. text: A string containing the C source code filename: Name of the file being parsed (for meaningful error messages) debuglevel: Debug level to yacc N)ÚinputZlexerr)rÚfilenameZ reset_linenorrrrÚparse)rÚtextr#Z debuglevelrrr r$~s   z CParser.parsecCs|jjtƒƒdS)N)rÚappendr)rrrr Ú _push_scope–szCParser._push_scopecCs|jjƒdS)N)rÚpop)rrrr Ú _pop_scope™szCParser._pop_scopecCs4|jdj|dƒs"|jd||ƒd|jd|<dS)zC Add a new typedef name (ie a TYPEID) to the current scope rTz;Typedef %r previously declared as non-typedef in this scopeNéÿÿÿÿr*)rÚgetÚ _parse_error)rÚnameÚcoordrrr Ú_add_typedef_name�s  zCParser._add_typedef_namecCs4|jdj|dƒr"|jd||ƒd|jd|<dS)ze Add a new object, function, or enum member name (ie an ID) to the current scope rFz;Non-typedef %r previously declared as typedef in this scopeNr*r*)rr+r,)rr-r.rrr Ú_add_identifier¦s  zCParser._add_identifiercCs.x(t|jƒD]}|j|ƒ}|dk r |Sq WdS)z8 Is *name* a typedef-name in the current scope? NF)Úreversedrr+)rr-ZscopeZin_scoperrr Ú_is_type_in_scope°s  zCParser._is_type_in_scopecCs|j||j||ƒƒdS)N)r,Ú_coord)rÚmsgÚlineÚcolumnrrr rºszCParser._lex_error_funccCs |jƒdS)N)r')rrrr r½szCParser._lex_on_lbrace_funccCs |jƒdS)N)r))rrrr rÀszCParser._lex_on_rbrace_funccCs|j|ƒ}|S)z§ Looks up types that were previously defined with typedef. Passed to the lexer for recognizing identifiers that are types. )r2)rr-Zis_typerrr rÃs zCParser._lex_type_lookup_funccCs|jjS)z§ We need access to yacc's lookahead token in certain cases. This is the last token yacc requested from the lexer, so we ask the lexer. )rZ last_token)rrrr Ú_get_yacc_lookahead_tokenÌsz!CParser._get_yacc_lookahead_tokencCsd|}|}x|jr|j}q Wt|tjƒr0||_|S|}xt|jtjƒsL|j}q6W|j|_||_|SdS)z  Tacks a type modifier on a declarator, and returns the modified declarator. Note: the declarator and modifier may be modified N)ÚtypeÚ isinstancerÚTypeDecl)rÚdeclÚmodifierZ modifier_headZ modifier_tailZ decl_tailrrr Ú_type_modify_declõs    zCParser._type_modify_declcCsÆ|}xt|tjƒs|j}qW|j|_|j|_x>|D]6}t|tjƒs2t|ƒdkr^|j d|j ƒq2||_|Sq2W|s¢t|jtj ƒsŒ|j d|j ƒtjdg|j d�|_n tjdd„|Dƒ|dj d�|_|S) z- Fixes a declaration. Modifies decl. rz Invalid multiple types specifiedzMissing type in declarationÚint)r.cSsg|]}|jD]}|‘qqSr)Únames)Ú.0Úidr-rrr ú Usz/CParser._fix_decl_name_type..r) r9rr:r8Údeclnamer-ÚqualsÚIdentifierTypeÚlenr,r.ÚFuncDecl)rr;Útypenamer8Ztnrrr Ú_fix_decl_name_type,s.       zCParser._fix_decl_name_typecCs(|ptggggd�}||jd|ƒ|S)aõ Declaration specifiers are represented by a dictionary with the entries: * qual: a list of type qualifiers * storage: a list of storage type qualifiers * type: a list of type specifiers * function: a list of function specifiers This method is given a declaration specifier, and a new specifier of a given kind. Returns the declaration specifier, with the new specifier incorporated. )ÚqualÚstorager8Úfunctionr)rÚinsert)rZdeclspecZnewspecZkindÚspecrrr Ú_add_declaration_specifierYs z"CParser._add_declaration_specifierc Cs@d|dk}g}|djdƒdk r&�n4|dddkrèt|dƒdksvt|ddjƒd ksv|j|ddjdƒ rªd }x"|dD]}t|d ƒr„|j}Pq„W|jd |ƒtj|ddjddd|ddjd �|dd<|dd=nrt |ddtj tj tj fƒ�sZ|dd}xt |tjƒ�s.|j }�qW|jdk�rZ|ddjd|_|dd=xà|D]Ø} |�r’tjd|d|d| d| djd�} nr)r.)rJrKr8rLrQé)rNr;r^r_rN)rrrEr3r€r`)rrwrNrrr Úp_function_definition_1-szCParser.p_function_definition_1cCs.|d}|j||d|d|dd�|d<dS)zi function_definition : declaration_specifiers declarator declaration_list_opt compound_statement rrQr‚é)rNr;r^r_rN)r`)rrwrNrrr Úp_function_definition_2>s zCParser.p_function_definition_2cCs|d|d<dS)a  statement : labeled_statement | expression_statement | compound_statement | selection_statement | iteration_statement | jump_statement rrNr)rrwrrr Ú p_statementIszCParser.p_statementc Cs¶|d}|ddkr–|d}tjtjtjf}t|ƒdkrzt|d|ƒrztjd|d|d|d|ddd|djd �g}qª|j|t ddd �gd d �}n|j||dd d �}||d<dS) zE decl_body : declaration_specifiers init_declarator_list_opt rrQNr8rrJrKrL)r-rDrKrTr8rSrPr.)r;rST)rNrYrZ) rrVrWÚEnumrFr9rXr.r]r)rrwrNZtyZs_u_or_erYrrr Ú p_decl_body\s.   zCParser.p_decl_bodycCs|d|d<dS)z& declaration : decl_body SEMI rrNr)rrwrrr Ú p_declaration—szCParser.p_declarationcCs,t|ƒdkr|dn|d|d|d<dS)zj declaration_list : declaration | declaration_list declaration rQrrN)rF)rrwrrr Úp_declaration_list szCParser.p_declaration_listcCs|j|d|ddƒ|d<dS)zM declaration_specifiers : type_qualifier declaration_specifiers_opt rQrrJrN)rO)rrwrrr Úp_declaration_specifiers_1¦sz"CParser.p_declaration_specifiers_1cCs|j|d|ddƒ|d<dS)zM declaration_specifiers : type_specifier declaration_specifiers_opt rQrr8rN)rO)rrwrrr Úp_declaration_specifiers_2«sz"CParser.p_declaration_specifiers_2cCs|j|d|ddƒ|d<dS)zV declaration_specifiers : storage_class_specifier declaration_specifiers_opt rQrrKrN)rO)rrwrrr Úp_declaration_specifiers_3°sz"CParser.p_declaration_specifiers_3cCs|j|d|ddƒ|d<dS)zQ declaration_specifiers : function_specifier declaration_specifiers_opt rQrrLrN)rO)rrwrrr Úp_declaration_specifiers_4µsz"CParser.p_declaration_specifiers_4cCs|d|d<dS)zß storage_class_specifier : AUTO | REGISTER | STATIC | EXTERN | TYPEDEF rrNr)rrwrrr Úp_storage_class_specifierºsz!CParser.p_storage_class_specifiercCs|d|d<dS)z& function_specifier : INLINE rrNr)rrwrrr Úp_function_specifierÃszCParser.p_function_specifiercCs(tj|dg|j|jdƒƒd�|d<dS)aŒ type_specifier : VOID | _BOOL | CHAR | SHORT | INT | LONG | FLOAT | DOUBLE | _COMPLEX | SIGNED | UNSIGNED r)r.rN)rrEr3r€)rrwrrr Úp_type_specifier_1Ès zCParser.p_type_specifier_1cCs|d|d<dS)z� type_specifier : typedef_name | enum_specifier | struct_or_union_specifier rrNr)rrwrrr Úp_type_specifier_2×szCParser.p_type_specifier_2cCs|d|d<dS)zo type_qualifier : CONST | RESTRICT | VOLATILE rrNr)rrwrrr Úp_type_qualifierÞszCParser.p_type_qualifiercCs0t|ƒdkr|d|dgn|dg|d<dS)z„ init_declarator_list : init_declarator | init_declarator_list COMMA init_declarator r„rr‚rN)rF)rrwrrr Úp_init_declarator_list_1åsz CParser.p_init_declarator_list_1cCstd|dd�g|d<dS)z6 init_declarator_list : EQUALS initializer NrQ)r;rSr)r)rrwrrr Úp_init_declarator_list_2ðsz CParser.p_init_declarator_list_2cCst|ddd�g|d<dS)z7 init_declarator_list : abstract_declarator rN)r;rSr)r)rrwrrr Úp_init_declarator_list_3øsz CParser.p_init_declarator_list_3cCs,t|dt|ƒdkr|dndd�|d<dS)zb init_declarator : declarator | declarator EQUALS initializer rrQr‚N)r;rSr)rrF)rrwrrr Úp_init_declaratorszCParser.p_init_declaratorcCs|j|d|ddƒ|d<dS)zS specifier_qualifier_list : type_qualifier specifier_qualifier_list_opt rQrrJrN)rO)rrwrrr Úp_specifier_qualifier_list_1sz$CParser.p_specifier_qualifier_list_1cCs|j|d|ddƒ|d<dS)zS specifier_qualifier_list : type_specifier specifier_qualifier_list_opt rQrr8rN)rO)rrwrrr Úp_specifier_qualifier_list_2 sz$CParser.p_specifier_qualifier_list_2cCs4|j|dƒ}||dd|j|jdƒƒd�|d<dS)z{ struct_or_union_specifier : struct_or_union ID | struct_or_union TYPEID rrQN)r-rYr.r)rcr3r€)rrwÚklassrrr Úp_struct_or_union_specifier_1s z%CParser.p_struct_or_union_specifier_1cCs4|j|dƒ}|d|d|j|jdƒƒd�|d<dS)zd struct_or_union_specifier : struct_or_union brace_open struct_declaration_list brace_close rNr‚rQ)r-rYr.r)rcr3r€)rrwršrrr Úp_struct_or_union_specifier_2s z%CParser.p_struct_or_union_specifier_2cCs8|j|dƒ}||d|d|j|jdƒƒd�|d<dS)zÙ struct_or_union_specifier : struct_or_union ID brace_open struct_declaration_list brace_close | struct_or_union TYPEID brace_open struct_declaration_list brace_close rrQr„)r-rYr.rN)rcr3r€)rrwršrrr Úp_struct_or_union_specifier_3&s z%CParser.p_struct_or_union_specifier_3cCs|d|d<dS)zF struct_or_union : STRUCT | UNION rrNr)rrwrrr Úp_struct_or_union0szCParser.p_struct_or_unioncCs,t|ƒdkr|dn|d|d|d<dS)z� struct_declaration_list : struct_declaration | struct_declaration_list struct_declaration rQrrN)rF)rrwrrr Úp_struct_declaration_list8sz!CParser.p_struct_declaration_listcCsœ|d}|ddk r(|j||dd�}nht|dƒdkrx|dd}t|tjƒrV|}n tj|ƒ}|j|t|d�gd�}n|j|tddd�gd�}||d<dS) zW struct_declaration : specifier_qualifier_list struct_declarator_list_opt SEMI rrQN)rNrYr8r)r;)r;rS)r]rFr9rZNoderEr)rrwrNrYZnodeZ decl_typerrr Úp_struct_declaration_1>s"    zCParser.p_struct_declaration_1cCs(|j|dt|ddd�gd�|d<dS)zP struct_declaration : specifier_qualifier_list abstract_declarator SEMI rrQN)r;rS)rNrYr)r]r)rrwrrr Úp_struct_declaration_2ds zCParser.p_struct_declaration_2cCs0t|ƒdkr|d|dgn|dg|d<dS)zŠ struct_declarator_list : struct_declarator | struct_declarator_list COMMA struct_declarator r„rr‚rN)rF)rrwrrr Úp_struct_declarator_listrsz CParser.p_struct_declarator_listcCs|dddœ|d<dS)z( struct_declarator : declarator rN)r;rPrr)rrwrrr Úp_struct_declarator_1{szCParser.p_struct_declarator_1cCsDt|ƒdkr$|d|ddœ|d<ntjdddƒ|ddœ|d<dS)z€ struct_declarator : declarator COLON constant_expression | COLON constant_expression r‚r)r;rPrNrQ)rFrr:)rrwrrr Úp_struct_declarator_2€s zCParser.p_struct_declarator_2cCs&tj|dd|j|jdƒƒƒ|d<dS)zM enum_specifier : ENUM ID | ENUM TYPEID rQNrr)rr‡r3r€)rrwrrr Úp_enum_specifier_1‰szCParser.p_enum_specifier_1cCs&tjd|d|j|jdƒƒƒ|d<dS)zG enum_specifier : ENUM brace_open enumerator_list brace_close Nr‚rr)rr‡r3r€)rrwrrr Úp_enum_specifier_2�szCParser.p_enum_specifier_2cCs*tj|d|d|j|jdƒƒƒ|d<dS)z› enum_specifier : ENUM ID brace_open enumerator_list brace_close | ENUM TYPEID brace_open enumerator_list brace_close rQr„rrN)rr‡r3r€)rrwrrr Úp_enum_specifier_3”szCParser.p_enum_specifier_3cCsht|ƒdkr*tj|dg|djƒ|d<n:t|ƒdkrD|d|d<n |djj|dƒ|d|d<dS)z™ enumerator_list : enumerator | enumerator_list COMMA | enumerator_list COMMA enumerator rQrrr‚N)rFrZEnumeratorListr.Z enumeratorsr&)rrwrrr Úp_enumerator_listšs   zCParser.p_enumerator_listcCsjt|ƒdkr,tj|dd|j|jdƒƒƒ}n"tj|d|d|j|jdƒƒƒ}|j|j|jƒ||d<dS)zR enumerator : ID | ID EQUALS constant_expression rQrNr‚r)rFrZ Enumeratorr3r€r0r-r.)rrwZ enumeratorrrr Ú p_enumerator§s  zCParser.p_enumeratorcCs|d|d<dS)z) declarator : direct_declarator rrNr)rrwrrr Úp_declarator_1·szCParser.p_declarator_1cCs|j|d|dƒ|d<dS)z1 declarator : pointer direct_declarator rQrrN)r=)rrwrrr Úp_declarator_2¼szCParser.p_declarator_2cCs:tj|ddd|j|jdƒƒd�}|j||dƒ|d<dS)z& declarator : pointer TYPEID rQN)rCr8rDr.rr)rr:r3r€r=)rrwr;rrr Úp_declarator_3Ås zCParser.p_declarator_3cCs*tj|ddd|j|jdƒƒd�|d<dS)z" direct_declarator : ID rN)rCr8rDr.r)rr:r3r€)rrwrrr Úp_direct_declarator_1Ðs zCParser.p_direct_declarator_1cCs|d|d<dS)z8 direct_declarator : LPAREN declarator RPAREN rQrNr)rrwrrr Úp_direct_declarator_2ÙszCParser.p_direct_declarator_2cCsft|ƒdkr|dngpg}tjdt|ƒdkr6|dn|d||djd�}|j|d|d�|d<dS) zu direct_declarator : direct_declarator LBRACKET type_qualifier_list_opt assignment_expression_opt RBRACKET ér‚Nr„r)r8ÚdimÚ dim_qualsr.)r;r<r)rFrÚ ArrayDeclr.r=)rrwrDÚarrrrr Úp_direct_declarator_3ÞszCParser.p_direct_declarator_3cCs^dd„|d|dgDƒ}dd„|Dƒ}tjd|d||djd �}|j|d|d �|d <dS) zï direct_declarator : direct_declarator LBRACKET STATIC type_qualifier_list_opt assignment_expression RBRACKET | direct_declarator LBRACKET type_qualifier_list STATIC assignment_expression RBRACKET cSs g|]}t|tƒr|n|g‘qSr)r9Úlist)r@Úitemrrr rBósz1CParser.p_direct_declarator_4..r‚r„cSs"g|]}|D]}|dk r |‘q qS)Nr)r@ZsublistrJrrr rBõs Nr¯r)r8r°r±r.)r;r<r)rr²r.r=)rrwZ listed_qualsr±r³rrr Úp_direct_declarator_4ìszCParser.p_direct_declarator_4cCs^tjdtj|d|j|jdƒƒƒ|ddkr4|dng|djd�}|j|d|d�|d<dS)za direct_declarator : direct_declarator LBRACKET type_qualifier_list_opt TIMES RBRACKET Nr„r‚r)r8r°r±r.)r;r<r)rr²ÚIDr3r€r.r=)rrwr³rrr Úp_direct_declarator_5s zCParser.p_direct_declarator_5cCs|tj|dd|djd�}|jƒjdkrb|jdk rbx.|jjD]"}t|tjƒrNP|j |j |jƒqW||_|d|d<n||d<dS)zm pointer : TIMES type_qualifier_list_opt | TIMES type_qualifier_list_opt pointer rrQN)rDr8r.r‚r)r3r€rZPtrDeclrFr8)rrwr.Z nested_typeZ tail_typerrr Ú p_pointer(s   zCParser.p_pointercCs0t|ƒdkr|dgn|d|dg|d<dS)zs type_qualifier_list : type_qualifier | type_qualifier_list type_qualifier rQrrN)rF)rrwrrr Úp_type_qualifier_listFszCParser.p_type_qualifier_listcCs>t|ƒdkr.|djjtj|j|jdƒƒƒƒ|d|d<dS)zn parameter_type_list : parameter_list | parameter_list COMMA ELLIPSIS rQrr‚rN)rFr¼r&rr½r3r€)rrwrrr Úp_parameter_type_listLs "zCParser.p_parameter_type_listcCsNt|ƒdkr*tj|dg|djƒ|d<n |djj|dƒ|d|d<dS)zz parameter_list : parameter_declaration | parameter_list COMMA parameter_declaration rQrrr‚N)rFrÚ ParamListr.r¼r&)rrwrrr Úp_parameter_listUs zCParser.p_parameter_listcCsX|d}|ds2tjdg|j|jdƒƒd�g|d<|j|t|dd�gd�d|d<d S) zE parameter_declaration : declaration_specifiers declarator rr8r>)r.rQ)r;)rNrYrN)rrEr3r€r]r)rrwrNrrr Úp_parameter_declaration_1_sz!CParser.p_parameter_declaration_1cCsæ|d}|ds2tjdg|j|jdƒƒd�g|d<t|dƒdkr’t|dd jƒdkr’|j|ddjdƒr’|j|t|ddd�gd �d}nHtj d |d |dp²tj dddƒ|j|jdƒƒd �}|d}|j ||ƒ}||d<dS)zR parameter_declaration : declaration_specifiers abstract_declarator_opt rr8r>)r.rrQN)r;rS)rNrYr rJ)r-rDr8r.r*r*) rrEr3r€rFr?r2r]rÚTypenamer:rI)rrwrNr;rHrrr Úp_parameter_declaration_2js"& z!CParser.p_parameter_declaration_2cCsNt|ƒdkr*tj|dg|djƒ|d<n |djj|dƒ|d|d<dS)ze identifier_list : identifier | identifier_list COMMA identifier rQrrr‚N)rFrrÃr.r¼r&)rrwrrr Úp_identifier_list‰s zCParser.p_identifier_listcCs|d|d<dS)z- initializer : assignment_expression rrNr)rrwrrr Úp_initializer_1“szCParser.p_initializer_1cCs:|ddkr*tjg|j|jdƒƒƒ|d<n |d|d<dS)z‹ initializer : brace_open initializer_list_opt brace_close | brace_open initializer_list COMMA brace_close rQNrr)rÚInitListr3r€)rrwrrr Úp_initializer_2˜s zCParser.p_initializer_2cCs–t|ƒdkrN|ddkr |dntj|d|dƒ}tj|g|djƒ|d<nD|ddkrb|dntj|d|dƒ}|djj|ƒ|d|d<dS)z� initializer_list : designation_opt initializer | initializer_list COMMA designation_opt initializer r‚rNrQrr„)rFrZNamedInitializerrÊr.Úexprsr&)rrwrSrrr Úp_initializer_list¡s  ((zCParser.p_initializer_listcCs|d|d<dS)z. designation : designator_list EQUALS rrNr)rrwrrr Ú p_designation­szCParser.p_designationcCs0t|ƒdkr|dgn|d|dg|d<dS)z_ designator_list : designator | designator_list designator rQrrN)rF)rrwrrr Úp_designator_listµszCParser.p_designator_listcCs|d|d<dS)zi designator : LBRACKET constant_expression RBRACKET | PERIOD identifier rQrNr)rrwrrr Ú p_designator»szCParser.p_designatorcCsTtjd|dd|dp$tjdddƒ|j|jdƒƒd�}|j||ddƒ|d<dS) zH type_name : specifier_qualifier_list abstract_declarator_opt r rrJrQN)r-rDr8r.r8r)rrÆr:r3r€rI)rrwrHrrr Ú p_type_nameÁs  zCParser.p_type_namecCs(tjdddƒ}|j||dd�|d<dS)z+ abstract_declarator : pointer Nr)r;r<r)rr:r=)rrwZ dummytyperrr Úp_abstract_declarator_1ÒszCParser.p_abstract_declarator_1cCs|j|d|dƒ|d<dS)zF abstract_declarator : pointer direct_abstract_declarator rQrrN)r=)rrwrrr Úp_abstract_declarator_2ÚszCParser.p_abstract_declarator_2cCs|d|d<dS)z> abstract_declarator : direct_abstract_declarator rrNr)rrwrrr Úp_abstract_declarator_3ßszCParser.p_abstract_declarator_3cCs|d|d<dS)zA direct_abstract_declarator : LPAREN abstract_declarator RPAREN rQrNr)rrwrrr Úp_direct_abstract_declarator_1ész&CParser.p_direct_abstract_declarator_1cCs6tjd|dg|djd�}|j|d|d�|d<dS)zn direct_abstract_declarator : direct_abstract_declarator LBRACKET assignment_expression_opt RBRACKET Nr‚r)r8r°r±r.)r;r<r)rr²r.r=)rrwr³rrr Úp_direct_abstract_declarator_2ís z&CParser.p_direct_abstract_declarator_2cCs4tjtjdddƒ|dg|j|jdƒƒd�|d<dS)zS direct_abstract_declarator : LBRACKET assignment_expression_opt RBRACKET NrQr)r8r°r±r.r)rr²r:r3r€)rrwrrr Úp_direct_abstract_declarator_3øs  z&CParser.p_direct_abstract_declarator_3cCsJtjdtj|d|j|jdƒƒƒg|djd�}|j|d|d�|d<dS)zZ direct_abstract_declarator : direct_abstract_declarator LBRACKET TIMES RBRACKET Nr‚r)r8r°r±r.)r;r<r)rr²r¸r3r€r.r=)rrwr³rrr Úp_direct_abstract_declarator_4s z&CParser.p_direct_abstract_declarator_4cCsHtjtjdddƒtj|d|j|jdƒƒƒg|j|jdƒƒd�|d<dS)z? direct_abstract_declarator : LBRACKET TIMES RBRACKET Nr‚r)r8r°r±r.r)rr²r:r¸r3r€)rrwrrr Úp_direct_abstract_declarator_5 s  z&CParser.p_direct_abstract_declarator_5cCs4tj|dd|djd�}|j|d|d�|d<dS)zh direct_abstract_declarator : direct_abstract_declarator LPAREN parameter_type_list_opt RPAREN r‚Nr)rºr8r.)r;r<r)rrGr.r=)rrwr¾rrr Úp_direct_abstract_declarator_6s z&CParser.p_direct_abstract_declarator_6cCs2tj|dtjdddƒ|j|jdƒƒd�|d<dS)zM direct_abstract_declarator : LPAREN parameter_type_list_opt RPAREN rQNr)rºr8r.r)rrGr:r3r€)rrwrrr Úp_direct_abstract_declarator_7s z&CParser.p_direct_abstract_declarator_7cCs(t|dtƒr|dn|dg|d<dS)zG block_item : declaration | statement rrN)r9rµ)rrwrrr Ú p_block_item*szCParser.p_block_itemcCs:t|ƒdks|ddgkr"|dn|d|d|d<dS)z_ block_item_list : block_item | block_item_list block_item rQNrr)rF)rrwrrr Úp_block_item_list2szCParser.p_block_item_listcCs&tj|d|j|jdƒƒd�|d<dS)zA compound_statement : brace_open block_item_list_opt brace_close rQr)Z block_itemsr.rN)rZCompoundr3r€)rrwrrr Úp_compound_statement_19szCParser.p_compound_statement_1cCs*tj|d|d|j|jdƒƒƒ|d<dS)z( labeled_statement : ID COLON statement rr‚rN)rZLabelr3r€)rrwrrr Úp_labeled_statement_1?szCParser.p_labeled_statement_1cCs,tj|d|dg|j|jdƒƒƒ|d<dS)z> labeled_statement : CASE constant_expression COLON statement rQr„rrN)rZCaser3r€)rrwrrr Úp_labeled_statement_2CszCParser.p_labeled_statement_2cCs&tj|dg|j|jdƒƒƒ|d<dS)z- labeled_statement : DEFAULT COLON statement r‚rrN)rZDefaultr3r€)rrwrrr Úp_labeled_statement_3GszCParser.p_labeled_statement_3cCs,tj|d|dd|j|jdƒƒƒ|d<dS)z= selection_statement : IF LPAREN expression RPAREN statement r‚r¯Nrr)rÚIfr3r€)rrwrrr Úp_selection_statement_1KszCParser.p_selection_statement_1cCs0tj|d|d|d|j|jdƒƒƒ|d<dS)zL selection_statement : IF LPAREN expression RPAREN statement ELSE statement r‚r¯érrN)rrâr3r€)rrwrrr Úp_selection_statement_2OszCParser.p_selection_statement_2cCs.ttj|d|d|j|jdƒƒƒƒ|d<dS)zA selection_statement : SWITCH LPAREN expression RPAREN statement r‚r¯rrN)r rZSwitchr3r€)rrwrrr Úp_selection_statement_3SszCParser.p_selection_statement_3cCs*tj|d|d|j|jdƒƒƒ|d<dS)z@ iteration_statement : WHILE LPAREN expression RPAREN statement r‚r¯rrN)rZWhiler3r€)rrwrrr Úp_iteration_statement_1XszCParser.p_iteration_statement_1cCs*tj|d|d|j|jdƒƒƒ|d<dS)zH iteration_statement : DO statement WHILE LPAREN expression RPAREN SEMI r¯rQrrN)rZDoWhiler3r€)rrwrrr Úp_iteration_statement_2\szCParser.p_iteration_statement_2cCs6tj|d|d|d|d|j|jdƒƒƒ|d<dS)zj iteration_statement : FOR LPAREN expression_opt SEMI expression_opt SEMI expression_opt RPAREN statement r‚r¯räé rrN)rÚForr3r€)rrwrrr Úp_iteration_statement_3`szCParser.p_iteration_statement_3cCsJtjtj|d|j|jdƒƒƒ|d|d|d|j|jdƒƒƒ|d<dS)zb iteration_statement : FOR LPAREN declaration expression_opt SEMI expression_opt RPAREN statement r‚rr„éérN)rrêZDeclListr3r€)rrwrrr Úp_iteration_statement_4dszCParser.p_iteration_statement_4cCs$tj|d|j|jdƒƒƒ|d<dS)z jump_statement : GOTO ID SEMI rQrrN)rZGotor3r€)rrwrrr Úp_jump_statement_1iszCParser.p_jump_statement_1cCstj|j|jdƒƒƒ|d<dS)z jump_statement : BREAK SEMI rrN)rZBreakr3r€)rrwrrr Úp_jump_statement_2mszCParser.p_jump_statement_2cCstj|j|jdƒƒƒ|d<dS)z! jump_statement : CONTINUE SEMI rrN)rZContinuer3r€)rrwrrr Úp_jump_statement_3qszCParser.p_jump_statement_3cCs4tjt|ƒdkr|dnd|j|jdƒƒƒ|d<dS)z\ jump_statement : RETURN expression SEMI | RETURN SEMI r„rQNrr)rZReturnrFr3r€)rrwrrr Úp_jump_statement_4uszCParser.p_jump_statement_4cCs8|ddkr(tj|j|jdƒƒƒ|d<n |d|d<dS)z, expression_statement : expression_opt SEMI rNr)rZEmptyStatementr3r€)rrwrrr Úp_expression_statement{s zCParser.p_expression_statementcCsjt|ƒdkr|d|d<nLt|dtjƒsFtj|dg|djƒ|d<|djj|dƒ|d|d<dS)zn expression : assignment_expression | expression COMMA assignment_expression rQrrr‚N)rFr9rÚExprListr.rÌr&)rrwrrr Ú p_expression‚s  zCParser.p_expressioncCs(tj|dg|j|jdƒƒd�|d<dS)z typedef_name : TYPEID r)r.rN)rrEr3r€)rrwrrr Úp_typedef_name�szCParser.p_typedef_namecCsDt|ƒdkr|d|d<n&tj|d|d|d|djƒ|d<dS)z› assignment_expression : conditional_expression | unary_expression assignment_operator assignment_expression rQrrr‚N)rFrZ Assignmentr.)rrwrrr Úp_assignment_expression“s zCParser.p_assignment_expressioncCs|d|d<dS)aÞ assignment_operator : EQUALS | XOREQUAL | TIMESEQUAL | DIVEQUAL | MODEQUAL | PLUSEQUAL | MINUSEQUAL | LSHIFTEQUAL | RSHIFTEQUAL | ANDEQUAL | OREQUAL rrNr)rrwrrr Úp_assignment_operator¡s zCParser.p_assignment_operatorcCs|d|d<dS)z. constant_expression : conditional_expression rrNr)rrwrrr Úp_constant_expression°szCParser.p_constant_expressioncCsDt|ƒdkr|d|d<n&tj|d|d|d|djƒ|d<dS)zœ conditional_expression : binary_expression | binary_expression CONDOP expression COLON conditional_expression rQrrr‚r¯N)rFrZ TernaryOpr.)rrwrrr Úp_conditional_expression´s z CParser.p_conditional_expressioncCsDt|ƒdkr|d|d<n&tj|d|d|d|djƒ|d<dS)ak binary_expression : cast_expression | binary_expression TIMES binary_expression | binary_expression DIVIDE binary_expression | binary_expression MOD binary_expression | binary_expression PLUS binary_expression | binary_expression MINUS binary_expression | binary_expression RSHIFT binary_expression | binary_expression LSHIFT binary_expression | binary_expression LT binary_expression | binary_expression LE binary_expression | binary_expression GE binary_expression | binary_expression GT binary_expression | binary_expression EQ binary_expression | binary_expression NE binary_expression | binary_expression AND binary_expression | binary_expression OR binary_expression | binary_expression XOR binary_expression | binary_expression LAND binary_expression | binary_expression LOR binary_expression rQrrr‚N)rFrZBinaryOpr.)rrwrrr Úp_binary_expression½s zCParser.p_binary_expressioncCs|d|d<dS)z$ cast_expression : unary_expression rrNr)rrwrrr Úp_cast_expression_1×szCParser.p_cast_expression_1cCs*tj|d|d|j|jdƒƒƒ|d<dS)z; cast_expression : LPAREN type_name RPAREN cast_expression rQr„rrN)rZCastr3r€)rrwrrr Úp_cast_expression_2ÛszCParser.p_cast_expression_2cCs|d|d<dS)z* unary_expression : postfix_expression rrNr)rrwrrr Úp_unary_expression_1ßszCParser.p_unary_expression_1cCs$tj|d|d|djƒ|d<dS)z¸ unary_expression : PLUSPLUS unary_expression | MINUSMINUS unary_expression | unary_operator cast_expression rrQrN)rÚUnaryOpr.)rrwrrr Úp_unary_expression_2ãszCParser.p_unary_expression_2cCs>tj|dt|ƒdkr|dn|d|j|jdƒƒƒ|d<dS)zx unary_expression : SIZEOF unary_expression | SIZEOF LPAREN type_name RPAREN rr‚rQrN)rrÿrFr3r€)rrwrrr Úp_unary_expression_3êszCParser.p_unary_expression_3cCs|d|d<dS)zÏ unary_operator : AND | TIMES | PLUS | MINUS | NOT | LNOT rrNr)rrwrrr Úp_unary_operatorószCParser.p_unary_operatorcCs|d|d<dS)z* postfix_expression : primary_expression rrNr)rrwrrr Úp_postfix_expression_1ýszCParser.p_postfix_expression_1cCs$tj|d|d|djƒ|d<dS)zG postfix_expression : postfix_expression LBRACKET expression RBRACKET rr‚rN)rZArrayRefr.)rrwrrr Úp_postfix_expression_2szCParser.p_postfix_expression_2cCs4tj|dt|ƒdkr|dnd|djƒ|d<dS)zœ postfix_expression : postfix_expression LPAREN argument_expression_list RPAREN | postfix_expression LPAREN RPAREN rr¯r‚Nr)rÚFuncCallrFr.)rrwrrr Úp_postfix_expression_3szCParser.p_postfix_expression_3cCsBtj|d|j|jdƒƒƒ}tj|d|d||djƒ|d<dS)zÿ postfix_expression : postfix_expression PERIOD ID | postfix_expression PERIOD TYPEID | postfix_expression ARROW ID | postfix_expression ARROW TYPEID r‚rrQrN)rr¸r3r€Z StructRefr.)rrwZfieldrrr Úp_postfix_expression_4 szCParser.p_postfix_expression_4cCs(tjd|d|d|djƒ|d<dS)z{ postfix_expression : postfix_expression PLUSPLUS | postfix_expression MINUSMINUS rwrQrrN)rrÿr.)rrwrrr Úp_postfix_expression_5szCParser.p_postfix_expression_5cCstj|d|dƒ|d<dS)zÇ postfix_expression : LPAREN type_name RPAREN brace_open initializer_list brace_close | LPAREN type_name RPAREN brace_open initializer_list COMMA brace_close rQr¯rN)rZCompoundLiteral)rrwrrr Úp_postfix_expression_6szCParser.p_postfix_expression_6cCs|d|d<dS)z" primary_expression : identifier rrNr)rrwrrr Úp_primary_expression_1 szCParser.p_primary_expression_1cCs|d|d<dS)z primary_expression : constant rrNr)rrwrrr Úp_primary_expression_2$szCParser.p_primary_expression_2cCs|d|d<dS)zp primary_expression : unified_string_literal | unified_wstring_literal rrNr)rrwrrr Úp_primary_expression_3(szCParser.p_primary_expression_3cCs|d|d<dS)z0 primary_expression : LPAREN expression RPAREN rQrNr)rrwrrr Úp_primary_expression_4.szCParser.p_primary_expression_4cCsF|j|jdƒƒ}tjtj|d|ƒtj|d|dg|ƒ|ƒ|d<dS)zQ primary_expression : OFFSETOF LPAREN type_name COMMA identifier RPAREN rr‚r¯rN)r3r€rrr¸rô)rrwr.rrr Úp_primary_expression_52szCParser.p_primary_expression_5cCsNt|ƒdkr*tj|dg|djƒ|d<n |djj|dƒ|d|d<dS)zœ argument_expression_list : assignment_expression | argument_expression_list COMMA assignment_expression rQrrr‚N)rFrrôr.rÌr&)rrwrrr Úp_argument_expression_list:s z"CParser.p_argument_expression_listcCs$tj|d|j|jdƒƒƒ|d<dS)z identifier : ID rrN)rr¸r3r€)rrwrrr Ú p_identifierDszCParser.p_identifiercCs&tjd|d|j|jdƒƒƒ|d<dS)z� constant : INT_CONST_DEC | INT_CONST_OCT | INT_CONST_HEX | INT_CONST_BIN r>rrN)rÚConstantr3r€)rrwrrr Ú p_constant_1HszCParser.p_constant_1cCs&tjd|d|j|jdƒƒƒ|d<dS)zM constant : FLOAT_CONST | HEX_FLOAT_CONST ÚfloatrrN)rrr3r€)rrwrrr Ú p_constant_2QszCParser.p_constant_2cCs&tjd|d|j|jdƒƒƒ|d<dS)zH constant : CHAR_CONST | WCHAR_CONST ÚcharrrN)rrr3r€)rrwrrr Ú p_constant_3XszCParser.p_constant_3cCsht|ƒdkr0tjd|d|j|jdƒƒƒ|d<n4|djdd…|ddd…|d_|d|d<dS)z~ unified_string_literal : STRING_LITERAL | unified_string_literal STRING_LITERAL rQÚstringrrNr*)rFrrr3r€Úvalue)rrwrrr Úp_unified_string_literalds   (z CParser.p_unified_string_literalcCslt|ƒdkr0tjd|d|j|jdƒƒƒ|d<n8|djjƒdd…|ddd…|d_|d|d<dS)z� unified_wstring_literal : WSTRING_LITERAL | unified_wstring_literal WSTRING_LITERAL rQrrrNr*)rFrrr3r€rÚrstrip)rrwrrr Úp_unified_wstring_literalos   ,z!CParser.p_unified_wstring_literalcCs|d|d<dS)z brace_open : LBRACE rrNr)rrwrrr Ú p_brace_openzszCParser.p_brace_opencCs|d|d<dS)z brace_close : RBRACE rrNr)rrwrrr Ú p_brace_closeszCParser.p_brace_closecCs d|d<dS)zempty : Nrr)rrwrrr Úp_empty„szCParser.p_emptycCs<|r,|jd|j|j|j|jj|ƒd�ƒn |jddƒdS)Nz before: %s)r€r6zAt end of inputr )r,rr3r€rZfind_tok_column)rrwrrr Úp_errorˆs zCParser.p_errorN)Tr Tr Fr )r r)F©rdre©rdrf©rdrg©rdrh©rdri©rdrjrk©rdrlrmrnro©rdrprq©rdrrrs©rdrtrurv) r r!r"r#r$r%r&r'r(r))˜Ú__name__Ú __module__Ú __qualname__r!r$r'r)r/r0r2rrrrr7r=rIrOr]r`rcZ precedencerxryr{r|r}r~rr�rƒr…r†rˆr‰rŠr‹rŒr�rŽr�r�r‘r’r“r”r•r–r—r˜r™r›rœr�ržrŸr r¡r¢r£r¤r¥r¦r§r¨r©rªr«r¬r­r®r´r·r¹r¿rÀrÁrÂrÄrÅrÇrÈrÉrËrÍrÎrÏrÐrÑrÒrÓrÔrÕrÖr×rØrÙrÚrÛrÜrÝrÞrßràrárãrårærçrèrërîrïrðrñròrórõrör÷rørùrúrûrürýrþrrrrrrrrr r r r r rrrrrrrrrrrrrrrr r sF c     )7- Y      ;      &                                            r Ú__main__)ÚreZplyrr rZc_lexerrZ plyparserrrrZast_transformsr r r*ÚpprintZtimeÚsysrrrr Ú s,