1 /***********************************************************************/
5 /* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
7 /* Copyright 1996 Institut National de Recherche en Informatique et */
8 /* en Automatique. All rights reserved. This file is distributed */
9 /* under the terms of the Q Public License version 1.0. */
11 /***********************************************************************/
13 /* Based on public-domain code from Berkeley Yacc */
15 /* $Id: reader.c 9271 2009-05-20 11:58:43Z doligez $ */
20 /* The line size must be a positive integer. One hundred was chosen */
21 /* because few lines in Yacc input grammars exceed 100 characters. */
22 /* Note that if a line exceeds LINESIZE characters, the line buffer */
23 /* will be expanded to accomodate it. */
33 char saw_eof, unionized;
51 char line_format[] = "# %d \"%s\"\n";
55 void start_rule (register bucket *bp, int s_lineno);
60 if (cinc >= cache_size)
63 cache = REALLOC(cache, cache_size);
64 if (cache == 0) no_space();
73 register FILE *f = input_file;
77 if (saw_eof || (c = getc(f)) == EOF)
79 if (line) { FREE(line); line = 0; }
85 if (line == 0 || linesize != (LINESIZE + 1))
88 linesize = LINESIZE + 1;
89 line = MALLOC(linesize);
90 if (line == 0) no_space();
100 linesize += LINESIZE;
101 line = REALLOC(line, linesize);
102 if (line == 0) no_space();
104 if (c == '\n') { line[i] = '\0'; cptr = line; return; }
106 if (c == EOF) { saw_eof = 1; c = '\n'; }
114 register char *p, *s, *t;
116 if (line == 0) return (0);
118 while (*s != '\n') ++s;
119 p = MALLOC(s - line + 1);
120 if (p == 0) no_space();
124 while ((*t++ = *s++) != '\n') continue;
129 void skip_comment(void)
133 int st_lineno = lineno;
134 char *st_line = dup_line();
135 char *st_cptr = st_line + (cptr - line);
140 if (*s == '*' && s[1] == '/')
150 unterminated_comment(st_lineno, st_line, st_cptr);
158 char *substring (char *str, int start, int len)
161 char *buf = MALLOC (len+1);
162 if (buf == NULL) return NULL;
163 for (i = 0; i < len; i++){
164 buf[i] = str[start+i];
166 buf[i] = '\0'; /* PR#4796 */
170 void parse_line_directive (void)
174 char *file_name = NULL;
177 if (line == 0) return;
178 if (line[i] != '#') return;
180 while (line[i] == ' ' || line[i] == '\t') ++ i;
181 if (line[i] < '0' || line[i] > '9') return;
182 while (line[i] >= '0' && line[i] <= '9'){
183 line_number = line_number * 10 + line[i] - '0';
186 while (line[i] == ' ' || line[i] == '\t') ++ i;
190 while (line[j] != '"' && line[j] != '\0') ++j;
192 file_name = substring (line, i, j - i);
193 if (file_name == NULL) no_space ();
196 lineno = line_number - 1;
197 if (file_name != NULL){
198 if (virtual_input_file_name != NULL) FREE (virtual_input_file_name);
199 virtual_input_file_name = file_name;
213 parse_line_directive ();
225 parse_line_directive ();
226 if (line == 0) return (EOF);
252 else if (s[1] == '/')
255 parse_line_directive ();
256 if (line == 0) return (EOF);
284 if (isupper(c)) c = tolower(c);
287 else if (isdigit(c) || c == '_' || c == '.' || c == '$')
295 if (strcmp(cache, "token") == 0 || strcmp(cache, "term") == 0)
297 if (strcmp(cache, "type") == 0)
299 if (strcmp(cache, "left") == 0)
301 if (strcmp(cache, "right") == 0)
303 if (strcmp(cache, "nonassoc") == 0 || strcmp(cache, "binary") == 0)
305 if (strcmp(cache, "start") == 0)
307 if (strcmp(cache, "union") == 0)
309 if (strcmp(cache, "ident") == 0)
317 if (c == '%' || c == '\\')
328 syntax_error(lineno, line, t_cptr);
334 void copy_ident(void)
337 register FILE *f = output_file;
340 if (c == EOF) unexpected_EOF();
341 if (c != '"') syntax_error(lineno, line, cptr);
343 fprintf(f, "#ident \"");
367 register FILE *f = text_file;
368 int need_newline = 0;
369 int t_lineno = lineno;
370 char *t_line = dup_line();
371 char *t_cptr = t_line + (cptr - line - 2);
377 unterminated_text(t_lineno, t_line, t_cptr);
379 fprintf(f, line_format, lineno, input_file_name);
390 unterminated_text(t_lineno, t_line, t_cptr);
394 int s_lineno = lineno;
395 char *s_line = dup_line();
396 char *s_cptr = s_line + (cptr - line - 1);
411 unterminated_string(s_lineno, s_line, s_cptr);
420 unterminated_string(s_lineno, s_line, s_cptr);
428 if (cptr[0] != 0 && cptr[0] != '\\' && cptr[1] == '\'') {
429 fwrite(cptr, 1, 2, f);
433 && isdigit((unsigned char) cptr[1])
434 && isdigit((unsigned char) cptr[2])
435 && isdigit((unsigned char) cptr[3])
436 && cptr[4] == '\'') {
437 fwrite(cptr, 1, 5, f);
440 if (cptr[0] == '\\' && cptr[2] == '\'') {
441 fwrite(cptr, 1, 3, f);
452 int c_lineno = lineno;
453 char *c_line = dup_line();
454 char *c_cptr = c_line + (cptr - line - 1);
462 if (c == '*' && *cptr == ')')
473 unterminated_comment(c_lineno, c_line, c_cptr);
484 if (need_newline) putc('\n', f);
499 void copy_union(void)
504 int u_lineno = lineno;
505 char *u_line = dup_line();
506 char *u_cptr = u_line + (cptr - line - 6);
508 if (unionized) over_unionized(cptr - 6);
512 fprintf(text_file, line_format, lineno, input_file_name);
514 fprintf(text_file, "typedef union");
515 if (dflag) fprintf(union_file, "typedef union");
523 if (dflag) putc(c, union_file);
528 if (line == 0) unterminated_union(u_lineno, u_line, u_cptr);
537 if (c == '}' && depth == 0) {
538 fprintf(text_file, " YYSTYPE;\n");
547 int s_lineno = lineno;
548 char *s_line = dup_line();
549 char *s_cptr = s_line + (cptr - line - 1);
556 if (dflag) putc(c, union_file);
563 unterminated_string(s_lineno, s_line, s_cptr);
568 if (dflag) putc(c, union_file);
573 unterminated_string(s_lineno, s_line, s_cptr);
583 int c_lineno = lineno;
584 char *c_line = dup_line();
585 char *c_cptr = c_line + (cptr - line - 1);
587 putc('*', text_file);
588 if (dflag) putc('*', union_file);
594 if (dflag) putc(c, union_file);
595 if (c == '*' && *cptr == ')')
597 putc(')', text_file);
598 if (dflag) putc(')', union_file);
607 unterminated_comment(c_lineno, c_line, c_cptr);
622 if (c >= '0' && c <= '9')
624 if (c >= 'A' && c <= 'F')
625 return (c - 'A' + 10);
626 if (c >= 'a' && c <= 'f')
627 return (c - 'a' + 10);
635 register int c, quote;
640 int s_lineno = lineno;
641 char *s_line = dup_line();
642 char *s_cptr = s_line + (cptr - line);
649 if (c == quote) break;
650 if (c == '\n') unterminated_string(s_lineno, s_line, s_cptr);
653 char *c_cptr = cptr - 1;
660 if (line == 0) unterminated_string(s_lineno, s_line, s_cptr);
663 case '0': case '1': case '2': case '3':
664 case '4': case '5': case '6': case '7':
669 n = (n << 3) + (c - '0');
673 n = (n << 3) + (c - '0');
677 if (n > MAXCHAR) illegal_character(c_cptr);
684 if (n < 0 || n >= 16)
685 illegal_character(c_cptr);
690 if (i < 0 || i >= 16) break;
693 if (n > MAXCHAR) illegal_character(c_cptr);
698 case 'a': c = 7; break;
699 case 'b': c = '\b'; break;
700 case 'f': c = '\f'; break;
701 case 'n': c = '\n'; break;
702 case 'r': c = '\r'; break;
703 case 't': c = '\t'; break;
704 case 'v': c = '\v'; break;
713 if (s == 0) no_space();
715 for (i = 0; i < n; ++i)
724 for (i = 0; i < n; ++i)
726 c = ((unsigned char *)s)[i];
727 if (c == '\\' || c == cache[0])
739 case 7: cachec('a'); break;
740 case '\b': cachec('b'); break;
741 case '\f': cachec('f'); break;
742 case '\n': cachec('n'); break;
743 case '\r': cachec('r'); break;
744 case '\t': cachec('t'); break;
745 case '\v': cachec('v'); break;
747 cachec(((c >> 6) & 7) + '0');
748 cachec(((c >> 3) & 7) + '0');
749 cachec((c & 7) + '0');
763 if (n == 1 && bp->value == UNDEFINED)
764 bp->value = *(unsigned char *)s;
772 is_reserved(char *name)
776 if (strcmp(name, ".") == 0 ||
777 strcmp(name, "$accept") == 0 ||
778 strcmp(name, "$end") == 0)
781 if (name[0] == '$' && name[1] == '$' && isdigit((unsigned char) name[2]))
784 while (isdigit((unsigned char) *s)) ++s;
785 if (*s == NUL) return (1);
798 for (c = *cptr; IS_IDENT(c); c = *++cptr)
802 if (is_reserved(cache)) used_reserved(cache);
804 return (lookup(cache));
815 for (c = *cptr; isdigit(c); c = *++cptr)
816 n = 10*n + (c - '0');
828 char *t_line = dup_line();
835 if (c == EOF) unexpected_EOF();
836 if (c == '\n') syntax_error(lineno, line, cptr);
837 if (c == '>' && 0 == bracket_depth && cptr[-1] != '-') break;
838 if (c == '[') ++ bracket_depth;
839 if (c == ']') -- bracket_depth;
845 for (i = 0; i < ntags; ++i)
847 if (strcmp(cache, tag_table[i]) == 0)
848 return (tag_table[i]);
854 tag_table = (char **)
855 (tag_table ? REALLOC(tag_table, tagmax*sizeof(char *))
856 : MALLOC(tagmax*sizeof(char *)));
857 if (tag_table == 0) no_space();
861 if (s == 0) no_space();
863 tag_table[ntags] = s;
870 void declare_tokens(int assoc)
877 if (assoc != TOKEN) ++prec;
880 if (c == EOF) unexpected_EOF();
885 if (c == EOF) unexpected_EOF();
890 if (isalpha(c) || c == '_' || c == '.' || c == '$')
892 else if (c == '\'' || c == '"')
897 if (bp == goal) tokenized_start(bp->name);
902 if (bp->tag && tag != bp->tag)
903 retyped_warning(bp->name);
913 if (bp->prec && prec != bp->prec)
914 reprec_warning(bp->name);
919 if (strcmp(bp->name, "EOF") == 0)
923 if (c == EOF) unexpected_EOF();
927 value = get_number();
928 if (bp->value != UNDEFINED && value != bp->value)
929 revalued_warning(bp->name);
932 if (c == EOF) unexpected_EOF();
938 void declare_types(void)
945 if (c == EOF) unexpected_EOF();
946 if (c != '<') syntax_error(lineno, line, cptr);
952 if (isalpha(c) || c == '_' || c == '.' || c == '$')
954 else if (c == '\'' || c == '"')
959 if (bp->tag && tag != bp->tag)
960 retyped_warning(bp->name);
966 void declare_start(void)
970 static int entry_counter = 0;
974 if (!isalpha(c) && c != '_' && c != '.' && c != '$') return;
977 if (bp->class == TERM)
978 terminal_start(bp->name);
979 bp->entry = ++entry_counter;
980 if (entry_counter == 256)
986 void read_declarations(void)
991 cache = MALLOC(cache_size);
992 if (cache == 0) no_space();
997 if (c == EOF) unexpected_EOF();
998 if (c != '%') syntax_error(lineno, line, cptr);
999 switch (k = keyword())
1034 void output_token_type(void)
1039 fprintf(interface_file, "type token =\n");
1040 if (!rflag) ++outline;
1041 fprintf(output_file, "type token =\n");
1043 for (bp = first_symbol; bp; bp = bp->next) {
1044 if (bp->class == TERM && bp->true_token) {
1045 fprintf(interface_file, " | %s", bp->name);
1046 fprintf(output_file, " | %s", bp->name);
1048 /* Print the type expression in parentheses to make sure
1049 that the constructor is unary */
1050 fprintf(interface_file, " of (%s)", bp->tag);
1051 fprintf(output_file, " of (%s)", bp->tag);
1053 fprintf(interface_file, "\n");
1054 if (!rflag) ++outline;
1055 fprintf(output_file, "\n");
1059 fprintf(interface_file, "\n");
1060 if (!rflag) ++outline;
1061 fprintf(output_file, "\n");
1064 void initialize_grammar(void)
1068 pitem = (bucket **) MALLOC(maxitems*sizeof(bucket *));
1069 if (pitem == 0) no_space();
1077 plhs = (bucket **) MALLOC(maxrules*sizeof(bucket *));
1078 if (plhs == 0) no_space();
1082 rprec = (short *) MALLOC(maxrules*sizeof(short));
1083 if (rprec == 0) no_space();
1087 rassoc = (char *) MALLOC(maxrules*sizeof(char));
1088 if (rassoc == 0) no_space();
1095 void expand_items(void)
1098 pitem = (bucket **) REALLOC(pitem, maxitems*sizeof(bucket *));
1099 if (pitem == 0) no_space();
1103 void expand_rules(void)
1106 plhs = (bucket **) REALLOC(plhs, maxrules*sizeof(bucket *));
1107 if (plhs == 0) no_space();
1108 rprec = (short *) REALLOC(rprec, maxrules*sizeof(short));
1109 if (rprec == 0) no_space();
1110 rassoc = (char *) REALLOC(rassoc, maxrules*sizeof(char));
1111 if (rassoc == 0) no_space();
1115 void advance_to_start(void)
1118 register bucket *bp;
1125 if (c != '%') break;
1141 syntax_error(lineno, line, s_cptr);
1146 if (!isalpha(c) && c != '_' && c != '.' && c != '_')
1147 syntax_error(lineno, line, cptr);
1151 if (bp->class == TERM)
1152 terminal_start(bp->name);
1158 if (c == EOF) unexpected_EOF();
1159 if (c != ':') syntax_error(lineno, line, cptr);
1160 start_rule(bp, s_lineno);
1167 void start_rule(register bucket *bp, int s_lineno)
1169 if (bp->class == TERM)
1170 terminal_lhs(s_lineno);
1171 bp->class = NONTERM;
1172 if (nrules >= maxrules)
1175 rprec[nrules] = UNDEFINED;
1176 rassoc[nrules] = TOKEN;
1183 if (!last_was_action) default_action_error();
1185 last_was_action = 0;
1186 if (nitems >= maxitems) expand_items();
1193 void insert_empty_rule(void)
1195 register bucket *bp, **bpp;
1198 sprintf(cache, "$$%d", ++gensym);
1199 bp = make_bucket(cache);
1200 last_symbol->next = bp;
1202 bp->tag = plhs[nrules]->tag;
1203 bp->class = NONTERM;
1205 if ((nitems += 2) > maxitems)
1207 bpp = pitem + nitems - 1;
1209 while ((bpp[0] = bpp[-1])) --bpp;
1211 if (++nrules >= maxrules)
1213 plhs[nrules] = plhs[nrules-1];
1214 plhs[nrules-1] = bp;
1215 rprec[nrules] = rprec[nrules-1];
1216 rprec[nrules-1] = 0;
1217 rassoc[nrules] = rassoc[nrules-1];
1218 rassoc[nrules-1] = TOKEN;
1222 void add_symbol(void)
1225 register bucket *bp;
1226 int s_lineno = lineno;
1230 if (c == '\'' || c == '"')
1239 start_rule(bp, s_lineno);
1244 if (last_was_action) syntax_error (lineno, line, ecptr);
1245 last_was_action = 0;
1247 if (++nitems > maxitems)
1249 pitem[nitems-1] = bp;
1253 void copy_action(void)
1261 register FILE *f = action_file;
1262 int a_lineno = lineno;
1263 char *a_line = dup_line();
1264 char *a_cptr = a_line + (cptr - line);
1266 if (last_was_action) syntax_error (lineno, line, cptr);
1267 last_was_action = 1;
1270 fprintf(f, "(* Rule %d, file %s, line %d *)\n",
1271 nrules-2, input_file_name, lineno);
1274 fprintf(f, "yyact.(%d) <- (fun __caml_parser_env ->\n", nrules-2);
1276 fprintf(f, "; (fun __caml_parser_env ->\n");
1279 for (i = nitems - 1; pitem[i]; --i) ++n;
1281 for (i = 1; i <= n; i++) {
1282 item = pitem[nitems + i - n - 1];
1283 if (item->class == TERM && !item->tag) continue;
1284 fprintf(f, " let _%d = ", i);
1286 fprintf(f, "(Parsing.peek_val __caml_parser_env %d : %s) in\n", n - i,
1289 fprintf(f, "Parsing.peek_val __caml_parser_env %d in\n", n - i);
1291 fprintf(f, "(Parsing.peek_val __caml_parser_env %d : '%s) in\n", n - i,
1294 fprintf(f, " Obj.repr(\n");
1295 fprintf(f, line_format, lineno, input_file_name);
1296 for (i = 0; i < cptr - line; i++) fputc(' ', f);
1306 if (isdigit((unsigned char) cptr[1]))
1311 if (i <= 0 || i > n)
1313 item = pitem[nitems + i - n - 1];
1314 if (item->class == TERM && !item->tag)
1315 illegal_token_ref(i, item->name);
1316 fprintf(f, "_%d", i);
1320 if (isalpha(c) || c == '_' || c == '$')
1326 } while (isalnum(c) || c == '_' || c == '$');
1329 if (c == '}' && depth == 1) {
1330 fprintf(f, ")\n# 0\n ");
1332 tagres = plhs[nrules]->tag;
1334 fprintf(f, " : %s))\n", tagres);
1338 fprintf(f, " : '%s))\n", plhs[nrules]->name);
1349 if (line) goto loop;
1350 unterminated_action(a_lineno, a_line, a_cptr);
1362 int s_lineno = lineno;
1363 char *s_line = dup_line();
1364 char *s_cptr = s_line + (cptr - line - 1);
1377 unterminated_string(s_lineno, s_line, s_cptr);
1386 unterminated_string(s_lineno, s_line, s_cptr);
1393 if (cptr[0] != 0 && cptr[0] != '\\' && cptr[1] == '\'') {
1394 fwrite(cptr, 1, 2, f);
1398 && isdigit((unsigned char) cptr[1])
1399 && isdigit((unsigned char) cptr[2])
1400 && isdigit((unsigned char) cptr[3])
1401 && cptr[4] == '\'') {
1402 fwrite(cptr, 1, 5, f);
1405 if (cptr[0] == '\\' && cptr[2] == '\'') {
1406 fwrite(cptr, 1, 3, f);
1415 int c_lineno = lineno;
1416 char *c_line = dup_line();
1417 char *c_cptr = c_line + (cptr - line - 1);
1425 if (c == '*' && *cptr == ')')
1436 unterminated_comment(c_lineno, c_line, c_cptr);
1452 register bucket *bp;
1455 if (c == '%' || c == '\\')
1463 else if ((c == 'p' || c == 'P') &&
1464 ((c = cptr[2]) == 'r' || c == 'R') &&
1465 ((c = cptr[3]) == 'e' || c == 'E') &&
1466 ((c = cptr[4]) == 'c' || c == 'C') &&
1467 ((c = cptr[5], !IS_IDENT(c))))
1470 syntax_error(lineno, line, cptr);
1473 if (isalpha(c) || c == '_' || c == '.' || c == '$')
1475 else if (c == '\'' || c == '"')
1479 syntax_error(lineno, line, cptr);
1483 if (rprec[nrules] != UNDEFINED && bp->prec != rprec[nrules])
1486 rprec[nrules] = bp->prec;
1487 rassoc[nrules] = bp->assoc;
1492 void read_grammar(void)
1496 initialize_grammar();
1502 if (c == '|' && at_first){
1507 if (c == EOF) break;
1508 if (isalpha(c) || c == '_' || c == '.' || c == '$' || c == '\'' ||
1511 else if (c == '{' || c == '=')
1516 start_rule(plhs[nrules-1], 0);
1521 if (mark_symbol()) break;
1524 syntax_error(lineno, line, cptr);
1530 void free_tags(void)
1534 if (tag_table == 0) return;
1536 for (i = 0; i < ntags; ++i)
1538 assert(tag_table[i]);
1545 void pack_names(void)
1547 register bucket *bp;
1548 register char *p, *s, *t;
1550 name_pool_size = 13; /* 13 == sizeof("$end") + sizeof("$accept") */
1551 for (bp = first_symbol; bp; bp = bp->next)
1552 name_pool_size += strlen(bp->name) + 1;
1553 name_pool = MALLOC(name_pool_size);
1554 if (name_pool == 0) no_space();
1556 strcpy(name_pool, "$accept");
1557 strcpy(name_pool+8, "$end");
1559 for (bp = first_symbol; bp; bp = bp->next)
1563 while ((*t++ = *s++)) continue;
1570 void check_symbols(void)
1572 register bucket *bp;
1574 if (goal->class == UNKNOWN)
1575 undefined_goal(goal->name);
1577 for (bp = first_symbol; bp; bp = bp->next)
1579 if (bp->class == UNKNOWN)
1581 undefined_symbol(bp->name);
1588 void pack_symbols(void)
1590 register bucket *bp;
1591 register bucket **v;
1592 register int i, j, k, n;
1596 for (bp = first_symbol; bp; bp = bp->next)
1599 if (bp->class == TERM) ++ntokens;
1601 start_symbol = ntokens;
1602 nvars = nsyms - ntokens;
1604 symbol_name = (char **) MALLOC(nsyms*sizeof(char *));
1605 if (symbol_name == 0) no_space();
1606 symbol_value = (short *) MALLOC(nsyms*sizeof(short));
1607 if (symbol_value == 0) no_space();
1608 symbol_prec = (short *) MALLOC(nsyms*sizeof(short));
1609 if (symbol_prec == 0) no_space();
1610 symbol_assoc = MALLOC(nsyms);
1611 if (symbol_assoc == 0) no_space();
1612 symbol_tag = (char **) MALLOC(nsyms*sizeof(char *));
1613 if (symbol_tag == 0) no_space();
1614 symbol_true_token = (char *) MALLOC(nsyms*sizeof(char));
1615 if (symbol_true_token == 0) no_space();
1617 v = (bucket **) MALLOC(nsyms*sizeof(bucket *));
1618 if (v == 0) no_space();
1621 v[start_symbol] = 0;
1624 j = start_symbol + 1;
1625 for (bp = first_symbol; bp; bp = bp->next)
1627 if (bp->class == TERM)
1632 assert(i == ntokens && j == nsyms);
1634 for (i = 1; i < ntokens; ++i)
1637 goal->index = start_symbol + 1;
1638 k = start_symbol + 2;
1648 for (i = start_symbol + 1; i < nsyms; ++i)
1658 for (i = 1; i < ntokens; ++i)
1663 for (j = k++; j > 0 && symbol_value[j-1] > n; --j)
1664 symbol_value[j] = symbol_value[j-1];
1665 symbol_value[j] = n;
1669 if (v[1]->value == UNDEFINED)
1674 for (i = 2; i < ntokens; ++i)
1676 if (v[i]->value == UNDEFINED)
1678 while (j < k && n == symbol_value[j])
1680 while (++j < k && n == symbol_value[j]) continue;
1688 symbol_name[0] = name_pool + 8;
1689 symbol_value[0] = 0;
1691 symbol_assoc[0] = TOKEN;
1693 symbol_true_token[0] = 0;
1694 for (i = 1; i < ntokens; ++i)
1696 symbol_name[i] = v[i]->name;
1697 symbol_value[i] = v[i]->value;
1698 symbol_prec[i] = v[i]->prec;
1699 symbol_assoc[i] = v[i]->assoc;
1700 symbol_tag[i] = v[i]->tag;
1701 symbol_true_token[i] = v[i]->true_token;
1703 symbol_name[start_symbol] = name_pool;
1704 symbol_value[start_symbol] = -1;
1705 symbol_prec[start_symbol] = 0;
1706 symbol_assoc[start_symbol] = TOKEN;
1707 symbol_tag[start_symbol] = "";
1708 symbol_true_token[start_symbol] = 0;
1709 for (++i; i < nsyms; ++i)
1712 symbol_name[k] = v[i]->name;
1713 symbol_value[k] = v[i]->value;
1714 symbol_prec[k] = v[i]->prec;
1715 symbol_assoc[k] = v[i]->assoc;
1716 symbol_tag[i] = v[i]->tag;
1717 symbol_true_token[i] = v[i]->true_token;
1723 static unsigned char caml_ident_start[32] =
1724 "\000\000\000\000\000\000\000\000\376\377\377\207\376\377\377\007\000\000\000\000\000\000\000\000\377\377\177\377\377\377\177\377";
1725 static unsigned char caml_ident_body[32] =
1726 "\000\000\000\000\200\000\377\003\376\377\377\207\376\377\377\007\000\000\000\000\000\000\000\000\377\377\177\377\377\377\177\377";
1728 #define In_bitmap(bm,c) (bm[(unsigned char)(c) >> 3] & (1 << ((c) & 7)))
1730 static int is_polymorphic(char * s)
1734 if (c == '\'' || c == '#') return 1;
1737 while (c == ' ' || c == '\t' || c == '\r' || c == '\n') c = *++s;
1738 if (c == '<' || c == '>') return 1;
1740 if (In_bitmap(caml_ident_start, c)) {
1741 while (In_bitmap(caml_ident_body, *s)) s++;
1747 void make_goal(void)
1749 static char name[7] = "'\\xxx'";
1753 goal = lookup("%entry%");
1754 ntotalrules = nrules - 2;
1755 for(bp = first_symbol; bp != 0; bp = bp->next) {
1757 start_rule(goal, 0);
1758 if (nitems + 2> maxitems)
1760 name[2] = '0' + ((bp->entry >> 6) & 7);
1761 name[3] = '0' + ((bp->entry >> 3) & 7);
1762 name[4] = '0' + (bp->entry & 7);
1765 bc->value = (unsigned char) bp->entry;
1766 pitem[nitems++] = bc;
1767 pitem[nitems++] = bp;
1768 if (bp->tag == NULL)
1769 entry_without_type(bp->name);
1770 if (is_polymorphic(bp->tag))
1771 polymorphic_entry_point(bp->name);
1773 "let %s (lexfun : Lexing.lexbuf -> token) (lexbuf : Lexing.lexbuf) =\n (Parsing.yyparse yytables %d lexfun lexbuf : %s)\n",
1774 bp->name, bp->entry, bp->tag);
1775 fprintf(interface_file,
1776 "val %s :\n (Lexing.lexbuf -> token) -> Lexing.lexbuf -> %s\n",
1779 fprintf(action_file,
1780 "(* Entry %s *)\n", bp->name);
1782 fprintf(action_file,
1783 "yyact.(%d) <- (fun __caml_parser_env -> raise "
1784 "(Parsing.YYexit (Parsing.peek_val __caml_parser_env 0)))\n",
1787 fprintf(action_file,
1788 "; (fun __caml_parser_env -> raise "
1789 "(Parsing.YYexit (Parsing.peek_val __caml_parser_env 0)))\n");
1791 last_was_action = 1;
1797 void pack_grammar(void)
1802 ritem = (short *) MALLOC(nitems*sizeof(short));
1803 if (ritem == 0) no_space();
1804 rlhs = (short *) MALLOC(nrules*sizeof(short));
1805 if (rlhs == 0) no_space();
1806 rrhs = (short *) MALLOC((nrules+1)*sizeof(short));
1807 if (rrhs == 0) no_space();
1808 rprec = (short *) REALLOC(rprec, nrules*sizeof(short));
1809 if (rprec == 0) no_space();
1810 rassoc = REALLOC(rassoc, nrules);
1811 if (rassoc == 0) no_space();
1814 ritem[1] = goal->index;
1819 rlhs[2] = start_symbol;
1825 for (i = 3; i < nrules; ++i)
1827 rlhs[i] = plhs[i]->index;
1833 ritem[j] = pitem[j]->index;
1834 if (pitem[j]->class == TERM)
1836 prec = pitem[j]->prec;
1837 assoc = pitem[j]->assoc;
1843 if (rprec[i] == UNDEFINED)
1856 void print_grammar(void)
1858 register int i, j, k;
1860 register FILE *f = verbose_file;
1865 for (i = 2; i < nrules; ++i)
1867 if (rlhs[i] != rlhs[i-1])
1869 if (i != 2) fprintf(f, "\n");
1870 fprintf(f, "%4d %s :", i - 2, symbol_name[rlhs[i]]);
1871 spacing = strlen(symbol_name[rlhs[i]]) + 1;
1875 fprintf(f, "%4d ", i - 2);
1877 while (--j >= 0) putc(' ', f);
1881 while (ritem[k] >= 0)
1883 fprintf(f, " %s", symbol_name[ritem[k]]);
1894 virtual_input_file_name = substring (input_file_name, 0,
1895 strlen (input_file_name));
1896 create_symbol_table();
1897 read_declarations();
1898 output_token_type();
1901 free_symbol_table();