main.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #include <signal.h>
  2. #include "defs.h"
  3. char dflag;
  4. char lflag;
  5. char rflag;
  6. char tflag;
  7. char vflag;
  8. char *file_prefix = "y";
  9. char *myname = "yacc";
  10. char *temp_form = "yacc.XXXXXXX";
  11. int lineno;
  12. int outline;
  13. char *action_file_name;
  14. char *code_file_name;
  15. char *defines_file_name;
  16. char *input_file_name = "";
  17. char *output_file_name;
  18. char *text_file_name;
  19. char *union_file_name;
  20. char *verbose_file_name;
  21. FILE *action_file; /* a temp file, used to save actions associated */
  22. /* with rules until the parser is written */
  23. FILE *code_file; /* y.code.c (used when the -r option is specified) */
  24. FILE *defines_file; /* y.tab.h */
  25. FILE *input_file; /* the input file */
  26. FILE *output_file; /* y.tab.c */
  27. FILE *text_file; /* a temp file, used to save text until all */
  28. /* symbols have been defined */
  29. FILE *union_file; /* a temp file, used to save the union */
  30. /* definition until all symbol have been */
  31. /* defined */
  32. FILE *verbose_file; /* y.output */
  33. int nitems;
  34. int nrules;
  35. int nsyms;
  36. int ntokens;
  37. int nvars;
  38. int start_symbol;
  39. char **symbol_name;
  40. short *symbol_value;
  41. short *symbol_prec;
  42. char *symbol_assoc;
  43. short *ritem;
  44. short *rlhs;
  45. short *rrhs;
  46. short *rprec;
  47. char *rassoc;
  48. short **derives;
  49. char *nullable;
  50. extern char *mktemp();
  51. extern char *getenv();
  52. done(k)
  53. int k;
  54. {
  55. if (action_file) { fclose(action_file); unlink(action_file_name); }
  56. if (text_file) { fclose(text_file); unlink(text_file_name); }
  57. if (union_file) { fclose(union_file); unlink(union_file_name); }
  58. exit(k);
  59. }
  60. void
  61. onintr()
  62. {
  63. done(1);
  64. }
  65. set_signals()
  66. {
  67. #ifdef SIGINT
  68. if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  69. signal(SIGINT, onintr);
  70. #endif
  71. #ifdef SIGTERM
  72. if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
  73. signal(SIGTERM, onintr);
  74. #endif
  75. #ifdef SIGHUP
  76. if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  77. signal(SIGHUP, onintr);
  78. #endif
  79. }
  80. usage()
  81. {
  82. fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] filename\n", myname);
  83. exit(1);
  84. }
  85. getargs(argc, argv)
  86. int argc;
  87. char *argv[];
  88. {
  89. register int i;
  90. register char *s;
  91. if (argc > 0) myname = argv[0];
  92. for (i = 1; i < argc; ++i)
  93. {
  94. s = argv[i];
  95. if (*s != '-') break;
  96. switch (*++s)
  97. {
  98. case '\0':
  99. input_file = stdin;
  100. if (i + 1 < argc) usage();
  101. return;
  102. case '-':
  103. ++i;
  104. goto no_more_options;
  105. case 'b':
  106. if (*++s)
  107. file_prefix = s;
  108. else if (++i < argc)
  109. file_prefix = argv[i];
  110. else
  111. usage();
  112. continue;
  113. case 'd':
  114. dflag = 1;
  115. break;
  116. case 'l':
  117. lflag = 1;
  118. break;
  119. case 'r':
  120. rflag = 1;
  121. break;
  122. case 't':
  123. tflag = 1;
  124. break;
  125. case 'v':
  126. vflag = 1;
  127. break;
  128. default:
  129. usage();
  130. }
  131. for (;;)
  132. {
  133. switch (*++s)
  134. {
  135. case '\0':
  136. goto end_of_option;
  137. case 'd':
  138. dflag = 1;
  139. break;
  140. case 'l':
  141. lflag = 1;
  142. break;
  143. case 'r':
  144. rflag = 1;
  145. break;
  146. case 't':
  147. tflag = 1;
  148. break;
  149. case 'v':
  150. vflag = 1;
  151. break;
  152. default:
  153. usage();
  154. }
  155. }
  156. end_of_option:;
  157. }
  158. no_more_options:;
  159. if (i + 1 != argc) usage();
  160. input_file_name = argv[i];
  161. }
  162. char *
  163. allocate(n)
  164. unsigned n;
  165. {
  166. register char *p;
  167. p = NULL;
  168. if (n)
  169. {
  170. p = CALLOC(1, n);
  171. if (!p) no_space();
  172. }
  173. return (p);
  174. }
  175. create_file_names()
  176. {
  177. int i, len;
  178. char *tmpdir;
  179. tmpdir = getenv("TMPDIR");
  180. if (tmpdir == 0) tmpdir = "/tmp";
  181. len = strlen(tmpdir);
  182. i = len + 13;
  183. if (len && tmpdir[len-1] != '/')
  184. ++i;
  185. action_file_name = MALLOC(i);
  186. if (action_file_name == 0) no_space();
  187. text_file_name = MALLOC(i);
  188. if (text_file_name == 0) no_space();
  189. union_file_name = MALLOC(i);
  190. if (union_file_name == 0) no_space();
  191. strcpy(action_file_name, tmpdir);
  192. strcpy(text_file_name, tmpdir);
  193. strcpy(union_file_name, tmpdir);
  194. if (len && tmpdir[len - 1] != '/')
  195. {
  196. action_file_name[len] = '/';
  197. text_file_name[len] = '/';
  198. union_file_name[len] = '/';
  199. ++len;
  200. }
  201. strcpy(action_file_name + len, temp_form);
  202. strcpy(text_file_name + len, temp_form);
  203. strcpy(union_file_name + len, temp_form);
  204. action_file_name[len + 5] = 'a';
  205. text_file_name[len + 5] = 't';
  206. union_file_name[len + 5] = 'u';
  207. mktemp(action_file_name);
  208. mktemp(text_file_name);
  209. mktemp(union_file_name);
  210. len = strlen(file_prefix);
  211. output_file_name = MALLOC(len + 7);
  212. if (output_file_name == 0)
  213. no_space();
  214. strcpy(output_file_name, file_prefix);
  215. strcpy(output_file_name + len, OUTPUT_SUFFIX);
  216. if (rflag)
  217. {
  218. code_file_name = MALLOC(len + 8);
  219. if (code_file_name == 0)
  220. no_space();
  221. strcpy(code_file_name, file_prefix);
  222. strcpy(code_file_name + len, CODE_SUFFIX);
  223. }
  224. else
  225. code_file_name = output_file_name;
  226. if (dflag)
  227. {
  228. defines_file_name = MALLOC(len + 7);
  229. if (defines_file_name == 0)
  230. no_space();
  231. strcpy(defines_file_name, file_prefix);
  232. strcpy(defines_file_name + len, DEFINES_SUFFIX);
  233. }
  234. if (vflag)
  235. {
  236. verbose_file_name = MALLOC(len + 8);
  237. if (verbose_file_name == 0)
  238. no_space();
  239. strcpy(verbose_file_name, file_prefix);
  240. strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
  241. }
  242. }
  243. open_files()
  244. {
  245. create_file_names();
  246. if (input_file == 0)
  247. {
  248. input_file = fopen(input_file_name, "r");
  249. if (input_file == 0)
  250. open_error(input_file_name);
  251. }
  252. action_file = fopen(action_file_name, "w");
  253. if (action_file == 0)
  254. open_error(action_file_name);
  255. text_file = fopen(text_file_name, "w");
  256. if (text_file == 0)
  257. open_error(text_file_name);
  258. if (vflag)
  259. {
  260. verbose_file = fopen(verbose_file_name, "w");
  261. if (verbose_file == 0)
  262. open_error(verbose_file_name);
  263. }
  264. if (dflag)
  265. {
  266. defines_file = fopen(defines_file_name, "w");
  267. if (defines_file == 0)
  268. open_error(defines_file_name);
  269. union_file = fopen(union_file_name, "w");
  270. if (union_file == 0)
  271. open_error(union_file_name);
  272. }
  273. output_file = fopen(output_file_name, "w");
  274. if (output_file == 0)
  275. open_error(output_file_name);
  276. if (rflag)
  277. {
  278. code_file = fopen(code_file_name, "w");
  279. if (code_file == 0)
  280. open_error(code_file_name);
  281. }
  282. else
  283. code_file = output_file;
  284. }
  285. int
  286. main(argc, argv)
  287. int argc;
  288. char *argv[];
  289. {
  290. set_signals();
  291. getargs(argc, argv);
  292. open_files();
  293. reader();
  294. lr0();
  295. lalr();
  296. make_parser();
  297. verbose();
  298. output();
  299. done(0);
  300. /*NOTREACHED*/
  301. }