pci: don't do sanity check for missing pci bus, the check can misfire.
[minix.git] / commands / byacc / reader.c
blob059a04371c7d1f50dbaf29e05ab4b169f9ea4f4e
1 #include "defs.h"
3 /* The line size must be a positive integer. One hundred was chosen */
4 /* because few lines in Yacc input grammars exceed 100 characters. */
5 /* Note that if a line exceeds LINESIZE characters, the line buffer */
6 /* will be expanded to accomodate it. */
8 #define LINESIZE 100
10 char *cache;
11 int cinc, cache_size;
13 int ntags, tagmax;
14 char **tag_table;
16 char saw_eof, unionized;
17 char *cptr, *line;
18 int linesize;
20 bucket *goal;
21 int prec;
22 int gensym;
23 char last_was_action;
25 int maxitems;
26 bucket **pitem;
28 int maxrules;
29 bucket **plhs;
31 int name_pool_size;
32 char *name_pool;
34 char line_format[] = "#line %d \"%s\"\n";
37 cachec(c)
38 int c;
40 assert(cinc >= 0);
41 if (cinc >= cache_size)
43 cache_size += 256;
44 cache = REALLOC(cache, cache_size);
45 if (cache == 0) no_space();
47 cache[cinc] = c;
48 ++cinc;
52 get_line()
54 register FILE *f = input_file;
55 register int c;
56 register int i;
58 if (saw_eof || (c = getc(f)) == EOF)
60 if (line) { FREE(line); line = 0; }
61 cptr = 0;
62 saw_eof = 1;
63 return;
66 if (line == 0 || linesize != (LINESIZE + 1))
68 if (line) FREE(line);
69 linesize = LINESIZE + 1;
70 line = MALLOC(linesize);
71 if (line == 0) no_space();
74 i = 0;
75 ++lineno;
76 for (;;)
78 line[i] = c;
79 if (c == '\n') { cptr = line; return; }
80 if (++i >= linesize)
82 linesize += LINESIZE;
83 line = REALLOC(line, linesize);
84 if (line == 0) no_space();
86 c = getc(f);
87 if (c == EOF)
89 line[i] = '\n';
90 saw_eof = 1;
91 cptr = line;
92 return;
98 char *
99 dup_line()
101 register char *p, *s, *t;
103 if (line == 0) return (0);
104 s = line;
105 while (*s != '\n') ++s;
106 p = MALLOC(s - line + 1);
107 if (p == 0) no_space();
109 s = line;
110 t = p;
111 while ((*t++ = *s++) != '\n') continue;
112 return (p);
116 skip_comment()
118 register char *s;
120 int st_lineno = lineno;
121 char *st_line = dup_line();
122 char *st_cptr = st_line + (cptr - line);
124 s = cptr + 2;
125 for (;;)
127 if (*s == '*' && s[1] == '/')
129 cptr = s + 2;
130 FREE(st_line);
131 return;
133 if (*s == '\n')
135 get_line();
136 if (line == 0)
137 unterminated_comment(st_lineno, st_line, st_cptr);
138 s = cptr;
140 else
141 ++s;
147 nextc()
149 register char *s;
151 if (line == 0)
153 get_line();
154 if (line == 0)
155 return (EOF);
158 s = cptr;
159 for (;;)
161 switch (*s)
163 case '\n':
164 get_line();
165 if (line == 0) return (EOF);
166 s = cptr;
167 break;
169 case ' ':
170 case '\t':
171 case '\f':
172 case '\r':
173 case '\v':
174 case ',':
175 case ';':
176 ++s;
177 break;
179 case '\\':
180 cptr = s;
181 return ('%');
183 case '/':
184 if (s[1] == '*')
186 cptr = s;
187 skip_comment();
188 s = cptr;
189 break;
191 else if (s[1] == '/')
193 get_line();
194 if (line == 0) return (EOF);
195 s = cptr;
196 break;
198 /* fall through */
200 default:
201 cptr = s;
202 return (*s);
209 keyword()
211 register int c;
212 char *t_cptr = cptr;
214 c = *++cptr;
215 if (isalpha(c))
217 cinc = 0;
218 for (;;)
220 if (isalpha(c))
222 if (isupper(c)) c = tolower(c);
223 cachec(c);
225 else if (isdigit(c) || c == '_' || c == '.' || c == '$')
226 cachec(c);
227 else
228 break;
229 c = *++cptr;
231 cachec(NUL);
233 if (strcmp(cache, "token") == 0 || strcmp(cache, "term") == 0)
234 return (TOKEN);
235 if (strcmp(cache, "type") == 0)
236 return (TYPE);
237 if (strcmp(cache, "left") == 0)
238 return (LEFT);
239 if (strcmp(cache, "right") == 0)
240 return (RIGHT);
241 if (strcmp(cache, "nonassoc") == 0 || strcmp(cache, "binary") == 0)
242 return (NONASSOC);
243 if (strcmp(cache, "start") == 0)
244 return (START);
245 if (strcmp(cache, "union") == 0)
246 return (UNION);
247 if (strcmp(cache, "ident") == 0)
248 return (IDENT);
250 else
252 ++cptr;
253 if (c == '{')
254 return (TEXT);
255 if (c == '%' || c == '\\')
256 return (MARK);
257 if (c == '<')
258 return (LEFT);
259 if (c == '>')
260 return (RIGHT);
261 if (c == '0')
262 return (TOKEN);
263 if (c == '2')
264 return (NONASSOC);
266 syntax_error(lineno, line, t_cptr);
267 /*NOTREACHED*/
271 copy_ident()
273 register int c;
274 register FILE *f = output_file;
276 c = nextc();
277 if (c == EOF) unexpected_EOF();
278 if (c != '"') syntax_error(lineno, line, cptr);
279 ++outline;
280 fprintf(f, "#ident \"");
281 for (;;)
283 c = *++cptr;
284 if (c == '\n')
286 fprintf(f, "\"\n");
287 return;
289 putc(c, f);
290 if (c == '"')
292 putc('\n', f);
293 ++cptr;
294 return;
300 copy_text()
302 register int c;
303 int quote;
304 register FILE *f = text_file;
305 int need_newline = 0;
306 int t_lineno = lineno;
307 char *t_line = dup_line();
308 char *t_cptr = t_line + (cptr - line - 2);
310 if (*cptr == '\n')
312 get_line();
313 if (line == 0)
314 unterminated_text(t_lineno, t_line, t_cptr);
316 if (!lflag) fprintf(f, line_format, lineno, input_file_name);
318 loop:
319 c = *cptr++;
320 switch (c)
322 case '\n':
323 next_line:
324 putc('\n', f);
325 need_newline = 0;
326 get_line();
327 if (line) goto loop;
328 unterminated_text(t_lineno, t_line, t_cptr);
330 case '\'':
331 case '"':
333 int s_lineno = lineno;
334 char *s_line = dup_line();
335 char *s_cptr = s_line + (cptr - line - 1);
337 quote = c;
338 putc(c, f);
339 for (;;)
341 c = *cptr++;
342 putc(c, f);
343 if (c == quote)
345 need_newline = 1;
346 FREE(s_line);
347 goto loop;
349 if (c == '\n')
350 unterminated_string(s_lineno, s_line, s_cptr);
351 if (c == '\\')
353 c = *cptr++;
354 putc(c, f);
355 if (c == '\n')
357 get_line();
358 if (line == 0)
359 unterminated_string(s_lineno, s_line, s_cptr);
365 case '/':
366 putc(c, f);
367 need_newline = 1;
368 c = *cptr;
369 if (c == '/')
371 putc('*', f);
372 while ((c = *++cptr) != '\n')
374 if (c == '*' && cptr[1] == '/')
375 fprintf(f, "* ");
376 else
377 putc(c, f);
379 fprintf(f, "*/");
380 goto next_line;
382 if (c == '*')
384 int c_lineno = lineno;
385 char *c_line = dup_line();
386 char *c_cptr = c_line + (cptr - line - 1);
388 putc('*', f);
389 ++cptr;
390 for (;;)
392 c = *cptr++;
393 putc(c, f);
394 if (c == '*' && *cptr == '/')
396 putc('/', f);
397 ++cptr;
398 FREE(c_line);
399 goto loop;
401 if (c == '\n')
403 get_line();
404 if (line == 0)
405 unterminated_comment(c_lineno, c_line, c_cptr);
409 need_newline = 1;
410 goto loop;
412 case '%':
413 case '\\':
414 if (*cptr == '}')
416 if (need_newline) putc('\n', f);
417 ++cptr;
418 FREE(t_line);
419 return;
421 /* fall through */
423 default:
424 putc(c, f);
425 need_newline = 1;
426 goto loop;
431 copy_union()
433 register int c;
434 int quote;
435 int depth;
436 int u_lineno = lineno;
437 char *u_line = dup_line();
438 char *u_cptr = u_line + (cptr - line - 6);
440 if (unionized) over_unionized(cptr - 6);
441 unionized = 1;
443 if (!lflag)
444 fprintf(text_file, line_format, lineno, input_file_name);
446 fprintf(text_file, "typedef union");
447 if (dflag) fprintf(union_file, "typedef union");
449 depth = 0;
450 loop:
451 c = *cptr++;
452 putc(c, text_file);
453 if (dflag) putc(c, union_file);
454 switch (c)
456 case '\n':
457 next_line:
458 get_line();
459 if (line == 0) unterminated_union(u_lineno, u_line, u_cptr);
460 goto loop;
462 case '{':
463 ++depth;
464 goto loop;
466 case '}':
467 if (--depth == 0)
469 fprintf(text_file, " YYSTYPE;\n");
470 FREE(u_line);
471 return;
473 goto loop;
475 case '\'':
476 case '"':
478 int s_lineno = lineno;
479 char *s_line = dup_line();
480 char *s_cptr = s_line + (cptr - line - 1);
482 quote = c;
483 for (;;)
485 c = *cptr++;
486 putc(c, text_file);
487 if (dflag) putc(c, union_file);
488 if (c == quote)
490 FREE(s_line);
491 goto loop;
493 if (c == '\n')
494 unterminated_string(s_lineno, s_line, s_cptr);
495 if (c == '\\')
497 c = *cptr++;
498 putc(c, text_file);
499 if (dflag) putc(c, union_file);
500 if (c == '\n')
502 get_line();
503 if (line == 0)
504 unterminated_string(s_lineno, s_line, s_cptr);
510 case '/':
511 c = *cptr;
512 if (c == '/')
514 putc('*', text_file);
515 if (dflag) putc('*', union_file);
516 while ((c = *++cptr) != '\n')
518 if (c == '*' && cptr[1] == '/')
520 fprintf(text_file, "* ");
521 if (dflag) fprintf(union_file, "* ");
523 else
525 putc(c, text_file);
526 if (dflag) putc(c, union_file);
529 fprintf(text_file, "*/\n");
530 if (dflag) fprintf(union_file, "*/\n");
531 goto next_line;
533 if (c == '*')
535 int c_lineno = lineno;
536 char *c_line = dup_line();
537 char *c_cptr = c_line + (cptr - line - 1);
539 putc('*', text_file);
540 if (dflag) putc('*', union_file);
541 ++cptr;
542 for (;;)
544 c = *cptr++;
545 putc(c, text_file);
546 if (dflag) putc(c, union_file);
547 if (c == '*' && *cptr == '/')
549 putc('/', text_file);
550 if (dflag) putc('/', union_file);
551 ++cptr;
552 FREE(c_line);
553 goto loop;
555 if (c == '\n')
557 get_line();
558 if (line == 0)
559 unterminated_comment(c_lineno, c_line, c_cptr);
563 goto loop;
565 default:
566 goto loop;
572 hexval(c)
573 int c;
575 if (c >= '0' && c <= '9')
576 return (c - '0');
577 if (c >= 'A' && c <= 'F')
578 return (c - 'A' + 10);
579 if (c >= 'a' && c <= 'f')
580 return (c - 'a' + 10);
581 return (-1);
585 bucket *
586 get_literal()
588 register int c, quote;
589 register int i;
590 register int n;
591 register char *s;
592 register bucket *bp;
593 int s_lineno = lineno;
594 char *s_line = dup_line();
595 char *s_cptr = s_line + (cptr - line);
597 quote = *cptr++;
598 cinc = 0;
599 for (;;)
601 c = *cptr++;
602 if (c == quote) break;
603 if (c == '\n') unterminated_string(s_lineno, s_line, s_cptr);
604 if (c == '\\')
606 char *c_cptr = cptr - 1;
608 c = *cptr++;
609 switch (c)
611 case '\n':
612 get_line();
613 if (line == 0) unterminated_string(s_lineno, s_line, s_cptr);
614 continue;
616 case '0': case '1': case '2': case '3':
617 case '4': case '5': case '6': case '7':
618 n = c - '0';
619 c = *cptr;
620 if (IS_OCTAL(c))
622 n = (n << 3) + (c - '0');
623 c = *++cptr;
624 if (IS_OCTAL(c))
626 n = (n << 3) + (c - '0');
627 ++cptr;
630 if (n > MAXCHAR) illegal_character(c_cptr);
631 c = n;
632 break;
634 case 'x':
635 c = *cptr++;
636 n = hexval(c);
637 if (n < 0 || n >= 16)
638 illegal_character(c_cptr);
639 for (;;)
641 c = *cptr;
642 i = hexval(c);
643 if (i < 0 || i >= 16) break;
644 ++cptr;
645 n = (n << 4) + i;
646 if (n > MAXCHAR) illegal_character(c_cptr);
648 c = n;
649 break;
651 case 'a': c = 7; break;
652 case 'b': c = '\b'; break;
653 case 'f': c = '\f'; break;
654 case 'n': c = '\n'; break;
655 case 'r': c = '\r'; break;
656 case 't': c = '\t'; break;
657 case 'v': c = '\v'; break;
660 cachec(c);
662 FREE(s_line);
664 n = cinc;
665 s = MALLOC(n);
666 if (s == 0) no_space();
668 for (i = 0; i < n; ++i)
669 s[i] = cache[i];
671 cinc = 0;
672 if (n == 1)
673 cachec('\'');
674 else
675 cachec('"');
677 for (i = 0; i < n; ++i)
679 c = ((unsigned char *)s)[i];
680 if (c == '\\' || c == cache[0])
682 cachec('\\');
683 cachec(c);
685 else if (isprint(c))
686 cachec(c);
687 else
689 cachec('\\');
690 switch (c)
692 case 7: cachec('a'); break;
693 case '\b': cachec('b'); break;
694 case '\f': cachec('f'); break;
695 case '\n': cachec('n'); break;
696 case '\r': cachec('r'); break;
697 case '\t': cachec('t'); break;
698 case '\v': cachec('v'); break;
699 default:
700 cachec(((c >> 6) & 7) + '0');
701 cachec(((c >> 3) & 7) + '0');
702 cachec((c & 7) + '0');
703 break;
708 if (n == 1)
709 cachec('\'');
710 else
711 cachec('"');
713 cachec(NUL);
714 bp = lookup(cache);
715 bp->class = TERM;
716 if (n == 1 && bp->value == UNDEFINED)
717 bp->value = *(unsigned char *)s;
718 FREE(s);
720 return (bp);
725 is_reserved(name)
726 char *name;
728 char *s;
730 if (strcmp(name, ".") == 0 ||
731 strcmp(name, "$accept") == 0 ||
732 strcmp(name, "$end") == 0)
733 return (1);
735 if (name[0] == '$' && name[1] == '$' && isdigit(name[2]))
737 s = name + 3;
738 while (isdigit(*s)) ++s;
739 if (*s == NUL) return (1);
742 return (0);
746 bucket *
747 get_name()
749 register int c;
751 cinc = 0;
752 for (c = *cptr; IS_IDENT(c); c = *++cptr)
753 cachec(c);
754 cachec(NUL);
756 if (is_reserved(cache)) used_reserved(cache);
758 return (lookup(cache));
763 get_number()
765 register int c;
766 register int n;
768 n = 0;
769 for (c = *cptr; isdigit(c); c = *++cptr)
770 n = 10*n + (c - '0');
772 return (n);
776 char *
777 get_tag()
779 register int c;
780 register int i;
781 register char *s;
782 int t_lineno = lineno;
783 char *t_line = dup_line();
784 char *t_cptr = t_line + (cptr - line);
786 ++cptr;
787 c = nextc();
788 if (c == EOF) unexpected_EOF();
789 if (!isalpha(c) && c != '_' && c != '$')
790 illegal_tag(t_lineno, t_line, t_cptr);
792 cinc = 0;
793 do { cachec(c); c = *++cptr; } while (IS_IDENT(c));
794 cachec(NUL);
796 c = nextc();
797 if (c == EOF) unexpected_EOF();
798 if (c != '>')
799 illegal_tag(t_lineno, t_line, t_cptr);
800 ++cptr;
802 for (i = 0; i < ntags; ++i)
804 if (strcmp(cache, tag_table[i]) == 0)
805 return (tag_table[i]);
808 if (ntags >= tagmax)
810 tagmax += 16;
811 tag_table = (char **)
812 (tag_table ? REALLOC(tag_table, tagmax*sizeof(char *))
813 : MALLOC(tagmax*sizeof(char *)));
814 if (tag_table == 0) no_space();
817 s = MALLOC(cinc);
818 if (s == 0) no_space();
819 strcpy(s, cache);
820 tag_table[ntags] = s;
821 ++ntags;
822 FREE(t_line);
823 return (s);
827 declare_tokens(assoc)
828 int assoc;
830 register int c;
831 register bucket *bp;
832 int value;
833 char *tag = 0;
835 if (assoc != TOKEN) ++prec;
837 c = nextc();
838 if (c == EOF) unexpected_EOF();
839 if (c == '<')
841 tag = get_tag();
842 c = nextc();
843 if (c == EOF) unexpected_EOF();
846 for (;;)
848 if (isalpha(c) || c == '_' || c == '.' || c == '$')
849 bp = get_name();
850 else if (c == '\'' || c == '"')
851 bp = get_literal();
852 else
853 return;
855 if (bp == goal) tokenized_start(bp->name);
856 bp->class = TERM;
858 if (tag)
860 if (bp->tag && tag != bp->tag)
861 retyped_warning(bp->name);
862 bp->tag = tag;
865 if (assoc != TOKEN)
867 if (bp->prec && prec != bp->prec)
868 reprec_warning(bp->name);
869 bp->assoc = assoc;
870 bp->prec = prec;
873 c = nextc();
874 if (c == EOF) unexpected_EOF();
875 value = UNDEFINED;
876 if (isdigit(c))
878 value = get_number();
879 if (bp->value != UNDEFINED && value != bp->value)
880 revalued_warning(bp->name);
881 bp->value = value;
882 c = nextc();
883 if (c == EOF) unexpected_EOF();
889 declare_types()
891 register int c;
892 register bucket *bp;
893 char *tag;
895 c = nextc();
896 if (c == EOF) unexpected_EOF();
897 if (c != '<') syntax_error(lineno, line, cptr);
898 tag = get_tag();
900 for (;;)
902 c = nextc();
903 if (isalpha(c) || c == '_' || c == '.' || c == '$')
904 bp = get_name();
905 else if (c == '\'' || c == '"')
906 bp = get_literal();
907 else
908 return;
910 if (bp->tag && tag != bp->tag)
911 retyped_warning(bp->name);
912 bp->tag = tag;
917 declare_start()
919 register int c;
920 register bucket *bp;
922 c = nextc();
923 if (c == EOF) unexpected_EOF();
924 if (!isalpha(c) && c != '_' && c != '.' && c != '$')
925 syntax_error(lineno, line, cptr);
926 bp = get_name();
927 if (bp->class == TERM)
928 terminal_start(bp->name);
929 if (goal && goal != bp)
930 restarted_warning();
931 goal = bp;
935 read_declarations()
937 register int c, k;
939 cache_size = 256;
940 cache = MALLOC(cache_size);
941 if (cache == 0) no_space();
943 for (;;)
945 c = nextc();
946 if (c == EOF) unexpected_EOF();
947 if (c != '%') syntax_error(lineno, line, cptr);
948 switch (k = keyword())
950 case MARK:
951 return;
953 case IDENT:
954 copy_ident();
955 break;
957 case TEXT:
958 copy_text();
959 break;
961 case UNION:
962 copy_union();
963 break;
965 case TOKEN:
966 case LEFT:
967 case RIGHT:
968 case NONASSOC:
969 declare_tokens(k);
970 break;
972 case TYPE:
973 declare_types();
974 break;
976 case START:
977 declare_start();
978 break;
984 initialize_grammar()
986 nitems = 4;
987 maxitems = 300;
988 pitem = (bucket **) MALLOC(maxitems*sizeof(bucket *));
989 if (pitem == 0) no_space();
990 pitem[0] = 0;
991 pitem[1] = 0;
992 pitem[2] = 0;
993 pitem[3] = 0;
995 nrules = 3;
996 maxrules = 100;
997 plhs = (bucket **) MALLOC(maxrules*sizeof(bucket *));
998 if (plhs == 0) no_space();
999 plhs[0] = 0;
1000 plhs[1] = 0;
1001 plhs[2] = 0;
1002 rprec = (short *) MALLOC(maxrules*sizeof(short));
1003 if (rprec == 0) no_space();
1004 rprec[0] = 0;
1005 rprec[1] = 0;
1006 rprec[2] = 0;
1007 rassoc = (char *) MALLOC(maxrules*sizeof(char));
1008 if (rassoc == 0) no_space();
1009 rassoc[0] = TOKEN;
1010 rassoc[1] = TOKEN;
1011 rassoc[2] = TOKEN;
1015 expand_items()
1017 maxitems += 300;
1018 pitem = (bucket **) REALLOC(pitem, maxitems*sizeof(bucket *));
1019 if (pitem == 0) no_space();
1023 expand_rules()
1025 maxrules += 100;
1026 plhs = (bucket **) REALLOC(plhs, maxrules*sizeof(bucket *));
1027 if (plhs == 0) no_space();
1028 rprec = (short *) REALLOC(rprec, maxrules*sizeof(short));
1029 if (rprec == 0) no_space();
1030 rassoc = (char *) REALLOC(rassoc, maxrules*sizeof(char));
1031 if (rassoc == 0) no_space();
1035 advance_to_start()
1037 register int c;
1038 register bucket *bp;
1039 char *s_cptr;
1040 int s_lineno;
1042 for (;;)
1044 c = nextc();
1045 if (c != '%') break;
1046 s_cptr = cptr;
1047 switch (keyword())
1049 case MARK:
1050 no_grammar();
1052 case TEXT:
1053 copy_text();
1054 break;
1056 case START:
1057 declare_start();
1058 break;
1060 default:
1061 syntax_error(lineno, line, s_cptr);
1065 c = nextc();
1066 if (!isalpha(c) && c != '_' && c != '.' && c != '_')
1067 syntax_error(lineno, line, cptr);
1068 bp = get_name();
1069 if (goal == 0)
1071 if (bp->class == TERM)
1072 terminal_start(bp->name);
1073 goal = bp;
1076 s_lineno = lineno;
1077 c = nextc();
1078 if (c == EOF) unexpected_EOF();
1079 if (c != ':') syntax_error(lineno, line, cptr);
1080 start_rule(bp, s_lineno);
1081 ++cptr;
1085 start_rule(bp, s_lineno)
1086 register bucket *bp;
1087 int s_lineno;
1089 if (bp->class == TERM)
1090 terminal_lhs(s_lineno);
1091 bp->class = NONTERM;
1092 if (nrules >= maxrules)
1093 expand_rules();
1094 plhs[nrules] = bp;
1095 rprec[nrules] = UNDEFINED;
1096 rassoc[nrules] = TOKEN;
1100 end_rule()
1102 register int i;
1104 if (!last_was_action && plhs[nrules]->tag)
1106 for (i = nitems - 1; pitem[i]; --i) continue;
1107 if (pitem[i+1] == 0 || pitem[i+1]->tag != plhs[nrules]->tag)
1108 default_action_warning();
1111 last_was_action = 0;
1112 if (nitems >= maxitems) expand_items();
1113 pitem[nitems] = 0;
1114 ++nitems;
1115 ++nrules;
1119 insert_empty_rule()
1121 register bucket *bp, **bpp;
1123 assert(cache);
1124 sprintf(cache, "$$%d", ++gensym);
1125 bp = make_bucket(cache);
1126 last_symbol->next = bp;
1127 last_symbol = bp;
1128 bp->tag = plhs[nrules]->tag;
1129 bp->class = NONTERM;
1131 if ((nitems += 2) > maxitems)
1132 expand_items();
1133 bpp = pitem + nitems - 1;
1134 *bpp-- = bp;
1135 while (bpp[0] = bpp[-1]) --bpp;
1137 if (++nrules >= maxrules)
1138 expand_rules();
1139 plhs[nrules] = plhs[nrules-1];
1140 plhs[nrules-1] = bp;
1141 rprec[nrules] = rprec[nrules-1];
1142 rprec[nrules-1] = 0;
1143 rassoc[nrules] = rassoc[nrules-1];
1144 rassoc[nrules-1] = TOKEN;
1148 add_symbol()
1150 register int c;
1151 register bucket *bp;
1152 int s_lineno = lineno;
1154 c = *cptr;
1155 if (c == '\'' || c == '"')
1156 bp = get_literal();
1157 else
1158 bp = get_name();
1160 c = nextc();
1161 if (c == ':')
1163 end_rule();
1164 start_rule(bp, s_lineno);
1165 ++cptr;
1166 return;
1169 if (last_was_action)
1170 insert_empty_rule();
1171 last_was_action = 0;
1173 if (++nitems > maxitems)
1174 expand_items();
1175 pitem[nitems-1] = bp;
1179 copy_action()
1181 register int c;
1182 register int i, n;
1183 int depth;
1184 int quote;
1185 char *tag;
1186 register FILE *f = action_file;
1187 int a_lineno = lineno;
1188 char *a_line = dup_line();
1189 char *a_cptr = a_line + (cptr - line);
1191 if (last_was_action)
1192 insert_empty_rule();
1193 last_was_action = 1;
1195 fprintf(f, "case %d:\n", nrules - 2);
1196 if (!lflag)
1197 fprintf(f, line_format, lineno, input_file_name);
1198 if (*cptr == '=') ++cptr;
1200 n = 0;
1201 for (i = nitems - 1; pitem[i]; --i) ++n;
1203 depth = 0;
1204 loop:
1205 c = *cptr;
1206 if (c == '$')
1208 if (cptr[1] == '<')
1210 int d_lineno = lineno;
1211 char *d_line = dup_line();
1212 char *d_cptr = d_line + (cptr - line);
1214 ++cptr;
1215 tag = get_tag();
1216 c = *cptr;
1217 if (c == '$')
1219 fprintf(f, "yyval.%s", tag);
1220 ++cptr;
1221 FREE(d_line);
1222 goto loop;
1224 else if (isdigit(c))
1226 i = get_number();
1227 if (i > n) dollar_warning(d_lineno, i);
1228 fprintf(f, "yyvsp[%d].%s", i - n, tag);
1229 FREE(d_line);
1230 goto loop;
1232 else if (c == '-' && isdigit(cptr[1]))
1234 ++cptr;
1235 i = -get_number() - n;
1236 fprintf(f, "yyvsp[%d].%s", i, tag);
1237 FREE(d_line);
1238 goto loop;
1240 else
1241 dollar_error(d_lineno, d_line, d_cptr);
1243 else if (cptr[1] == '$')
1245 if (ntags)
1247 tag = plhs[nrules]->tag;
1248 if (tag == 0) untyped_lhs();
1249 fprintf(f, "yyval.%s", tag);
1251 else
1252 fprintf(f, "yyval");
1253 cptr += 2;
1254 goto loop;
1256 else if (isdigit(cptr[1]))
1258 ++cptr;
1259 i = get_number();
1260 if (ntags)
1262 if (i <= 0 || i > n)
1263 unknown_rhs(i);
1264 tag = pitem[nitems + i - n - 1]->tag;
1265 if (tag == 0) untyped_rhs(i, pitem[nitems + i - n - 1]->name);
1266 fprintf(f, "yyvsp[%d].%s", i - n, tag);
1268 else
1270 if (i > n)
1271 dollar_warning(lineno, i);
1272 fprintf(f, "yyvsp[%d]", i - n);
1274 goto loop;
1276 else if (cptr[1] == '-')
1278 cptr += 2;
1279 i = get_number();
1280 if (ntags)
1281 unknown_rhs(-i);
1282 fprintf(f, "yyvsp[%d]", -i - n);
1283 goto loop;
1286 if (isalpha(c) || c == '_' || c == '$')
1290 putc(c, f);
1291 c = *++cptr;
1292 } while (isalnum(c) || c == '_' || c == '$');
1293 goto loop;
1295 putc(c, f);
1296 ++cptr;
1297 switch (c)
1299 case '\n':
1300 next_line:
1301 get_line();
1302 if (line) goto loop;
1303 unterminated_action(a_lineno, a_line, a_cptr);
1305 case ';':
1306 if (depth > 0) goto loop;
1307 fprintf(f, "\nbreak;\n");
1308 return;
1310 case '{':
1311 ++depth;
1312 goto loop;
1314 case '}':
1315 if (--depth > 0) goto loop;
1316 fprintf(f, "\nbreak;\n");
1317 return;
1319 case '\'':
1320 case '"':
1322 int s_lineno = lineno;
1323 char *s_line = dup_line();
1324 char *s_cptr = s_line + (cptr - line - 1);
1326 quote = c;
1327 for (;;)
1329 c = *cptr++;
1330 putc(c, f);
1331 if (c == quote)
1333 FREE(s_line);
1334 goto loop;
1336 if (c == '\n')
1337 unterminated_string(s_lineno, s_line, s_cptr);
1338 if (c == '\\')
1340 c = *cptr++;
1341 putc(c, f);
1342 if (c == '\n')
1344 get_line();
1345 if (line == 0)
1346 unterminated_string(s_lineno, s_line, s_cptr);
1352 case '/':
1353 c = *cptr;
1354 if (c == '/')
1356 putc('*', f);
1357 while ((c = *++cptr) != '\n')
1359 if (c == '*' && cptr[1] == '/')
1360 fprintf(f, "* ");
1361 else
1362 putc(c, f);
1364 fprintf(f, "*/\n");
1365 goto next_line;
1367 if (c == '*')
1369 int c_lineno = lineno;
1370 char *c_line = dup_line();
1371 char *c_cptr = c_line + (cptr - line - 1);
1373 putc('*', f);
1374 ++cptr;
1375 for (;;)
1377 c = *cptr++;
1378 putc(c, f);
1379 if (c == '*' && *cptr == '/')
1381 putc('/', f);
1382 ++cptr;
1383 FREE(c_line);
1384 goto loop;
1386 if (c == '\n')
1388 get_line();
1389 if (line == 0)
1390 unterminated_comment(c_lineno, c_line, c_cptr);
1394 goto loop;
1396 default:
1397 goto loop;
1403 mark_symbol()
1405 register int c;
1406 register bucket *bp;
1408 c = cptr[1];
1409 if (c == '%' || c == '\\')
1411 cptr += 2;
1412 return (1);
1415 if (c == '=')
1416 cptr += 2;
1417 else if ((c == 'p' || c == 'P') &&
1418 ((c = cptr[2]) == 'r' || c == 'R') &&
1419 ((c = cptr[3]) == 'e' || c == 'E') &&
1420 ((c = cptr[4]) == 'c' || c == 'C') &&
1421 ((c = cptr[5], !IS_IDENT(c))))
1422 cptr += 5;
1423 else
1424 syntax_error(lineno, line, cptr);
1426 c = nextc();
1427 if (isalpha(c) || c == '_' || c == '.' || c == '$')
1428 bp = get_name();
1429 else if (c == '\'' || c == '"')
1430 bp = get_literal();
1431 else
1433 syntax_error(lineno, line, cptr);
1434 /*NOTREACHED*/
1437 if (rprec[nrules] != UNDEFINED && bp->prec != rprec[nrules])
1438 prec_redeclared();
1440 rprec[nrules] = bp->prec;
1441 rassoc[nrules] = bp->assoc;
1442 return (0);
1446 read_grammar()
1448 register int c;
1450 initialize_grammar();
1451 advance_to_start();
1453 for (;;)
1455 c = nextc();
1456 if (c == EOF) break;
1457 if (isalpha(c) || c == '_' || c == '.' || c == '$' || c == '\'' ||
1458 c == '"')
1459 add_symbol();
1460 else if (c == '{' || c == '=')
1461 copy_action();
1462 else if (c == '|')
1464 end_rule();
1465 start_rule(plhs[nrules-1], 0);
1466 ++cptr;
1468 else if (c == '%')
1470 if (mark_symbol()) break;
1472 else
1473 syntax_error(lineno, line, cptr);
1475 end_rule();
1479 free_tags()
1481 register int i;
1483 if (tag_table == 0) return;
1485 for (i = 0; i < ntags; ++i)
1487 assert(tag_table[i]);
1488 FREE(tag_table[i]);
1490 FREE(tag_table);
1494 pack_names()
1496 register bucket *bp;
1497 register char *p, *s, *t;
1499 name_pool_size = 13; /* 13 == sizeof("$end") + sizeof("$accept") */
1500 for (bp = first_symbol; bp; bp = bp->next)
1501 name_pool_size += strlen(bp->name) + 1;
1502 name_pool = MALLOC(name_pool_size);
1503 if (name_pool == 0) no_space();
1505 strcpy(name_pool, "$accept");
1506 strcpy(name_pool+8, "$end");
1507 t = name_pool + 13;
1508 for (bp = first_symbol; bp; bp = bp->next)
1510 p = t;
1511 s = bp->name;
1512 while (*t++ = *s++) continue;
1513 FREE(bp->name);
1514 bp->name = p;
1519 check_symbols()
1521 register bucket *bp;
1523 if (goal->class == UNKNOWN)
1524 undefined_goal(goal->name);
1526 for (bp = first_symbol; bp; bp = bp->next)
1528 if (bp->class == UNKNOWN)
1530 undefined_symbol_warning(bp->name);
1531 bp->class = TERM;
1537 pack_symbols()
1539 register bucket *bp;
1540 register bucket **v;
1541 register int i, j, k, n;
1543 nsyms = 2;
1544 ntokens = 1;
1545 for (bp = first_symbol; bp; bp = bp->next)
1547 ++nsyms;
1548 if (bp->class == TERM) ++ntokens;
1550 start_symbol = ntokens;
1551 nvars = nsyms - ntokens;
1553 symbol_name = (char **) MALLOC(nsyms*sizeof(char *));
1554 if (symbol_name == 0) no_space();
1555 symbol_value = (short *) MALLOC(nsyms*sizeof(short));
1556 if (symbol_value == 0) no_space();
1557 symbol_prec = (short *) MALLOC(nsyms*sizeof(short));
1558 if (symbol_prec == 0) no_space();
1559 symbol_assoc = MALLOC(nsyms);
1560 if (symbol_assoc == 0) no_space();
1562 v = (bucket **) MALLOC(nsyms*sizeof(bucket *));
1563 if (v == 0) no_space();
1565 v[0] = 0;
1566 v[start_symbol] = 0;
1568 i = 1;
1569 j = start_symbol + 1;
1570 for (bp = first_symbol; bp; bp = bp->next)
1572 if (bp->class == TERM)
1573 v[i++] = bp;
1574 else
1575 v[j++] = bp;
1577 assert(i == ntokens && j == nsyms);
1579 for (i = 1; i < ntokens; ++i)
1580 v[i]->index = i;
1582 goal->index = start_symbol + 1;
1583 k = start_symbol + 2;
1584 while (++i < nsyms)
1585 if (v[i] != goal)
1587 v[i]->index = k;
1588 ++k;
1591 goal->value = 0;
1592 k = 1;
1593 for (i = start_symbol + 1; i < nsyms; ++i)
1595 if (v[i] != goal)
1597 v[i]->value = k;
1598 ++k;
1602 k = 0;
1603 for (i = 1; i < ntokens; ++i)
1605 n = v[i]->value;
1606 if (n > 256)
1608 for (j = k++; j > 0 && symbol_value[j-1] > n; --j)
1609 symbol_value[j] = symbol_value[j-1];
1610 symbol_value[j] = n;
1614 if (v[1]->value == UNDEFINED)
1615 v[1]->value = 256;
1617 j = 0;
1618 n = 257;
1619 for (i = 2; i < ntokens; ++i)
1621 if (v[i]->value == UNDEFINED)
1623 while (j < k && n == symbol_value[j])
1625 while (++j < k && n == symbol_value[j]) continue;
1626 ++n;
1628 v[i]->value = n;
1629 ++n;
1633 symbol_name[0] = name_pool + 8;
1634 symbol_value[0] = 0;
1635 symbol_prec[0] = 0;
1636 symbol_assoc[0] = TOKEN;
1637 for (i = 1; i < ntokens; ++i)
1639 symbol_name[i] = v[i]->name;
1640 symbol_value[i] = v[i]->value;
1641 symbol_prec[i] = v[i]->prec;
1642 symbol_assoc[i] = v[i]->assoc;
1644 symbol_name[start_symbol] = name_pool;
1645 symbol_value[start_symbol] = -1;
1646 symbol_prec[start_symbol] = 0;
1647 symbol_assoc[start_symbol] = TOKEN;
1648 for (++i; i < nsyms; ++i)
1650 k = v[i]->index;
1651 symbol_name[k] = v[i]->name;
1652 symbol_value[k] = v[i]->value;
1653 symbol_prec[k] = v[i]->prec;
1654 symbol_assoc[k] = v[i]->assoc;
1657 FREE(v);
1661 pack_grammar()
1663 register int i, j;
1664 int assoc, prec;
1666 ritem = (short *) MALLOC(nitems*sizeof(short));
1667 if (ritem == 0) no_space();
1668 rlhs = (short *) MALLOC(nrules*sizeof(short));
1669 if (rlhs == 0) no_space();
1670 rrhs = (short *) MALLOC((nrules+1)*sizeof(short));
1671 if (rrhs == 0) no_space();
1672 rprec = (short *) REALLOC(rprec, nrules*sizeof(short));
1673 if (rprec == 0) no_space();
1674 rassoc = REALLOC(rassoc, nrules);
1675 if (rassoc == 0) no_space();
1677 ritem[0] = -1;
1678 ritem[1] = goal->index;
1679 ritem[2] = 0;
1680 ritem[3] = -2;
1681 rlhs[0] = 0;
1682 rlhs[1] = 0;
1683 rlhs[2] = start_symbol;
1684 rrhs[0] = 0;
1685 rrhs[1] = 0;
1686 rrhs[2] = 1;
1688 j = 4;
1689 for (i = 3; i < nrules; ++i)
1691 rlhs[i] = plhs[i]->index;
1692 rrhs[i] = j;
1693 assoc = TOKEN;
1694 prec = 0;
1695 while (pitem[j])
1697 ritem[j] = pitem[j]->index;
1698 if (pitem[j]->class == TERM)
1700 prec = pitem[j]->prec;
1701 assoc = pitem[j]->assoc;
1703 ++j;
1705 ritem[j] = -i;
1706 ++j;
1707 if (rprec[i] == UNDEFINED)
1709 rprec[i] = prec;
1710 rassoc[i] = assoc;
1713 rrhs[i] = j;
1715 FREE(plhs);
1716 FREE(pitem);
1720 print_grammar()
1722 register int i, j, k;
1723 int spacing;
1724 register FILE *f = verbose_file;
1726 if (!vflag) return;
1728 k = 1;
1729 for (i = 2; i < nrules; ++i)
1731 if (rlhs[i] != rlhs[i-1])
1733 if (i != 2) fprintf(f, "\n");
1734 fprintf(f, "%4d %s :", i - 2, symbol_name[rlhs[i]]);
1735 spacing = strlen(symbol_name[rlhs[i]]) + 1;
1737 else
1739 fprintf(f, "%4d ", i - 2);
1740 j = spacing;
1741 while (--j >= 0) putc(' ', f);
1742 putc('|', f);
1745 while (ritem[k] >= 0)
1747 fprintf(f, " %s", symbol_name[ritem[k]]);
1748 ++k;
1750 ++k;
1751 putc('\n', f);
1756 reader()
1758 write_section(banner);
1759 create_symbol_table();
1760 read_declarations();
1761 read_grammar();
1762 free_symbol_table();
1763 free_tags();
1764 pack_names();
1765 check_symbols();
1766 pack_symbols();
1767 pack_grammar();
1768 free_symbols();
1769 print_grammar();