From 1e9798d5699ca67718fd3b0d6baa9abcf27e6cd3 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Mon, 16 Oct 2000 18:10:46 +0000 Subject: [PATCH] * src/symtab.c (bucket_new): New function. (getsym): Use it. * src/output.c (output_short_table): New argument to display the comment associated with the table. Adjust dependencies. (output_gram): Use it. (output_rule_data): Nicer output layout for YYTNAME. --- ChangeLog | 12 ++++ src/output.c | 162 +++++++++++++++++++++++---------------------------- src/reader.c | 8 ++- src/symtab.c | 38 +++++++++--- src/symtab.h | 7 ++- 5 files changed, 128 insertions(+), 99 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9fb13c39..e0d44232 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2000-10-16 Akim Demaille + + * src/symtab.c (bucket_new): New function. + (getsym): Use it. + + * src/output.c (output_short_table): New argument to display the + comment associated with the table. + Adjust dependencies. + (output_gram): Use it. + (output_rule_data): Nicer output layout for YYTNAME. + + 2000-10-16 Akim Demaille * src/lex.c (read_typename): New function. diff --git a/src/output.c b/src/output.c index a426ad8f..54ec5c19 100644 --- a/src/output.c +++ b/src/output.c @@ -126,6 +126,7 @@ static int high; static inline void output_short_table (FILE *out, + const char *comment, const char *table_name, short *short_table, short first_value, @@ -133,16 +134,20 @@ output_short_table (FILE *out, { int i, j; - fprintf (out, "static const short %s[] = {%6d", table_name, first_value); + if (comment) + fprintf (out, "/* %s. */\n", comment); - j = 10; + fprintf (out, "static const short %s[] =\n{\n %6d", + table_name, first_value); + + j = 1; for (i = begin; i < end; i++) { putc (',', out); if (j >= 10) { - putc ('\n', out); + fputs ("\n ", out); j = 1; } else @@ -295,42 +300,31 @@ output_token_translations (void) static void output_gram (void) { - int j; - short *sp; - /* With the ordinary parser, yyprhs and yyrhs are needed only for yydebug. */ /* With the no_parser option, all tables are generated */ if (!semantic_parser && !no_parser_flag) fprintf (ftable, "\n#if YYDEBUG != 0\n"); - output_short_table (ftable, "yyprhs", rrhs, + output_short_table (ftable, NULL, "yyprhs", rrhs, 0, 1, nrules + 1); - fprintf (ftable, "\nstatic const short yyrhs[] = {%6d", ritem[0]); + { + size_t yyrhs_size = 1; + short *yyrhs, *sp; + int i; - j = 10; - for (sp = ritem + 1; *sp; sp++) - { - putc (',', ftable); + for (sp = ritem + 1; *sp; sp++) + ++yyrhs_size; + yyrhs = XMALLOC (short, yyrhs_size); - if (j >= 10) - { - putc ('\n', ftable); - j = 1; - } - else - { - j++; - } + for (sp = ritem + 1, i = 1; *sp; ++sp, ++i) + yyrhs[i] = *sp > 0 ? *sp : 0; - if (*sp > 0) - fprintf (ftable, "%6d", *sp); - else - fprintf (ftable, " 0"); - } - - fprintf (ftable, "\n};\n"); + output_short_table (ftable, NULL, "yyrhs", yyrhs, + ritem[0], 1, yyrhs_size); + XFREE (yyrhs); + } if (!semantic_parser && !no_parser_flag) fprintf (ftable, "\n#endif\n"); @@ -340,7 +334,7 @@ output_gram (void) static void output_stos (void) { - output_short_table (ftable, "yystos", accessing_symbol, + output_short_table (ftable, NULL, "yystos", accessing_symbol, 0, 1, nstates); } @@ -350,13 +344,15 @@ output_rule_data (void) { int i; int j; + short *short_tab = NULL; fputs ("\n\ -#if YYDEBUG != 0\n\ -/* YYRLINE[yyn]: source line where rule number YYN was defined. */\n", +#if YYDEBUG != 0\n", ftable); - output_short_table (ftable, "yyrline", rline, + output_short_table (ftable, + "YYRLINE[YYN] -- source line where rule number YYN was defined", + "yyrline", rline, 0, 1, nrules + 1); fputs ("#endif\n\n", ftable); @@ -370,71 +366,74 @@ output_rule_data (void) fprintf (ftable, "#define YYMAXUTOK %d\n\n", max_user_token_number); } - if (!token_table_flag && !no_parser_flag) - fprintf (ftable, "\n#if YYDEBUG != 0 || defined (YYERROR_VERBOSE)\n\n"); - /* Output the table of symbol names. */ - + if (!token_table_flag && !no_parser_flag) + fputs ("\n#if YYDEBUG != 0 || defined YYERROR_VERBOSE\n\n", ftable); + fputs ("\ +/* YYTNAME[TOKEN_NUM] -- String name of the token TOKEN_NUM. */\n", + ftable); fprintf (ftable, - "static const char * const yytname[] = { \"%s\"", tags[0]); + "static const char *const yytname[] =\n{\n "); - j = strlen (tags[0]) + 44; - for (i = 1; i < nsyms; i++) + j = 0; + for (i = 0; i < nsyms; i++) /* this used to be i<=nsyms, but that output a final "" symbol almost by accident */ { + /* Width of the next token, including the two quotes, the coma + and the space. */ + int strsize = 4; char *p; - putc (',', ftable); - j++; - if (j > 75) + for (p = tags[i]; p && *p; p++) + if (*p == '"' || *p == '\\' || *p == '\n' || *p == '\t' + || *p == '\b') + strsize += 2; + else if (*p < 040 || *p >= 0177) + strsize += 4; + else + strsize++; + + if (j + strsize > 75) { - putc ('\n', ftable); - j = 0; + fputs ("\n ", ftable); + j = 2; } putc ('\"', ftable); - j++; - for (p = tags[i]; p && *p; p++) { if (*p == '"' || *p == '\\') { fprintf (ftable, "\\%c", *p); - j += 2; } else if (*p == '\n') { fprintf (ftable, "\\n"); - j += 2; } else if (*p == '\t') { fprintf (ftable, "\\t"); - j += 2; } else if (*p == '\b') { fprintf (ftable, "\\b"); - j += 2; } else if (*p < 040 || *p >= 0177) { fprintf (ftable, "\\%03o", *p); - j += 4; } else { putc (*p, ftable); - j++; } } - putc ('\"', ftable); - j++; + fputs ("\", ", ftable); + j += strsize; } /* add a NULL entry to list of tokens */ - fprintf (ftable, ", NULL\n};\n"); + fprintf (ftable, "NULL\n};\n"); if (!token_table_flag && !no_parser_flag) fprintf (ftable, "#endif\n\n"); @@ -442,47 +441,34 @@ output_rule_data (void) /* Output YYTOKNUM. */ if (token_table_flag) { - output_short_table (ftable, "yytoknum", user_toknums, + output_short_table (ftable, + "YYTOKNUM[YYLEX] -- Index in YYTNAME corresponding to YYLEX", + "yytoknum", user_toknums, 0, 1, ntokens + 1); } /* Output YYR1. */ - fputs ("\ -/* YYR1[YYN]: Symbol number of symbol that rule YYN derives. */\n", ftable); - - output_short_table (ftable, "yyr1", rlhs, + output_short_table (ftable, + "YYR1[YYN] -- Symbol number of symbol that rule YYN derives", + "yyr1", rlhs, 0, 1, nrules + 1); XFREE (rlhs + 1); putc ('\n', ftable); /* Output YYR2. */ - fputs ("\ -/* YYR2[YYN]: Number of symbols composing right hand side of rule YYN. */\n\ -static const short yyr2[] = { 0", ftable); - j = 10; + short_tab = XMALLOC (short, nrules + 1); for (i = 1; i < nrules; i++) - { - putc (',', ftable); - - if (j >= 10) - { - putc ('\n', ftable); - j = 1; - } - else - { - j++; - } - - fprintf (ftable, "%6d", rrhs[i + 1] - rrhs[i] - 1); - } + short_tab[i] = rrhs[i + 1] - rrhs[i] - 1; + short_tab[nrules] = nitems - rrhs[nrules] - 1; + output_short_table (ftable, + "YYR2[YYN] -- Number of symbols composing right hand side of rule YYN", + "yyr2", short_tab, + 0, 1, nrules + 1); + putc ('\n', ftable); - putc (',', ftable); - if (j >= 10) - putc ('\n', ftable); + XFREE (short_tab); - fprintf (ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1); XFREE (rrhs + 1); } @@ -738,7 +724,7 @@ token_actions (void) } XFREE (actrow); - output_short_table (ftable, "yydefact", yydefact, + output_short_table (ftable, NULL, "yydefact", yydefact, yydefact[0], 1, nstates); XFREE (yydefact); } @@ -1095,12 +1081,12 @@ pack_table (void) static void output_base (void) { - output_short_table (ftable, "yypact", base, + output_short_table (ftable, NULL, "yypact", base, base[0], 1, nstates); putc ('\n', ftable); - output_short_table (ftable, "yypgoto", base, + output_short_table (ftable, NULL, "yypgoto", base, base[nstates], nstates + 1, nvectors); XFREE (base); @@ -1111,7 +1097,7 @@ static void output_table (void) { fprintf (ftable, "\n\n#define\tYYLAST\t\t%d\n\n\n", high); - output_short_table (ftable, "yytable", table, + output_short_table (ftable, NULL, "yytable", table, table[0], 1, high + 1); XFREE (table); } @@ -1120,7 +1106,7 @@ output_table (void) static void output_check (void) { - output_short_table (ftable, "yycheck", check, + output_short_table (ftable, NULL, "yycheck", check, check[0], 1, high + 1); XFREE (check); } diff --git a/src/reader.c b/src/reader.c index 0b2f643f..e5aca8c4 100644 --- a/src/reader.c +++ b/src/reader.c @@ -462,12 +462,18 @@ parse_token_decl (symbol_class what_is, symbol_class what_is_not) { int token = 0; char *typename = 0; - struct bucket *symbol = NULL; /* pts to symbol being defined */ + /* The symbol being defined. */ + struct bucket *symbol = NULL; + + /* After `%token' and `%nterm', any number of symbols maybe be + defined. */ for (;;) { int tmp_char = ungetc (skip_white_space (), finput); + /* `%' (for instance from `%token', or from `%%' etc.) is the + only valid means to end this declaration. */ if (tmp_char == '%') return; if (tmp_char == EOF) diff --git a/src/symtab.c b/src/symtab.c index 08361539..5ba5ca25 100644 --- a/src/symtab.c +++ b/src/symtab.c @@ -43,6 +43,31 @@ hash (const char *key) return k % TABSIZE; } +/*--------------------------------------------------------------. +| Create a new symbol, named TAG, which hash value is HASHVAL. | +`--------------------------------------------------------------*/ + +static bucket * +bucket_new (const char *tag, int hashval) +{ + bucket *res = XMALLOC (bucket, 1); + + res->link = symtab[hashval]; + res->next = NULL; + res->tag = xstrdup (tag); + res->type_name = NULL; + res->value = 0; + res->prec = 0; + res->assoc = right_assoc; + res->user_token_number = 0; + res->alias = NULL; + res->class = unknown_sym; + + nsyms++; + + return res; +} + void tabinit (void) @@ -54,6 +79,11 @@ tabinit (void) } +/*----------------------------------------------------------------. +| Find the symbol named KEY, and return it. If it does not exist | +| yet, create it. | +`----------------------------------------------------------------*/ + bucket * getsym (const char *key) { @@ -75,13 +105,7 @@ getsym (const char *key) if (found == 0) { - nsyms++; - - bp = XCALLOC (bucket, 1); - bp->link = symtab[hashval]; - bp->next = NULL; - bp->tag = xstrdup (key); - bp->class = unknown_sym; + bp = bucket_new (key, hashval); if (firstsymbol == NULL) { diff --git a/src/symtab.h b/src/symtab.h index 354fb8fd..8f79abb2 100644 --- a/src/symtab.h +++ b/src/symtab.h @@ -37,6 +37,7 @@ typedef enum typedef struct bucket { + /* Needed for the hash table. */ struct bucket *link; struct bucket *next; @@ -48,10 +49,10 @@ typedef struct bucket short prec; associativity assoc; short user_token_number; - /* special value SALIAS in the identifier half of the - identifier-symbol pair for an alias */ + /* Points to the other in the identifier-symbol pair for an + alias. Special value SALIAS in the identifier half of the + identifier-symbol pair for an alias. */ struct bucket *alias; - /* points to the other in the identifier-symbol pair for an alias */ symbol_class class; } bucket; -- 2.45.2