/usr.bin/yacc/skeleton.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 426 lines · 368 code · 16 blank · 42 comment · 2 complexity · 42830e8461d5eb5862209c50999fc715 MD5 · raw file

  1. /*
  2. * Copyright (c) 1989 The Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Robert Paul Corbett.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 4. Neither the name of the University nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. #if 0
  33. #ifndef lint
  34. static char sccsid[] = "@(#)skeleton.c 5.8 (Berkeley) 4/29/95";
  35. #endif
  36. #endif
  37. #include <sys/cdefs.h>
  38. __FBSDID("$FreeBSD$");
  39. #include "defs.h"
  40. /* The definition of yysccsid in the banner should be replaced with */
  41. /* a #pragma ident directive if the target C compiler supports */
  42. /* #pragma ident directives. */
  43. /* */
  44. /* If the skeleton is changed, the banner should be changed so that */
  45. /* the altered version can be easily distinguished from the original. */
  46. /* */
  47. /* The #defines included with the banner are there because they are */
  48. /* useful in subsequent code. The macros #defined in the header or */
  49. /* the body either are not useful outside of semantic actions or */
  50. /* are conditional. */
  51. const char *banner[] =
  52. {
  53. "#include <stdlib.h>",
  54. "#include <string.h>",
  55. "#ifndef lint",
  56. "#ifdef __unused",
  57. "__unused",
  58. "#endif",
  59. "static char const ",
  60. "yyrcsid[] = \"$FreeBSD$\";",
  61. "#endif",
  62. "#define YYBYACC 1",
  63. "#define YYMAJOR 1",
  64. "#define YYMINOR 9",
  65. "#define YYLEX yylex()",
  66. "#define YYEMPTY -1",
  67. "#define yyclearin (yychar=(YYEMPTY))",
  68. "#define yyerrok (yyerrflag=0)",
  69. "#define YYRECOVERING() (yyerrflag!=0)",
  70. "#if defined(__cplusplus) || __STDC__",
  71. "static int yygrowstack(void);",
  72. "#else",
  73. "static int yygrowstack();",
  74. "#endif",
  75. 0
  76. };
  77. const char *tables[] =
  78. {
  79. "extern const short yylhs[];",
  80. "extern const short yylen[];",
  81. "extern const short yydefred[];",
  82. "extern const short yydgoto[];",
  83. "extern const short yysindex[];",
  84. "extern const short yyrindex[];",
  85. "extern const short yygindex[];",
  86. "extern const short yytable[];",
  87. "extern const short yycheck[];",
  88. "#if YYDEBUG",
  89. "extern char *yyname[];",
  90. "extern char *yyrule[];",
  91. "#endif",
  92. 0
  93. };
  94. const char *header[] =
  95. {
  96. "#if YYDEBUG",
  97. "#include <stdio.h>",
  98. "#endif",
  99. "#ifdef YYSTACKSIZE",
  100. "#undef YYMAXDEPTH",
  101. "#define YYMAXDEPTH YYSTACKSIZE",
  102. "#else",
  103. "#ifdef YYMAXDEPTH",
  104. "#define YYSTACKSIZE YYMAXDEPTH",
  105. "#else",
  106. "#define YYSTACKSIZE 10000",
  107. "#define YYMAXDEPTH 10000",
  108. "#endif",
  109. "#endif",
  110. "#define YYINITSTACKSIZE 200",
  111. "int yydebug;",
  112. "int yynerrs;",
  113. "int yyerrflag;",
  114. "int yychar;",
  115. "short *yyssp;",
  116. "YYSTYPE *yyvsp;",
  117. "YYSTYPE yyval;",
  118. "YYSTYPE yylval;",
  119. "short *yyss;",
  120. "short *yysslim;",
  121. "YYSTYPE *yyvs;",
  122. "int yystacksize;",
  123. 0
  124. };
  125. const char *body[] =
  126. {
  127. "/* allocate initial stack or double stack size, up to YYMAXDEPTH */",
  128. "#if defined(__cplusplus) || __STDC__",
  129. "static int yygrowstack(void)",
  130. "#else",
  131. "static int yygrowstack()",
  132. "#endif",
  133. "{",
  134. " int newsize, i;",
  135. " short *newss;",
  136. " YYSTYPE *newvs;",
  137. "",
  138. " if ((newsize = yystacksize) == 0)",
  139. " newsize = YYINITSTACKSIZE;",
  140. " else if (newsize >= YYMAXDEPTH)",
  141. " return -1;",
  142. " else if ((newsize *= 2) > YYMAXDEPTH)",
  143. " newsize = YYMAXDEPTH;",
  144. " i = yyssp - yyss;",
  145. " newss = yyss ? (short *)realloc(yyss, newsize * sizeof *newss) :",
  146. " (short *)malloc(newsize * sizeof *newss);",
  147. " if (newss == NULL)",
  148. " return -1;",
  149. " yyss = newss;",
  150. " yyssp = newss + i;",
  151. " newvs = yyvs ? (YYSTYPE *)realloc(yyvs, newsize * sizeof *newvs) :",
  152. " (YYSTYPE *)malloc(newsize * sizeof *newvs);",
  153. " if (newvs == NULL)",
  154. " return -1;",
  155. " yyvs = newvs;",
  156. " yyvsp = newvs + i;",
  157. " yystacksize = newsize;",
  158. " yysslim = yyss + newsize - 1;",
  159. " return 0;",
  160. "}",
  161. "",
  162. "#define YYABORT goto yyabort",
  163. "#define YYREJECT goto yyabort",
  164. "#define YYACCEPT goto yyaccept",
  165. "#define YYERROR goto yyerrlab",
  166. "",
  167. "#ifndef YYPARSE_PARAM",
  168. "#if defined(__cplusplus) || __STDC__",
  169. "#define YYPARSE_PARAM_ARG void",
  170. "#define YYPARSE_PARAM_DECL",
  171. "#else /* ! ANSI-C/C++ */",
  172. "#define YYPARSE_PARAM_ARG",
  173. "#define YYPARSE_PARAM_DECL",
  174. "#endif /* ANSI-C/C++ */",
  175. "#else /* YYPARSE_PARAM */",
  176. "#ifndef YYPARSE_PARAM_TYPE",
  177. "#define YYPARSE_PARAM_TYPE void *",
  178. "#endif",
  179. "#if defined(__cplusplus) || __STDC__",
  180. "#define YYPARSE_PARAM_ARG YYPARSE_PARAM_TYPE YYPARSE_PARAM",
  181. "#define YYPARSE_PARAM_DECL",
  182. "#else /* ! ANSI-C/C++ */",
  183. "#define YYPARSE_PARAM_ARG YYPARSE_PARAM",
  184. "#define YYPARSE_PARAM_DECL YYPARSE_PARAM_TYPE YYPARSE_PARAM;",
  185. "#endif /* ANSI-C/C++ */",
  186. "#endif /* ! YYPARSE_PARAM */",
  187. "",
  188. "int",
  189. "yyparse (YYPARSE_PARAM_ARG)",
  190. " YYPARSE_PARAM_DECL",
  191. "{",
  192. " int yym, yyn, yystate;",
  193. "#if YYDEBUG",
  194. " const char *yys;",
  195. "",
  196. " if ((yys = getenv(\"YYDEBUG\")))",
  197. " {",
  198. " yyn = *yys;",
  199. " if (yyn >= '0' && yyn <= '9')",
  200. " yydebug = yyn - '0';",
  201. " }",
  202. "#endif",
  203. "",
  204. " yynerrs = 0;",
  205. " yyerrflag = 0;",
  206. " yychar = (-1);",
  207. "",
  208. " if (yyss == NULL && yygrowstack()) goto yyoverflow;",
  209. " yyssp = yyss;",
  210. " yyvsp = yyvs;",
  211. " *yyssp = yystate = 0;",
  212. "",
  213. "yyloop:",
  214. " if ((yyn = yydefred[yystate])) goto yyreduce;",
  215. " if (yychar < 0)",
  216. " {",
  217. " if ((yychar = yylex()) < 0) yychar = 0;",
  218. "#if YYDEBUG",
  219. " if (yydebug)",
  220. " {",
  221. " yys = 0;",
  222. " if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
  223. " if (!yys) yys = \"illegal-symbol\";",
  224. " printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
  225. " YYPREFIX, yystate, yychar, yys);",
  226. " }",
  227. "#endif",
  228. " }",
  229. " if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&",
  230. " yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
  231. " {",
  232. "#if YYDEBUG",
  233. " if (yydebug)",
  234. " printf(\"%sdebug: state %d, shifting to state %d\\n\",",
  235. " YYPREFIX, yystate, yytable[yyn]);",
  236. "#endif",
  237. " if (yyssp >= yysslim && yygrowstack())",
  238. " {",
  239. " goto yyoverflow;",
  240. " }",
  241. " *++yyssp = yystate = yytable[yyn];",
  242. " *++yyvsp = yylval;",
  243. " yychar = (-1);",
  244. " if (yyerrflag > 0) --yyerrflag;",
  245. " goto yyloop;",
  246. " }",
  247. " if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&",
  248. " yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
  249. " {",
  250. " yyn = yytable[yyn];",
  251. " goto yyreduce;",
  252. " }",
  253. " if (yyerrflag) goto yyinrecovery;",
  254. "#if defined(lint) || defined(__GNUC__)",
  255. " goto yynewerror;",
  256. "#endif",
  257. "yynewerror:",
  258. " yyerror(\"syntax error\");",
  259. "#if defined(lint) || defined(__GNUC__)",
  260. " goto yyerrlab;",
  261. "#endif",
  262. "yyerrlab:",
  263. " ++yynerrs;",
  264. "yyinrecovery:",
  265. " if (yyerrflag < 3)",
  266. " {",
  267. " yyerrflag = 3;",
  268. " for (;;)",
  269. " {",
  270. " if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&",
  271. " yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)",
  272. " {",
  273. "#if YYDEBUG",
  274. " if (yydebug)",
  275. " printf(\"%sdebug: state %d, error recovery shifting\\",
  276. " to state %d\\n\", YYPREFIX, *yyssp, yytable[yyn]);",
  277. "#endif",
  278. " if (yyssp >= yysslim && yygrowstack())",
  279. " {",
  280. " goto yyoverflow;",
  281. " }",
  282. " *++yyssp = yystate = yytable[yyn];",
  283. " *++yyvsp = yylval;",
  284. " goto yyloop;",
  285. " }",
  286. " else",
  287. " {",
  288. "#if YYDEBUG",
  289. " if (yydebug)",
  290. " printf(\"%sdebug: error recovery discarding state %d\
  291. \\n\",",
  292. " YYPREFIX, *yyssp);",
  293. "#endif",
  294. " if (yyssp <= yyss) goto yyabort;",
  295. " --yyssp;",
  296. " --yyvsp;",
  297. " }",
  298. " }",
  299. " }",
  300. " else",
  301. " {",
  302. " if (yychar == 0) goto yyabort;",
  303. "#if YYDEBUG",
  304. " if (yydebug)",
  305. " {",
  306. " yys = 0;",
  307. " if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
  308. " if (!yys) yys = \"illegal-symbol\";",
  309. " printf(\"%sdebug: state %d, error recovery discards token %d\
  310. (%s)\\n\",",
  311. " YYPREFIX, yystate, yychar, yys);",
  312. " }",
  313. "#endif",
  314. " yychar = (-1);",
  315. " goto yyloop;",
  316. " }",
  317. "yyreduce:",
  318. "#if YYDEBUG",
  319. " if (yydebug)",
  320. " printf(\"%sdebug: state %d, reducing by rule %d (%s)\\n\",",
  321. " YYPREFIX, yystate, yyn, yyrule[yyn]);",
  322. "#endif",
  323. " yym = yylen[yyn];",
  324. " if (yym)",
  325. " yyval = yyvsp[1-yym];",
  326. " else",
  327. " memset(&yyval, 0, sizeof yyval);",
  328. " switch (yyn)",
  329. " {",
  330. 0
  331. };
  332. const char *trailer[] =
  333. {
  334. " }",
  335. " yyssp -= yym;",
  336. " yystate = *yyssp;",
  337. " yyvsp -= yym;",
  338. " yym = yylhs[yyn];",
  339. " if (yystate == 0 && yym == 0)",
  340. " {",
  341. "#if YYDEBUG",
  342. " if (yydebug)",
  343. " printf(\"%sdebug: after reduction, shifting from state 0 to\\",
  344. " state %d\\n\", YYPREFIX, YYFINAL);",
  345. "#endif",
  346. " yystate = YYFINAL;",
  347. " *++yyssp = YYFINAL;",
  348. " *++yyvsp = yyval;",
  349. " if (yychar < 0)",
  350. " {",
  351. " if ((yychar = yylex()) < 0) yychar = 0;",
  352. "#if YYDEBUG",
  353. " if (yydebug)",
  354. " {",
  355. " yys = 0;",
  356. " if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
  357. " if (!yys) yys = \"illegal-symbol\";",
  358. " printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
  359. " YYPREFIX, YYFINAL, yychar, yys);",
  360. " }",
  361. "#endif",
  362. " }",
  363. " if (yychar == 0) goto yyaccept;",
  364. " goto yyloop;",
  365. " }",
  366. " if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&",
  367. " yyn <= YYTABLESIZE && yycheck[yyn] == yystate)",
  368. " yystate = yytable[yyn];",
  369. " else",
  370. " yystate = yydgoto[yym];",
  371. "#if YYDEBUG",
  372. " if (yydebug)",
  373. " printf(\"%sdebug: after reduction, shifting from state %d \\",
  374. "to state %d\\n\", YYPREFIX, *yyssp, yystate);",
  375. "#endif",
  376. " if (yyssp >= yysslim && yygrowstack())",
  377. " {",
  378. " goto yyoverflow;",
  379. " }",
  380. " *++yyssp = yystate;",
  381. " *++yyvsp = yyval;",
  382. " goto yyloop;",
  383. "yyoverflow:",
  384. " yyerror(\"yacc stack overflow\");",
  385. "yyabort:",
  386. " return (1);",
  387. "yyaccept:",
  388. " return (0);",
  389. "}",
  390. 0
  391. };
  392. void
  393. write_section(const char *section[])
  394. {
  395. int c;
  396. int i;
  397. const char *s;
  398. FILE *f;
  399. f = code_file;
  400. for (i = 0; (s = section[i]); ++i)
  401. {
  402. ++outline;
  403. while ((c = *s))
  404. {
  405. putc(c, f);
  406. ++s;
  407. }
  408. putc('\n', f);
  409. }
  410. }