main.c 6.4 KB

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