main.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. onintr()
  61. {
  62. done(1);
  63. }
  64. set_signals()
  65. {
  66. #ifdef SIGINT
  67. if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  68. signal(SIGINT, onintr);
  69. #endif
  70. #ifdef SIGTERM
  71. if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
  72. signal(SIGTERM, onintr);
  73. #endif
  74. #ifdef SIGHUP
  75. if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  76. signal(SIGHUP, onintr);
  77. #endif
  78. }
  79. usage()
  80. {
  81. fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] filename\n", myname);
  82. exit(1);
  83. }
  84. getargs(argc, argv)
  85. int argc;
  86. char *argv[];
  87. {
  88. register int i;
  89. register char *s;
  90. if (argc > 0) myname = argv[0];
  91. for (i = 1; i < argc; ++i)
  92. {
  93. s = argv[i];
  94. if (*s != '-') break;
  95. switch (*++s)
  96. {
  97. case '\0':
  98. input_file = stdin;
  99. if (i + 1 < argc) usage();
  100. return;
  101. case '-':
  102. ++i;
  103. goto no_more_options;
  104. case 'b':
  105. if (*++s)
  106. file_prefix = s;
  107. else if (++i < argc)
  108. file_prefix = argv[i];
  109. else
  110. usage();
  111. continue;
  112. case 'd':
  113. dflag = 1;
  114. break;
  115. case 'l':
  116. lflag = 1;
  117. break;
  118. case 'r':
  119. rflag = 1;
  120. break;
  121. case 't':
  122. tflag = 1;
  123. break;
  124. case 'v':
  125. vflag = 1;
  126. break;
  127. default:
  128. usage();
  129. }
  130. for (;;)
  131. {
  132. switch (*++s)
  133. {
  134. case '\0':
  135. goto end_of_option;
  136. case 'd':
  137. dflag = 1;
  138. break;
  139. case 'l':
  140. lflag = 1;
  141. break;
  142. case 'r':
  143. rflag = 1;
  144. break;
  145. case 't':
  146. tflag = 1;
  147. break;
  148. case 'v':
  149. vflag = 1;
  150. break;
  151. default:
  152. usage();
  153. }
  154. }
  155. end_of_option:;
  156. }
  157. no_more_options:;
  158. if (i + 1 != argc) usage();
  159. input_file_name = argv[i];
  160. }
  161. char *
  162. allocate(n)
  163. unsigned n;
  164. {
  165. register char *p;
  166. p = NULL;
  167. if (n)
  168. {
  169. p = CALLOC(1, n);
  170. if (!p) no_space();
  171. }
  172. return (p);
  173. }
  174. create_file_names()
  175. {
  176. int i, len;
  177. char *tmpdir;
  178. tmpdir = getenv("TMPDIR");
  179. if (tmpdir == 0) tmpdir = "/tmp";
  180. len = strlen(tmpdir);
  181. i = len + 13;
  182. if (len && tmpdir[len-1] != '/')
  183. ++i;
  184. action_file_name = MALLOC(i);
  185. if (action_file_name == 0) no_space();
  186. text_file_name = MALLOC(i);
  187. if (text_file_name == 0) no_space();
  188. union_file_name = MALLOC(i);
  189. if (union_file_name == 0) no_space();
  190. strcpy(action_file_name, tmpdir);
  191. strcpy(text_file_name, tmpdir);
  192. strcpy(union_file_name, tmpdir);
  193. if (len && tmpdir[len - 1] != '/')
  194. {
  195. action_file_name[len] = '/';
  196. text_file_name[len] = '/';
  197. union_file_name[len] = '/';
  198. ++len;
  199. }
  200. strcpy(action_file_name + len, temp_form);
  201. strcpy(text_file_name + len, temp_form);
  202. strcpy(union_file_name + len, temp_form);
  203. action_file_name[len + 5] = 'a';
  204. text_file_name[len + 5] = 't';
  205. union_file_name[len + 5] = 'u';
  206. mktemp(action_file_name);
  207. mktemp(text_file_name);
  208. mktemp(union_file_name);
  209. len = strlen(file_prefix);
  210. output_file_name = MALLOC(len + 7);
  211. if (output_file_name == 0)
  212. no_space();
  213. strcpy(output_file_name, file_prefix);
  214. strcpy(output_file_name + len, OUTPUT_SUFFIX);
  215. if (rflag)
  216. {
  217. code_file_name = MALLOC(len + 8);
  218. if (code_file_name == 0)
  219. no_space();
  220. strcpy(code_file_name, file_prefix);
  221. strcpy(code_file_name + len, CODE_SUFFIX);
  222. }
  223. else
  224. code_file_name = output_file_name;
  225. if (dflag)
  226. {
  227. defines_file_name = MALLOC(len + 7);
  228. if (defines_file_name == 0)
  229. no_space();
  230. strcpy(defines_file_name, file_prefix);
  231. strcpy(defines_file_name + len, DEFINES_SUFFIX);
  232. }
  233. if (vflag)
  234. {
  235. verbose_file_name = MALLOC(len + 8);
  236. if (verbose_file_name == 0)
  237. no_space();
  238. strcpy(verbose_file_name, file_prefix);
  239. strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
  240. }
  241. }
  242. open_files()
  243. {
  244. create_file_names();
  245. if (input_file == 0)
  246. {
  247. input_file = fopen(input_file_name, "r");
  248. if (input_file == 0)
  249. open_error(input_file_name);
  250. }
  251. action_file = fopen(action_file_name, "w");
  252. if (action_file == 0)
  253. open_error(action_file_name);
  254. text_file = fopen(text_file_name, "w");
  255. if (text_file == 0)
  256. open_error(text_file_name);
  257. if (vflag)
  258. {
  259. verbose_file = fopen(verbose_file_name, "w");
  260. if (verbose_file == 0)
  261. open_error(verbose_file_name);
  262. }
  263. if (dflag)
  264. {
  265. defines_file = fopen(defines_file_name, "w");
  266. if (defines_file == 0)
  267. open_error(defines_file_name);
  268. union_file = fopen(union_file_name, "w");
  269. if (union_file == 0)
  270. open_error(union_file_name);
  271. }
  272. output_file = fopen(output_file_name, "w");
  273. if (output_file == 0)
  274. open_error(output_file_name);
  275. if (rflag)
  276. {
  277. code_file = fopen(code_file_name, "w");
  278. if (code_file == 0)
  279. open_error(code_file_name);
  280. }
  281. else
  282. code_file = output_file;
  283. }
  284. int
  285. main(argc, argv)
  286. int argc;
  287. char *argv[];
  288. {
  289. set_signals();
  290. getargs(argc, argv);
  291. open_files();
  292. reader();
  293. lr0();
  294. lalr();
  295. make_parser();
  296. verbose();
  297. output();
  298. done(0);
  299. /*NOTREACHED*/
  300. }