main.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* Copyright (c) 1991 by the Vrije Universiteit, Amsterdam, the Netherlands.
  2. * For full copyright and restrictions on use see the file COPYING in the top
  3. * level of the LLgen tree.
  4. */
  5. /*
  6. * L L G E N
  7. *
  8. * An Extended LL(1) Parser Generator
  9. *
  10. * Author : Ceriel J.H. Jacobs
  11. */
  12. /*
  13. * main.c
  14. * Contains main program, and some error message routines
  15. */
  16. # include "types.h"
  17. # include "io.h"
  18. # include "extern.h"
  19. # include "sets.h"
  20. # include "assert.h"
  21. # ifndef NORCSID
  22. static string rcsid6 = "$Id$";
  23. # endif
  24. /* In this file the following routines are defined: */
  25. extern int main();
  26. STATIC readgrammar();
  27. STATIC doparse();
  28. extern error();
  29. extern fatal();
  30. extern comfatal();
  31. extern copyfile();
  32. extern install();
  33. extern char *mktemp();
  34. extern char *sbrk();
  35. main(argc,argv) register string argv[]; {
  36. register string arg;
  37. string libpath();
  38. char *beg_sbrk;
  39. /* Initialize */
  40. assval = 0400;
  41. /* read options */
  42. while (argc >= 2 && (arg = argv[1], *arg == '-')) {
  43. while (*++arg) {
  44. switch(*arg) {
  45. case 'j':
  46. case 'J':
  47. jmptable_option = 1;
  48. if (*++arg)
  49. min_cases_for_jmptable = atoi(arg);
  50. break;
  51. case 'w':
  52. case 'W':
  53. wflag = 1;
  54. continue;
  55. case 'v':
  56. case 'V':
  57. verbose++;
  58. continue;
  59. case 'l':
  60. case 'L':
  61. low_percentage = atoi(++arg);
  62. break;
  63. case 'h':
  64. case 'H':
  65. high_percentage = atoi(++arg);
  66. break;
  67. # ifndef NDEBUG
  68. case 'd':
  69. case 'D':
  70. debug++;
  71. continue;
  72. case 'r':
  73. case 'R':
  74. if (rec_file) {
  75. fprintf(stderr,"duplicate -r flag\n");
  76. exit(1);
  77. }
  78. rec_file = ++arg;
  79. break;
  80. case 'i':
  81. case 'I':
  82. if (incl_file) {
  83. fprintf(stderr,"duplicate -i flag\n");
  84. exit(1);
  85. }
  86. incl_file = ++arg;
  87. break;
  88. #endif /* not NDEBUG */
  89. case 'x':
  90. case 'X':
  91. ntneeded = 1;
  92. ntprint = 1;
  93. continue;
  94. case 'a':
  95. case 'A':
  96. ansi_c = 1;
  97. continue;
  98. case 's':
  99. case 'S':
  100. strip_grammar = 1;
  101. continue;
  102. default:
  103. fprintf(stderr,"illegal option : %c\n",*arg);
  104. exit(1);
  105. }
  106. break;
  107. }
  108. argv++;
  109. argc--;
  110. }
  111. if (verbose) beg_sbrk = sbrk(0);
  112. /*
  113. * Now check wether the sets should include nonterminals
  114. */
  115. if (verbose == 2) ntneeded = 1;
  116. /*
  117. * Initialise
  118. */
  119. # ifndef NDEBUG
  120. if (!rec_file) {
  121. # endif
  122. rec_file = libpath("rec");
  123. # ifndef NDEBUG
  124. }
  125. if (!incl_file) {
  126. # endif
  127. incl_file = libpath("incl");
  128. # ifndef NDEBUG
  129. }
  130. # endif
  131. mktemp(f_temp);
  132. mktemp(f_pars);
  133. if ((fact = fopen(f_temp,"w")) == NULL) {
  134. fputs("Cannot create temporary\n",stderr);
  135. exit(1);
  136. }
  137. name_init();
  138. readgrammar(argc,argv);
  139. sprintf(f_out, OUTFILE, prefix ? prefix : "LL");
  140. /* for the following two filenames only one L is used; historical
  141. reasons ...
  142. */
  143. sprintf(f_include, HFILE, prefix ? prefix : "L");
  144. sprintf(f_rec, RFILE, prefix ? prefix : "L");
  145. setinit(ntneeded);
  146. maxnt = &nonterms[nnonterms];
  147. maxt = &tokens[ntokens];
  148. fclose(fact);
  149. /*
  150. * Now, the grammar is read. Do some computations
  151. */
  152. co_reach(); /* Check for undefined and unreachable */
  153. if (nerrors) comfatal();
  154. do_compute();
  155. conflchecks();
  156. if (nerrors) comfatal();
  157. if (argc-- == 1) {
  158. fputs("No code generation for input from standard input\n",
  159. stderr);
  160. }
  161. else gencode(argc);
  162. UNLINK(f_temp);
  163. UNLINK(f_pars);
  164. if (verbose) {
  165. fprintf(stderr, "number of nonterminals: %d\n", nnonterms);
  166. fprintf(stderr, "number of tokens: %d\n", ntokens);
  167. fprintf(stderr, "number of term structures: %d\n", nterms);
  168. fprintf(stderr, "number of alternation structures: %d\n", nalts);
  169. fprintf(stderr, "total memory used: %ld\n", (long)(sbrk(0) - beg_sbrk));
  170. }
  171. exit(0);
  172. }
  173. STATIC
  174. readgrammar(argc,argv) char *argv[]; {
  175. /*
  176. * Do just what the name suggests : read the grammar
  177. */
  178. register p_file p;
  179. p_mem alloc();
  180. linecount = 0;
  181. f_input = "no filename";
  182. /*
  183. * Build the file structure
  184. */
  185. files = p = (p_file) alloc((unsigned) (argc+1) * sizeof(t_file));
  186. if (argc-- == 1) {
  187. finput = stdin;
  188. f_input = "standard input";
  189. doparse(p++);
  190. } else {
  191. while (argc--) {
  192. if ((finput = fopen(f_input=argv[1],"r")) == NULL) {
  193. fatal(0,e_noopen,f_input);
  194. }
  195. doparse(p++);
  196. argv++;
  197. fclose(finput);
  198. }
  199. }
  200. maxfiles = p;
  201. if (! lexical) lexical = "yylex";
  202. /*
  203. * There must be a start symbol!
  204. */
  205. if (start == 0) {
  206. fatal(linecount,"Missing %%start");
  207. }
  208. if (nerrors) comfatal();
  209. }
  210. STATIC
  211. doparse(p) register p_file p; {
  212. linecount = 0;
  213. p->f_name = f_input;
  214. p->f_firsts = 0;
  215. pfile = p;
  216. torder = -1;
  217. norder = -1;
  218. LLparse();
  219. p->f_nonterminals = norder;
  220. p->f_terminals = torder;
  221. }
  222. /* VARARGS1 */
  223. error(lineno,s,t,u) string s,t,u; {
  224. /*
  225. * Just an error message
  226. */
  227. ++nerrors;
  228. if (!lineno) lineno = 1;
  229. fprintf(stderr,"\"%s\", line %d: ",f_input, lineno);
  230. fprintf(stderr,s,t,u);
  231. fputs("\n",stderr);
  232. }
  233. /* VARARGS1 */
  234. warning(lineno,s,t,u) string s,t,u; {
  235. /*
  236. * Just a warning
  237. */
  238. if (wflag) return;
  239. if (!lineno) lineno = 1;
  240. fprintf(stderr,"\"%s\", line %d: (Warning) ",f_input, lineno);
  241. fprintf(stderr,s,t,u);
  242. fputs("\n",stderr);
  243. }
  244. /* VARARGS1 */
  245. fatal(lineno,s,t,u) string s,t,u; {
  246. /*
  247. * Fatal error
  248. */
  249. error(lineno,s,t,u);
  250. comfatal();
  251. }
  252. comfatal() {
  253. /*
  254. * Some common code for exit on errors
  255. */
  256. if (fact != NULL) {
  257. fclose(fact);
  258. UNLINK(f_temp);
  259. }
  260. if (fpars != NULL) fclose(fpars);
  261. UNLINK(f_pars);
  262. exit(1);
  263. }
  264. copyfile(file) string file; {
  265. /*
  266. * Copies a file indicated by the parameter to filedescriptor fpars.
  267. */
  268. register int c;
  269. register FILE *f;
  270. if ((f = fopen(file,"r")) == NULL) {
  271. fatal(0,"Cannot open libraryfile, call an expert");
  272. }
  273. while ((c = getc(f)) != EOF) putc(c,fpars);
  274. fclose(f);
  275. }
  276. install(target, source) string target, source; {
  277. /*
  278. * Copy the temporary file generated from source to target
  279. * if allowed (which means that the target must be generated
  280. * by LLgen from the source, or that the target is not present
  281. */
  282. register int c1, c2;
  283. register FILE *f1, *f2;
  284. int cnt;
  285. /*
  286. * First open temporary, generated for source
  287. */
  288. if ((f1 = fopen(f_pars,"r")) == NULL) {
  289. fatal(0,e_noopen,f_pars);
  290. }
  291. /*
  292. * Now open target for reading
  293. */
  294. if ((f2 = fopen(target,"r")) == NULL) {
  295. fclose(f1);
  296. RENAME(f_pars, target);
  297. return;
  298. }
  299. /*
  300. * Compute length of LLgen identification string. The target must
  301. * start with that!
  302. */
  303. cnt = strlen(LLgenid) + strlen(source) - 2;
  304. /*
  305. * Now compare the target with the temporary
  306. */
  307. do {
  308. c1 = getc(f1);
  309. c2 = getc(f2);
  310. if (cnt >= 0) cnt--;
  311. } while (c1 == c2 && c1 != EOF);
  312. fclose(f1);
  313. fclose(f2);
  314. /*
  315. * Here, if c1 != c2 the target must be recreated
  316. */
  317. if (c1 != c2) {
  318. if (cnt >= 0) {
  319. fatal(0,"%s : not a file generated by LLgen",target);
  320. }
  321. RENAME(f_pars,target);
  322. }
  323. }