c_parser.py 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  1. #------------------------------------------------------------------------------
  2. # pycparser: c_parser.py
  3. #
  4. # CParser class: Parser and AST builder for the C language
  5. #
  6. # Eli Bendersky [https://eli.thegreenplace.net/]
  7. # License: BSD
  8. #------------------------------------------------------------------------------
  9. import re
  10. from .ply import yacc
  11. from . import c_ast
  12. from .c_lexer import CLexer
  13. from .plyparser import PLYParser, Coord, ParseError, parameterized, template
  14. from .ast_transforms import fix_switch_cases
  15. @template
  16. class CParser(PLYParser):
  17. def __init__(
  18. self,
  19. lex_optimize=True,
  20. lexer=CLexer,
  21. lextab='pycparser.lextab',
  22. yacc_optimize=True,
  23. yacctab='pycparser.yacctab',
  24. yacc_debug=False,
  25. taboutputdir=''):
  26. """ Create a new CParser.
  27. Some arguments for controlling the debug/optimization
  28. level of the parser are provided. The defaults are
  29. tuned for release/performance mode.
  30. The simple rules for using them are:
  31. *) When tweaking CParser/CLexer, set these to False
  32. *) When releasing a stable parser, set to True
  33. lex_optimize:
  34. Set to False when you're modifying the lexer.
  35. Otherwise, changes in the lexer won't be used, if
  36. some lextab.py file exists.
  37. When releasing with a stable lexer, set to True
  38. to save the re-generation of the lexer table on
  39. each run.
  40. lexer:
  41. Set this parameter to define the lexer to use if
  42. you're not using the default CLexer.
  43. lextab:
  44. Points to the lex table that's used for optimized
  45. mode. Only if you're modifying the lexer and want
  46. some tests to avoid re-generating the table, make
  47. this point to a local lex table file (that's been
  48. earlier generated with lex_optimize=True)
  49. yacc_optimize:
  50. Set to False when you're modifying the parser.
  51. Otherwise, changes in the parser won't be used, if
  52. some parsetab.py file exists.
  53. When releasing with a stable parser, set to True
  54. to save the re-generation of the parser table on
  55. each run.
  56. yacctab:
  57. Points to the yacc table that's used for optimized
  58. mode. Only if you're modifying the parser, make
  59. this point to a local yacc table file
  60. yacc_debug:
  61. Generate a parser.out file that explains how yacc
  62. built the parsing table from the grammar.
  63. taboutputdir:
  64. Set this parameter to control the location of generated
  65. lextab and yacctab files.
  66. """
  67. self.clex = lexer(
  68. error_func=self._lex_error_func,
  69. on_lbrace_func=self._lex_on_lbrace_func,
  70. on_rbrace_func=self._lex_on_rbrace_func,
  71. type_lookup_func=self._lex_type_lookup_func)
  72. self.clex.build(
  73. optimize=lex_optimize,
  74. lextab=lextab,
  75. outputdir=taboutputdir)
  76. self.tokens = self.clex.tokens
  77. rules_with_opt = [
  78. 'abstract_declarator',
  79. 'assignment_expression',
  80. 'declaration_list',
  81. 'declaration_specifiers_no_type',
  82. 'designation',
  83. 'expression',
  84. 'identifier_list',
  85. 'init_declarator_list',
  86. 'id_init_declarator_list',
  87. 'initializer_list',
  88. 'parameter_type_list',
  89. 'block_item_list',
  90. 'type_qualifier_list',
  91. 'struct_declarator_list'
  92. ]
  93. for rule in rules_with_opt:
  94. self._create_opt_rule(rule)
  95. self.cparser = yacc.yacc(
  96. module=self,
  97. start='translation_unit_or_empty',
  98. debug=yacc_debug,
  99. optimize=yacc_optimize,
  100. tabmodule=yacctab,
  101. outputdir=taboutputdir)
  102. # Stack of scopes for keeping track of symbols. _scope_stack[-1] is
  103. # the current (topmost) scope. Each scope is a dictionary that
  104. # specifies whether a name is a type. If _scope_stack[n][name] is
  105. # True, 'name' is currently a type in the scope. If it's False,
  106. # 'name' is used in the scope but not as a type (for instance, if we
  107. # saw: int name;
  108. # If 'name' is not a key in _scope_stack[n] then 'name' was not defined
  109. # in this scope at all.
  110. self._scope_stack = [dict()]
  111. # Keeps track of the last token given to yacc (the lookahead token)
  112. self._last_yielded_token = None
  113. def parse(self, text, filename='', debuglevel=0):
  114. """ Parses C code and returns an AST.
  115. text:
  116. A string containing the C source code
  117. filename:
  118. Name of the file being parsed (for meaningful
  119. error messages)
  120. debuglevel:
  121. Debug level to yacc
  122. """
  123. self.clex.filename = filename
  124. self.clex.reset_lineno()
  125. self._scope_stack = [dict()]
  126. self._last_yielded_token = None
  127. return self.cparser.parse(
  128. input=text,
  129. lexer=self.clex,
  130. debug=debuglevel)
  131. ######################-- PRIVATE --######################
  132. def _push_scope(self):
  133. self._scope_stack.append(dict())
  134. def _pop_scope(self):
  135. assert len(self._scope_stack) > 1
  136. self._scope_stack.pop()
  137. def _add_typedef_name(self, name, coord):
  138. """ Add a new typedef name (ie a TYPEID) to the current scope
  139. """
  140. if not self._scope_stack[-1].get(name, True):
  141. self._parse_error(
  142. "Typedef %r previously declared as non-typedef "
  143. "in this scope" % name, coord)
  144. self._scope_stack[-1][name] = True
  145. def _add_identifier(self, name, coord):
  146. """ Add a new object, function, or enum member name (ie an ID) to the
  147. current scope
  148. """
  149. if self._scope_stack[-1].get(name, False):
  150. self._parse_error(
  151. "Non-typedef %r previously declared as typedef "
  152. "in this scope" % name, coord)
  153. self._scope_stack[-1][name] = False
  154. def _is_type_in_scope(self, name):
  155. """ Is *name* a typedef-name in the current scope?
  156. """
  157. for scope in reversed(self._scope_stack):
  158. # If name is an identifier in this scope it shadows typedefs in
  159. # higher scopes.
  160. in_scope = scope.get(name)
  161. if in_scope is not None: return in_scope
  162. return False
  163. def _lex_error_func(self, msg, line, column):
  164. self._parse_error(msg, self._coord(line, column))
  165. def _lex_on_lbrace_func(self):
  166. self._push_scope()
  167. def _lex_on_rbrace_func(self):
  168. self._pop_scope()
  169. def _lex_type_lookup_func(self, name):
  170. """ Looks up types that were previously defined with
  171. typedef.
  172. Passed to the lexer for recognizing identifiers that
  173. are types.
  174. """
  175. is_type = self._is_type_in_scope(name)
  176. return is_type
  177. def _get_yacc_lookahead_token(self):
  178. """ We need access to yacc's lookahead token in certain cases.
  179. This is the last token yacc requested from the lexer, so we
  180. ask the lexer.
  181. """
  182. return self.clex.last_token
  183. # To understand what's going on here, read sections A.8.5 and
  184. # A.8.6 of K&R2 very carefully.
  185. #
  186. # A C type consists of a basic type declaration, with a list
  187. # of modifiers. For example:
  188. #
  189. # int *c[5];
  190. #
  191. # The basic declaration here is 'int c', and the pointer and
  192. # the array are the modifiers.
  193. #
  194. # Basic declarations are represented by TypeDecl (from module c_ast) and the
  195. # modifiers are FuncDecl, PtrDecl and ArrayDecl.
  196. #
  197. # The standard states that whenever a new modifier is parsed, it should be
  198. # added to the end of the list of modifiers. For example:
  199. #
  200. # K&R2 A.8.6.2: Array Declarators
  201. #
  202. # In a declaration T D where D has the form
  203. # D1 [constant-expression-opt]
  204. # and the type of the identifier in the declaration T D1 is
  205. # "type-modifier T", the type of the
  206. # identifier of D is "type-modifier array of T"
  207. #
  208. # This is what this method does. The declarator it receives
  209. # can be a list of declarators ending with TypeDecl. It
  210. # tacks the modifier to the end of this list, just before
  211. # the TypeDecl.
  212. #
  213. # Additionally, the modifier may be a list itself. This is
  214. # useful for pointers, that can come as a chain from the rule
  215. # p_pointer. In this case, the whole modifier list is spliced
  216. # into the new location.
  217. def _type_modify_decl(self, decl, modifier):
  218. """ Tacks a type modifier on a declarator, and returns
  219. the modified declarator.
  220. Note: the declarator and modifier may be modified
  221. """
  222. #~ print '****'
  223. #~ decl.show(offset=3)
  224. #~ modifier.show(offset=3)
  225. #~ print '****'
  226. modifier_head = modifier
  227. modifier_tail = modifier
  228. # The modifier may be a nested list. Reach its tail.
  229. #
  230. while modifier_tail.type:
  231. modifier_tail = modifier_tail.type
  232. # If the decl is a basic type, just tack the modifier onto
  233. # it
  234. #
  235. if isinstance(decl, c_ast.TypeDecl):
  236. modifier_tail.type = decl
  237. return modifier
  238. else:
  239. # Otherwise, the decl is a list of modifiers. Reach
  240. # its tail and splice the modifier onto the tail,
  241. # pointing to the underlying basic type.
  242. #
  243. decl_tail = decl
  244. while not isinstance(decl_tail.type, c_ast.TypeDecl):
  245. decl_tail = decl_tail.type
  246. modifier_tail.type = decl_tail.type
  247. decl_tail.type = modifier_head
  248. return decl
  249. # Due to the order in which declarators are constructed,
  250. # they have to be fixed in order to look like a normal AST.
  251. #
  252. # When a declaration arrives from syntax construction, it has
  253. # these problems:
  254. # * The innermost TypeDecl has no type (because the basic
  255. # type is only known at the uppermost declaration level)
  256. # * The declaration has no variable name, since that is saved
  257. # in the innermost TypeDecl
  258. # * The typename of the declaration is a list of type
  259. # specifiers, and not a node. Here, basic identifier types
  260. # should be separated from more complex types like enums
  261. # and structs.
  262. #
  263. # This method fixes these problems.
  264. #
  265. def _fix_decl_name_type(self, decl, typename):
  266. """ Fixes a declaration. Modifies decl.
  267. """
  268. # Reach the underlying basic type
  269. #
  270. type = decl
  271. while not isinstance(type, c_ast.TypeDecl):
  272. type = type.type
  273. decl.name = type.declname
  274. type.quals = decl.quals
  275. # The typename is a list of types. If any type in this
  276. # list isn't an IdentifierType, it must be the only
  277. # type in the list (it's illegal to declare "int enum ..")
  278. # If all the types are basic, they're collected in the
  279. # IdentifierType holder.
  280. #
  281. for tn in typename:
  282. if not isinstance(tn, c_ast.IdentifierType):
  283. if len(typename) > 1:
  284. self._parse_error(
  285. "Invalid multiple types specified", tn.coord)
  286. else:
  287. type.type = tn
  288. return decl
  289. if not typename:
  290. # Functions default to returning int
  291. #
  292. if not isinstance(decl.type, c_ast.FuncDecl):
  293. self._parse_error(
  294. "Missing type in declaration", decl.coord)
  295. type.type = c_ast.IdentifierType(
  296. ['int'],
  297. coord=decl.coord)
  298. else:
  299. # At this point, we know that typename is a list of IdentifierType
  300. # nodes. Concatenate all the names into a single list.
  301. #
  302. type.type = c_ast.IdentifierType(
  303. [name for id in typename for name in id.names],
  304. coord=typename[0].coord)
  305. return decl
  306. def _add_declaration_specifier(self, declspec, newspec, kind, append=False):
  307. """ Declaration specifiers are represented by a dictionary
  308. with the entries:
  309. * qual: a list of type qualifiers
  310. * storage: a list of storage type qualifiers
  311. * type: a list of type specifiers
  312. * function: a list of function specifiers
  313. This method is given a declaration specifier, and a
  314. new specifier of a given kind.
  315. If `append` is True, the new specifier is added to the end of
  316. the specifiers list, otherwise it's added at the beginning.
  317. Returns the declaration specifier, with the new
  318. specifier incorporated.
  319. """
  320. spec = declspec or dict(qual=[], storage=[], type=[], function=[])
  321. if append:
  322. spec[kind].append(newspec)
  323. else:
  324. spec[kind].insert(0, newspec)
  325. return spec
  326. def _build_declarations(self, spec, decls, typedef_namespace=False):
  327. """ Builds a list of declarations all sharing the given specifiers.
  328. If typedef_namespace is true, each declared name is added
  329. to the "typedef namespace", which also includes objects,
  330. functions, and enum constants.
  331. """
  332. is_typedef = 'typedef' in spec['storage']
  333. declarations = []
  334. # Bit-fields are allowed to be unnamed.
  335. #
  336. if decls[0].get('bitsize') is not None:
  337. pass
  338. # When redeclaring typedef names as identifiers in inner scopes, a
  339. # problem can occur where the identifier gets grouped into
  340. # spec['type'], leaving decl as None. This can only occur for the
  341. # first declarator.
  342. #
  343. elif decls[0]['decl'] is None:
  344. if len(spec['type']) < 2 or len(spec['type'][-1].names) != 1 or \
  345. not self._is_type_in_scope(spec['type'][-1].names[0]):
  346. coord = '?'
  347. for t in spec['type']:
  348. if hasattr(t, 'coord'):
  349. coord = t.coord
  350. break
  351. self._parse_error('Invalid declaration', coord)
  352. # Make this look as if it came from "direct_declarator:ID"
  353. decls[0]['decl'] = c_ast.TypeDecl(
  354. declname=spec['type'][-1].names[0],
  355. type=None,
  356. quals=None,
  357. coord=spec['type'][-1].coord)
  358. # Remove the "new" type's name from the end of spec['type']
  359. del spec['type'][-1]
  360. # A similar problem can occur where the declaration ends up looking
  361. # like an abstract declarator. Give it a name if this is the case.
  362. #
  363. elif not isinstance(decls[0]['decl'],
  364. (c_ast.Struct, c_ast.Union, c_ast.IdentifierType)):
  365. decls_0_tail = decls[0]['decl']
  366. while not isinstance(decls_0_tail, c_ast.TypeDecl):
  367. decls_0_tail = decls_0_tail.type
  368. if decls_0_tail.declname is None:
  369. decls_0_tail.declname = spec['type'][-1].names[0]
  370. del spec['type'][-1]
  371. for decl in decls:
  372. assert decl['decl'] is not None
  373. if is_typedef:
  374. declaration = c_ast.Typedef(
  375. name=None,
  376. quals=spec['qual'],
  377. storage=spec['storage'],
  378. type=decl['decl'],
  379. coord=decl['decl'].coord)
  380. else:
  381. declaration = c_ast.Decl(
  382. name=None,
  383. quals=spec['qual'],
  384. storage=spec['storage'],
  385. funcspec=spec['function'],
  386. type=decl['decl'],
  387. init=decl.get('init'),
  388. bitsize=decl.get('bitsize'),
  389. coord=decl['decl'].coord)
  390. if isinstance(declaration.type,
  391. (c_ast.Struct, c_ast.Union, c_ast.IdentifierType)):
  392. fixed_decl = declaration
  393. else:
  394. fixed_decl = self._fix_decl_name_type(declaration, spec['type'])
  395. # Add the type name defined by typedef to a
  396. # symbol table (for usage in the lexer)
  397. #
  398. if typedef_namespace:
  399. if is_typedef:
  400. self._add_typedef_name(fixed_decl.name, fixed_decl.coord)
  401. else:
  402. self._add_identifier(fixed_decl.name, fixed_decl.coord)
  403. declarations.append(fixed_decl)
  404. return declarations
  405. def _build_function_definition(self, spec, decl, param_decls, body):
  406. """ Builds a function definition.
  407. """
  408. assert 'typedef' not in spec['storage']
  409. declaration = self._build_declarations(
  410. spec=spec,
  411. decls=[dict(decl=decl, init=None)],
  412. typedef_namespace=True)[0]
  413. return c_ast.FuncDef(
  414. decl=declaration,
  415. param_decls=param_decls,
  416. body=body,
  417. coord=decl.coord)
  418. def _select_struct_union_class(self, token):
  419. """ Given a token (either STRUCT or UNION), selects the
  420. appropriate AST class.
  421. """
  422. if token == 'struct':
  423. return c_ast.Struct
  424. else:
  425. return c_ast.Union
  426. ##
  427. ## Precedence and associativity of operators
  428. ##
  429. precedence = (
  430. ('left', 'LOR'),
  431. ('left', 'LAND'),
  432. ('left', 'OR'),
  433. ('left', 'XOR'),
  434. ('left', 'AND'),
  435. ('left', 'EQ', 'NE'),
  436. ('left', 'GT', 'GE', 'LT', 'LE'),
  437. ('left', 'RSHIFT', 'LSHIFT'),
  438. ('left', 'PLUS', 'MINUS'),
  439. ('left', 'TIMES', 'DIVIDE', 'MOD')
  440. )
  441. ##
  442. ## Grammar productions
  443. ## Implementation of the BNF defined in K&R2 A.13
  444. ##
  445. # Wrapper around a translation unit, to allow for empty input.
  446. # Not strictly part of the C99 Grammar, but useful in practice.
  447. #
  448. def p_translation_unit_or_empty(self, p):
  449. """ translation_unit_or_empty : translation_unit
  450. | empty
  451. """
  452. if p[1] is None:
  453. p[0] = c_ast.FileAST([])
  454. else:
  455. p[0] = c_ast.FileAST(p[1])
  456. def p_translation_unit_1(self, p):
  457. """ translation_unit : external_declaration
  458. """
  459. # Note: external_declaration is already a list
  460. #
  461. p[0] = p[1]
  462. def p_translation_unit_2(self, p):
  463. """ translation_unit : translation_unit external_declaration
  464. """
  465. p[1].extend(p[2])
  466. p[0] = p[1]
  467. # Declarations always come as lists (because they can be
  468. # several in one line), so we wrap the function definition
  469. # into a list as well, to make the return value of
  470. # external_declaration homogenous.
  471. #
  472. def p_external_declaration_1(self, p):
  473. """ external_declaration : function_definition
  474. """
  475. p[0] = [p[1]]
  476. def p_external_declaration_2(self, p):
  477. """ external_declaration : declaration
  478. """
  479. p[0] = p[1]
  480. def p_external_declaration_3(self, p):
  481. """ external_declaration : pp_directive
  482. | pppragma_directive
  483. """
  484. p[0] = [p[1]]
  485. def p_external_declaration_4(self, p):
  486. """ external_declaration : SEMI
  487. """
  488. p[0] = []
  489. def p_pp_directive(self, p):
  490. """ pp_directive : PPHASH
  491. """
  492. self._parse_error('Directives not supported yet',
  493. self._token_coord(p, 1))
  494. def p_pppragma_directive(self, p):
  495. """ pppragma_directive : PPPRAGMA
  496. | PPPRAGMA PPPRAGMASTR
  497. """
  498. if len(p) == 3:
  499. p[0] = c_ast.Pragma(p[2], self._token_coord(p, 2))
  500. else:
  501. p[0] = c_ast.Pragma("", self._token_coord(p, 1))
  502. # In function definitions, the declarator can be followed by
  503. # a declaration list, for old "K&R style" function definitios.
  504. #
  505. def p_function_definition_1(self, p):
  506. """ function_definition : id_declarator declaration_list_opt compound_statement
  507. """
  508. # no declaration specifiers - 'int' becomes the default type
  509. spec = dict(
  510. qual=[],
  511. storage=[],
  512. type=[c_ast.IdentifierType(['int'],
  513. coord=self._token_coord(p, 1))],
  514. function=[])
  515. p[0] = self._build_function_definition(
  516. spec=spec,
  517. decl=p[1],
  518. param_decls=p[2],
  519. body=p[3])
  520. def p_function_definition_2(self, p):
  521. """ function_definition : declaration_specifiers id_declarator declaration_list_opt compound_statement
  522. """
  523. spec = p[1]
  524. p[0] = self._build_function_definition(
  525. spec=spec,
  526. decl=p[2],
  527. param_decls=p[3],
  528. body=p[4])
  529. def p_statement(self, p):
  530. """ statement : labeled_statement
  531. | expression_statement
  532. | compound_statement
  533. | selection_statement
  534. | iteration_statement
  535. | jump_statement
  536. | pppragma_directive
  537. """
  538. p[0] = p[1]
  539. # A pragma is generally considered a decorator rather than an actual statement.
  540. # Still, for the purposes of analyzing an abstract syntax tree of C code,
  541. # pragma's should not be ignored and were previously treated as a statement.
  542. # This presents a problem for constructs that take a statement such as labeled_statements,
  543. # selection_statements, and iteration_statements, causing a misleading structure
  544. # in the AST. For example, consider the following C code.
  545. #
  546. # for (int i = 0; i < 3; i++)
  547. # #pragma omp critical
  548. # sum += 1;
  549. #
  550. # This code will compile and execute "sum += 1;" as the body of the for loop.
  551. # Previous implementations of PyCParser would render the AST for this
  552. # block of code as follows:
  553. #
  554. # For:
  555. # DeclList:
  556. # Decl: i, [], [], []
  557. # TypeDecl: i, []
  558. # IdentifierType: ['int']
  559. # Constant: int, 0
  560. # BinaryOp: <
  561. # ID: i
  562. # Constant: int, 3
  563. # UnaryOp: p++
  564. # ID: i
  565. # Pragma: omp critical
  566. # Assignment: +=
  567. # ID: sum
  568. # Constant: int, 1
  569. #
  570. # This AST misleadingly takes the Pragma as the body of the loop and the
  571. # assignment then becomes a sibling of the loop.
  572. #
  573. # To solve edge cases like these, the pragmacomp_or_statement rule groups
  574. # a pragma and its following statement (which would otherwise be orphaned)
  575. # using a compound block, effectively turning the above code into:
  576. #
  577. # for (int i = 0; i < 3; i++) {
  578. # #pragma omp critical
  579. # sum += 1;
  580. # }
  581. def p_pragmacomp_or_statement(self, p):
  582. """ pragmacomp_or_statement : pppragma_directive statement
  583. | statement
  584. """
  585. if isinstance(p[1], c_ast.Pragma) and len(p) == 3:
  586. p[0] = c_ast.Compound(
  587. block_items=[p[1], p[2]],
  588. coord=self._token_coord(p, 1))
  589. else:
  590. p[0] = p[1]
  591. # In C, declarations can come several in a line:
  592. # int x, *px, romulo = 5;
  593. #
  594. # However, for the AST, we will split them to separate Decl
  595. # nodes.
  596. #
  597. # This rule splits its declarations and always returns a list
  598. # of Decl nodes, even if it's one element long.
  599. #
  600. def p_decl_body(self, p):
  601. """ decl_body : declaration_specifiers init_declarator_list_opt
  602. | declaration_specifiers_no_type id_init_declarator_list_opt
  603. """
  604. spec = p[1]
  605. # p[2] (init_declarator_list_opt) is either a list or None
  606. #
  607. if p[2] is None:
  608. # By the standard, you must have at least one declarator unless
  609. # declaring a structure tag, a union tag, or the members of an
  610. # enumeration.
  611. #
  612. ty = spec['type']
  613. s_u_or_e = (c_ast.Struct, c_ast.Union, c_ast.Enum)
  614. if len(ty) == 1 and isinstance(ty[0], s_u_or_e):
  615. decls = [c_ast.Decl(
  616. name=None,
  617. quals=spec['qual'],
  618. storage=spec['storage'],
  619. funcspec=spec['function'],
  620. type=ty[0],
  621. init=None,
  622. bitsize=None,
  623. coord=ty[0].coord)]
  624. # However, this case can also occur on redeclared identifiers in
  625. # an inner scope. The trouble is that the redeclared type's name
  626. # gets grouped into declaration_specifiers; _build_declarations
  627. # compensates for this.
  628. #
  629. else:
  630. decls = self._build_declarations(
  631. spec=spec,
  632. decls=[dict(decl=None, init=None)],
  633. typedef_namespace=True)
  634. else:
  635. decls = self._build_declarations(
  636. spec=spec,
  637. decls=p[2],
  638. typedef_namespace=True)
  639. p[0] = decls
  640. # The declaration has been split to a decl_body sub-rule and
  641. # SEMI, because having them in a single rule created a problem
  642. # for defining typedefs.
  643. #
  644. # If a typedef line was directly followed by a line using the
  645. # type defined with the typedef, the type would not be
  646. # recognized. This is because to reduce the declaration rule,
  647. # the parser's lookahead asked for the token after SEMI, which
  648. # was the type from the next line, and the lexer had no chance
  649. # to see the updated type symbol table.
  650. #
  651. # Splitting solves this problem, because after seeing SEMI,
  652. # the parser reduces decl_body, which actually adds the new
  653. # type into the table to be seen by the lexer before the next
  654. # line is reached.
  655. def p_declaration(self, p):
  656. """ declaration : decl_body SEMI
  657. """
  658. p[0] = p[1]
  659. # Since each declaration is a list of declarations, this
  660. # rule will combine all the declarations and return a single
  661. # list
  662. #
  663. def p_declaration_list(self, p):
  664. """ declaration_list : declaration
  665. | declaration_list declaration
  666. """
  667. p[0] = p[1] if len(p) == 2 else p[1] + p[2]
  668. # To know when declaration-specifiers end and declarators begin,
  669. # we require declaration-specifiers to have at least one
  670. # type-specifier, and disallow typedef-names after we've seen any
  671. # type-specifier. These are both required by the spec.
  672. #
  673. def p_declaration_specifiers_no_type_1(self, p):
  674. """ declaration_specifiers_no_type : type_qualifier declaration_specifiers_no_type_opt
  675. """
  676. p[0] = self._add_declaration_specifier(p[2], p[1], 'qual')
  677. def p_declaration_specifiers_no_type_2(self, p):
  678. """ declaration_specifiers_no_type : storage_class_specifier declaration_specifiers_no_type_opt
  679. """
  680. p[0] = self._add_declaration_specifier(p[2], p[1], 'storage')
  681. def p_declaration_specifiers_no_type_3(self, p):
  682. """ declaration_specifiers_no_type : function_specifier declaration_specifiers_no_type_opt
  683. """
  684. p[0] = self._add_declaration_specifier(p[2], p[1], 'function')
  685. def p_declaration_specifiers_1(self, p):
  686. """ declaration_specifiers : declaration_specifiers type_qualifier
  687. """
  688. p[0] = self._add_declaration_specifier(p[1], p[2], 'qual', append=True)
  689. def p_declaration_specifiers_2(self, p):
  690. """ declaration_specifiers : declaration_specifiers storage_class_specifier
  691. """
  692. p[0] = self._add_declaration_specifier(p[1], p[2], 'storage', append=True)
  693. def p_declaration_specifiers_3(self, p):
  694. """ declaration_specifiers : declaration_specifiers function_specifier
  695. """
  696. p[0] = self._add_declaration_specifier(p[1], p[2], 'function', append=True)
  697. def p_declaration_specifiers_4(self, p):
  698. """ declaration_specifiers : declaration_specifiers type_specifier_no_typeid
  699. """
  700. p[0] = self._add_declaration_specifier(p[1], p[2], 'type', append=True)
  701. def p_declaration_specifiers_5(self, p):
  702. """ declaration_specifiers : type_specifier
  703. """
  704. p[0] = self._add_declaration_specifier(None, p[1], 'type')
  705. def p_declaration_specifiers_6(self, p):
  706. """ declaration_specifiers : declaration_specifiers_no_type type_specifier
  707. """
  708. p[0] = self._add_declaration_specifier(p[1], p[2], 'type', append=True)
  709. def p_storage_class_specifier(self, p):
  710. """ storage_class_specifier : AUTO
  711. | REGISTER
  712. | STATIC
  713. | EXTERN
  714. | TYPEDEF
  715. """
  716. p[0] = p[1]
  717. def p_function_specifier(self, p):
  718. """ function_specifier : INLINE
  719. """
  720. p[0] = p[1]
  721. def p_type_specifier_no_typeid(self, p):
  722. """ type_specifier_no_typeid : VOID
  723. | _BOOL
  724. | CHAR
  725. | SHORT
  726. | INT
  727. | LONG
  728. | FLOAT
  729. | DOUBLE
  730. | _COMPLEX
  731. | SIGNED
  732. | UNSIGNED
  733. | __INT128
  734. """
  735. p[0] = c_ast.IdentifierType([p[1]], coord=self._token_coord(p, 1))
  736. def p_type_specifier(self, p):
  737. """ type_specifier : typedef_name
  738. | enum_specifier
  739. | struct_or_union_specifier
  740. | type_specifier_no_typeid
  741. """
  742. p[0] = p[1]
  743. def p_type_qualifier(self, p):
  744. """ type_qualifier : CONST
  745. | RESTRICT
  746. | VOLATILE
  747. """
  748. p[0] = p[1]
  749. def p_init_declarator_list(self, p):
  750. """ init_declarator_list : init_declarator
  751. | init_declarator_list COMMA init_declarator
  752. """
  753. p[0] = p[1] + [p[3]] if len(p) == 4 else [p[1]]
  754. # Returns a {decl=<declarator> : init=<initializer>} dictionary
  755. # If there's no initializer, uses None
  756. #
  757. def p_init_declarator(self, p):
  758. """ init_declarator : declarator
  759. | declarator EQUALS initializer
  760. """
  761. p[0] = dict(decl=p[1], init=(p[3] if len(p) > 2 else None))
  762. def p_id_init_declarator_list(self, p):
  763. """ id_init_declarator_list : id_init_declarator
  764. | id_init_declarator_list COMMA init_declarator
  765. """
  766. p[0] = p[1] + [p[3]] if len(p) == 4 else [p[1]]
  767. def p_id_init_declarator(self, p):
  768. """ id_init_declarator : id_declarator
  769. | id_declarator EQUALS initializer
  770. """
  771. p[0] = dict(decl=p[1], init=(p[3] if len(p) > 2 else None))
  772. # Require at least one type specifier in a specifier-qualifier-list
  773. #
  774. def p_specifier_qualifier_list_1(self, p):
  775. """ specifier_qualifier_list : specifier_qualifier_list type_specifier_no_typeid
  776. """
  777. p[0] = self._add_declaration_specifier(p[1], p[2], 'type', append=True)
  778. def p_specifier_qualifier_list_2(self, p):
  779. """ specifier_qualifier_list : specifier_qualifier_list type_qualifier
  780. """
  781. p[0] = self._add_declaration_specifier(p[1], p[2], 'qual', append=True)
  782. def p_specifier_qualifier_list_3(self, p):
  783. """ specifier_qualifier_list : type_specifier
  784. """
  785. p[0] = self._add_declaration_specifier(None, p[1], 'type')
  786. def p_specifier_qualifier_list_4(self, p):
  787. """ specifier_qualifier_list : type_qualifier_list type_specifier
  788. """
  789. spec = dict(qual=p[1], storage=[], type=[], function=[])
  790. p[0] = self._add_declaration_specifier(spec, p[2], 'type', append=True)
  791. # TYPEID is allowed here (and in other struct/enum related tag names), because
  792. # struct/enum tags reside in their own namespace and can be named the same as types
  793. #
  794. def p_struct_or_union_specifier_1(self, p):
  795. """ struct_or_union_specifier : struct_or_union ID
  796. | struct_or_union TYPEID
  797. """
  798. klass = self._select_struct_union_class(p[1])
  799. # None means no list of members
  800. p[0] = klass(
  801. name=p[2],
  802. decls=None,
  803. coord=self._token_coord(p, 2))
  804. def p_struct_or_union_specifier_2(self, p):
  805. """ struct_or_union_specifier : struct_or_union brace_open struct_declaration_list brace_close
  806. | struct_or_union brace_open brace_close
  807. """
  808. klass = self._select_struct_union_class(p[1])
  809. if len(p) == 4:
  810. # Empty sequence means an empty list of members
  811. p[0] = klass(
  812. name=None,
  813. decls=[],
  814. coord=self._token_coord(p, 2))
  815. else:
  816. p[0] = klass(
  817. name=None,
  818. decls=p[3],
  819. coord=self._token_coord(p, 2))
  820. def p_struct_or_union_specifier_3(self, p):
  821. """ struct_or_union_specifier : struct_or_union ID brace_open struct_declaration_list brace_close
  822. | struct_or_union ID brace_open brace_close
  823. | struct_or_union TYPEID brace_open struct_declaration_list brace_close
  824. | struct_or_union TYPEID brace_open brace_close
  825. """
  826. klass = self._select_struct_union_class(p[1])
  827. if len(p) == 5:
  828. # Empty sequence means an empty list of members
  829. p[0] = klass(
  830. name=p[2],
  831. decls=[],
  832. coord=self._token_coord(p, 2))
  833. else:
  834. p[0] = klass(
  835. name=p[2],
  836. decls=p[4],
  837. coord=self._token_coord(p, 2))
  838. def p_struct_or_union(self, p):
  839. """ struct_or_union : STRUCT
  840. | UNION
  841. """
  842. p[0] = p[1]
  843. # Combine all declarations into a single list
  844. #
  845. def p_struct_declaration_list(self, p):
  846. """ struct_declaration_list : struct_declaration
  847. | struct_declaration_list struct_declaration
  848. """
  849. if len(p) == 2:
  850. p[0] = p[1] or []
  851. else:
  852. p[0] = p[1] + (p[2] or [])
  853. def p_struct_declaration_1(self, p):
  854. """ struct_declaration : specifier_qualifier_list struct_declarator_list_opt SEMI
  855. """
  856. spec = p[1]
  857. assert 'typedef' not in spec['storage']
  858. if p[2] is not None:
  859. decls = self._build_declarations(
  860. spec=spec,
  861. decls=p[2])
  862. elif len(spec['type']) == 1:
  863. # Anonymous struct/union, gcc extension, C1x feature.
  864. # Although the standard only allows structs/unions here, I see no
  865. # reason to disallow other types since some compilers have typedefs
  866. # here, and pycparser isn't about rejecting all invalid code.
  867. #
  868. node = spec['type'][0]
  869. if isinstance(node, c_ast.Node):
  870. decl_type = node
  871. else:
  872. decl_type = c_ast.IdentifierType(node)
  873. decls = self._build_declarations(
  874. spec=spec,
  875. decls=[dict(decl=decl_type)])
  876. else:
  877. # Structure/union members can have the same names as typedefs.
  878. # The trouble is that the member's name gets grouped into
  879. # specifier_qualifier_list; _build_declarations compensates.
  880. #
  881. decls = self._build_declarations(
  882. spec=spec,
  883. decls=[dict(decl=None, init=None)])
  884. p[0] = decls
  885. def p_struct_declaration_2(self, p):
  886. """ struct_declaration : SEMI
  887. """
  888. p[0] = None
  889. def p_struct_declaration_3(self, p):
  890. """ struct_declaration : pppragma_directive
  891. """
  892. p[0] = [p[1]]
  893. def p_struct_declarator_list(self, p):
  894. """ struct_declarator_list : struct_declarator
  895. | struct_declarator_list COMMA struct_declarator
  896. """
  897. p[0] = p[1] + [p[3]] if len(p) == 4 else [p[1]]
  898. # struct_declarator passes up a dict with the keys: decl (for
  899. # the underlying declarator) and bitsize (for the bitsize)
  900. #
  901. def p_struct_declarator_1(self, p):
  902. """ struct_declarator : declarator
  903. """
  904. p[0] = {'decl': p[1], 'bitsize': None}
  905. def p_struct_declarator_2(self, p):
  906. """ struct_declarator : declarator COLON constant_expression
  907. | COLON constant_expression
  908. """
  909. if len(p) > 3:
  910. p[0] = {'decl': p[1], 'bitsize': p[3]}
  911. else:
  912. p[0] = {'decl': c_ast.TypeDecl(None, None, None), 'bitsize': p[2]}
  913. def p_enum_specifier_1(self, p):
  914. """ enum_specifier : ENUM ID
  915. | ENUM TYPEID
  916. """
  917. p[0] = c_ast.Enum(p[2], None, self._token_coord(p, 1))
  918. def p_enum_specifier_2(self, p):
  919. """ enum_specifier : ENUM brace_open enumerator_list brace_close
  920. """
  921. p[0] = c_ast.Enum(None, p[3], self._token_coord(p, 1))
  922. def p_enum_specifier_3(self, p):
  923. """ enum_specifier : ENUM ID brace_open enumerator_list brace_close
  924. | ENUM TYPEID brace_open enumerator_list brace_close
  925. """
  926. p[0] = c_ast.Enum(p[2], p[4], self._token_coord(p, 1))
  927. def p_enumerator_list(self, p):
  928. """ enumerator_list : enumerator
  929. | enumerator_list COMMA
  930. | enumerator_list COMMA enumerator
  931. """
  932. if len(p) == 2:
  933. p[0] = c_ast.EnumeratorList([p[1]], p[1].coord)
  934. elif len(p) == 3:
  935. p[0] = p[1]
  936. else:
  937. p[1].enumerators.append(p[3])
  938. p[0] = p[1]
  939. def p_enumerator(self, p):
  940. """ enumerator : ID
  941. | ID EQUALS constant_expression
  942. """
  943. if len(p) == 2:
  944. enumerator = c_ast.Enumerator(
  945. p[1], None,
  946. self._token_coord(p, 1))
  947. else:
  948. enumerator = c_ast.Enumerator(
  949. p[1], p[3],
  950. self._token_coord(p, 1))
  951. self._add_identifier(enumerator.name, enumerator.coord)
  952. p[0] = enumerator
  953. def p_declarator(self, p):
  954. """ declarator : id_declarator
  955. | typeid_declarator
  956. """
  957. p[0] = p[1]
  958. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
  959. def p_xxx_declarator_1(self, p):
  960. """ xxx_declarator : direct_xxx_declarator
  961. """
  962. p[0] = p[1]
  963. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
  964. def p_xxx_declarator_2(self, p):
  965. """ xxx_declarator : pointer direct_xxx_declarator
  966. """
  967. p[0] = self._type_modify_decl(p[2], p[1])
  968. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
  969. def p_direct_xxx_declarator_1(self, p):
  970. """ direct_xxx_declarator : yyy
  971. """
  972. p[0] = c_ast.TypeDecl(
  973. declname=p[1],
  974. type=None,
  975. quals=None,
  976. coord=self._token_coord(p, 1))
  977. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'))
  978. def p_direct_xxx_declarator_2(self, p):
  979. """ direct_xxx_declarator : LPAREN xxx_declarator RPAREN
  980. """
  981. p[0] = p[2]
  982. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
  983. def p_direct_xxx_declarator_3(self, p):
  984. """ direct_xxx_declarator : direct_xxx_declarator LBRACKET type_qualifier_list_opt assignment_expression_opt RBRACKET
  985. """
  986. quals = (p[3] if len(p) > 5 else []) or []
  987. # Accept dimension qualifiers
  988. # Per C99 6.7.5.3 p7
  989. arr = c_ast.ArrayDecl(
  990. type=None,
  991. dim=p[4] if len(p) > 5 else p[3],
  992. dim_quals=quals,
  993. coord=p[1].coord)
  994. p[0] = self._type_modify_decl(decl=p[1], modifier=arr)
  995. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
  996. def p_direct_xxx_declarator_4(self, p):
  997. """ direct_xxx_declarator : direct_xxx_declarator LBRACKET STATIC type_qualifier_list_opt assignment_expression RBRACKET
  998. | direct_xxx_declarator LBRACKET type_qualifier_list STATIC assignment_expression RBRACKET
  999. """
  1000. # Using slice notation for PLY objects doesn't work in Python 3 for the
  1001. # version of PLY embedded with pycparser; see PLY Google Code issue 30.
  1002. # Work around that here by listing the two elements separately.
  1003. listed_quals = [item if isinstance(item, list) else [item]
  1004. for item in [p[3],p[4]]]
  1005. dim_quals = [qual for sublist in listed_quals for qual in sublist
  1006. if qual is not None]
  1007. arr = c_ast.ArrayDecl(
  1008. type=None,
  1009. dim=p[5],
  1010. dim_quals=dim_quals,
  1011. coord=p[1].coord)
  1012. p[0] = self._type_modify_decl(decl=p[1], modifier=arr)
  1013. # Special for VLAs
  1014. #
  1015. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
  1016. def p_direct_xxx_declarator_5(self, p):
  1017. """ direct_xxx_declarator : direct_xxx_declarator LBRACKET type_qualifier_list_opt TIMES RBRACKET
  1018. """
  1019. arr = c_ast.ArrayDecl(
  1020. type=None,
  1021. dim=c_ast.ID(p[4], self._token_coord(p, 4)),
  1022. dim_quals=p[3] if p[3] != None else [],
  1023. coord=p[1].coord)
  1024. p[0] = self._type_modify_decl(decl=p[1], modifier=arr)
  1025. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
  1026. def p_direct_xxx_declarator_6(self, p):
  1027. """ direct_xxx_declarator : direct_xxx_declarator LPAREN parameter_type_list RPAREN
  1028. | direct_xxx_declarator LPAREN identifier_list_opt RPAREN
  1029. """
  1030. func = c_ast.FuncDecl(
  1031. args=p[3],
  1032. type=None,
  1033. coord=p[1].coord)
  1034. # To see why _get_yacc_lookahead_token is needed, consider:
  1035. # typedef char TT;
  1036. # void foo(int TT) { TT = 10; }
  1037. # Outside the function, TT is a typedef, but inside (starting and
  1038. # ending with the braces) it's a parameter. The trouble begins with
  1039. # yacc's lookahead token. We don't know if we're declaring or
  1040. # defining a function until we see LBRACE, but if we wait for yacc to
  1041. # trigger a rule on that token, then TT will have already been read
  1042. # and incorrectly interpreted as TYPEID. We need to add the
  1043. # parameters to the scope the moment the lexer sees LBRACE.
  1044. #
  1045. if self._get_yacc_lookahead_token().type == "LBRACE":
  1046. if func.args is not None:
  1047. for param in func.args.params:
  1048. if isinstance(param, c_ast.EllipsisParam): break
  1049. self._add_identifier(param.name, param.coord)
  1050. p[0] = self._type_modify_decl(decl=p[1], modifier=func)
  1051. def p_pointer(self, p):
  1052. """ pointer : TIMES type_qualifier_list_opt
  1053. | TIMES type_qualifier_list_opt pointer
  1054. """
  1055. coord = self._token_coord(p, 1)
  1056. # Pointer decls nest from inside out. This is important when different
  1057. # levels have different qualifiers. For example:
  1058. #
  1059. # char * const * p;
  1060. #
  1061. # Means "pointer to const pointer to char"
  1062. #
  1063. # While:
  1064. #
  1065. # char ** const p;
  1066. #
  1067. # Means "const pointer to pointer to char"
  1068. #
  1069. # So when we construct PtrDecl nestings, the leftmost pointer goes in
  1070. # as the most nested type.
  1071. nested_type = c_ast.PtrDecl(quals=p[2] or [], type=None, coord=coord)
  1072. if len(p) > 3:
  1073. tail_type = p[3]
  1074. while tail_type.type is not None:
  1075. tail_type = tail_type.type
  1076. tail_type.type = nested_type
  1077. p[0] = p[3]
  1078. else:
  1079. p[0] = nested_type
  1080. def p_type_qualifier_list(self, p):
  1081. """ type_qualifier_list : type_qualifier
  1082. | type_qualifier_list type_qualifier
  1083. """
  1084. p[0] = [p[1]] if len(p) == 2 else p[1] + [p[2]]
  1085. def p_parameter_type_list(self, p):
  1086. """ parameter_type_list : parameter_list
  1087. | parameter_list COMMA ELLIPSIS
  1088. """
  1089. if len(p) > 2:
  1090. p[1].params.append(c_ast.EllipsisParam(self._token_coord(p, 3)))
  1091. p[0] = p[1]
  1092. def p_parameter_list(self, p):
  1093. """ parameter_list : parameter_declaration
  1094. | parameter_list COMMA parameter_declaration
  1095. """
  1096. if len(p) == 2: # single parameter
  1097. p[0] = c_ast.ParamList([p[1]], p[1].coord)
  1098. else:
  1099. p[1].params.append(p[3])
  1100. p[0] = p[1]
  1101. # From ISO/IEC 9899:TC2, 6.7.5.3.11:
  1102. # "If, in a parameter declaration, an identifier can be treated either
  1103. # as a typedef name or as a parameter name, it shall be taken as a
  1104. # typedef name."
  1105. #
  1106. # Inside a parameter declaration, once we've reduced declaration specifiers,
  1107. # if we shift in an LPAREN and see a TYPEID, it could be either an abstract
  1108. # declarator or a declarator nested inside parens. This rule tells us to
  1109. # always treat it as an abstract declarator. Therefore, we only accept
  1110. # `id_declarator`s and `typeid_noparen_declarator`s.
  1111. def p_parameter_declaration_1(self, p):
  1112. """ parameter_declaration : declaration_specifiers id_declarator
  1113. | declaration_specifiers typeid_noparen_declarator
  1114. """
  1115. spec = p[1]
  1116. if not spec['type']:
  1117. spec['type'] = [c_ast.IdentifierType(['int'],
  1118. coord=self._token_coord(p, 1))]
  1119. p[0] = self._build_declarations(
  1120. spec=spec,
  1121. decls=[dict(decl=p[2])])[0]
  1122. def p_parameter_declaration_2(self, p):
  1123. """ parameter_declaration : declaration_specifiers abstract_declarator_opt
  1124. """
  1125. spec = p[1]
  1126. if not spec['type']:
  1127. spec['type'] = [c_ast.IdentifierType(['int'],
  1128. coord=self._token_coord(p, 1))]
  1129. # Parameters can have the same names as typedefs. The trouble is that
  1130. # the parameter's name gets grouped into declaration_specifiers, making
  1131. # it look like an old-style declaration; compensate.
  1132. #
  1133. if len(spec['type']) > 1 and len(spec['type'][-1].names) == 1 and \
  1134. self._is_type_in_scope(spec['type'][-1].names[0]):
  1135. decl = self._build_declarations(
  1136. spec=spec,
  1137. decls=[dict(decl=p[2], init=None)])[0]
  1138. # This truly is an old-style parameter declaration
  1139. #
  1140. else:
  1141. decl = c_ast.Typename(
  1142. name='',
  1143. quals=spec['qual'],
  1144. type=p[2] or c_ast.TypeDecl(None, None, None),
  1145. coord=self._token_coord(p, 2))
  1146. typename = spec['type']
  1147. decl = self._fix_decl_name_type(decl, typename)
  1148. p[0] = decl
  1149. def p_identifier_list(self, p):
  1150. """ identifier_list : identifier
  1151. | identifier_list COMMA identifier
  1152. """
  1153. if len(p) == 2: # single parameter
  1154. p[0] = c_ast.ParamList([p[1]], p[1].coord)
  1155. else:
  1156. p[1].params.append(p[3])
  1157. p[0] = p[1]
  1158. def p_initializer_1(self, p):
  1159. """ initializer : assignment_expression
  1160. """
  1161. p[0] = p[1]
  1162. def p_initializer_2(self, p):
  1163. """ initializer : brace_open initializer_list_opt brace_close
  1164. | brace_open initializer_list COMMA brace_close
  1165. """
  1166. if p[2] is None:
  1167. p[0] = c_ast.InitList([], self._token_coord(p, 1))
  1168. else:
  1169. p[0] = p[2]
  1170. def p_initializer_list(self, p):
  1171. """ initializer_list : designation_opt initializer
  1172. | initializer_list COMMA designation_opt initializer
  1173. """
  1174. if len(p) == 3: # single initializer
  1175. init = p[2] if p[1] is None else c_ast.NamedInitializer(p[1], p[2])
  1176. p[0] = c_ast.InitList([init], p[2].coord)
  1177. else:
  1178. init = p[4] if p[3] is None else c_ast.NamedInitializer(p[3], p[4])
  1179. p[1].exprs.append(init)
  1180. p[0] = p[1]
  1181. def p_designation(self, p):
  1182. """ designation : designator_list EQUALS
  1183. """
  1184. p[0] = p[1]
  1185. # Designators are represented as a list of nodes, in the order in which
  1186. # they're written in the code.
  1187. #
  1188. def p_designator_list(self, p):
  1189. """ designator_list : designator
  1190. | designator_list designator
  1191. """
  1192. p[0] = [p[1]] if len(p) == 2 else p[1] + [p[2]]
  1193. def p_designator(self, p):
  1194. """ designator : LBRACKET constant_expression RBRACKET
  1195. | PERIOD identifier
  1196. """
  1197. p[0] = p[2]
  1198. def p_type_name(self, p):
  1199. """ type_name : specifier_qualifier_list abstract_declarator_opt
  1200. """
  1201. typename = c_ast.Typename(
  1202. name='',
  1203. quals=p[1]['qual'],
  1204. type=p[2] or c_ast.TypeDecl(None, None, None),
  1205. coord=self._token_coord(p, 2))
  1206. p[0] = self._fix_decl_name_type(typename, p[1]['type'])
  1207. def p_abstract_declarator_1(self, p):
  1208. """ abstract_declarator : pointer
  1209. """
  1210. dummytype = c_ast.TypeDecl(None, None, None)
  1211. p[0] = self._type_modify_decl(
  1212. decl=dummytype,
  1213. modifier=p[1])
  1214. def p_abstract_declarator_2(self, p):
  1215. """ abstract_declarator : pointer direct_abstract_declarator
  1216. """
  1217. p[0] = self._type_modify_decl(p[2], p[1])
  1218. def p_abstract_declarator_3(self, p):
  1219. """ abstract_declarator : direct_abstract_declarator
  1220. """
  1221. p[0] = p[1]
  1222. # Creating and using direct_abstract_declarator_opt here
  1223. # instead of listing both direct_abstract_declarator and the
  1224. # lack of it in the beginning of _1 and _2 caused two
  1225. # shift/reduce errors.
  1226. #
  1227. def p_direct_abstract_declarator_1(self, p):
  1228. """ direct_abstract_declarator : LPAREN abstract_declarator RPAREN """
  1229. p[0] = p[2]
  1230. def p_direct_abstract_declarator_2(self, p):
  1231. """ direct_abstract_declarator : direct_abstract_declarator LBRACKET assignment_expression_opt RBRACKET
  1232. """
  1233. arr = c_ast.ArrayDecl(
  1234. type=None,
  1235. dim=p[3],
  1236. dim_quals=[],
  1237. coord=p[1].coord)
  1238. p[0] = self._type_modify_decl(decl=p[1], modifier=arr)
  1239. def p_direct_abstract_declarator_3(self, p):
  1240. """ direct_abstract_declarator : LBRACKET type_qualifier_list_opt assignment_expression_opt RBRACKET
  1241. """
  1242. quals = (p[2] if len(p) > 4 else []) or []
  1243. p[0] = c_ast.ArrayDecl(
  1244. type=c_ast.TypeDecl(None, None, None),
  1245. dim=p[3] if len(p) > 4 else p[2],
  1246. dim_quals=quals,
  1247. coord=self._token_coord(p, 1))
  1248. def p_direct_abstract_declarator_4(self, p):
  1249. """ direct_abstract_declarator : direct_abstract_declarator LBRACKET TIMES RBRACKET
  1250. """
  1251. arr = c_ast.ArrayDecl(
  1252. type=None,
  1253. dim=c_ast.ID(p[3], self._token_coord(p, 3)),
  1254. dim_quals=[],
  1255. coord=p[1].coord)
  1256. p[0] = self._type_modify_decl(decl=p[1], modifier=arr)
  1257. def p_direct_abstract_declarator_5(self, p):
  1258. """ direct_abstract_declarator : LBRACKET TIMES RBRACKET
  1259. """
  1260. p[0] = c_ast.ArrayDecl(
  1261. type=c_ast.TypeDecl(None, None, None),
  1262. dim=c_ast.ID(p[3], self._token_coord(p, 3)),
  1263. dim_quals=[],
  1264. coord=self._token_coord(p, 1))
  1265. def p_direct_abstract_declarator_6(self, p):
  1266. """ direct_abstract_declarator : direct_abstract_declarator LPAREN parameter_type_list_opt RPAREN
  1267. """
  1268. func = c_ast.FuncDecl(
  1269. args=p[3],
  1270. type=None,
  1271. coord=p[1].coord)
  1272. p[0] = self._type_modify_decl(decl=p[1], modifier=func)
  1273. def p_direct_abstract_declarator_7(self, p):
  1274. """ direct_abstract_declarator : LPAREN parameter_type_list_opt RPAREN
  1275. """
  1276. p[0] = c_ast.FuncDecl(
  1277. args=p[2],
  1278. type=c_ast.TypeDecl(None, None, None),
  1279. coord=self._token_coord(p, 1))
  1280. # declaration is a list, statement isn't. To make it consistent, block_item
  1281. # will always be a list
  1282. #
  1283. def p_block_item(self, p):
  1284. """ block_item : declaration
  1285. | statement
  1286. """
  1287. p[0] = p[1] if isinstance(p[1], list) else [p[1]]
  1288. # Since we made block_item a list, this just combines lists
  1289. #
  1290. def p_block_item_list(self, p):
  1291. """ block_item_list : block_item
  1292. | block_item_list block_item
  1293. """
  1294. # Empty block items (plain ';') produce [None], so ignore them
  1295. p[0] = p[1] if (len(p) == 2 or p[2] == [None]) else p[1] + p[2]
  1296. def p_compound_statement_1(self, p):
  1297. """ compound_statement : brace_open block_item_list_opt brace_close """
  1298. p[0] = c_ast.Compound(
  1299. block_items=p[2],
  1300. coord=self._token_coord(p, 1))
  1301. def p_labeled_statement_1(self, p):
  1302. """ labeled_statement : ID COLON pragmacomp_or_statement """
  1303. p[0] = c_ast.Label(p[1], p[3], self._token_coord(p, 1))
  1304. def p_labeled_statement_2(self, p):
  1305. """ labeled_statement : CASE constant_expression COLON pragmacomp_or_statement """
  1306. p[0] = c_ast.Case(p[2], [p[4]], self._token_coord(p, 1))
  1307. def p_labeled_statement_3(self, p):
  1308. """ labeled_statement : DEFAULT COLON pragmacomp_or_statement """
  1309. p[0] = c_ast.Default([p[3]], self._token_coord(p, 1))
  1310. def p_selection_statement_1(self, p):
  1311. """ selection_statement : IF LPAREN expression RPAREN pragmacomp_or_statement """
  1312. p[0] = c_ast.If(p[3], p[5], None, self._token_coord(p, 1))
  1313. def p_selection_statement_2(self, p):
  1314. """ selection_statement : IF LPAREN expression RPAREN statement ELSE pragmacomp_or_statement """
  1315. p[0] = c_ast.If(p[3], p[5], p[7], self._token_coord(p, 1))
  1316. def p_selection_statement_3(self, p):
  1317. """ selection_statement : SWITCH LPAREN expression RPAREN pragmacomp_or_statement """
  1318. p[0] = fix_switch_cases(
  1319. c_ast.Switch(p[3], p[5], self._token_coord(p, 1)))
  1320. def p_iteration_statement_1(self, p):
  1321. """ iteration_statement : WHILE LPAREN expression RPAREN pragmacomp_or_statement """
  1322. p[0] = c_ast.While(p[3], p[5], self._token_coord(p, 1))
  1323. def p_iteration_statement_2(self, p):
  1324. """ iteration_statement : DO pragmacomp_or_statement WHILE LPAREN expression RPAREN SEMI """
  1325. p[0] = c_ast.DoWhile(p[5], p[2], self._token_coord(p, 1))
  1326. def p_iteration_statement_3(self, p):
  1327. """ iteration_statement : FOR LPAREN expression_opt SEMI expression_opt SEMI expression_opt RPAREN pragmacomp_or_statement """
  1328. p[0] = c_ast.For(p[3], p[5], p[7], p[9], self._token_coord(p, 1))
  1329. def p_iteration_statement_4(self, p):
  1330. """ iteration_statement : FOR LPAREN declaration expression_opt SEMI expression_opt RPAREN pragmacomp_or_statement """
  1331. p[0] = c_ast.For(c_ast.DeclList(p[3], self._token_coord(p, 1)),
  1332. p[4], p[6], p[8], self._token_coord(p, 1))
  1333. def p_jump_statement_1(self, p):
  1334. """ jump_statement : GOTO ID SEMI """
  1335. p[0] = c_ast.Goto(p[2], self._token_coord(p, 1))
  1336. def p_jump_statement_2(self, p):
  1337. """ jump_statement : BREAK SEMI """
  1338. p[0] = c_ast.Break(self._token_coord(p, 1))
  1339. def p_jump_statement_3(self, p):
  1340. """ jump_statement : CONTINUE SEMI """
  1341. p[0] = c_ast.Continue(self._token_coord(p, 1))
  1342. def p_jump_statement_4(self, p):
  1343. """ jump_statement : RETURN expression SEMI
  1344. | RETURN SEMI
  1345. """
  1346. p[0] = c_ast.Return(p[2] if len(p) == 4 else None, self._token_coord(p, 1))
  1347. def p_expression_statement(self, p):
  1348. """ expression_statement : expression_opt SEMI """
  1349. if p[1] is None:
  1350. p[0] = c_ast.EmptyStatement(self._token_coord(p, 2))
  1351. else:
  1352. p[0] = p[1]
  1353. def p_expression(self, p):
  1354. """ expression : assignment_expression
  1355. | expression COMMA assignment_expression
  1356. """
  1357. if len(p) == 2:
  1358. p[0] = p[1]
  1359. else:
  1360. if not isinstance(p[1], c_ast.ExprList):
  1361. p[1] = c_ast.ExprList([p[1]], p[1].coord)
  1362. p[1].exprs.append(p[3])
  1363. p[0] = p[1]
  1364. def p_typedef_name(self, p):
  1365. """ typedef_name : TYPEID """
  1366. p[0] = c_ast.IdentifierType([p[1]], coord=self._token_coord(p, 1))
  1367. def p_assignment_expression(self, p):
  1368. """ assignment_expression : conditional_expression
  1369. | unary_expression assignment_operator assignment_expression
  1370. """
  1371. if len(p) == 2:
  1372. p[0] = p[1]
  1373. else:
  1374. p[0] = c_ast.Assignment(p[2], p[1], p[3], p[1].coord)
  1375. # K&R2 defines these as many separate rules, to encode
  1376. # precedence and associativity. Why work hard ? I'll just use
  1377. # the built in precedence/associativity specification feature
  1378. # of PLY. (see precedence declaration above)
  1379. #
  1380. def p_assignment_operator(self, p):
  1381. """ assignment_operator : EQUALS
  1382. | XOREQUAL
  1383. | TIMESEQUAL
  1384. | DIVEQUAL
  1385. | MODEQUAL
  1386. | PLUSEQUAL
  1387. | MINUSEQUAL
  1388. | LSHIFTEQUAL
  1389. | RSHIFTEQUAL
  1390. | ANDEQUAL
  1391. | OREQUAL
  1392. """
  1393. p[0] = p[1]
  1394. def p_constant_expression(self, p):
  1395. """ constant_expression : conditional_expression """
  1396. p[0] = p[1]
  1397. def p_conditional_expression(self, p):
  1398. """ conditional_expression : binary_expression
  1399. | binary_expression CONDOP expression COLON conditional_expression
  1400. """
  1401. if len(p) == 2:
  1402. p[0] = p[1]
  1403. else:
  1404. p[0] = c_ast.TernaryOp(p[1], p[3], p[5], p[1].coord)
  1405. def p_binary_expression(self, p):
  1406. """ binary_expression : cast_expression
  1407. | binary_expression TIMES binary_expression
  1408. | binary_expression DIVIDE binary_expression
  1409. | binary_expression MOD binary_expression
  1410. | binary_expression PLUS binary_expression
  1411. | binary_expression MINUS binary_expression
  1412. | binary_expression RSHIFT binary_expression
  1413. | binary_expression LSHIFT binary_expression
  1414. | binary_expression LT binary_expression
  1415. | binary_expression LE binary_expression
  1416. | binary_expression GE binary_expression
  1417. | binary_expression GT binary_expression
  1418. | binary_expression EQ binary_expression
  1419. | binary_expression NE binary_expression
  1420. | binary_expression AND binary_expression
  1421. | binary_expression OR binary_expression
  1422. | binary_expression XOR binary_expression
  1423. | binary_expression LAND binary_expression
  1424. | binary_expression LOR binary_expression
  1425. """
  1426. if len(p) == 2:
  1427. p[0] = p[1]
  1428. else:
  1429. p[0] = c_ast.BinaryOp(p[2], p[1], p[3], p[1].coord)
  1430. def p_cast_expression_1(self, p):
  1431. """ cast_expression : unary_expression """
  1432. p[0] = p[1]
  1433. def p_cast_expression_2(self, p):
  1434. """ cast_expression : LPAREN type_name RPAREN cast_expression """
  1435. p[0] = c_ast.Cast(p[2], p[4], self._token_coord(p, 1))
  1436. def p_unary_expression_1(self, p):
  1437. """ unary_expression : postfix_expression """
  1438. p[0] = p[1]
  1439. def p_unary_expression_2(self, p):
  1440. """ unary_expression : PLUSPLUS unary_expression
  1441. | MINUSMINUS unary_expression
  1442. | unary_operator cast_expression
  1443. """
  1444. p[0] = c_ast.UnaryOp(p[1], p[2], p[2].coord)
  1445. def p_unary_expression_3(self, p):
  1446. """ unary_expression : SIZEOF unary_expression
  1447. | SIZEOF LPAREN type_name RPAREN
  1448. """
  1449. p[0] = c_ast.UnaryOp(
  1450. p[1],
  1451. p[2] if len(p) == 3 else p[3],
  1452. self._token_coord(p, 1))
  1453. def p_unary_operator(self, p):
  1454. """ unary_operator : AND
  1455. | TIMES
  1456. | PLUS
  1457. | MINUS
  1458. | NOT
  1459. | LNOT
  1460. """
  1461. p[0] = p[1]
  1462. def p_postfix_expression_1(self, p):
  1463. """ postfix_expression : primary_expression """
  1464. p[0] = p[1]
  1465. def p_postfix_expression_2(self, p):
  1466. """ postfix_expression : postfix_expression LBRACKET expression RBRACKET """
  1467. p[0] = c_ast.ArrayRef(p[1], p[3], p[1].coord)
  1468. def p_postfix_expression_3(self, p):
  1469. """ postfix_expression : postfix_expression LPAREN argument_expression_list RPAREN
  1470. | postfix_expression LPAREN RPAREN
  1471. """
  1472. p[0] = c_ast.FuncCall(p[1], p[3] if len(p) == 5 else None, p[1].coord)
  1473. def p_postfix_expression_4(self, p):
  1474. """ postfix_expression : postfix_expression PERIOD ID
  1475. | postfix_expression PERIOD TYPEID
  1476. | postfix_expression ARROW ID
  1477. | postfix_expression ARROW TYPEID
  1478. """
  1479. field = c_ast.ID(p[3], self._token_coord(p, 3))
  1480. p[0] = c_ast.StructRef(p[1], p[2], field, p[1].coord)
  1481. def p_postfix_expression_5(self, p):
  1482. """ postfix_expression : postfix_expression PLUSPLUS
  1483. | postfix_expression MINUSMINUS
  1484. """
  1485. p[0] = c_ast.UnaryOp('p' + p[2], p[1], p[1].coord)
  1486. def p_postfix_expression_6(self, p):
  1487. """ postfix_expression : LPAREN type_name RPAREN brace_open initializer_list brace_close
  1488. | LPAREN type_name RPAREN brace_open initializer_list COMMA brace_close
  1489. """
  1490. p[0] = c_ast.CompoundLiteral(p[2], p[5])
  1491. def p_primary_expression_1(self, p):
  1492. """ primary_expression : identifier """
  1493. p[0] = p[1]
  1494. def p_primary_expression_2(self, p):
  1495. """ primary_expression : constant """
  1496. p[0] = p[1]
  1497. def p_primary_expression_3(self, p):
  1498. """ primary_expression : unified_string_literal
  1499. | unified_wstring_literal
  1500. """
  1501. p[0] = p[1]
  1502. def p_primary_expression_4(self, p):
  1503. """ primary_expression : LPAREN expression RPAREN """
  1504. p[0] = p[2]
  1505. def p_primary_expression_5(self, p):
  1506. """ primary_expression : OFFSETOF LPAREN type_name COMMA offsetof_member_designator RPAREN
  1507. """
  1508. coord = self._token_coord(p, 1)
  1509. p[0] = c_ast.FuncCall(c_ast.ID(p[1], coord),
  1510. c_ast.ExprList([p[3], p[5]], coord),
  1511. coord)
  1512. def p_offsetof_member_designator(self, p):
  1513. """ offsetof_member_designator : identifier
  1514. | offsetof_member_designator PERIOD identifier
  1515. | offsetof_member_designator LBRACKET expression RBRACKET
  1516. """
  1517. if len(p) == 2:
  1518. p[0] = p[1]
  1519. elif len(p) == 4:
  1520. p[0] = c_ast.StructRef(p[1], p[2], p[3], p[1].coord)
  1521. elif len(p) == 5:
  1522. p[0] = c_ast.ArrayRef(p[1], p[3], p[1].coord)
  1523. else:
  1524. raise NotImplementedError("Unexpected parsing state. len(p): %u" % len(p))
  1525. def p_argument_expression_list(self, p):
  1526. """ argument_expression_list : assignment_expression
  1527. | argument_expression_list COMMA assignment_expression
  1528. """
  1529. if len(p) == 2: # single expr
  1530. p[0] = c_ast.ExprList([p[1]], p[1].coord)
  1531. else:
  1532. p[1].exprs.append(p[3])
  1533. p[0] = p[1]
  1534. def p_identifier(self, p):
  1535. """ identifier : ID """
  1536. p[0] = c_ast.ID(p[1], self._token_coord(p, 1))
  1537. def p_constant_1(self, p):
  1538. """ constant : INT_CONST_DEC
  1539. | INT_CONST_OCT
  1540. | INT_CONST_HEX
  1541. | INT_CONST_BIN
  1542. | INT_CONST_CHAR
  1543. """
  1544. uCount = 0
  1545. lCount = 0
  1546. for x in p[1][-3:]:
  1547. if x in ('l', 'L'):
  1548. lCount += 1
  1549. elif x in ('u', 'U'):
  1550. uCount += 1
  1551. t = ''
  1552. if uCount > 1:
  1553. raise ValueError('Constant cannot have more than one u/U suffix.')
  1554. elif lCount > 2:
  1555. raise ValueError('Constant cannot have more than two l/L suffix.')
  1556. prefix = 'unsigned ' * uCount + 'long ' * lCount
  1557. p[0] = c_ast.Constant(
  1558. prefix + 'int', p[1], self._token_coord(p, 1))
  1559. def p_constant_2(self, p):
  1560. """ constant : FLOAT_CONST
  1561. | HEX_FLOAT_CONST
  1562. """
  1563. if 'x' in p[1].lower():
  1564. t = 'float'
  1565. else:
  1566. if p[1][-1] in ('f', 'F'):
  1567. t = 'float'
  1568. elif p[1][-1] in ('l', 'L'):
  1569. t = 'long double'
  1570. else:
  1571. t = 'double'
  1572. p[0] = c_ast.Constant(
  1573. t, p[1], self._token_coord(p, 1))
  1574. def p_constant_3(self, p):
  1575. """ constant : CHAR_CONST
  1576. | WCHAR_CONST
  1577. """
  1578. p[0] = c_ast.Constant(
  1579. 'char', p[1], self._token_coord(p, 1))
  1580. # The "unified" string and wstring literal rules are for supporting
  1581. # concatenation of adjacent string literals.
  1582. # I.e. "hello " "world" is seen by the C compiler as a single string literal
  1583. # with the value "hello world"
  1584. #
  1585. def p_unified_string_literal(self, p):
  1586. """ unified_string_literal : STRING_LITERAL
  1587. | unified_string_literal STRING_LITERAL
  1588. """
  1589. if len(p) == 2: # single literal
  1590. p[0] = c_ast.Constant(
  1591. 'string', p[1], self._token_coord(p, 1))
  1592. else:
  1593. p[1].value = p[1].value[:-1] + p[2][1:]
  1594. p[0] = p[1]
  1595. def p_unified_wstring_literal(self, p):
  1596. """ unified_wstring_literal : WSTRING_LITERAL
  1597. | unified_wstring_literal WSTRING_LITERAL
  1598. """
  1599. if len(p) == 2: # single literal
  1600. p[0] = c_ast.Constant(
  1601. 'string', p[1], self._token_coord(p, 1))
  1602. else:
  1603. p[1].value = p[1].value.rstrip()[:-1] + p[2][2:]
  1604. p[0] = p[1]
  1605. def p_brace_open(self, p):
  1606. """ brace_open : LBRACE
  1607. """
  1608. p[0] = p[1]
  1609. p.set_lineno(0, p.lineno(1))
  1610. def p_brace_close(self, p):
  1611. """ brace_close : RBRACE
  1612. """
  1613. p[0] = p[1]
  1614. p.set_lineno(0, p.lineno(1))
  1615. def p_empty(self, p):
  1616. 'empty : '
  1617. p[0] = None
  1618. def p_error(self, p):
  1619. # If error recovery is added here in the future, make sure
  1620. # _get_yacc_lookahead_token still works!
  1621. #
  1622. if p:
  1623. self._parse_error(
  1624. 'before: %s' % p.value,
  1625. self._coord(lineno=p.lineno,
  1626. column=self.clex.find_tok_column(p)))
  1627. else:
  1628. self._parse_error('At end of input', self.clex.filename)