1 /* $NetBSD: reader.c,v 1.17 2006/11/24 19:47:00 christos Exp $ */
4 * Copyright (c) 1989 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 #if defined(__RCSID) && !defined(lint)
38 static char sccsid
[] = "@(#)reader.c 5.7 (Berkeley) 1/20/91";
40 __RCSID("$NetBSD: reader.c,v 1.17 2006/11/24 19:47:00 christos Exp $");
46 /* The line size must be a positive integer. One hundred was chosen */
47 /* because few lines in Yacc input grammars exceed 100 characters. */
48 /* Note that if a line exceeds LINESIZE characters, the line buffer */
49 /* will be expanded to accommodate it. */
58 static int cinc
, cache_size
;
61 static char **tag_table
;
68 static char last_was_action
;
71 static bucket
**pitem
;
76 static int name_pool_size
;
77 static char *name_pool
;
79 static void cachec(int);
80 static void get_line(void);
81 static char * dup_line(void);
82 static void skip_comment(void);
83 static int nextc(void);
84 static int keyword(void);
85 static void copy_ident(void);
86 static void copy_text(void);
87 static void copy_union(void);
88 static int hexval(int);
89 static bucket
* get_literal(void);
90 static int is_reserved(char *);
91 static bucket
* get_name(void);
92 static int get_number(void);
93 static char * get_tag(void);
94 static void declare_tokens(int);
95 static void declare_types(void);
96 static void declare_start(void);
97 static void handle_expect(void);
98 static void read_declarations(void);
99 static void initialize_grammar(void);
100 static void expand_items(void);
101 static void expand_rules(void);
102 static void advance_to_start(void);
103 static void start_rule(bucket
*, int);
104 static void end_rule(void);
105 static void insert_empty_rule(void);
106 static void add_symbol(void);
107 static void copy_action(void);
108 static int mark_symbol(void);
109 static void read_grammar(void);
110 static void free_tags(void);
111 static void pack_names(void);
112 static void check_symbols(void);
113 static void pack_symbols(void);
114 static void pack_grammar(void);
115 static void print_grammar(void);
118 static const char line_format
[] = "#line %d \"%s\"\n";
124 if (cinc
>= cache_size
)
127 cache
= REALLOC(cache
, cache_size
);
128 if (cache
== 0) no_space();
137 FILE *f
= input_file
;
141 if (saw_eof
|| (c
= getc(f
)) == EOF
)
143 if (line
) { FREE(line
); line
= 0; }
149 if (line
== 0 || linesize
!= (LINESIZE
+ 1))
151 if (line
) FREE(line
);
152 linesize
= LINESIZE
+ 1;
153 line
= MALLOC(linesize
);
154 if (line
== 0) no_space();
162 if (c
== '\n') { cptr
= line
; return; }
165 linesize
+= LINESIZE
;
166 line
= REALLOC(line
, linesize
);
167 if (line
== 0) no_space();
186 if (line
== 0) return (0);
188 while (*s
!= '\n') ++s
;
189 p
= MALLOC(s
- line
+ 1);
190 if (p
== 0) no_space();
194 while ((*t
++ = *s
++) != '\n') continue;
204 int st_lineno
= lineno
;
205 char *st_line
= dup_line();
206 char *st_cptr
= st_line
+ (cptr
- line
);
211 if (*s
== '*' && s
[1] == '/')
221 unterminated_comment(st_lineno
, st_line
, st_cptr
);
249 if (line
== 0) return (EOF
);
275 else if (s
[1] == '/')
278 if (line
== 0) return (EOF
);
306 if (isupper(c
)) c
= tolower(c
);
309 else if (isdigit(c
) || c
== '_' || c
== '.' || c
== '$')
317 if (strcmp(cache
, "token") == 0 || strcmp(cache
, "term") == 0)
319 if (strcmp(cache
, "type") == 0)
321 if (strcmp(cache
, "left") == 0)
323 if (strcmp(cache
, "right") == 0)
325 if (strcmp(cache
, "nonassoc") == 0 || strcmp(cache
, "binary") == 0)
327 if (strcmp(cache
, "start") == 0)
329 if (strcmp(cache
, "union") == 0)
331 if (strcmp(cache
, "ident") == 0)
333 if (strcmp(cache
, "expect") == 0)
341 if (c
== '%' || c
== '\\')
352 syntax_error(lineno
, line
, t_cptr
);
362 FILE *f
= output_file
;
365 if (c
== EOF
) unexpected_EOF();
366 if (c
!= '"') syntax_error(lineno
, line
, cptr
);
368 fprintf(f
, "#ident \"");
394 int need_newline
= 0;
395 int t_lineno
= lineno
;
396 char *t_line
= dup_line();
397 char *t_cptr
= t_line
+ (cptr
- line
- 2);
403 unterminated_text(t_lineno
, t_line
, t_cptr
);
405 if (!lflag
) fprintf(f
, line_format
, lineno
, input_file_name
);
417 unterminated_text(t_lineno
, t_line
, t_cptr
);
422 int s_lineno
= lineno
;
423 char *s_line
= dup_line();
424 char *s_cptr
= s_line
+ (cptr
- line
- 1);
439 unterminated_string(s_lineno
, s_line
, s_cptr
);
448 unterminated_string(s_lineno
, s_line
, s_cptr
);
461 while ((c
= *++cptr
) != '\n')
463 if (c
== '*' && cptr
[1] == '/')
473 int c_lineno
= lineno
;
474 char *c_line
= dup_line();
475 char *c_cptr
= c_line
+ (cptr
- line
- 1);
483 if (c
== '*' && *cptr
== '/')
494 unterminated_comment(c_lineno
, c_line
, c_cptr
);
505 if (need_newline
) putc('\n', f
);
526 int u_lineno
= lineno
;
527 char *u_line
= dup_line();
528 char *u_cptr
= u_line
+ (cptr
- line
- 6);
530 if (unionized
) over_unionized(cptr
- 6);
534 fprintf(text_file
, line_format
, lineno
, input_file_name
);
536 fprintf(text_file
, "typedef union");
537 if (dflag
) fprintf(union_file
, "typedef union");
543 if (dflag
) putc(c
, union_file
);
549 if (line
== 0) unterminated_union(u_lineno
, u_line
, u_cptr
);
559 fprintf(text_file
, " YYSTYPE;\n");
568 int s_lineno
= lineno
;
569 char *s_line
= dup_line();
570 char *s_cptr
= s_line
+ (cptr
- line
- 1);
577 if (dflag
) putc(c
, union_file
);
584 unterminated_string(s_lineno
, s_line
, s_cptr
);
589 if (dflag
) putc(c
, union_file
);
594 unterminated_string(s_lineno
, s_line
, s_cptr
);
604 putc('*', text_file
);
605 if (dflag
) putc('*', union_file
);
606 while ((c
= *++cptr
) != '\n')
608 if (c
== '*' && cptr
[1] == '/')
610 fprintf(text_file
, "* ");
611 if (dflag
) fprintf(union_file
, "* ");
616 if (dflag
) putc(c
, union_file
);
619 fprintf(text_file
, "*/\n");
620 if (dflag
) fprintf(union_file
, "*/\n");
625 int c_lineno
= lineno
;
626 char *c_line
= dup_line();
627 char *c_cptr
= c_line
+ (cptr
- line
- 1);
629 putc('*', text_file
);
630 if (dflag
) putc('*', union_file
);
636 if (dflag
) putc(c
, union_file
);
637 if (c
== '*' && *cptr
== '/')
639 putc('/', text_file
);
640 if (dflag
) putc('/', union_file
);
649 unterminated_comment(c_lineno
, c_line
, c_cptr
);
664 if (c
>= '0' && c
<= '9')
666 if (c
>= 'A' && c
<= 'F')
667 return (c
- 'A' + 10);
668 if (c
>= 'a' && c
<= 'f')
669 return (c
- 'a' + 10);
682 int s_lineno
= lineno
;
683 char *s_line
= dup_line();
684 char *s_cptr
= s_line
+ (cptr
- line
);
691 if (c
== quote
) break;
692 if (c
== '\n') unterminated_string(s_lineno
, s_line
, s_cptr
);
695 char *c_cptr
= cptr
- 1;
702 if (line
== 0) unterminated_string(s_lineno
, s_line
, s_cptr
);
705 case '0': case '1': case '2': case '3':
706 case '4': case '5': case '6': case '7':
711 n
= (n
<< 3) + (c
- '0');
715 n
= (n
<< 3) + (c
- '0');
719 if (n
> MAXCHAR
) illegal_character(c_cptr
);
726 if (n
< 0 || n
>= 16)
727 illegal_character(c_cptr
);
732 if (i
< 0 || i
>= 16) break;
735 if (n
> MAXCHAR
) illegal_character(c_cptr
);
740 case 'a': c
= 7; break;
741 case 'b': c
= '\b'; break;
742 case 'f': c
= '\f'; break;
743 case 'n': c
= '\n'; break;
744 case 'r': c
= '\r'; break;
745 case 't': c
= '\t'; break;
746 case 'v': c
= '\v'; break;
755 if (s
== 0) no_space();
757 for (i
= 0; i
< n
; ++i
)
766 for (i
= 0; i
< n
; ++i
)
768 c
= ((unsigned char *)s
)[i
];
769 if (c
== '\\' || c
== cache
[0])
781 case 7: cachec('a'); break;
782 case '\b': cachec('b'); break;
783 case '\f': cachec('f'); break;
784 case '\n': cachec('n'); break;
785 case '\r': cachec('r'); break;
786 case '\t': cachec('t'); break;
787 case '\v': cachec('v'); break;
789 cachec(((c
>> 6) & 7) + '0');
790 cachec(((c
>> 3) & 7) + '0');
791 cachec((c
& 7) + '0');
805 if (n
== 1 && bp
->value
== UNDEFINED
)
806 bp
->value
= *(unsigned char *)s
;
814 is_reserved(char *name
)
818 if (strcmp(name
, ".") == 0 ||
819 strcmp(name
, "$accept") == 0 ||
820 strcmp(name
, "$end") == 0)
823 if (name
[0] == '$' && name
[1] == '$' && isdigit((unsigned char)name
[2]))
826 while (isdigit((unsigned char)*s
)) ++s
;
827 if (*s
== NUL
) return (1);
840 for (c
= *cptr
; IS_IDENT(c
); c
= *++cptr
)
844 if (is_reserved(cache
)) used_reserved(cache
);
846 return (lookup(cache
));
857 for (c
= *cptr
; isdigit(c
); c
= *++cptr
)
858 n
= 10*n
+ (c
- '0');
870 int t_lineno
= lineno
;
871 char *t_line
= dup_line();
872 char *t_cptr
= t_line
+ (cptr
- line
);
876 if (c
== EOF
) unexpected_EOF();
877 if (!isalpha(c
) && c
!= '_' && c
!= '$')
878 illegal_tag(t_lineno
, t_line
, t_cptr
);
881 do { cachec(c
); c
= *++cptr
; } while (IS_IDENT(c
));
885 if (c
== EOF
) unexpected_EOF();
887 illegal_tag(t_lineno
, t_line
, t_cptr
);
891 for (i
= 0; i
< ntags
; ++i
)
893 if (strcmp(cache
, tag_table
[i
]) == 0)
894 return (tag_table
[i
]);
900 tag_table
= (char **)
901 (tag_table
? REALLOC(tag_table
, tagmax
*sizeof(char *))
902 : MALLOC(tagmax
*sizeof(char *)));
903 if (tag_table
== 0) no_space();
907 if (s
== 0) no_space();
909 tag_table
[ntags
] = s
;
916 declare_tokens(int assoc
)
924 if (assoc
!= TOKEN
) ++prec
;
927 if (c
== EOF
) unexpected_EOF();
932 if (c
== EOF
) unexpected_EOF();
937 if (isalpha(c
) || c
== '_' || c
== '.' || c
== '$')
939 else if (c
== '\'' || c
== '"')
944 if (bp
== goal
) tokenized_start(bp
->name
);
949 if (bp
->tag
&& tag
!= bp
->tag
)
950 retyped_warning(bp
->name
);
956 if (bp
->prec
&& prec
!= bp
->prec
)
957 reprec_warning(bp
->name
);
963 if (c
== EOF
) unexpected_EOF();
967 value
= get_number();
968 if (bp
->value
!= UNDEFINED
&& value
!= bp
->value
)
969 revalued_warning(bp
->name
);
972 if (c
== EOF
) unexpected_EOF();
986 if (c
== EOF
) unexpected_EOF();
987 if (c
!= '<') syntax_error(lineno
, line
, cptr
);
993 if (isalpha(c
) || c
== '_' || c
== '.' || c
== '$')
995 else if (c
== '\'' || c
== '"')
1000 if (bp
->tag
&& tag
!= bp
->tag
)
1001 retyped_warning(bp
->name
);
1014 if (c
== EOF
) unexpected_EOF();
1015 if (!isalpha(c
) && c
!= '_' && c
!= '.' && c
!= '$')
1016 syntax_error(lineno
, line
, cptr
);
1018 if (bp
->class == TERM
)
1019 terminal_start(bp
->name
);
1020 if (goal
&& goal
!= bp
)
1021 restarted_warning();
1033 if (c
== EOF
) unexpected_EOF();
1035 syntax_error(lineno
, line
, cptr
);
1038 fprintf (stderr
, "%s: Expect 1 shift/reduce conflict.\n", myname
);
1040 fprintf (stderr
, "%s: Expect %d shift/reduce conflicts.\n", myname
, num
);
1045 read_declarations(void)
1050 cache
= MALLOC(cache_size
);
1051 if (cache
== 0) no_space();
1056 if (c
== EOF
) unexpected_EOF();
1057 if (c
!= '%') syntax_error(lineno
, line
, cptr
);
1058 switch (k
= keyword())
1099 initialize_grammar(void)
1103 pitem
= (bucket
**) MALLOC(maxitems
*sizeof(bucket
*));
1104 if (pitem
== 0) no_space();
1112 plhs
= (bucket
**) MALLOC(maxrules
*sizeof(bucket
*));
1113 if (plhs
== 0) no_space();
1117 rprec
= (short *) MALLOC(maxrules
*sizeof(short));
1118 if (rprec
== 0) no_space();
1122 rassoc
= (char *) MALLOC(maxrules
*sizeof(char));
1123 if (rassoc
== 0) no_space();
1134 pitem
= (bucket
**) REALLOC(pitem
, maxitems
*sizeof(bucket
*));
1135 if (pitem
== 0) no_space();
1143 plhs
= (bucket
**) REALLOC(plhs
, maxrules
*sizeof(bucket
*));
1144 if (plhs
== 0) no_space();
1145 rprec
= (short *) REALLOC(rprec
, maxrules
*sizeof(short));
1146 if (rprec
== 0) no_space();
1147 rassoc
= (char *) REALLOC(rassoc
, maxrules
*sizeof(char));
1148 if (rassoc
== 0) no_space();
1153 advance_to_start(void)
1163 if (c
!= '%') break;
1179 syntax_error(lineno
, line
, s_cptr
);
1184 if (!isalpha(c
) && c
!= '_' && c
!= '.' && c
!= '_')
1185 syntax_error(lineno
, line
, cptr
);
1189 if (bp
->class == TERM
)
1190 terminal_start(bp
->name
);
1196 if (c
== EOF
) unexpected_EOF();
1197 if (c
!= ':') syntax_error(lineno
, line
, cptr
);
1198 start_rule(bp
, s_lineno
);
1204 start_rule(bucket
*bp
, int s_lineno
)
1206 if (bp
->class == TERM
)
1207 terminal_lhs(s_lineno
);
1208 bp
->class = NONTERM
;
1209 if (nrules
>= maxrules
)
1212 rprec
[nrules
] = UNDEFINED
;
1213 rassoc
[nrules
] = TOKEN
;
1222 if (!last_was_action
&& plhs
[nrules
]->tag
)
1224 for (i
= nitems
- 1; pitem
[i
]; --i
) continue;
1225 if (pitem
[i
+1] == 0 || pitem
[i
+1]->tag
!= plhs
[nrules
]->tag
)
1226 default_action_warning();
1229 last_was_action
= 0;
1230 if (nitems
>= maxitems
) expand_items();
1238 insert_empty_rule(void)
1243 snprintf(cache
, cache_size
, "$$%d", ++gensym
);
1244 bp
= make_bucket(cache
);
1245 last_symbol
->next
= bp
;
1247 bp
->tag
= plhs
[nrules
]->tag
;
1248 bp
->class = NONTERM
;
1250 if ((nitems
+= 2) > maxitems
)
1252 bpp
= pitem
+ nitems
- 1;
1254 while ((bpp
[0] = bpp
[-1])) --bpp
;
1256 if (++nrules
>= maxrules
)
1258 plhs
[nrules
] = plhs
[nrules
-1];
1259 plhs
[nrules
-1] = bp
;
1260 rprec
[nrules
] = rprec
[nrules
-1];
1261 rprec
[nrules
-1] = 0;
1262 rassoc
[nrules
] = rassoc
[nrules
-1];
1263 rassoc
[nrules
-1] = TOKEN
;
1272 int s_lineno
= lineno
;
1275 if (c
== '\'' || c
== '"')
1284 start_rule(bp
, s_lineno
);
1289 if (last_was_action
)
1290 insert_empty_rule();
1291 last_was_action
= 0;
1293 if (++nitems
> maxitems
)
1295 pitem
[nitems
-1] = bp
;
1307 FILE *f
= action_file
;
1308 int a_lineno
= lineno
;
1309 char *a_line
= dup_line();
1310 char *a_cptr
= a_line
+ (cptr
- line
);
1312 if (last_was_action
)
1313 insert_empty_rule();
1314 last_was_action
= 1;
1316 fprintf(f
, "case %d:\n", nrules
- 2);
1318 fprintf(f
, line_format
, lineno
, input_file_name
);
1319 if (*cptr
== '=') ++cptr
;
1322 for (i
= nitems
- 1; pitem
[i
]; --i
) ++n
;
1331 int d_lineno
= lineno
;
1332 char *d_line
= dup_line();
1333 char *d_cptr
= d_line
+ (cptr
- line
);
1340 fprintf(f
, "yyval.%s", tag
);
1345 else if (isdigit(c
))
1348 if (i
> n
) dollar_warning(d_lineno
, i
);
1349 fprintf(f
, "yyvsp[%d].%s", i
- n
, tag
);
1353 else if (c
== '-' && isdigit((unsigned char)cptr
[1]))
1356 i
= -get_number() - n
;
1357 fprintf(f
, "yyvsp[%d].%s", i
, tag
);
1362 dollar_error(d_lineno
, d_line
, d_cptr
);
1364 else if (cptr
[1] == '$')
1368 tag
= plhs
[nrules
]->tag
;
1369 if (tag
== 0) untyped_lhs();
1370 fprintf(f
, "yyval.%s", tag
);
1373 fprintf(f
, "yyval");
1377 else if (isdigit((unsigned char)cptr
[1]))
1383 if (i
<= 0 || i
> n
)
1385 tag
= pitem
[nitems
+ i
- n
- 1]->tag
;
1386 if (tag
== 0) untyped_rhs(i
, pitem
[nitems
+ i
- n
- 1]->name
);
1387 fprintf(f
, "yyvsp[%d].%s", i
- n
, tag
);
1392 dollar_warning(lineno
, i
);
1393 fprintf(f
, "yyvsp[%d]", i
- n
);
1397 else if (cptr
[1] == '-')
1403 fprintf(f
, "yyvsp[%d]", -i
- n
);
1407 if (isalpha(c
) || c
== '_' || c
== '$')
1413 } while (isalnum(c
) || c
== '_' || c
== '$');
1423 if (line
) goto loop
;
1424 unterminated_action(a_lineno
, a_line
, a_cptr
);
1427 if (depth
> 0) goto loop
;
1428 fprintf(f
, "\nbreak;\n");
1437 if (--depth
> 0) goto loop
;
1438 fprintf(f
, "\nbreak;\n");
1445 int s_lineno
= lineno
;
1446 char *s_line
= dup_line();
1447 char *s_cptr
= s_line
+ (cptr
- line
- 1);
1460 unterminated_string(s_lineno
, s_line
, s_cptr
);
1469 unterminated_string(s_lineno
, s_line
, s_cptr
);
1480 while ((c
= *++cptr
) != '\n')
1482 if (c
== '*' && cptr
[1] == '/')
1492 int c_lineno
= lineno
;
1493 char *c_line
= dup_line();
1494 char *c_cptr
= c_line
+ (cptr
- line
- 1);
1502 if (c
== '*' && *cptr
== '/')
1513 unterminated_comment(c_lineno
, c_line
, c_cptr
);
1534 if (c
== '%' || c
== '\\')
1542 else if ((c
== 'p' || c
== 'P') &&
1543 ((c
= cptr
[2]) == 'r' || c
== 'R') &&
1544 ((c
= cptr
[3]) == 'e' || c
== 'E') &&
1545 ((c
= cptr
[4]) == 'c' || c
== 'C') &&
1546 ((c
= cptr
[5], !IS_IDENT(c
))))
1549 syntax_error(lineno
, line
, cptr
);
1552 if (isalpha(c
) || c
== '_' || c
== '.' || c
== '$')
1554 else if (c
== '\'' || c
== '"')
1558 syntax_error(lineno
, line
, cptr
);
1562 if (rprec
[nrules
] != UNDEFINED
&& bp
->prec
!= rprec
[nrules
])
1565 rprec
[nrules
] = bp
->prec
;
1566 rassoc
[nrules
] = bp
->assoc
;
1576 initialize_grammar();
1582 if (c
== EOF
) break;
1583 if (isalpha(c
) || c
== '_' || c
== '.' || c
== '$' || c
== '\'' ||
1586 else if (c
== '{' || c
== '=')
1591 start_rule(plhs
[nrules
-1], 0);
1596 if (mark_symbol()) break;
1599 syntax_error(lineno
, line
, cptr
);
1610 if (tag_table
== 0) return;
1612 for (i
= 0; i
< ntags
; ++i
)
1614 assert(tag_table
[i
]);
1627 name_pool_size
= 13; /* 13 == sizeof("$end") + sizeof("$accept") */
1628 for (bp
= first_symbol
; bp
; bp
= bp
->next
)
1629 name_pool_size
+= strlen(bp
->name
) + 1;
1630 name_pool
= MALLOC(name_pool_size
);
1631 if (name_pool
== 0) no_space();
1633 strlcpy(name_pool
, "$accept", name_pool_size
);
1634 strlcpy(name_pool
+8, "$end", name_pool_size
- 8);
1636 for (bp
= first_symbol
; bp
; bp
= bp
->next
)
1640 while ((*t
++ = *s
++) != '\0') continue;
1652 if (goal
->class == UNKNOWN
)
1653 undefined_goal(goal
->name
);
1655 for (bp
= first_symbol
; bp
; bp
= bp
->next
)
1657 if (bp
->class == UNKNOWN
)
1659 undefined_symbol_warning(bp
->name
);
1675 for (bp
= first_symbol
; bp
; bp
= bp
->next
)
1678 if (bp
->class == TERM
) ++ntokens
;
1680 start_symbol
= ntokens
;
1681 nvars
= nsyms
- ntokens
;
1683 symbol_name
= (char **) MALLOC(nsyms
*sizeof(char *));
1684 if (symbol_name
== 0) no_space();
1685 symbol_value
= (short *) MALLOC(nsyms
*sizeof(short));
1686 if (symbol_value
== 0) no_space();
1687 symbol_prec
= (short *) MALLOC(nsyms
*sizeof(short));
1688 if (symbol_prec
== 0) no_space();
1689 symbol_assoc
= MALLOC(nsyms
);
1690 if (symbol_assoc
== 0) no_space();
1692 v
= (bucket
**) MALLOC(nsyms
*sizeof(bucket
*));
1693 if (v
== 0) no_space();
1696 v
[start_symbol
] = 0;
1699 j
= start_symbol
+ 1;
1700 for (bp
= first_symbol
; bp
; bp
= bp
->next
)
1702 if (bp
->class == TERM
)
1707 assert(i
== ntokens
&& j
== nsyms
);
1709 for (i
= 1; i
< ntokens
; ++i
)
1712 goal
->index
= start_symbol
+ 1;
1713 k
= start_symbol
+ 2;
1723 for (i
= start_symbol
+ 1; i
< nsyms
; ++i
)
1733 for (i
= 1; i
< ntokens
; ++i
)
1738 for (j
= k
++; j
> 0 && symbol_value
[j
-1] > n
; --j
)
1739 symbol_value
[j
] = symbol_value
[j
-1];
1740 symbol_value
[j
] = n
;
1744 if (v
[1]->value
== UNDEFINED
)
1749 for (i
= 2; i
< ntokens
; ++i
)
1751 if (v
[i
]->value
== UNDEFINED
)
1753 while (j
< k
&& n
== symbol_value
[j
])
1755 while (++j
< k
&& n
== symbol_value
[j
]) continue;
1763 symbol_name
[0] = name_pool
+ 8;
1764 symbol_value
[0] = 0;
1766 symbol_assoc
[0] = TOKEN
;
1767 for (i
= 1; i
< ntokens
; ++i
)
1769 symbol_name
[i
] = v
[i
]->name
;
1770 symbol_value
[i
] = v
[i
]->value
;
1771 symbol_prec
[i
] = v
[i
]->prec
;
1772 symbol_assoc
[i
] = v
[i
]->assoc
;
1774 symbol_name
[start_symbol
] = name_pool
;
1775 symbol_value
[start_symbol
] = -1;
1776 symbol_prec
[start_symbol
] = 0;
1777 symbol_assoc
[start_symbol
] = TOKEN
;
1778 for (++i
; i
< nsyms
; ++i
)
1781 symbol_name
[k
] = v
[i
]->name
;
1782 symbol_value
[k
] = v
[i
]->value
;
1783 symbol_prec
[k
] = v
[i
]->prec
;
1784 symbol_assoc
[k
] = v
[i
]->assoc
;
1797 ritem
= (short *) MALLOC(nitems
*sizeof(short));
1798 if (ritem
== 0) no_space();
1799 rlhs
= (short *) MALLOC(nrules
*sizeof(short));
1800 if (rlhs
== 0) no_space();
1801 rrhs
= (short *) MALLOC((nrules
+1)*sizeof(short));
1802 if (rrhs
== 0) no_space();
1803 rprec
= (short *) REALLOC(rprec
, nrules
*sizeof(short));
1804 if (rprec
== 0) no_space();
1805 rassoc
= REALLOC(rassoc
, nrules
);
1806 if (rassoc
== 0) no_space();
1809 ritem
[1] = goal
->index
;
1814 rlhs
[2] = start_symbol
;
1820 for (i
= 3; i
< nrules
; ++i
)
1822 rlhs
[i
] = plhs
[i
]->index
;
1828 ritem
[j
] = pitem
[j
]->index
;
1829 if (pitem
[j
]->class == TERM
)
1831 prec
= pitem
[j
]->prec
;
1832 assoc
= pitem
[j
]->assoc
;
1838 if (rprec
[i
] == UNDEFINED
)
1856 FILE *f
= verbose_file
;
1863 for (i
= 2; i
< nrules
; ++i
)
1865 if (rlhs
[i
] != rlhs
[i
-1])
1867 if (i
!= 2) fprintf(f
, "\n");
1868 fprintf(f
, "%4d %s :", i
- 2, symbol_name
[rlhs
[i
]]);
1869 spacing
= strlen(symbol_name
[rlhs
[i
]]) + 1;
1873 fprintf(f
, "%4d ", i
- 2);
1875 while (--j
>= 0) putc(' ', f
);
1879 while (ritem
[k
] >= 0)
1881 fprintf(f
, " %s", symbol_name
[ritem
[k
]]);
1893 write_section(banner
);
1894 create_symbol_table();
1895 read_declarations();
1897 free_symbol_table();