1 /* $Id: reader.c,v 1.74 2017/12/04 17:50:02 tom Exp $ */
5 /* The line size must be a positive integer. One hundred was chosen */
6 /* because few lines in Yacc input grammars exceed 100 characters. */
7 /* Note that if a line exceeds LINESIZE characters, the line buffer */
8 /* will be expanded to accomodate it. */
19 /* the maximum number of arguments (inherited attributes) to a non-terminal */
20 /* this is a hard limit, but seems more than adequate */
23 static void start_rule(bucket *bp, int s_lineno);
25 static void copy_initial_action(void);
26 static void copy_destructor(void);
27 static char *process_destructor_XX(char *code, char *tag);
30 #define CACHE_SIZE 256
32 static int cinc, cache_size;
35 static int tagmax, havetags;
36 static char **tag_table;
46 static char last_was_action;
48 static int trialaction;
52 static bucket **pitem;
57 static size_t name_pool_size;
58 static char *name_pool;
60 char line_format[] = "#line %d \"%s\"\n";
66 int destructor = 0; /* =1 if at least one %destructor */
68 static bucket *default_destructor[3] =
71 #define UNTYPED_DEFAULT 0
72 #define TYPED_DEFAULT 1
73 #define TYPE_SPECIFIED 2
76 lookup_type_destructor(char *tag)
78 const char fmt[] = "%.*s destructor";
79 char name[1024] = "\0";
80 bucket *bp, **bpp = &default_destructor[TYPE_SPECIFIED];
82 while ((bp = *bpp) != NULL)
89 sprintf(name, fmt, (int)(sizeof(name) - sizeof(fmt)), tag);
90 *bpp = bp = make_bucket(name);
95 #endif /* defined(YYBTYACC) */
101 if (cinc >= cache_size)
103 cache_size += CACHE_SIZE;
104 cache = TREALLOC(char, cache, cache_size);
107 cache[cinc] = (char)c;
126 * Expect this pattern:
127 * /^[[:space:]]*#[[:space:]]*
130 * ([[:space:]]*|[[:space:]]+"[^"]+")/
135 #define UNLESS(what) if (what) { ld = ldERR; break; }
140 LINE_DIR ld = ldSPC1;
141 for (n = 0; (ld <= ldOK) && (line[n] != '\0'); ++n)
143 int ch = UCH(line[n]);
147 if (isspace(UCH(ch)))
156 if (isspace(UCH(ch)))
162 UNLESS(strncmp(line + n, "line", 4));
170 UNLESS(!isspace(UCH(line[n])));
174 if (isspace(UCH(ch)))
179 UNLESS(!isdigit(UCH(ch)));
184 if (isdigit(UCH(ch)))
189 UNLESS(!isspace(UCH(ch)));
193 if (isspace(UCH(ch)))
199 UNLESS(line[n + 1] == '"');
219 size_t need = (size_t) (name_end - name_1st);
220 if ((long)need > (long)input_file_name_len)
222 input_file_name_len = ((need + 1) * 3) / 2;
223 input_file_name = TREALLOC(char, input_file_name, input_file_name_len);
224 NO_SPACE(input_file_name);
228 memcpy(input_file_name, line + name_1st + 1, need - 1);
229 input_file_name[need - 1] = '\0';
233 input_file_name[0] = '\0';
237 if (ld >= ldNUM && ld < ldERR)
241 lineno = (int)strtol(line + line_1st, NULL, 10) - 1;
256 FILE *f = input_file;
262 if (saw_eof || (c = getc(f)) == EOF)
274 if (line == NULL || linesize != (LINESIZE + 1))
278 linesize = LINESIZE + 1;
279 line = TMALLOC(char, linesize);
290 if ((i + 3) >= linesize)
292 linesize += LINESIZE;
293 line = TREALLOC(char, line, linesize);
306 while (line_directive());
321 p = TMALLOC(char, s - line + 1);
326 while ((*t++ = *s++) != '\n')
337 a.a_line = dup_line();
338 a.a_cptr = a.a_line + (cptr - line);
343 if (*s == '*' && s[1] == '/')
353 unterminated_comment(&a);
386 else if (s[1] == '/')
411 switch (ch = next_inline())
438 static struct keyword
444 { "binary", NONASSOC },
445 { "debug", XXXDEBUG },
446 #if defined(YYBTYACC)
447 { "destructor", DESTRUCTOR },
449 { "error-verbose",ERROR_VERBOSE },
450 { "expect", EXPECT },
451 { "expect-rr", EXPECT_RR },
453 #if defined(YYBTYACC)
454 { "initial-action", INITIAL_ACTION },
457 { "lex-param", LEX_PARAM },
458 #if defined(YYBTYACC)
459 { "locations", LOCATIONS },
461 { "nonassoc", NONASSOC },
462 { "parse-param", PARSE_PARAM },
463 { "pure-parser", PURE_PARSER },
468 { "token-table", TOKEN_TABLE },
471 { "yacc", POSIX_YACC },
476 compare_keys(const void *a, const void *b)
478 const struct keyword *p = (const struct keyword *)a;
479 const struct keyword *q = (const struct keyword *)b;
480 return strcmp(p->name, q->name);
502 else if (isdigit(UCH(c))
511 /* treat keywords spelled with '_' as if it were '-' */
522 if ((key = bsearch(cache, keywords,
523 sizeof(keywords) / sizeof(*key),
524 sizeof(*key), compare_keys)))
532 if (c == '%' || c == '\\')
543 syntax_error(lineno, line, t_cptr);
551 FILE *f = output_file;
557 syntax_error(lineno, line, cptr);
559 fprintf(f, "#ident \"");
579 copy_string(int quote)
581 struct mstring *temp = msnew();
585 a.a_line = dup_line();
586 a.a_cptr = a.a_line + (cptr - line - 1);
598 unterminated_string(&a);
607 unterminated_string(&a);
616 struct mstring *temp = msnew();
623 while ((c = *++cptr) != '\n')
626 if (c == '*' && cptr[1] == '/')
636 a.a_line = dup_line();
637 a.a_cptr = a.a_line + (cptr - line - 1);
645 if (c == '*' && *cptr == '/')
656 unterminated_comment(&a);
668 int need_newline = 0;
671 a.a_line = dup_line();
672 a.a_cptr = a.a_line + (cptr - line - 2);
678 unterminated_text(&a);
681 fprintf(f, line_format, lineno, input_file_name);
693 unterminated_text(&a);
699 char *s = copy_string(c);
709 char *s = copy_comment();
736 puts_both(const char *s)
740 fputs(s, union_file);
758 a.a_line = dup_line();
759 a.a_cptr = a.a_line + (cptr - line - 6);
762 over_unionized(cptr - 6);
765 puts_both("#ifdef YYSTYPE\n");
766 puts_both("#undef YYSTYPE_IS_DECLARED\n");
767 puts_both("#define YYSTYPE_IS_DECLARED 1\n");
768 puts_both("#endif\n");
769 puts_both("#ifndef YYSTYPE_IS_DECLARED\n");
770 puts_both("#define YYSTYPE_IS_DECLARED 1\n");
773 fprintf(text_file, line_format, lineno, input_file_name);
774 puts_both("typedef union");
785 unterminated_union(&a);
795 puts_both(" YYSTYPE;\n");
796 puts_both("#endif /* !YYSTYPE_IS_DECLARED */\n");
805 char *s = copy_string(c);
813 char *s = copy_comment();
825 after_blanks(char *s)
827 while (*s != '\0' && isspace(UCH(*s)))
833 * Trim leading/trailing blanks, and collapse multiple embedded blanks to a
834 * single space. Return index to last character in the buffer.
837 trim_blanks(char *buffer)
842 char *s = after_blanks(d);
844 while ((*d++ = *s++) != '\0')
850 while ((--d != buffer) && isspace(UCH(*d)))
853 for (s = d = buffer; (*d++ = *s++) != '\0';)
855 if (isspace(UCH(*s)))
858 while (isspace(UCH(*s)))
867 return (int)strlen(buffer) - 1;
871 * Scan forward in the current line-buffer looking for a right-curly bracket.
873 * Parameters begin with a left-curly bracket, and continue until there are no
874 * more interesting characters after the last right-curly bracket on the
875 * current line. Bison documents parameters as separated like this:
876 * {type param1} {type2 param2}
877 * but also accepts commas (although some versions of bison mishandle this)
878 * {type param1, type2 param2}
888 switch (next_inline())
907 save_param(int k, char *buffer, int name, int type2)
911 p = TMALLOC(param, 1);
914 p->type2 = strdup(buffer + type2);
916 buffer[type2] = '\0';
917 (void)trim_blanks(p->type2);
919 p->name = strdup(buffer + name);
922 (void)trim_blanks(p->name);
924 p->type = strdup(buffer);
926 (void)trim_blanks(p->type);
950 * Keep a linked list of parameters. This may be multi-line, if the trailing
951 * right-curly bracket is absent.
962 int st_lineno = lineno;
996 if ((curly == 1) && (cptr == line))
1006 if (curly == 0 && !isspace(UCH(c)))
1014 buf_size = (size_t) linesize;
1015 buf = TMALLOC(char, buf_size);
1023 buf_size += (size_t) linesize;
1024 buf = TREALLOC(char, buf, buf_size);
1029 if ((state == 2) && (c == L_CURL))
1033 else if ((state == 2) && isspace(UCH(c)))
1037 else if ((c != L_CURL) && (c != R_CURL))
1044 while (curly < 2 || more_curly());
1057 (void)trim_blanks(buf);
1062 char *parms = (comma + 1);
1063 comma = strchr(parms, ',');
1067 (void)trim_blanks(parms);
1068 i = (int)strlen(parms) - 1;
1074 if (parms[i] == ']')
1077 while (i >= 0 && level > 0 && parms[i] != '[')
1079 if (parms[i] == ']')
1081 else if (parms[i] == '[')
1094 while (i > 0 && (isalnum(UCH(parms[i])) || UCH(parms[i]) == '_'))
1097 if (!isspace(UCH(parms[i])) && parms[i] != '*')
1102 save_param(k, parms, name, type2);
1110 syntax_error(lineno, line, cptr);
1116 if (c >= '0' && c <= '9')
1118 if (c >= 'A' && c <= 'F')
1119 return (c - 'A' + 10);
1120 if (c >= 'a' && c <= 'f')
1121 return (c - 'a' + 10);
1134 a.a_lineno = lineno;
1135 a.a_line = dup_line();
1136 a.a_cptr = a.a_line + (cptr - line);
1146 unterminated_string(&a);
1149 char *c_cptr = cptr - 1;
1157 unterminated_string(&a);
1172 n = (n << 3) + (c - '0');
1176 n = (n << 3) + (c - '0');
1181 illegal_character(c_cptr);
1188 if (n < 0 || n >= 16)
1189 illegal_character(c_cptr);
1194 if (i < 0 || i >= 16)
1199 illegal_character(c_cptr);
1232 s = TMALLOC(char, n);
1235 for (i = 0; i < n; ++i)
1244 for (i = 0; i < n; ++i)
1247 if (c == '\\' || c == cache[0])
1252 else if (isprint(UCH(c)))
1281 cachec(((c >> 6) & 7) + '0');
1282 cachec(((c >> 3) & 7) + '0');
1283 cachec((c & 7) + '0');
1297 if (n == 1 && bp->value == UNDEFINED)
1298 bp->value = UCH(*s);
1305 is_reserved(char *name)
1309 if (strcmp(name, ".") == 0 ||
1310 strcmp(name, "$accept") == 0 ||
1311 strcmp(name, "$end") == 0)
1314 if (name[0] == '$' && name[1] == '$' && isdigit(UCH(name[2])))
1317 while (isdigit(UCH(*s)))
1332 for (c = *cptr; IS_IDENT(c); c = *++cptr)
1336 if (is_reserved(cache))
1337 used_reserved(cache);
1339 return (lookup(cache));
1350 for (c = *cptr; isdigit(UCH(c)); c = *++cptr)
1352 n = (10 * n + (c - '0'));
1355 syntax_error(lineno, line, base);
1360 return (Value_t)(n);
1364 cache_tag(char *tag, size_t len)
1369 for (i = 0; i < ntags; ++i)
1371 if (strncmp(tag, tag_table[i], len) == 0 &&
1372 tag_table[i][len] == NUL)
1373 return (tag_table[i]);
1376 if (ntags >= tagmax)
1381 ? TREALLOC(char *, tag_table, tagmax)
1382 : TMALLOC(char *, tagmax));
1383 NO_SPACE(tag_table);
1386 s = TMALLOC(char, len + 1);
1389 strncpy(s, tag, len);
1391 tag_table[ntags++] = s;
1399 int t_lineno = lineno;
1400 char *t_line = dup_line();
1401 char *t_cptr = t_line + (cptr - line);
1408 illegal_tag(t_lineno, t_line, t_cptr);
1416 while (IS_IDENT(c));
1423 illegal_tag(t_lineno, t_line, t_cptr);
1428 return cache_tag(cache, (size_t) cinc);
1431 #if defined(YYBTYACC)
1437 while (IS_NAME2(UCH(*cptr)))
1439 return cache_tag(b, (size_t) (cptr - b));
1444 declare_tokens(int assoc)
1467 if (isalpha(UCH(c)) || c == '_' || c == '.' || c == '$')
1469 else if (c == '\'' || c == '"')
1475 tokenized_start(bp->name);
1480 if (bp->tag && tag != bp->tag)
1481 retyped_warning(bp->name);
1487 if (bp->prec && prec != bp->prec)
1488 reprec_warning(bp->name);
1489 bp->assoc = (Assoc_t)assoc;
1497 if (isdigit(UCH(c)))
1499 value = get_number();
1500 if (bp->value != UNDEFINED && value != bp->value)
1501 revalued_warning(bp->name);
1511 * %expect requires special handling
1512 * as it really isn't part of the yacc
1513 * grammar only a flag for yacc proper.
1516 declare_expect(int assoc)
1520 if (assoc != EXPECT && assoc != EXPECT_RR)
1524 * Stay away from nextc - doesn't
1525 * detect EOL and will read to EOF.
1533 if (isdigit(UCH(c)))
1535 if (assoc == EXPECT)
1536 SRexpect = get_number();
1538 RRexpect = get_number();
1542 * Looking for number before EOL.
1543 * Spaces, tabs, and numbers are ok,
1544 * words, punc., etc. are syntax errors.
1546 else if (c == '\n' || isalpha(UCH(c)) || !isspace(UCH(c)))
1548 syntax_error(lineno, line, cptr);
1559 #if defined(YYBTYACC)
1561 declare_argtypes(bucket *bp)
1563 char *tags[MAXARGS];
1567 retyped_warning(bp->name);
1568 cptr++; /* skip open paren */
1575 syntax_error(lineno, line, cptr);
1576 tags[args++] = get_tag();
1583 cptr++; /* skip close paren */
1585 bp->argnames = TMALLOC(char *, args);
1586 NO_SPACE(bp->argnames);
1587 bp->argtags = CALLOC(sizeof(char *), args + 1);
1588 NO_SPACE(bp->argtags);
1591 bp->argtags[args] = tags[args];
1592 bp->argnames[args] = NULL;
1615 if (isalpha(UCH(c)) || c == '_' || c == '.' || c == '$')
1618 #if defined(YYBTYACC)
1619 if (nextc() == L_PAREN)
1620 declare_argtypes(bp);
1625 else if (c == '\'' || c == '"')
1628 #if defined(YYBTYACC)
1637 if (bp->tag && tag != bp->tag)
1638 retyped_warning(bp->name);
1653 if (!isalpha(UCH(c)) && c != '_' && c != '.' && c != '$')
1654 syntax_error(lineno, line, cptr);
1656 if (bp->class == TERM)
1657 terminal_start(bp->name);
1658 if (goal && goal != bp)
1659 restarted_warning();
1664 read_declarations(void)
1668 cache_size = CACHE_SIZE;
1669 cache = TMALLOC(char, cache_size);
1678 syntax_error(lineno, line, cptr);
1679 switch (k = keyword())
1733 #if defined(YYBTYACC)
1742 case INITIAL_ACTION:
1743 copy_initial_action();
1752 /* noop for bison compatibility. byacc is already designed to be posix
1753 * yacc compatible. */
1760 initialize_grammar(void)
1765 pitem = TMALLOC(bucket *, maxitems);
1776 plhs = TMALLOC(bucket *, maxrules);
1783 rprec = TMALLOC(Value_t, maxrules);
1790 rassoc = TMALLOC(Assoc_t, maxrules);
1802 pitem = TREALLOC(bucket *, pitem, maxitems);
1811 plhs = TREALLOC(bucket *, plhs, maxrules);
1814 rprec = TREALLOC(Value_t, rprec, maxrules);
1817 rassoc = TREALLOC(Assoc_t, rassoc, maxrules);
1821 /* set immediately prior to where copy_args() could be called, and incremented by
1822 the various routines that will rescan the argument list as appropriate */
1823 static int rescan_lineno;
1824 #if defined(YYBTYACC)
1827 copy_args(int *alen)
1829 struct mstring *s = msnew();
1830 int depth = 0, len = 1;
1834 a.a_lineno = lineno;
1835 a.a_line = dup_line();
1836 a.a_cptr = a.a_line + (cptr - line - 1);
1838 while ((c = *cptr++) != R_PAREN || depth || quote)
1840 if (c == ',' && !quote && !depth)
1853 unterminated_string(&a);
1855 unterminated_arglist(&a);
1872 else if (c == R_PAREN)
1874 else if (c == '\"' || c == '\'')
1885 parse_id(char *p, char **save)
1889 while (isspace(UCH(*p)))
1892 if (!isalpha(UCH(*p)) && *p != '_')
1895 while (IS_NAME2(UCH(*p)))
1899 *save = cache_tag(b, (size_t) (p - b));
1905 parse_int(char *p, int *save)
1907 int neg = 0, val = 0;
1909 while (isspace(UCH(*p)))
1917 if (!isdigit(UCH(*p)))
1919 while (isdigit(UCH(*p)))
1920 val = val * 10 + *p++ - '0';
1929 parse_arginfo(bucket *a, char *args, int argslen)
1931 char *p = args, *tmp;
1936 if (a->args != argslen)
1937 arg_number_disagree_warning(rescan_lineno, a->name);
1942 if ((a->args = argslen) == 0)
1944 a->argnames = TMALLOC(char *, argslen);
1945 NO_SPACE(a->argnames);
1946 a->argtags = TMALLOC(char *, argslen);
1947 NO_SPACE(a->argtags);
1951 for (i = 0; i < argslen; i++)
1953 while (isspace(UCH(*p)))
1958 while (isspace(UCH(*p)))
1964 if (!(p = parse_id(p + 1, &tmp)))
1966 while (isspace(UCH(*p)))
1973 if (a->argtags[i] != tmp)
1974 arg_type_disagree_warning(rescan_lineno, i + 1, a->name);
1977 a->argtags[i] = tmp;
1980 a->argtags[i] = NULL;
1981 if (!(p = parse_id(p, &a->argnames[i])))
1983 while (isspace(UCH(*p)))
1993 compile_arg(char **theptr, char *yyvaltag)
1996 struct mstring *c = msnew();
1998 Value_t *offsets = NULL, maxoffset;
2003 for (i = nitems - 1; pitem[i]; --i)
2006 if (pitem[i]->class != ARGUMENT)
2011 offsets = TMALLOC(Value_t, maxoffset + 1);
2014 for (j = 0, i++; i < nitems; i++)
2015 if (pitem[i]->class != ARGUMENT)
2016 offsets[++j] = (Value_t)(i - nitems + 1);
2018 rhs = pitem + nitems - 1;
2021 msprintf(c, "yyval.%s = ", yyvaltag);
2023 msprintf(c, "yyval = ");
2030 if (!(p = parse_id(++p, &tag)) || *p++ != '>')
2031 illegal_tag(rescan_lineno, NULL, NULL);
2032 if (isdigit(UCH(*p)) || *p == '-')
2035 if (!(p = parse_int(p, &val)))
2036 dollar_error(rescan_lineno, NULL, NULL);
2039 else if (val > maxoffset)
2041 dollar_warning(rescan_lineno, val);
2042 i = val - maxoffset;
2044 else if (maxoffset > 0)
2047 if (!tag && !(tag = rhs[i]->tag) && havetags)
2048 untyped_rhs(val, rhs[i]->name);
2050 msprintf(c, "yystack.l_mark[%d]", i);
2052 msprintf(c, ".%s", tag);
2056 else if (isalpha(UCH(*p)) || *p == '_')
2059 if (!(p = parse_id(p, &arg)))
2060 dollar_error(rescan_lineno, NULL, NULL);
2061 for (i = plhs[nrules]->args - 1; i >= 0; i--)
2062 if (arg == plhs[nrules]->argnames[i])
2065 unknown_arg_warning(rescan_lineno, "$", arg, NULL, NULL);
2067 tag = plhs[nrules]->argtags[i];
2068 msprintf(c, "yystack.l_mark[%d]",
2069 i - plhs[nrules]->args + 1 - n);
2071 msprintf(c, ".%s", tag);
2073 untyped_arg_warning(rescan_lineno, "$", arg);
2076 dollar_error(rescan_lineno, NULL, NULL);
2080 at_error(rescan_lineno, NULL, NULL);
2096 can_elide_arg(char **theptr, char *yyvaltag)
2101 Value_t *offsets = NULL, maxoffset = 0;
2109 if (!(p = parse_id(++p, &tag)) || *p++ != '>')
2112 for (i = nitems - 1; pitem[i]; --i)
2115 if (pitem[i]->class != ARGUMENT)
2120 offsets = TMALLOC(Value_t, maxoffset + 1);
2123 for (j = 0, i++; i < nitems; i++)
2124 if (pitem[i]->class != ARGUMENT)
2125 offsets[++j] = (Value_t)(i - nitems + 1);
2127 rhs = pitem + nitems - 1;
2129 if (isdigit(UCH(*p)) || *p == '-')
2132 if (!(p = parse_int(p, &val)))
2138 else if (val > maxoffset)
2149 else if (isalpha(UCH(*p)) || *p == '_')
2152 if (!(p = parse_id(p, &arg)))
2154 for (i = plhs[nrules]->args - 1; i >= 0; i--)
2155 if (arg == plhs[nrules]->argnames[i])
2160 tag = plhs[nrules]->argtags[i];
2161 rv = plhs[nrules]->args + n - i;
2164 if (tag && yyvaltag)
2166 if (strcmp(tag, yyvaltag))
2169 else if (tag || yyvaltag)
2179 #define ARG_CACHE_SIZE 1024
2180 static struct arg_cache
2182 struct arg_cache *next;
2186 *arg_cache[ARG_CACHE_SIZE];
2189 lookup_arg_cache(char *code)
2191 struct arg_cache *entry;
2193 entry = arg_cache[strnshash(code) % ARG_CACHE_SIZE];
2196 if (!strnscmp(entry->code, code))
2198 entry = entry->next;
2204 insert_arg_cache(char *code, int rule)
2206 struct arg_cache *entry = NEW(struct arg_cache);
2210 i = strnshash(code) % ARG_CACHE_SIZE;
2213 entry->next = arg_cache[i];
2214 arg_cache[i] = entry;
2218 clean_arg_cache(void)
2220 struct arg_cache *e, *t;
2223 for (i = 0; i < ARG_CACHE_SIZE; i++)
2225 for (e = arg_cache[i]; (t = e); e = e->next, FREE(t))
2227 arg_cache[i] = NULL;
2230 #endif /* defined(YYBTYACC) */
2233 advance_to_start(void)
2239 #if defined(YYBTYACC)
2264 syntax_error(lineno, line, s_cptr);
2269 if (!isalpha(UCH(c)) && c != '_' && c != '.' && c != '_')
2270 syntax_error(lineno, line, cptr);
2274 if (bp->class == TERM)
2275 terminal_start(bp->name);
2283 rescan_lineno = lineno; /* line# for possible inherited args rescan */
2284 #if defined(YYBTYACC)
2288 args = copy_args(&argslen);
2294 syntax_error(lineno, line, cptr);
2295 start_rule(bp, s_lineno);
2296 #if defined(YYBTYACC)
2297 parse_arginfo(bp, args, argslen);
2303 start_rule(bucket *bp, int s_lineno)
2305 if (bp->class == TERM)
2306 terminal_lhs(s_lineno);
2307 bp->class = NONTERM;
2310 if (nrules >= maxrules)
2313 rprec[nrules] = UNDEFINED;
2314 rassoc[nrules] = TOKEN;
2322 if (!last_was_action && plhs[nrules]->tag)
2324 if (pitem[nitems - 1])
2326 for (i = nitems - 1; (i > 0) && pitem[i]; --i)
2328 if (pitem[i + 1] == 0 || pitem[i + 1]->tag != plhs[nrules]->tag)
2329 default_action_warning(plhs[nrules]->name);
2332 default_action_warning(plhs[nrules]->name);
2335 last_was_action = 0;
2336 if (nitems >= maxitems)
2344 insert_empty_rule(void)
2349 assert(cache_size >= CACHE_SIZE);
2350 sprintf(cache, "$$%d", ++gensym);
2351 bp = make_bucket(cache);
2352 last_symbol->next = bp;
2354 bp->tag = plhs[nrules]->tag;
2356 #if defined(YYBTYACC)
2360 nitems = (Value_t)(nitems + 2);
2361 if (nitems > maxitems)
2363 bpp = pitem + nitems - 1;
2365 while ((bpp[0] = bpp[-1]) != 0)
2368 if (++nrules >= maxrules)
2370 plhs[nrules] = plhs[nrules - 1];
2371 plhs[nrules - 1] = bp;
2372 rprec[nrules] = rprec[nrules - 1];
2373 rprec[nrules - 1] = 0;
2374 rassoc[nrules] = rassoc[nrules - 1];
2375 rassoc[nrules - 1] = TOKEN;
2378 #if defined(YYBTYACC)
2380 insert_arg_rule(char *arg, char *tag)
2382 int line_number = rescan_lineno;
2383 char *code = compile_arg(&arg, tag);
2384 int rule = lookup_arg_cache(code);
2385 FILE *f = action_file;
2390 insert_arg_cache(code, rule);
2391 trialaction = 1; /* arg rules always run in trial mode */
2392 fprintf(f, "case %d:\n", rule - 2);
2394 fprintf(f, line_format, line_number, input_file_name);
2395 fprintf(f, "%s;\n", code);
2396 fprintf(f, "break;\n");
2397 insert_empty_rule();
2398 plhs[rule]->tag = cache_tag(tag, strlen(tag));
2399 plhs[rule]->class = ARGUMENT;
2403 if (++nitems > maxitems)
2405 pitem[nitems - 1] = plhs[rule];
2417 int s_lineno = lineno;
2418 #if defined(YYBTYACC)
2424 if (c == '\'' || c == '"')
2430 rescan_lineno = lineno; /* line# for possible inherited args rescan */
2431 #if defined(YYBTYACC)
2435 args = copy_args(&argslen);
2443 start_rule(bp, s_lineno);
2444 #if defined(YYBTYACC)
2445 parse_arginfo(bp, args, argslen);
2451 if (last_was_action)
2452 insert_empty_rule();
2453 last_was_action = 0;
2455 #if defined(YYBTYACC)
2458 if (argslen == 0 && bp->args > 0 && pitem[nitems - 1] == NULL)
2461 if (plhs[nrules]->args != bp->args)
2462 wrong_number_args_warning("default ", bp->name);
2463 for (i = bp->args - 1; i >= 0; i--)
2464 if (plhs[nrules]->argtags[i] != bp->argtags[i])
2465 wrong_type_for_arg_warning(i + 1, bp->name);
2467 else if (bp->args != argslen)
2468 wrong_number_args_warning("", bp->name);
2473 int elide_cnt = can_elide_arg(&ap, bp->argtags[0]);
2475 if (elide_cnt > argslen)
2479 for (i = 1; i < elide_cnt; i++)
2480 if (can_elide_arg(&ap, bp->argtags[i]) != elide_cnt - i)
2488 assert(i == elide_cnt);
2495 for (; i < argslen; i++)
2496 ap = insert_arg_rule(ap, bp->argtags[i]);
2499 #endif /* defined(YYBTYACC) */
2501 if (++nitems > maxitems)
2503 pitem[nitems - 1] = bp;
2512 #if defined(YYBTYACC)
2516 FILE *f = action_file;
2518 Value_t *offsets = NULL, maxoffset;
2521 a.a_lineno = lineno;
2522 a.a_line = dup_line();
2523 a.a_cptr = a.a_line + (cptr - line);
2525 if (last_was_action)
2526 insert_empty_rule();
2527 last_was_action = 1;
2528 #if defined(YYBTYACC)
2529 trialaction = (*cptr == L_BRAC);
2532 fprintf(f, "case %d:\n", nrules - 2);
2533 #if defined(YYBTYACC)
2537 fprintf(f, " if (!yytrial)\n");
2541 fprintf(f, line_format, lineno, input_file_name);
2545 /* avoid putting curly-braces in first column, to ease editing */
2546 if (*after_blanks(cptr) == L_CURL)
2549 cptr = after_blanks(cptr);
2554 for (i = nitems - 1; pitem[i]; --i)
2557 if (pitem[i]->class != ARGUMENT)
2562 offsets = TMALLOC(Value_t, maxoffset + 1);
2565 for (j = 0, i++; i < nitems; i++)
2567 if (pitem[i]->class != ARGUMENT)
2569 offsets[++j] = (Value_t)(i - nitems + 1);
2573 rhs = pitem + nitems - 1;
2582 int d_lineno = lineno;
2583 char *d_line = dup_line();
2584 char *d_cptr = d_line + (cptr - line);
2591 fprintf(f, "yyval.%s", tag);
2596 else if (isdigit(UCH(c)))
2600 fprintf(f, "yystack.l_mark[%d].%s", -n, tag);
2601 else if (i > maxoffset)
2603 dollar_warning(d_lineno, i);
2604 fprintf(f, "yystack.l_mark[%d].%s", i - maxoffset, tag);
2607 fprintf(f, "yystack.l_mark[%d].%s", offsets[i], tag);
2611 else if (c == '-' && isdigit(UCH(cptr[1])))
2614 i = -get_number() - n;
2615 fprintf(f, "yystack.l_mark[%d].%s", i, tag);
2619 #if defined(YYBTYACC)
2620 else if (isalpha(UCH(c)) || c == '_')
2622 char *arg = scan_id();
2623 for (i = plhs[nrules]->args - 1; i >= 0; i--)
2624 if (arg == plhs[nrules]->argnames[i])
2627 unknown_arg_warning(d_lineno, "$", arg, d_line, d_cptr);
2628 fprintf(f, "yystack.l_mark[%d].%s",
2629 i - plhs[nrules]->args + 1 - n, tag);
2635 dollar_error(d_lineno, d_line, d_cptr);
2637 else if (cptr[1] == '$')
2641 tag = plhs[nrules]->tag;
2644 fprintf(f, "yyval.%s", tag);
2647 fprintf(f, "yyval");
2649 #if defined(YYBTYACC)
2654 else if (isdigit(UCH(cptr[1])))
2658 if (havetags && offsets)
2660 if (i <= 0 || i > maxoffset)
2662 tag = rhs[offsets[i]]->tag;
2664 untyped_rhs(i, rhs[offsets[i]]->name);
2665 fprintf(f, "yystack.l_mark[%d].%s", offsets[i], tag);
2670 fprintf(f, "yystack.l_mark[%d]", -n);
2671 else if (i > maxoffset)
2673 dollar_warning(lineno, i);
2674 fprintf(f, "yystack.l_mark[%d]", i - maxoffset);
2677 fprintf(f, "yystack.l_mark[%d]", offsets[i]);
2681 else if (cptr[1] == '-')
2687 fprintf(f, "yystack.l_mark[%d]", -i - n);
2690 #if defined(YYBTYACC)
2691 else if (isalpha(UCH(cptr[1])) || cptr[1] == '_')
2696 for (i = plhs[nrules]->args - 1; i >= 0; i--)
2697 if (arg == plhs[nrules]->argnames[i])
2700 unknown_arg_warning(lineno, "$", arg, line, cptr);
2701 tag = (i < 0 ? NULL : plhs[nrules]->argtags[i]);
2702 fprintf(f, "yystack.l_mark[%d]", i - plhs[nrules]->args + 1 - n);
2704 fprintf(f, ".%s", tag);
2706 untyped_arg_warning(lineno, "$", arg);
2711 #if defined(YYBTYACC)
2716 int l_lineno = lineno;
2717 char *l_line = dup_line();
2718 char *l_cptr = l_line + (cptr - line);
2719 syntax_error(l_lineno, l_line, l_cptr);
2723 fprintf(f, "yyloc");
2727 else if (isdigit(UCH(cptr[1])))
2732 fprintf(f, "yystack.p_mark[%d]", -n);
2733 else if (i > maxoffset)
2735 at_warning(lineno, i);
2736 fprintf(f, "yystack.p_mark[%d]", i - maxoffset);
2739 fprintf(f, "yystack.p_mark[%d]", offsets[i]);
2742 else if (cptr[1] == '-')
2746 fprintf(f, "yystack.p_mark[%d]", -i - n);
2758 while (IS_NAME2(c));
2762 #if defined(YYBTYACC)
2765 if (trialaction && c == L_BRAC && depth == 0)
2771 if (trialaction && c == R_BRAC && depth == 1)
2776 if (c == L_BRAC && !haveyyval)
2780 if (c == L_CURL && !haveyyval)
2782 fprintf(f, " if (!yytrial)\n");
2784 fprintf(f, line_format, lineno, input_file_name);
2788 fprintf(f, "\nbreak;\n");
2803 unterminated_action(&a);
2808 fprintf(f, "\nbreak;\n");
2814 #if defined(YYBTYACC)
2833 #if defined(YYBTYACC)
2837 if (c == L_BRAC && !haveyyval)
2842 if (c == L_CURL && !haveyyval)
2844 fprintf(f, " if (!yytrial)\n");
2846 fprintf(f, line_format, lineno, input_file_name);
2851 fprintf(f, "\nbreak;\n");
2860 char *s = copy_string(c);
2868 char *s = copy_comment();
2879 #if defined(YYBTYACC)
2881 get_code(struct ainfo *a, const char *loc)
2886 struct mstring *code_mstr = msnew();
2889 msprintf(code_mstr, line_format, lineno, input_file_name);
2891 cptr = after_blanks(cptr);
2892 if (*cptr == L_CURL)
2893 /* avoid putting curly-braces in first column, to ease editing */
2894 mputc(code_mstr, '\t');
2896 syntax_error(lineno, line, cptr);
2898 a->a_lineno = lineno;
2899 a->a_line = dup_line();
2900 a->a_cptr = a->a_line + (cptr - line);
2909 int d_lineno = lineno;
2910 char *d_line = dup_line();
2911 char *d_cptr = d_line + (cptr - line);
2918 msprintf(code_mstr, "(*val).%s", tag);
2924 dollar_error(d_lineno, d_line, d_cptr);
2926 else if (cptr[1] == '$')
2928 /* process '$$' later; replacement is context dependent */
2929 msprintf(code_mstr, "$$");
2934 if (c == '@' && cptr[1] == '$')
2938 int l_lineno = lineno;
2939 char *l_line = dup_line();
2940 char *l_cptr = l_line + (cptr - line);
2941 syntax_error(l_lineno, l_line, l_cptr);
2943 msprintf(code_mstr, "%s", loc);
2951 mputc(code_mstr, c);
2954 while (IS_NAME2(c));
2958 mputc(code_mstr, c);
2965 unterminated_action(a);
2979 char *s = copy_string(c);
2980 msprintf(code_mstr, "%s", s);
2987 char *s = copy_comment();
2988 msprintf(code_mstr, "%s", s);
2997 return msdone(code_mstr);
3001 copy_initial_action(void)
3005 initial_action = get_code(&a, "yyloc");
3010 copy_destructor(void)
3017 code_text = get_code(&a, "(*loc)");
3027 { /* "no semantic type" default destructor */
3029 if ((bp = default_destructor[UNTYPED_DEFAULT]) == NULL)
3031 static char untyped_default[] = "<>";
3032 bp = make_bucket("untyped default");
3033 bp->tag = untyped_default;
3034 default_destructor[UNTYPED_DEFAULT] = bp;
3036 if (bp->destructor != NULL)
3037 destructor_redeclared_warning(&a);
3039 /* replace "$$" with "(*val)" in destructor code */
3040 bp->destructor = process_destructor_XX(code_text, NULL);
3042 else if (cptr[1] == '*' && cptr[2] == '>')
3043 { /* "no per-symbol or per-type" default destructor */
3045 if ((bp = default_destructor[TYPED_DEFAULT]) == NULL)
3047 static char typed_default[] = "<*>";
3048 bp = make_bucket("typed default");
3049 bp->tag = typed_default;
3050 default_destructor[TYPED_DEFAULT] = bp;
3052 if (bp->destructor != NULL)
3053 destructor_redeclared_warning(&a);
3056 /* postpone re-processing destructor $$s until end of grammar spec */
3057 bp->destructor = TMALLOC(char, strlen(code_text) + 1);
3058 NO_SPACE(bp->destructor);
3059 strcpy(bp->destructor, code_text);
3063 { /* "semantic type" default destructor */
3064 char *tag = get_tag();
3065 bp = lookup_type_destructor(tag);
3066 if (bp->destructor != NULL)
3067 destructor_redeclared_warning(&a);
3069 /* replace "$$" with "(*val).tag" in destructor code */
3070 bp->destructor = process_destructor_XX(code_text, tag);
3073 else if (isalpha(UCH(c)) || c == '_' || c == '.' || c == '$')
3074 { /* "symbol" destructor */
3076 if (bp->destructor != NULL)
3077 destructor_redeclared_warning(&a);
3080 /* postpone re-processing destructor $$s until end of grammar spec */
3081 bp->destructor = TMALLOC(char, strlen(code_text) + 1);
3082 NO_SPACE(bp->destructor);
3083 strcpy(bp->destructor, code_text);
3094 process_destructor_XX(char *code, char *tag)
3099 struct mstring *new_code = msnew();
3100 char *codeptr = code;
3103 loop: /* step thru code */
3105 if (c == '$' && codeptr[1] == '$')
3109 msprintf(new_code, "(*val)");
3111 msprintf(new_code, "(*val).%s", tag);
3121 while (IS_NAME2(c));
3135 return msdone(new_code);
3163 if (c == '*' && *codeptr == '/')
3165 mputc(new_code, '/');
3177 #endif /* defined(YYBTYACC) */
3186 if (c == '%' || c == '\\')
3194 else if ((c == 'p' || c == 'P') &&
3195 ((c = cptr[2]) == 'r' || c == 'R') &&
3196 ((c = cptr[3]) == 'e' || c == 'E') &&
3197 ((c = cptr[4]) == 'c' || c == 'C') &&
3198 ((c = cptr[5], !IS_IDENT(c))))
3201 syntax_error(lineno, line, cptr);
3204 if (isalpha(UCH(c)) || c == '_' || c == '.' || c == '$')
3206 else if (c == '\'' || c == '"')
3210 syntax_error(lineno, line, cptr);
3214 if (rprec[nrules] != UNDEFINED && bp->prec != rprec[nrules])
3217 rprec[nrules] = bp->prec;
3218 rassoc[nrules] = bp->assoc;
3227 initialize_grammar();
3242 #if defined(YYBTYACC)
3243 else if (c == L_CURL || c == '=' || (backtrack && c == L_BRAC))
3245 else if (c == L_CURL || c == '=')
3251 start_rule(plhs[nrules - 1], 0);
3260 syntax_error(lineno, line, cptr);
3263 #if defined(YYBTYACC)
3265 start_requires_args(goal->name);
3277 for (i = 0; i < ntags; ++i)
3279 assert(tag_table[i]);
3291 name_pool_size = 13; /* 13 == sizeof("$end") + sizeof("$accept") */
3292 for (bp = first_symbol; bp; bp = bp->next)
3293 name_pool_size += strlen(bp->name) + 1;
3295 name_pool = TMALLOC(char, name_pool_size);
3296 NO_SPACE(name_pool);
3298 strcpy(name_pool, "$accept");
3299 strcpy(name_pool + 8, "$end");
3301 for (bp = first_symbol; bp; bp = bp->next)
3305 while ((*t++ = *s++) != 0)
3317 if (goal->class == UNKNOWN)
3318 undefined_goal(goal->name);
3320 for (bp = first_symbol; bp; bp = bp->next)
3322 if (bp->class == UNKNOWN)
3324 undefined_symbol_warning(bp->name);
3331 protect_string(char *src, char **des)
3344 if ('\\' == *s || '"' == *s)
3350 *des = d = TMALLOC(char, len);
3356 if ('\\' == *s || '"' == *s)
3370 #if defined(YYBTYACC)
3371 Value_t max_tok_pval;
3376 for (bp = first_symbol; bp; bp = bp->next)
3379 if (bp->class == TERM)
3382 start_symbol = (Value_t)ntokens;
3383 nvars = (Value_t)(nsyms - ntokens);
3385 symbol_name = TMALLOC(char *, nsyms);
3386 NO_SPACE(symbol_name);
3388 symbol_value = TMALLOC(Value_t, nsyms);
3389 NO_SPACE(symbol_value);
3391 symbol_prec = TMALLOC(Value_t, nsyms);
3392 NO_SPACE(symbol_prec);
3394 symbol_assoc = TMALLOC(char, nsyms);
3395 NO_SPACE(symbol_assoc);
3397 #if defined(YYBTYACC)
3398 symbol_pval = TMALLOC(Value_t, nsyms);
3399 NO_SPACE(symbol_pval);
3403 symbol_destructor = CALLOC(sizeof(char *), nsyms);
3404 NO_SPACE(symbol_destructor);
3406 symbol_type_tag = CALLOC(sizeof(char *), nsyms);
3407 NO_SPACE(symbol_type_tag);
3411 v = TMALLOC(bucket *, nsyms);
3415 v[start_symbol] = 0;
3418 j = (Value_t)(start_symbol + 1);
3419 for (bp = first_symbol; bp; bp = bp->next)
3421 if (bp->class == TERM)
3426 assert(i == ntokens && j == nsyms);
3428 for (i = 1; i < ntokens; ++i)
3431 goal->index = (Index_t)(start_symbol + 1);
3432 k = (Value_t)(start_symbol + 2);
3442 for (i = (Value_t)(start_symbol + 1); i < nsyms; ++i)
3452 for (i = 1; i < ntokens; ++i)
3457 for (j = k++; j > 0 && symbol_value[j - 1] > n; --j)
3458 symbol_value[j] = symbol_value[j - 1];
3459 symbol_value[j] = n;
3465 if (v[1]->value == UNDEFINED)
3470 for (i = 2; i < ntokens; ++i)
3472 if (v[i]->value == UNDEFINED)
3474 while (j < k && n == symbol_value[j])
3476 while (++j < k && n == symbol_value[j])
3485 symbol_name[0] = name_pool + 8;
3486 symbol_value[0] = 0;
3488 symbol_assoc[0] = TOKEN;
3489 #if defined(YYBTYACC)
3493 for (i = 1; i < ntokens; ++i)
3495 symbol_name[i] = v[i]->name;
3496 symbol_value[i] = v[i]->value;
3497 symbol_prec[i] = v[i]->prec;
3498 symbol_assoc[i] = v[i]->assoc;
3499 #if defined(YYBTYACC)
3500 symbol_pval[i] = v[i]->value;
3501 if (symbol_pval[i] > max_tok_pval)
3502 max_tok_pval = symbol_pval[i];
3505 symbol_destructor[i] = v[i]->destructor;
3506 symbol_type_tag[i] = v[i]->tag;
3510 symbol_name[start_symbol] = name_pool;
3511 symbol_value[start_symbol] = -1;
3512 symbol_prec[start_symbol] = 0;
3513 symbol_assoc[start_symbol] = TOKEN;
3514 #if defined(YYBTYACC)
3515 symbol_pval[start_symbol] = (Value_t)(max_tok_pval + 1);
3517 for (++i; i < nsyms; ++i)
3520 symbol_name[k] = v[i]->name;
3521 symbol_value[k] = v[i]->value;
3522 symbol_prec[k] = v[i]->prec;
3523 symbol_assoc[k] = v[i]->assoc;
3524 #if defined(YYBTYACC)
3525 symbol_pval[k] = (Value_t)((max_tok_pval + 1) + v[i]->value + 1);
3528 symbol_destructor[k] = v[i]->destructor;
3529 symbol_type_tag[k] = v[i]->tag;
3536 symbol_pname = TMALLOC(char *, nsyms);
3537 NO_SPACE(symbol_pname);
3539 for (i = 0; i < nsyms; ++i)
3540 protect_string(symbol_name[i], &(symbol_pname[i]));
3554 ritem = TMALLOC(Value_t, nitems);
3557 rlhs = TMALLOC(Value_t, nrules);
3560 rrhs = TMALLOC(Value_t, nrules + 1);
3563 rprec = TREALLOC(Value_t, rprec, nrules);
3566 rassoc = TREALLOC(Assoc_t, rassoc, nrules);
3570 ritem[1] = goal->index;
3575 rlhs[2] = start_symbol;
3581 for (i = 3; i < nrules; ++i)
3583 #if defined(YYBTYACC)
3584 if (plhs[i]->args > 0)
3586 if (plhs[i]->argnames)
3588 FREE(plhs[i]->argnames);
3589 plhs[i]->argnames = NULL;
3591 if (plhs[i]->argtags)
3593 FREE(plhs[i]->argtags);
3594 plhs[i]->argtags = NULL;
3597 #endif /* defined(YYBTYACC) */
3598 rlhs[i] = plhs[i]->index;
3604 ritem[j] = pitem[j]->index;
3605 if (pitem[j]->class == TERM)
3607 prec2 = pitem[j]->prec;
3608 assoc = pitem[j]->assoc;
3612 ritem[j] = (Value_t)-i;
3614 if (rprec[i] == UNDEFINED)
3624 #if defined(YYBTYACC)
3633 size_t j, spacing = 0;
3634 FILE *f = verbose_file;
3640 for (i = 2; i < nrules; ++i)
3642 if (rlhs[i] != rlhs[i - 1])
3646 fprintf(f, "%4d %s :", i - 2, symbol_name[rlhs[i]]);
3647 spacing = strlen(symbol_name[rlhs[i]]) + 1;
3651 fprintf(f, "%4d ", i - 2);
3658 while (ritem[k] >= 0)
3660 fprintf(f, " %s", symbol_name[ritem[k]]);
3668 #if defined(YYBTYACC)
3670 finalize_destructors(void)
3676 for (i = 2; i < nsyms; ++i)
3678 tag = symbol_type_tag[i];
3679 if (symbol_destructor[i] == NULL)
3682 { /* use <> destructor, if there is one */
3683 if ((bp = default_destructor[UNTYPED_DEFAULT]) != NULL)
3685 symbol_destructor[i] = TMALLOC(char,
3686 strlen(bp->destructor) + 1);
3687 NO_SPACE(symbol_destructor[i]);
3688 strcpy(symbol_destructor[i], bp->destructor);
3692 { /* use type destructor for this tag, if there is one */
3693 bp = lookup_type_destructor(tag);
3694 if (bp->destructor != NULL)
3696 symbol_destructor[i] = TMALLOC(char,
3697 strlen(bp->destructor) + 1);
3698 NO_SPACE(symbol_destructor[i]);
3699 strcpy(symbol_destructor[i], bp->destructor);
3702 { /* use <*> destructor, if there is one */
3703 if ((bp = default_destructor[TYPED_DEFAULT]) != NULL)
3704 /* replace "$$" with "(*val).tag" in destructor code */
3705 symbol_destructor[i]
3706 = process_destructor_XX(bp->destructor, tag);
3711 { /* replace "$$" with "(*val)[.tag]" in destructor code */
3712 symbol_destructor[i]
3713 = process_destructor_XX(symbol_destructor[i], tag);
3716 /* 'symbol_type_tag[]' elements are freed by 'free_tags()' */
3717 DO_FREE(symbol_type_tag); /* no longer needed */
3718 if ((bp = default_destructor[UNTYPED_DEFAULT]) != NULL)
3721 /* 'bp->tag' is a static value, don't free */
3722 FREE(bp->destructor);
3725 if ((bp = default_destructor[TYPED_DEFAULT]) != NULL)
3728 /* 'bp->tag' is a static value, don't free */
3729 FREE(bp->destructor);
3732 if ((bp = default_destructor[TYPE_SPECIFIED]) != NULL)
3739 /* 'bp->tag' freed by 'free_tags()' */
3740 FREE(bp->destructor);
3745 #endif /* defined(YYBTYACC) */
3750 write_section(code_file, banner);
3751 create_symbol_table();
3752 read_declarations();
3754 free_symbol_table();
3761 #if defined(YYBTYACC)
3763 finalize_destructors();
3770 free_declarations(param *list)
3774 param *next = list->next;
3787 lex_param = free_declarations(lex_param);
3788 parse_param = free_declarations(parse_param);
3798 DO_FREE(symbol_name);
3799 DO_FREE(symbol_prec);
3800 DO_FREE(symbol_assoc);
3801 DO_FREE(symbol_value);
3802 #if defined(YYBTYACC)
3803 DO_FREE(symbol_pval);
3804 DO_FREE(symbol_destructor);
3805 DO_FREE(symbol_type_tag);