main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. #include "defs.h"
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #if defined(_WIN32)
  5. #include <io.h>
  6. #else
  7. #include <unistd.h>
  8. #endif
  9. #if defined(_MSC_VER) && _MSC_VER >= 1500
  10. // ISO C++ conformant names
  11. #define unlink _unlink
  12. #define mktemp _mktemp
  13. #define strdup _strdup
  14. #endif
  15. char dflag;
  16. char lflag;
  17. char rflag;
  18. char tflag;
  19. char vflag;
  20. int Eflag = 0;
  21. char *file_prefix = "y";
  22. char *myname = "yacc";
  23. #if defined(__MSDOS__) || defined(WIN32)
  24. #define DIR_CHAR '\\'
  25. #define DEFAULT_TMPDIR "."
  26. #else /* Unix */
  27. #define DIR_CHAR '/'
  28. #define DEFAULT_TMPDIR "/tmp"
  29. #endif
  30. char *temp_form = "yacc_t_XXXXXX";
  31. int lineno;
  32. int outline;
  33. char *action_file_name;
  34. char *code_file_name;
  35. char *defines_file_name;
  36. char *input_file_name = "";
  37. char *output_file_name;
  38. char *text_file_name;
  39. char *union_file_name;
  40. char *verbose_file_name;
  41. FILE *action_file; /* a temp file, used to save actions associated */
  42. /* with rules until the parser is written */
  43. FILE *code_file; /* y.code.c (used when the -r option is specified) */
  44. FILE *defines_file; /* y.tab.h */
  45. FILE *input_file; /* the input file */
  46. FILE *output_file; /* y.tab.c */
  47. FILE *text_file; /* a temp file, used to save text until all */
  48. /* symbols have been defined */
  49. FILE *union_file; /* a temp file, used to save the union */
  50. /* definition until all symbol have been */
  51. /* defined */
  52. FILE *verbose_file; /* y.output */
  53. int nitems;
  54. int nrules;
  55. int nsyms;
  56. int ntokens;
  57. int nvars;
  58. int start_symbol;
  59. char **symbol_name;
  60. Yshort *symbol_value;
  61. Yshort *symbol_prec;
  62. char *symbol_assoc;
  63. Yshort *ritem;
  64. Yshort *rlhs;
  65. Yshort *rrhs;
  66. Yshort *rprec;
  67. char *rassoc;
  68. Yshort **derives;
  69. char *nullable;
  70. void done(int k)
  71. {
  72. if (action_file)
  73. {
  74. fclose(action_file);
  75. unlink(action_file_name);
  76. }
  77. if (text_file)
  78. {
  79. fclose(text_file);
  80. unlink(text_file_name);
  81. }
  82. if (union_file)
  83. {
  84. fclose(union_file);
  85. unlink(union_file_name);
  86. }
  87. exit(k);
  88. }
  89. void onintr(int sig)
  90. {
  91. done(1);
  92. }
  93. void set_signals()
  94. {
  95. #ifdef SIGINT
  96. if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  97. signal(SIGINT, onintr);
  98. #endif
  99. #ifdef SIGTERM
  100. if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
  101. signal(SIGTERM, onintr);
  102. #endif
  103. #ifdef SIGHUP
  104. if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  105. signal(SIGHUP, onintr);
  106. #endif
  107. }
  108. void usage()
  109. {
  110. fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] [-S skeleton file] "
  111. "filename\n", myname);
  112. exit(1);
  113. }
  114. void no_more_options(int i, int argc, char* argv[])
  115. {
  116. if (i + 1 != argc)
  117. usage();
  118. input_file_name = argv[i];
  119. if (!file_prefix)
  120. {
  121. if (input_file_name)
  122. {
  123. char* s;
  124. file_prefix = strdup(input_file_name);
  125. if ((s = strrchr(file_prefix, '.')))
  126. *s = 0;
  127. }
  128. else {
  129. file_prefix = "y";
  130. }
  131. }
  132. }
  133. void getargs(int argc, char **argv)
  134. {
  135. register int i;
  136. register char *s;
  137. if (argc > 0)
  138. myname = argv[0];
  139. for (i = 1; i < argc; ++i)
  140. {
  141. s = argv[i];
  142. if (*s != '-')
  143. break;
  144. switch (*++s)
  145. {
  146. case '\0':
  147. input_file = stdin;
  148. if (i + 1 < argc)
  149. usage();
  150. return;
  151. case '-':
  152. ++i;
  153. no_more_options(i, argc, argv);
  154. return;
  155. case 'b':
  156. if (*++s)
  157. file_prefix = s;
  158. else if (++i < argc)
  159. file_prefix = argv[i];
  160. else
  161. usage();
  162. continue;
  163. case 'd':
  164. dflag = 1;
  165. break;
  166. case 'D':
  167. /* Find the preprocessor variable */
  168. {
  169. char **ps;
  170. char *var_name = s + 1;
  171. extern char *defd_vars[];
  172. for (ps = &defd_vars[0]; *ps; ps++)
  173. {
  174. if (strcmp(*ps, var_name) == 0) {
  175. error(lineno, 0, 0, "Preprocessor variable %s already defined", var_name);
  176. }
  177. }
  178. *ps = MALLOC(strlen(var_name)+1);
  179. strcpy(*ps, var_name);
  180. *++ps = NULL;
  181. }
  182. continue;
  183. case 'E':
  184. Eflag = 1;
  185. break;
  186. case 'l':
  187. lflag = 1;
  188. break;
  189. case 'r':
  190. rflag = 1;
  191. break;
  192. case 't':
  193. tflag = 1;
  194. break;
  195. case 'v':
  196. vflag = 1;
  197. break;
  198. case 'S':
  199. if (*++s)
  200. read_skel(s);
  201. else if (++i < argc)
  202. read_skel(argv[i]);
  203. else
  204. usage();
  205. continue;
  206. default:
  207. usage();
  208. }
  209. for (;*++s;)
  210. {
  211. switch (*s)
  212. {
  213. //case '\0':
  214. //goto end_of_option;
  215. case 'd':
  216. dflag = 1;
  217. break;
  218. case 'l':
  219. lflag = 1;
  220. break;
  221. case 'r':
  222. rflag = 1;
  223. break;
  224. case 't':
  225. tflag = 1;
  226. break;
  227. case 'v':
  228. vflag = 1;
  229. break;
  230. default:
  231. usage();
  232. }
  233. }
  234. //end_of_option:
  235. }
  236. no_more_options(i, argc, argv);
  237. }
  238. char *allocate(unsigned n)
  239. {
  240. register char *p = NULL;
  241. if (n)
  242. {
  243. /* VM: add a few bytes here, cause
  244. * Linux calloc does not like sizes like 32768 */
  245. p = CALLOC(1, n + 10);
  246. if (!p)
  247. no_space();
  248. }
  249. return (p);
  250. }
  251. void create_file_names()
  252. {
  253. int i, len;
  254. char* tmpdir = getenv("TMPDIR");
  255. if (tmpdir == 0)
  256. tmpdir = DEFAULT_TMPDIR;
  257. len = strlen(tmpdir);
  258. i = len + strlen(temp_form) + 1;
  259. if (len && tmpdir[len-1] != DIR_CHAR)
  260. ++i;
  261. action_file_name = MALLOC(i);
  262. if (action_file_name == 0)
  263. no_space();
  264. text_file_name = MALLOC(i);
  265. if (text_file_name == 0)
  266. no_space();
  267. union_file_name = MALLOC(i);
  268. if (union_file_name == 0)
  269. no_space();
  270. strcpy(action_file_name, tmpdir);
  271. strcpy(text_file_name, tmpdir);
  272. strcpy(union_file_name, tmpdir);
  273. if (len && tmpdir[len - 1] != DIR_CHAR)
  274. {
  275. action_file_name[len] = DIR_CHAR;
  276. text_file_name[len] = DIR_CHAR;
  277. union_file_name[len] = DIR_CHAR;
  278. ++len;
  279. }
  280. strcpy(action_file_name + len, temp_form);
  281. strcpy(text_file_name + len, temp_form);
  282. strcpy(union_file_name + len, temp_form);
  283. action_file_name[len + 5] = 'a';
  284. text_file_name[len + 5] = 't';
  285. union_file_name[len + 5] = 'u';
  286. if(mktemp(action_file_name) == NULL) {
  287. fprintf(stderr, "btyacc: Cannot create temporary file\n");
  288. exit(1);
  289. }
  290. if(mktemp(text_file_name) == NULL) {
  291. fprintf(stderr, "btyacc: Cannot create temporary file\n");
  292. exit(1);
  293. }
  294. if(mktemp(union_file_name) == NULL) {
  295. fprintf(stderr, "btyacc: Cannot create temporary file\n");
  296. exit(1);
  297. }
  298. len = strlen(file_prefix);
  299. output_file_name = MALLOC(len + 7);
  300. if (output_file_name == 0)
  301. no_space();
  302. strcpy(output_file_name, file_prefix);
  303. strcpy(output_file_name + len, OUTPUT_SUFFIX);
  304. if (rflag)
  305. {
  306. code_file_name = MALLOC(len + 8);
  307. if (code_file_name == 0)
  308. no_space();
  309. strcpy(code_file_name, file_prefix);
  310. strcpy(code_file_name + len, CODE_SUFFIX);
  311. }
  312. else
  313. code_file_name = output_file_name;
  314. if (dflag)
  315. {
  316. defines_file_name = MALLOC(len + 7);
  317. if (defines_file_name == 0)
  318. no_space();
  319. strcpy(defines_file_name, file_prefix);
  320. strcpy(defines_file_name + len, DEFINES_SUFFIX);
  321. }
  322. if (vflag)
  323. {
  324. verbose_file_name = MALLOC(len + 8);
  325. if (verbose_file_name == 0)
  326. no_space();
  327. strcpy(verbose_file_name, file_prefix);
  328. strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
  329. }
  330. }
  331. void open_files()
  332. {
  333. create_file_names();
  334. if (input_file == 0)
  335. {
  336. input_file = fopen(input_file_name, "r");
  337. if (input_file == 0)
  338. open_error(input_file_name);
  339. }
  340. action_file = fopen(action_file_name, "w");
  341. if (action_file == 0)
  342. open_error(action_file_name);
  343. text_file = fopen(text_file_name, "w");
  344. if (text_file == 0)
  345. open_error(text_file_name);
  346. if (vflag)
  347. {
  348. verbose_file = fopen(verbose_file_name, "w");
  349. if (verbose_file == 0)
  350. open_error(verbose_file_name);
  351. }
  352. if (dflag)
  353. {
  354. defines_file = fopen(defines_file_name, "w");
  355. if (defines_file == 0)
  356. open_error(defines_file_name);
  357. union_file = fopen(union_file_name, "w");
  358. if (union_file == 0)
  359. open_error(union_file_name);
  360. }
  361. output_file = fopen(output_file_name, "w");
  362. if (output_file == 0)
  363. open_error(output_file_name);
  364. if (rflag)
  365. {
  366. code_file = fopen(code_file_name, "w");
  367. if (code_file == 0)
  368. open_error(code_file_name);
  369. }
  370. else
  371. code_file = output_file;
  372. }
  373. int main(int argc, char **argv)
  374. {
  375. set_signals();
  376. getargs(argc, argv);
  377. open_files();
  378. reader();
  379. lr0();
  380. lalr();
  381. make_parser();
  382. verbose();
  383. output();
  384. done(0);
  385. return 0;
  386. }