PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/usr.bin/xlint/lint1/cgram.y

http://github.com/davshao/dflygsocdrm
Happy | 1681 lines | 1539 code | 142 blank | 0 comment | 0 complexity | 3535c4477631b22fbb0ccef9a854a472 MD5 | raw file
Possible License(s): AGPL-1.0, CC-BY-SA-3.0, LGPL-2.0, GPL-3.0, LGPL-2.1, LGPL-3.0, MPL-2.0-no-copyleft-exception, 0BSD, BSD-3-Clause, GPL-2.0
  1. %{
  2. /* $NetBSD: cgram.y,v 1.8 1995/10/02 17:31:35 jpo Exp $ */
  3. /*
  4. * Copyright (c) 1994, 1995 Jochen Pohl
  5. * All Rights Reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. All advertising materials mentioning features or use of this software
  16. * must display the following acknowledgement:
  17. * This product includes software developed by Jochen Pohl for
  18. * The NetBSD Project.
  19. * 4. The name of the author may not be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  23. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  24. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  31. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. * $NetBSD: cgram.y,v 1.8 1995/10/02 17:31:35 jpo Exp $
  34. * $DragonFly: src/usr.bin/xlint/lint1/cgram.y,v 1.6 2004/07/07 12:26:51 asmodai Exp $
  35. */
  36. #include <stdlib.h>
  37. #include <limits.h>
  38. #include "lint1.h"
  39. /*
  40. * Contains the level of current declaration. 0 is extern.
  41. * Used for symbol table entries.
  42. */
  43. int blklev;
  44. /*
  45. * level for memory allocation. Normaly the same as blklev.
  46. * An exeption is the declaration of arguments in prototypes. Memory
  47. * for these can't be freed after the declaration, but symbols must
  48. * be removed from the symbol table after the declaration.
  49. */
  50. int mblklev;
  51. static int toicon(tnode_t *);
  52. static void idecl(sym_t *, int);
  53. static void ignuptorp(void);
  54. %}
  55. %union {
  56. int y_int;
  57. val_t *y_val;
  58. sbuf_t *y_sb;
  59. sym_t *y_sym;
  60. op_t y_op;
  61. scl_t y_scl;
  62. tspec_t y_tspec;
  63. tqual_t y_tqual;
  64. type_t *y_type;
  65. tnode_t *y_tnode;
  66. strg_t *y_strg;
  67. pqinf_t *y_pqinf;
  68. };
  69. %token T_LBRACE T_RBRACE T_LBRACK T_RBRACK T_LPARN T_RPARN
  70. %token <y_op> T_STROP
  71. %token <y_op> T_UNOP
  72. %token <y_op> T_INCDEC
  73. %token T_SIZEOF
  74. %token <y_op> T_MULT
  75. %token <y_op> T_DIVOP
  76. %token <y_op> T_ADDOP
  77. %token <y_op> T_SHFTOP
  78. %token <y_op> T_RELOP
  79. %token <y_op> T_EQOP
  80. %token <y_op> T_AND
  81. %token <y_op> T_XOR
  82. %token <y_op> T_OR
  83. %token <y_op> T_LOGAND
  84. %token <y_op> T_LOGOR
  85. %token T_QUEST
  86. %token T_COLON
  87. %token <y_op> T_ASSIGN
  88. %token <y_op> T_OPASS
  89. %token T_COMMA
  90. %token T_SEMI
  91. %token T_ELLIPSE
  92. /* storage classes (extern, static, auto, register and typedef) */
  93. %token <y_scl> T_SCLASS
  94. /* types (char, int, short, long, unsigned, signed, float, double, void) */
  95. %token <y_tspec> T_TYPE
  96. /* qualifiers (const, volatile) */
  97. %token <y_tqual> T_QUAL
  98. /* struct or union */
  99. %token <y_tspec> T_SOU
  100. /* enum */
  101. %token T_ENUM
  102. /* remaining keywords */
  103. %token T_CASE
  104. %token T_DEFAULT
  105. %token T_IF
  106. %token T_ELSE
  107. %token T_SWITCH
  108. %token T_DO
  109. %token T_WHILE
  110. %token T_FOR
  111. %token T_GOTO
  112. %token T_CONTINUE
  113. %token T_BREAK
  114. %token T_RETURN
  115. %token T_ASM
  116. %left T_COMMA
  117. %right T_ASSIGN T_OPASS
  118. %right T_QUEST T_COLON
  119. %left T_LOGOR
  120. %left T_LOGAND
  121. %left T_OR
  122. %left T_XOR
  123. %left T_AND
  124. %left T_EQOP
  125. %left T_RELOP
  126. %left T_SHFTOP
  127. %left T_ADDOP
  128. %left T_MULT T_DIVOP
  129. %right T_UNOP T_INCDEC T_SIZEOF
  130. %left T_LPARN T_LBRACK T_STROP
  131. %token <y_sb> T_NAME
  132. %token <y_sb> T_TYPENAME
  133. %token <y_val> T_CON
  134. %token <y_strg> T_STRING
  135. %type <y_sym> func_decl
  136. %type <y_sym> notype_decl
  137. %type <y_sym> type_decl
  138. %type <y_type> typespec
  139. %type <y_type> clrtyp_typespec
  140. %type <y_type> notype_typespec
  141. %type <y_type> struct_spec
  142. %type <y_type> enum_spec
  143. %type <y_sym> struct_tag
  144. %type <y_sym> enum_tag
  145. %type <y_tspec> struct
  146. %type <y_sym> struct_declaration
  147. %type <y_sb> identifier
  148. %type <y_sym> member_declaration_list_with_rbrace
  149. %type <y_sym> member_declaration_list
  150. %type <y_sym> member_declaration
  151. %type <y_sym> notype_member_decls
  152. %type <y_sym> type_member_decls
  153. %type <y_sym> notype_member_decl
  154. %type <y_sym> type_member_decl
  155. %type <y_tnode> constant
  156. %type <y_sym> enum_declaration
  157. %type <y_sym> enums_with_opt_comma
  158. %type <y_sym> enums
  159. %type <y_sym> enumerator
  160. %type <y_sym> ename
  161. %type <y_sym> notype_direct_decl
  162. %type <y_sym> type_direct_decl
  163. %type <y_pqinf> pointer
  164. %type <y_pqinf> asterisk
  165. %type <y_sym> param_decl
  166. %type <y_sym> param_list
  167. %type <y_sym> abs_decl_param_list
  168. %type <y_sym> direct_param_decl
  169. %type <y_sym> notype_param_decl
  170. %type <y_sym> direct_notype_param_decl
  171. %type <y_pqinf> type_qualifier_list
  172. %type <y_pqinf> type_qualifier
  173. %type <y_sym> identifier_list
  174. %type <y_sym> abs_decl
  175. %type <y_sym> direct_abs_decl
  176. %type <y_sym> vararg_parameter_type_list
  177. %type <y_sym> parameter_type_list
  178. %type <y_sym> parameter_declaration
  179. %type <y_tnode> expr
  180. %type <y_tnode> term
  181. %type <y_tnode> func_arg_list
  182. %type <y_op> point_or_arrow
  183. %type <y_type> type_name
  184. %type <y_sym> abstract_declaration
  185. %type <y_tnode> do_while_expr
  186. %type <y_tnode> opt_expr
  187. %type <y_strg> string
  188. %type <y_strg> string2
  189. %%
  190. program:
  191. /* empty */ {
  192. if (sflag) {
  193. /* empty translation unit */
  194. error(272);
  195. } else if (!tflag) {
  196. /* empty translation unit */
  197. warning(272);
  198. }
  199. }
  200. | translation_unit
  201. ;
  202. translation_unit:
  203. ext_decl
  204. | translation_unit ext_decl
  205. ;
  206. ext_decl:
  207. func_def {
  208. glclup(0);
  209. clrwflgs();
  210. }
  211. | data_def {
  212. glclup(0);
  213. clrwflgs();
  214. }
  215. ;
  216. data_def:
  217. T_SEMI {
  218. if (sflag) {
  219. /* syntax error: empty declaration */
  220. error(0);
  221. } else if (!tflag) {
  222. /* syntax error: empty declaration */
  223. warning(0);
  224. }
  225. }
  226. | clrtyp deftyp notype_init_decls T_SEMI {
  227. if (sflag) {
  228. /* old style declaration; add "int" */
  229. error(1);
  230. } else if (!tflag) {
  231. /* old style declaration; add "int" */
  232. warning(1);
  233. }
  234. }
  235. | declmods deftyp T_SEMI {
  236. if (dcs->d_scl == TYPEDEF) {
  237. /* typedef declares no type name */
  238. warning(72);
  239. } else {
  240. /* empty declaration */
  241. warning(2);
  242. }
  243. }
  244. | declmods deftyp notype_init_decls T_SEMI
  245. | declspecs deftyp T_SEMI {
  246. if (dcs->d_scl == TYPEDEF) {
  247. /* typedef declares no type name */
  248. warning(72);
  249. } else if (!dcs->d_nedecl) {
  250. /* empty declaration */
  251. warning(2);
  252. }
  253. }
  254. | declspecs deftyp type_init_decls T_SEMI
  255. | error T_SEMI {
  256. globclup();
  257. }
  258. | error T_RBRACE {
  259. globclup();
  260. }
  261. ;
  262. func_def:
  263. func_decl {
  264. if ($1->s_type->t_tspec != FUNC) {
  265. /* syntax error */
  266. error(249);
  267. YYERROR;
  268. }
  269. if ($1->s_type->t_typedef) {
  270. /* ()-less function definition */
  271. error(64);
  272. YYERROR;
  273. }
  274. funcdef($1);
  275. blklev++;
  276. pushdecl(ARG);
  277. } opt_arg_declaration_list {
  278. popdecl();
  279. blklev--;
  280. cluparg();
  281. pushctrl(0);
  282. } comp_stmnt {
  283. funcend();
  284. popctrl(0);
  285. }
  286. ;
  287. func_decl:
  288. clrtyp deftyp notype_decl {
  289. $$ = $3;
  290. }
  291. | declmods deftyp notype_decl {
  292. $$ = $3;
  293. }
  294. | declspecs deftyp type_decl {
  295. $$ = $3;
  296. }
  297. ;
  298. opt_arg_declaration_list:
  299. /* empty */
  300. | arg_declaration_list
  301. ;
  302. arg_declaration_list:
  303. arg_declaration
  304. | arg_declaration_list arg_declaration
  305. /* XXX or better "arg_declaration error" ? */
  306. | error
  307. ;
  308. /*
  309. * "arg_declaration" is separated from "declaration" because it
  310. * needs other error handling.
  311. */
  312. arg_declaration:
  313. declmods deftyp T_SEMI {
  314. /* empty declaration */
  315. warning(2);
  316. }
  317. | declmods deftyp notype_init_decls T_SEMI
  318. | declspecs deftyp T_SEMI {
  319. if (!dcs->d_nedecl) {
  320. /* empty declaration */
  321. warning(2);
  322. } else {
  323. tspec_t ts = dcs->d_type->t_tspec;
  324. /* %s declared in argument declaration list */
  325. warning(3, ts == STRUCT ? "struct" :
  326. (ts == UNION ? "union" : "enum"));
  327. }
  328. }
  329. | declspecs deftyp type_init_decls T_SEMI {
  330. if (dcs->d_nedecl) {
  331. tspec_t ts = dcs->d_type->t_tspec;
  332. /* %s declared in argument declaration list */
  333. warning(3, ts == STRUCT ? "struct" :
  334. (ts == UNION ? "union" : "enum"));
  335. }
  336. }
  337. | declmods error
  338. | declspecs error
  339. ;
  340. declaration:
  341. declmods deftyp T_SEMI {
  342. if (dcs->d_scl == TYPEDEF) {
  343. /* typedef declares no type name */
  344. warning(72);
  345. } else {
  346. /* empty declaration */
  347. warning(2);
  348. }
  349. }
  350. | declmods deftyp notype_init_decls T_SEMI
  351. | declspecs deftyp T_SEMI {
  352. if (dcs->d_scl == TYPEDEF) {
  353. /* typedef declares no type name */
  354. warning(72);
  355. } else if (!dcs->d_nedecl) {
  356. /* empty declaration */
  357. warning(2);
  358. }
  359. }
  360. | declspecs deftyp type_init_decls T_SEMI
  361. | error T_SEMI
  362. ;
  363. clrtyp:
  364. {
  365. clrtyp();
  366. }
  367. ;
  368. deftyp:
  369. /* empty */ {
  370. deftyp();
  371. }
  372. ;
  373. declspecs:
  374. clrtyp_typespec {
  375. addtype($1);
  376. }
  377. | declmods typespec {
  378. addtype($2);
  379. }
  380. | declspecs declmod
  381. | declspecs notype_typespec {
  382. addtype($2);
  383. }
  384. ;
  385. declmods:
  386. clrtyp T_QUAL {
  387. addqual($2);
  388. }
  389. | clrtyp T_SCLASS {
  390. addscl($2);
  391. }
  392. | declmods declmod
  393. ;
  394. declmod:
  395. T_QUAL {
  396. addqual($1);
  397. }
  398. | T_SCLASS {
  399. addscl($1);
  400. }
  401. ;
  402. clrtyp_typespec:
  403. clrtyp notype_typespec {
  404. $$ = $2;
  405. }
  406. | T_TYPENAME clrtyp {
  407. $$ = getsym($1)->s_type;
  408. }
  409. ;
  410. typespec:
  411. notype_typespec {
  412. $$ = $1;
  413. }
  414. | T_TYPENAME {
  415. $$ = getsym($1)->s_type;
  416. }
  417. ;
  418. notype_typespec:
  419. T_TYPE {
  420. $$ = gettyp($1);
  421. }
  422. | struct_spec {
  423. popdecl();
  424. $$ = $1;
  425. }
  426. | enum_spec {
  427. popdecl();
  428. $$ = $1;
  429. }
  430. ;
  431. struct_spec:
  432. struct struct_tag {
  433. /*
  434. * STDC requires that "struct a;" always introduces
  435. * a new tag if "a" is not declared at current level
  436. *
  437. * yychar is valid because otherwise the parser would
  438. * not been able to deceide if he must shift or reduce
  439. */
  440. $$ = mktag($2, $1, 0, yychar == T_SEMI);
  441. }
  442. | struct struct_tag {
  443. dcs->d_tagtyp = mktag($2, $1, 1, 0);
  444. } struct_declaration {
  445. $$ = compltag(dcs->d_tagtyp, $4);
  446. }
  447. | struct {
  448. dcs->d_tagtyp = mktag(NULL, $1, 1, 0);
  449. } struct_declaration {
  450. $$ = compltag(dcs->d_tagtyp, $3);
  451. }
  452. | struct error {
  453. symtyp = FVFT;
  454. $$ = gettyp(INT);
  455. }
  456. ;
  457. struct:
  458. T_SOU {
  459. symtyp = FTAG;
  460. pushdecl($1 == STRUCT ? MOS : MOU);
  461. dcs->d_offset = 0;
  462. dcs->d_stralign = CHAR_BIT;
  463. $$ = $1;
  464. }
  465. ;
  466. struct_tag:
  467. identifier {
  468. $$ = getsym($1);
  469. }
  470. ;
  471. struct_declaration:
  472. struct_decl_lbrace member_declaration_list_with_rbrace {
  473. $$ = $2;
  474. }
  475. ;
  476. struct_decl_lbrace:
  477. T_LBRACE {
  478. symtyp = FVFT;
  479. }
  480. ;
  481. member_declaration_list_with_rbrace:
  482. member_declaration_list T_SEMI T_RBRACE {
  483. $$ = $1;
  484. }
  485. | member_declaration_list T_RBRACE {
  486. if (sflag) {
  487. /* syntax req. ";" after last struct/union member */
  488. error(66);
  489. } else {
  490. /* syntax req. ";" after last struct/union member */
  491. warning(66);
  492. }
  493. $$ = $1;
  494. }
  495. | T_RBRACE {
  496. $$ = NULL;
  497. }
  498. ;
  499. member_declaration_list:
  500. member_declaration {
  501. $$ = $1;
  502. }
  503. | member_declaration_list T_SEMI member_declaration {
  504. $$ = lnklst($1, $3);
  505. }
  506. ;
  507. member_declaration:
  508. noclass_declmods deftyp {
  509. /* too late, i know, but getsym() compensates it */
  510. symtyp = FMOS;
  511. } notype_member_decls {
  512. symtyp = FVFT;
  513. $$ = $4;
  514. }
  515. | noclass_declspecs deftyp {
  516. symtyp = FMOS;
  517. } type_member_decls {
  518. symtyp = FVFT;
  519. $$ = $4;
  520. }
  521. | noclass_declmods deftyp {
  522. /* struct or union member must be named */
  523. warning(49);
  524. $$ = NULL;
  525. }
  526. | noclass_declspecs deftyp {
  527. /* struct or union member must be named */
  528. warning(49);
  529. $$ = NULL;
  530. }
  531. | error {
  532. symtyp = FVFT;
  533. $$ = NULL;
  534. }
  535. ;
  536. noclass_declspecs:
  537. clrtyp_typespec {
  538. addtype($1);
  539. }
  540. | noclass_declmods typespec {
  541. addtype($2);
  542. }
  543. | noclass_declspecs T_QUAL {
  544. addqual($2);
  545. }
  546. | noclass_declspecs notype_typespec {
  547. addtype($2);
  548. }
  549. ;
  550. noclass_declmods:
  551. clrtyp T_QUAL {
  552. addqual($2);
  553. }
  554. | noclass_declmods T_QUAL {
  555. addqual($2);
  556. }
  557. ;
  558. notype_member_decls:
  559. notype_member_decl {
  560. $$ = decl1str($1);
  561. }
  562. | notype_member_decls {
  563. symtyp = FMOS;
  564. } T_COMMA type_member_decl {
  565. $$ = lnklst($1, decl1str($4));
  566. }
  567. ;
  568. type_member_decls:
  569. type_member_decl {
  570. $$ = decl1str($1);
  571. }
  572. | type_member_decls {
  573. symtyp = FMOS;
  574. } T_COMMA type_member_decl {
  575. $$ = lnklst($1, decl1str($4));
  576. }
  577. ;
  578. notype_member_decl:
  579. notype_decl {
  580. $$ = $1;
  581. }
  582. | notype_decl T_COLON constant {
  583. $$ = bitfield($1, toicon($3));
  584. }
  585. | {
  586. symtyp = FVFT;
  587. } T_COLON constant {
  588. $$ = bitfield(NULL, toicon($3));
  589. }
  590. ;
  591. type_member_decl:
  592. type_decl {
  593. $$ = $1;
  594. }
  595. | type_decl T_COLON constant {
  596. $$ = bitfield($1, toicon($3));
  597. }
  598. | {
  599. symtyp = FVFT;
  600. } T_COLON constant {
  601. $$ = bitfield(NULL, toicon($3));
  602. }
  603. ;
  604. enum_spec:
  605. enum enum_tag {
  606. $$ = mktag($2, ENUM, 0, 0);
  607. }
  608. | enum enum_tag {
  609. dcs->d_tagtyp = mktag($2, ENUM, 1, 0);
  610. } enum_declaration {
  611. $$ = compltag(dcs->d_tagtyp, $4);
  612. }
  613. | enum {
  614. dcs->d_tagtyp = mktag(NULL, ENUM, 1, 0);
  615. } enum_declaration {
  616. $$ = compltag(dcs->d_tagtyp, $3);
  617. }
  618. | enum error {
  619. symtyp = FVFT;
  620. $$ = gettyp(INT);
  621. }
  622. ;
  623. enum:
  624. T_ENUM {
  625. symtyp = FTAG;
  626. pushdecl(ENUMCON);
  627. }
  628. ;
  629. enum_tag:
  630. identifier {
  631. $$ = getsym($1);
  632. }
  633. ;
  634. enum_declaration:
  635. enum_decl_lbrace enums_with_opt_comma T_RBRACE {
  636. $$ = $2;
  637. }
  638. ;
  639. enum_decl_lbrace:
  640. T_LBRACE {
  641. symtyp = FVFT;
  642. enumval = 0;
  643. }
  644. ;
  645. enums_with_opt_comma:
  646. enums {
  647. $$ = $1;
  648. }
  649. | enums T_COMMA {
  650. if (sflag) {
  651. /* trailing "," prohibited in enum declaration */
  652. error(54);
  653. } else {
  654. /* trailing "," prohibited in enum declaration */
  655. warning(54);
  656. }
  657. $$ = $1;
  658. }
  659. ;
  660. enums:
  661. enumerator {
  662. $$ = $1;
  663. }
  664. | enums T_COMMA enumerator {
  665. $$ = lnklst($1, $3);
  666. }
  667. | error {
  668. $$ = NULL;
  669. }
  670. ;
  671. enumerator:
  672. ename {
  673. $$ = ename($1, enumval, 1);
  674. }
  675. | ename T_ASSIGN constant {
  676. $$ = ename($1, toicon($3), 0);
  677. }
  678. ;
  679. ename:
  680. identifier {
  681. $$ = getsym($1);
  682. }
  683. ;
  684. notype_init_decls:
  685. notype_init_decl
  686. | notype_init_decls T_COMMA type_init_decl
  687. ;
  688. type_init_decls:
  689. type_init_decl
  690. | type_init_decls T_COMMA type_init_decl
  691. ;
  692. notype_init_decl:
  693. notype_decl opt_asm_spec {
  694. idecl($1, 0);
  695. chksz($1);
  696. }
  697. | notype_decl opt_asm_spec {
  698. idecl($1, 1);
  699. } T_ASSIGN initializer {
  700. chksz($1);
  701. }
  702. ;
  703. type_init_decl:
  704. type_decl opt_asm_spec {
  705. idecl($1, 0);
  706. chksz($1);
  707. }
  708. | type_decl opt_asm_spec {
  709. idecl($1, 1);
  710. } T_ASSIGN initializer {
  711. chksz($1);
  712. }
  713. ;
  714. notype_decl:
  715. notype_direct_decl {
  716. $$ = $1;
  717. }
  718. | pointer notype_direct_decl {
  719. $$ = addptr($2, $1);
  720. }
  721. ;
  722. notype_direct_decl:
  723. T_NAME {
  724. $$ = dname(getsym($1));
  725. }
  726. | T_LPARN type_decl T_RPARN {
  727. $$ = $2;
  728. }
  729. | notype_direct_decl T_LBRACK T_RBRACK {
  730. $$ = addarray($1, 0, 0);
  731. }
  732. | notype_direct_decl T_LBRACK constant T_RBRACK {
  733. $$ = addarray($1, 1, toicon($3));
  734. }
  735. | notype_direct_decl param_list {
  736. $$ = addfunc($1, $2);
  737. popdecl();
  738. blklev--;
  739. }
  740. ;
  741. type_decl:
  742. type_direct_decl {
  743. $$ = $1;
  744. }
  745. | pointer type_direct_decl {
  746. $$ = addptr($2, $1);
  747. }
  748. ;
  749. type_direct_decl:
  750. identifier {
  751. $$ = dname(getsym($1));
  752. }
  753. | T_LPARN type_decl T_RPARN {
  754. $$ = $2;
  755. }
  756. | type_direct_decl T_LBRACK T_RBRACK {
  757. $$ = addarray($1, 0, 0);
  758. }
  759. | type_direct_decl T_LBRACK constant T_RBRACK {
  760. $$ = addarray($1, 1, toicon($3));
  761. }
  762. | type_direct_decl param_list {
  763. $$ = addfunc($1, $2);
  764. popdecl();
  765. blklev--;
  766. }
  767. ;
  768. /*
  769. * param_decl and notype_param_decl exist to avoid a conflict in
  770. * argument lists. A typename enclosed in parens should always be
  771. * treated as a typename, not an argument.
  772. * "typedef int a; f(int (a));" is "typedef int a; f(int foo(a));"
  773. * not "typedef int a; f(int a);"
  774. */
  775. param_decl:
  776. direct_param_decl {
  777. $$ = $1;
  778. }
  779. | pointer direct_param_decl {
  780. $$ = addptr($2, $1);
  781. }
  782. ;
  783. direct_param_decl:
  784. identifier {
  785. $$ = dname(getsym($1));
  786. }
  787. | T_LPARN notype_param_decl T_RPARN {
  788. $$ = $2;
  789. }
  790. | direct_param_decl T_LBRACK T_RBRACK {
  791. $$ = addarray($1, 0, 0);
  792. }
  793. | direct_param_decl T_LBRACK constant T_RBRACK {
  794. $$ = addarray($1, 1, toicon($3));
  795. }
  796. | direct_param_decl param_list {
  797. $$ = addfunc($1, $2);
  798. popdecl();
  799. blklev--;
  800. }
  801. ;
  802. notype_param_decl:
  803. direct_notype_param_decl {
  804. $$ = $1;
  805. }
  806. | pointer direct_notype_param_decl {
  807. $$ = addptr($2, $1);
  808. }
  809. ;
  810. direct_notype_param_decl:
  811. T_NAME {
  812. $$ = dname(getsym($1));
  813. }
  814. | T_LPARN notype_param_decl T_RPARN {
  815. $$ = $2;
  816. }
  817. | direct_notype_param_decl T_LBRACK T_RBRACK {
  818. $$ = addarray($1, 0, 0);
  819. }
  820. | direct_notype_param_decl T_LBRACK constant T_RBRACK {
  821. $$ = addarray($1, 1, toicon($3));
  822. }
  823. | direct_notype_param_decl param_list {
  824. $$ = addfunc($1, $2);
  825. popdecl();
  826. blklev--;
  827. }
  828. ;
  829. pointer:
  830. asterisk {
  831. $$ = $1;
  832. }
  833. | asterisk type_qualifier_list {
  834. $$ = mergepq($1, $2);
  835. }
  836. | asterisk pointer {
  837. $$ = mergepq($1, $2);
  838. }
  839. | asterisk type_qualifier_list pointer {
  840. $$ = mergepq(mergepq($1, $2), $3);
  841. }
  842. ;
  843. asterisk:
  844. T_MULT {
  845. $$ = xcalloc(1, sizeof (pqinf_t));
  846. $$->p_pcnt = 1;
  847. }
  848. ;
  849. type_qualifier_list:
  850. type_qualifier {
  851. $$ = $1;
  852. }
  853. | type_qualifier_list type_qualifier {
  854. $$ = mergepq($1, $2);
  855. }
  856. ;
  857. type_qualifier:
  858. T_QUAL {
  859. $$ = xcalloc(1, sizeof (pqinf_t));
  860. if ($1 == CONST) {
  861. $$->p_const = 1;
  862. } else {
  863. $$->p_volatile = 1;
  864. }
  865. }
  866. ;
  867. param_list:
  868. id_list_lparn identifier_list T_RPARN {
  869. $$ = $2;
  870. }
  871. | abs_decl_param_list {
  872. $$ = $1;
  873. }
  874. ;
  875. id_list_lparn:
  876. T_LPARN {
  877. blklev++;
  878. pushdecl(PARG);
  879. }
  880. ;
  881. identifier_list:
  882. T_NAME {
  883. $$ = iname(getsym($1));
  884. }
  885. | identifier_list T_COMMA T_NAME {
  886. $$ = lnklst($1, iname(getsym($3)));
  887. }
  888. | identifier_list error {
  889. $$ = $1;
  890. }
  891. ;
  892. abs_decl_param_list:
  893. abs_decl_lparn T_RPARN {
  894. $$ = NULL;
  895. }
  896. | abs_decl_lparn vararg_parameter_type_list T_RPARN {
  897. dcs->d_proto = 1;
  898. $$ = $2;
  899. }
  900. | abs_decl_lparn error T_RPARN {
  901. $$ = NULL;
  902. }
  903. ;
  904. abs_decl_lparn:
  905. T_LPARN {
  906. blklev++;
  907. pushdecl(PARG);
  908. }
  909. ;
  910. vararg_parameter_type_list:
  911. parameter_type_list {
  912. $$ = $1;
  913. }
  914. | parameter_type_list T_COMMA T_ELLIPSE {
  915. dcs->d_vararg = 1;
  916. $$ = $1;
  917. }
  918. | T_ELLIPSE {
  919. if (sflag) {
  920. /* ANSI C requires formal parameter before "..." */
  921. error(84);
  922. } else if (!tflag) {
  923. /* ANSI C requires formal parameter before "..." */
  924. warning(84);
  925. }
  926. dcs->d_vararg = 1;
  927. $$ = NULL;
  928. }
  929. ;
  930. parameter_type_list:
  931. parameter_declaration opt_asm_spec {
  932. $$ = $1;
  933. }
  934. | parameter_type_list T_COMMA parameter_declaration opt_asm_spec {
  935. $$ = lnklst($1, $3);
  936. }
  937. ;
  938. parameter_declaration:
  939. declmods deftyp {
  940. $$ = decl1arg(aname(), 0);
  941. }
  942. | declspecs deftyp {
  943. $$ = decl1arg(aname(), 0);
  944. }
  945. | declmods deftyp notype_param_decl {
  946. $$ = decl1arg($3, 0);
  947. }
  948. /*
  949. * param_decl is needed because of following conflict:
  950. * "typedef int a; f(int (a));" could be parsed as
  951. * "function with argument a of type int", or
  952. * "function with an abstract argument of type function".
  953. * This grammar realizes the second case.
  954. */
  955. | declspecs deftyp param_decl {
  956. $$ = decl1arg($3, 0);
  957. }
  958. | declmods deftyp abs_decl {
  959. $$ = decl1arg($3, 0);
  960. }
  961. | declspecs deftyp abs_decl {
  962. $$ = decl1arg($3, 0);
  963. }
  964. ;
  965. opt_asm_spec:
  966. /* empty */
  967. | T_ASM T_LPARN T_STRING T_RPARN {
  968. freeyyv(&$3, T_STRING);
  969. }
  970. ;
  971. initializer:
  972. init_expr
  973. ;
  974. init_expr:
  975. expr %prec T_COMMA {
  976. mkinit($1);
  977. }
  978. | init_lbrace init_expr_list init_rbrace
  979. | init_lbrace init_expr_list T_COMMA init_rbrace
  980. | error
  981. ;
  982. init_expr_list:
  983. init_expr %prec T_COMMA
  984. | init_expr_list T_COMMA init_expr
  985. ;
  986. init_lbrace:
  987. T_LBRACE {
  988. initlbr();
  989. }
  990. ;
  991. init_rbrace:
  992. T_RBRACE {
  993. initrbr();
  994. }
  995. ;
  996. type_name:
  997. {
  998. pushdecl(ABSTRACT);
  999. } abstract_declaration {
  1000. popdecl();
  1001. $$ = $2->s_type;
  1002. }
  1003. ;
  1004. abstract_declaration:
  1005. noclass_declmods deftyp {
  1006. $$ = decl1abs(aname());
  1007. }
  1008. | noclass_declspecs deftyp {
  1009. $$ = decl1abs(aname());
  1010. }
  1011. | noclass_declmods deftyp abs_decl {
  1012. $$ = decl1abs($3);
  1013. }
  1014. | noclass_declspecs deftyp abs_decl {
  1015. $$ = decl1abs($3);
  1016. }
  1017. ;
  1018. abs_decl:
  1019. pointer {
  1020. $$ = addptr(aname(), $1);
  1021. }
  1022. | direct_abs_decl {
  1023. $$ = $1;
  1024. }
  1025. | pointer direct_abs_decl {
  1026. $$ = addptr($2, $1);
  1027. }
  1028. ;
  1029. direct_abs_decl:
  1030. T_LPARN abs_decl T_RPARN {
  1031. $$ = $2;
  1032. }
  1033. | T_LBRACK T_RBRACK {
  1034. $$ = addarray(aname(), 0, 0);
  1035. }
  1036. | T_LBRACK constant T_RBRACK {
  1037. $$ = addarray(aname(), 1, toicon($2));
  1038. }
  1039. | direct_abs_decl T_LBRACK T_RBRACK {
  1040. $$ = addarray($1, 0, 0);
  1041. }
  1042. | direct_abs_decl T_LBRACK constant T_RBRACK {
  1043. $$ = addarray($1, 1, toicon($3));
  1044. }
  1045. | abs_decl_param_list {
  1046. $$ = addfunc(aname(), $1);
  1047. popdecl();
  1048. blklev--;
  1049. }
  1050. | direct_abs_decl abs_decl_param_list {
  1051. $$ = addfunc($1, $2);
  1052. popdecl();
  1053. blklev--;
  1054. }
  1055. ;
  1056. stmnt:
  1057. labeled_stmnt
  1058. | expr_stmnt
  1059. | comp_stmnt
  1060. | selection_stmnt
  1061. | iteration_stmnt
  1062. | jump_stmnt {
  1063. ftflg = 0;
  1064. }
  1065. | asm_stmnt
  1066. ;
  1067. labeled_stmnt:
  1068. label stmnt
  1069. ;
  1070. label:
  1071. identifier T_COLON {
  1072. symtyp = FLAB;
  1073. label(T_NAME, getsym($1), NULL);
  1074. }
  1075. | T_CASE constant T_COLON {
  1076. label(T_CASE, NULL, $2);
  1077. ftflg = 1;
  1078. }
  1079. | T_DEFAULT T_COLON {
  1080. label(T_DEFAULT, NULL, NULL);
  1081. ftflg = 1;
  1082. }
  1083. ;
  1084. comp_stmnt:
  1085. compstmnt_lbrace declaration_list opt_stmnt_list compstmnt_rbrace
  1086. | compstmnt_lbrace opt_stmnt_list compstmnt_rbrace
  1087. ;
  1088. compstmnt_lbrace:
  1089. T_LBRACE {
  1090. blklev++;
  1091. mblklev++;
  1092. pushdecl(AUTO);
  1093. }
  1094. ;
  1095. compstmnt_rbrace:
  1096. T_RBRACE {
  1097. popdecl();
  1098. freeblk();
  1099. mblklev--;
  1100. blklev--;
  1101. ftflg = 0;
  1102. }
  1103. ;
  1104. opt_stmnt_list:
  1105. /* empty */
  1106. | stmnt_list
  1107. ;
  1108. stmnt_list:
  1109. stmnt {
  1110. clrwflgs();
  1111. }
  1112. | stmnt_list stmnt {
  1113. clrwflgs();
  1114. }
  1115. | stmnt_list error T_SEMI {
  1116. clrwflgs();
  1117. }
  1118. ;
  1119. expr_stmnt:
  1120. expr T_SEMI {
  1121. expr($1, 0, 0);
  1122. ftflg = 0;
  1123. }
  1124. | T_SEMI {
  1125. ftflg = 0;
  1126. }
  1127. ;
  1128. selection_stmnt:
  1129. if_without_else {
  1130. if2();
  1131. if3(0);
  1132. }
  1133. | if_without_else T_ELSE {
  1134. if2();
  1135. } stmnt {
  1136. if3(1);
  1137. }
  1138. | if_without_else T_ELSE error {
  1139. if3(0);
  1140. }
  1141. | switch_expr stmnt {
  1142. switch2();
  1143. }
  1144. | switch_expr error {
  1145. switch2();
  1146. }
  1147. ;
  1148. if_without_else:
  1149. if_expr stmnt
  1150. | if_expr error
  1151. ;
  1152. if_expr:
  1153. T_IF T_LPARN expr T_RPARN {
  1154. if1($3);
  1155. clrwflgs();
  1156. }
  1157. ;
  1158. switch_expr:
  1159. T_SWITCH T_LPARN expr T_RPARN {
  1160. switch1($3);
  1161. clrwflgs();
  1162. }
  1163. ;
  1164. iteration_stmnt:
  1165. while_expr stmnt {
  1166. while2();
  1167. }
  1168. | while_expr error {
  1169. while2();
  1170. }
  1171. | do stmnt do_while_expr {
  1172. do2($3);
  1173. ftflg = 0;
  1174. }
  1175. | do error {
  1176. do2(NULL);
  1177. }
  1178. | for_exprs stmnt {
  1179. for2();
  1180. }
  1181. | for_exprs error {
  1182. for2();
  1183. }
  1184. ;
  1185. while_expr:
  1186. T_WHILE T_LPARN expr T_RPARN {
  1187. while1($3);
  1188. clrwflgs();
  1189. }
  1190. ;
  1191. do:
  1192. T_DO {
  1193. do1();
  1194. }
  1195. ;
  1196. do_while_expr:
  1197. T_WHILE T_LPARN expr T_RPARN T_SEMI {
  1198. $$ = $3;
  1199. }
  1200. ;
  1201. for_exprs:
  1202. T_FOR T_LPARN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN {
  1203. for1($3, $5, $7);
  1204. clrwflgs();
  1205. }
  1206. ;
  1207. opt_expr:
  1208. /* empty */ {
  1209. $$ = NULL;
  1210. }
  1211. | expr {
  1212. $$ = $1;
  1213. }
  1214. ;
  1215. jump_stmnt:
  1216. goto identifier T_SEMI {
  1217. dogoto(getsym($2));
  1218. }
  1219. | goto error T_SEMI {
  1220. symtyp = FVFT;
  1221. }
  1222. | T_CONTINUE T_SEMI {
  1223. docont();
  1224. }
  1225. | T_BREAK T_SEMI {
  1226. dobreak();
  1227. }
  1228. | T_RETURN T_SEMI {
  1229. doreturn(NULL);
  1230. }
  1231. | T_RETURN expr T_SEMI {
  1232. doreturn($2);
  1233. }
  1234. ;
  1235. goto:
  1236. T_GOTO {
  1237. symtyp = FLAB;
  1238. }
  1239. ;
  1240. asm_stmnt:
  1241. T_ASM T_LPARN read_until_rparn T_SEMI {
  1242. setasm();
  1243. }
  1244. | T_ASM T_QUAL T_LPARN read_until_rparn T_SEMI {
  1245. setasm();
  1246. }
  1247. | T_ASM error
  1248. ;
  1249. read_until_rparn:
  1250. /* empty */ {
  1251. ignuptorp();
  1252. }
  1253. ;
  1254. declaration_list:
  1255. declaration {
  1256. clrwflgs();
  1257. }
  1258. | declaration_list declaration {
  1259. clrwflgs();
  1260. }
  1261. ;
  1262. constant:
  1263. expr %prec T_COMMA {
  1264. $$ = $1;
  1265. }
  1266. ;
  1267. expr:
  1268. expr T_MULT expr {
  1269. $$ = build(MULT, $1, $3);
  1270. }
  1271. | expr T_DIVOP expr {
  1272. $$ = build($2, $1, $3);
  1273. }
  1274. | expr T_ADDOP expr {
  1275. $$ = build($2, $1, $3);
  1276. }
  1277. | expr T_SHFTOP expr {
  1278. $$ = build($2, $1, $3);
  1279. }
  1280. | expr T_RELOP expr {
  1281. $$ = build($2, $1, $3);
  1282. }
  1283. | expr T_EQOP expr {
  1284. $$ = build($2, $1, $3);
  1285. }
  1286. | expr T_AND expr {
  1287. $$ = build(AND, $1, $3);
  1288. }
  1289. | expr T_XOR expr {
  1290. $$ = build(XOR, $1, $3);
  1291. }
  1292. | expr T_OR expr {
  1293. $$ = build(OR, $1, $3);
  1294. }
  1295. | expr T_LOGAND expr {
  1296. $$ = build(LOGAND, $1, $3);
  1297. }
  1298. | expr T_LOGOR expr {
  1299. $$ = build(LOGOR, $1, $3);
  1300. }
  1301. | expr T_QUEST expr T_COLON expr {
  1302. $$ = build(QUEST, $1, build(COLON, $3, $5));
  1303. }
  1304. | expr T_ASSIGN expr {
  1305. $$ = build(ASSIGN, $1, $3);
  1306. }
  1307. | expr T_OPASS expr {
  1308. $$ = build($2, $1, $3);
  1309. }
  1310. | expr T_COMMA expr {
  1311. $$ = build(COMMA, $1, $3);
  1312. }
  1313. | term {
  1314. $$ = $1;
  1315. }
  1316. ;
  1317. term:
  1318. T_NAME {
  1319. /* XXX realy neccessary? */
  1320. if (yychar < 0)
  1321. yychar = yylex();
  1322. $$ = getnnode(getsym($1), yychar);
  1323. }
  1324. | string {
  1325. $$ = getsnode($1);
  1326. }
  1327. | T_CON {
  1328. $$ = getcnode(gettyp($1->v_tspec), $1);
  1329. }
  1330. | T_LPARN expr T_RPARN {
  1331. if ($2 != NULL)
  1332. $2->tn_parn = 1;
  1333. $$ = $2;
  1334. }
  1335. | term T_INCDEC {
  1336. $$ = build($2 == INC ? INCAFT : DECAFT, $1, NULL);
  1337. }
  1338. | T_INCDEC term {
  1339. $$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL);
  1340. }
  1341. | T_MULT term {
  1342. $$ = build(STAR, $2, NULL);
  1343. }
  1344. | T_AND term {
  1345. $$ = build(AMPER, $2, NULL);
  1346. }
  1347. | T_UNOP term {
  1348. $$ = build($1, $2, NULL);
  1349. }
  1350. | T_ADDOP term {
  1351. if (tflag && $1 == PLUS) {
  1352. /* unary + is illegal in traditional C */
  1353. warning(100);
  1354. }
  1355. $$ = build($1 == PLUS ? UPLUS : UMINUS, $2, NULL);
  1356. }
  1357. | term T_LBRACK expr T_RBRACK {
  1358. $$ = build(STAR, build(PLUS, $1, $3), NULL);
  1359. }
  1360. | term T_LPARN T_RPARN {
  1361. $$ = funccall($1, NULL);
  1362. }
  1363. | term T_LPARN func_arg_list T_RPARN {
  1364. $$ = funccall($1, $3);
  1365. }
  1366. | term point_or_arrow T_NAME {
  1367. if ($1 != NULL) {
  1368. sym_t *msym;
  1369. /* XXX strmemb should be integrated in build() */
  1370. if ($2 == ARROW) {
  1371. /* must to this before strmemb is called */
  1372. $1 = cconv($1);
  1373. }
  1374. msym = strmemb($1, $2, getsym($3));
  1375. $$ = build($2, $1, getnnode(msym, 0));
  1376. } else {
  1377. $$ = NULL;
  1378. }
  1379. }
  1380. | T_SIZEOF term %prec T_SIZEOF {
  1381. if (($$ = $2 == NULL ? NULL : bldszof($2->tn_type)) != NULL)
  1382. chkmisc($2, 0, 0, 0, 0, 0, 1);
  1383. }
  1384. | T_SIZEOF T_LPARN type_name T_RPARN %prec T_SIZEOF {
  1385. $$ = bldszof($3);
  1386. }
  1387. | T_LPARN type_name T_RPARN term %prec T_UNOP {
  1388. $$ = cast($4, $2);
  1389. }
  1390. ;
  1391. string:
  1392. T_STRING {
  1393. $$ = $1;
  1394. }
  1395. | T_STRING string2 {
  1396. $$ = catstrg($1, $2);
  1397. }
  1398. ;
  1399. string2:
  1400. T_STRING {
  1401. if (tflag) {
  1402. /* concatenated strings are illegal in traditional C */
  1403. warning(219);
  1404. }
  1405. $$ = $1;
  1406. }
  1407. | string2 T_STRING {
  1408. $$ = catstrg($1, $2);
  1409. }
  1410. ;
  1411. func_arg_list:
  1412. expr %prec T_COMMA {
  1413. $$ = funcarg(NULL, $1);
  1414. }
  1415. | func_arg_list T_COMMA expr {
  1416. $$ = funcarg($1, $3);
  1417. }
  1418. ;
  1419. point_or_arrow:
  1420. T_STROP {
  1421. symtyp = FMOS;
  1422. $$ = $1;
  1423. }
  1424. ;
  1425. identifier:
  1426. T_NAME {
  1427. $$ = $1;
  1428. }
  1429. | T_TYPENAME {
  1430. $$ = $1;
  1431. }
  1432. ;
  1433. %%
  1434. /* ARGSUSED */
  1435. int
  1436. yyerror(char *msg)
  1437. {
  1438. error(249);
  1439. if (++sytxerr >= 5)
  1440. norecover();
  1441. return (0);
  1442. }
  1443. /*
  1444. * Gets a node for a constant and returns the value of this constant
  1445. * as integer.
  1446. * Is the node not constant or too large for int or of type float,
  1447. * a warning will be printed.
  1448. *
  1449. * toicon() should be used only inside declarations. If it is used in
  1450. * expressions, it frees the memory used for the expression.
  1451. */
  1452. static int
  1453. toicon(tnode_t *tn)
  1454. {
  1455. int i;
  1456. tspec_t t;
  1457. val_t *v;
  1458. v = constant(tn);
  1459. /*
  1460. * Abstract declarations are used inside expression. To free
  1461. * the memory would be a fatal error.
  1462. */
  1463. if (dcs->d_ctx != ABSTRACT)
  1464. tfreeblk();
  1465. if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) {
  1466. i = (int)v->v_ldbl;
  1467. /* integral constant expression expected */
  1468. error(55);
  1469. } else {
  1470. i = (int)v->v_quad;
  1471. if (isutyp(t)) {
  1472. if ((u_quad_t)v->v_quad > INT_MAX) {
  1473. /* integral constant too large */
  1474. warning(56);
  1475. }
  1476. } else {
  1477. #ifdef XXX_BROKEN_GCC
  1478. if (v->v_quad > INT_MAX) {
  1479. /* integral constant too large */
  1480. warning(56);
  1481. }
  1482. else if (v->v_quad < INT_MIN) {
  1483. /* integral constant too large */
  1484. warning(56);
  1485. }
  1486. #else
  1487. if (v->v_quad > INT_MAX || v->v_quad < INT_MIN) {
  1488. /* integral constant too large */
  1489. warning(56);
  1490. }
  1491. #endif
  1492. }
  1493. }
  1494. free(v);
  1495. return (i);
  1496. }
  1497. static void
  1498. idecl(sym_t *decl, int initflg)
  1499. {
  1500. initerr = 0;
  1501. initsym = decl;
  1502. switch (dcs->d_ctx) {
  1503. case EXTERN:
  1504. decl1ext(decl, initflg);
  1505. break;
  1506. case ARG:
  1507. (void)decl1arg(decl, initflg);
  1508. break;
  1509. case AUTO:
  1510. decl1loc(decl, initflg);
  1511. break;
  1512. default:
  1513. lerror("idecl()");
  1514. }
  1515. if (initflg && !initerr)
  1516. prepinit();
  1517. }
  1518. /*
  1519. * Discard all input tokens up to and including the next
  1520. * unmatched right paren
  1521. */
  1522. void
  1523. ignuptorp(void)
  1524. {
  1525. int level;
  1526. if (yychar < 0)
  1527. yychar = yylex();
  1528. freeyyv(&yylval, yychar);
  1529. level = 1;
  1530. while (yychar != T_RPARN || --level > 0) {
  1531. if (yychar == T_LPARN) {
  1532. level++;
  1533. } else if (yychar <= 0) {
  1534. break;
  1535. }
  1536. freeyyv(&yylval, yychar = yylex());
  1537. }
  1538. yyclearin;
  1539. }