* 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.
This commit is contained in:
Akim Demaille
2000-10-16 18:10:46 +00:00
parent f282676b7e
commit 1e9798d569
5 changed files with 128 additions and 99 deletions

View File

@@ -1,3 +1,15 @@
2000-10-16 Akim Demaille <akim@epita.fr>
* 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 <akim@epita.fr> 2000-10-16 Akim Demaille <akim@epita.fr>
* src/lex.c (read_typename): New function. * src/lex.c (read_typename): New function.

View File

@@ -126,6 +126,7 @@ static int high;
static inline void static inline void
output_short_table (FILE *out, output_short_table (FILE *out,
const char *comment,
const char *table_name, const char *table_name,
short *short_table, short *short_table,
short first_value, short first_value,
@@ -133,16 +134,20 @@ output_short_table (FILE *out,
{ {
int i, j; 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++) for (i = begin; i < end; i++)
{ {
putc (',', out); putc (',', out);
if (j >= 10) if (j >= 10)
{ {
putc ('\n', out); fputs ("\n ", out);
j = 1; j = 1;
} }
else else
@@ -295,42 +300,31 @@ output_token_translations (void)
static void static void
output_gram (void) output_gram (void)
{ {
int j;
short *sp;
/* With the ordinary parser, /* With the ordinary parser,
yyprhs and yyrhs are needed only for yydebug. */ yyprhs and yyrhs are needed only for yydebug. */
/* With the no_parser option, all tables are generated */ /* With the no_parser option, all tables are generated */
if (!semantic_parser && !no_parser_flag) if (!semantic_parser && !no_parser_flag)
fprintf (ftable, "\n#if YYDEBUG != 0\n"); fprintf (ftable, "\n#if YYDEBUG != 0\n");
output_short_table (ftable, "yyprhs", rrhs, output_short_table (ftable, NULL, "yyprhs", rrhs,
0, 1, nrules + 1); 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++)
for (sp = ritem + 1; *sp; sp++) ++yyrhs_size;
{ yyrhs = XMALLOC (short, yyrhs_size);
putc (',', ftable);
if (j >= 10) for (sp = ritem + 1, i = 1; *sp; ++sp, ++i)
{ yyrhs[i] = *sp > 0 ? *sp : 0;
putc ('\n', ftable);
j = 1;
}
else
{
j++;
}
if (*sp > 0) output_short_table (ftable, NULL, "yyrhs", yyrhs,
fprintf (ftable, "%6d", *sp); ritem[0], 1, yyrhs_size);
else XFREE (yyrhs);
fprintf (ftable, " 0"); }
}
fprintf (ftable, "\n};\n");
if (!semantic_parser && !no_parser_flag) if (!semantic_parser && !no_parser_flag)
fprintf (ftable, "\n#endif\n"); fprintf (ftable, "\n#endif\n");
@@ -340,7 +334,7 @@ output_gram (void)
static void static void
output_stos (void) output_stos (void)
{ {
output_short_table (ftable, "yystos", accessing_symbol, output_short_table (ftable, NULL, "yystos", accessing_symbol,
0, 1, nstates); 0, 1, nstates);
} }
@@ -350,13 +344,15 @@ output_rule_data (void)
{ {
int i; int i;
int j; int j;
short *short_tab = NULL;
fputs ("\n\ fputs ("\n\
#if YYDEBUG != 0\n\ #if YYDEBUG != 0\n",
/* YYRLINE[yyn]: source line where rule number YYN was defined. */\n",
ftable); 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); 0, 1, nrules + 1);
fputs ("#endif\n\n", ftable); fputs ("#endif\n\n", ftable);
@@ -370,71 +366,74 @@ output_rule_data (void)
fprintf (ftable, "#define YYMAXUTOK %d\n\n", max_user_token_number); 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. */ /* 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, fprintf (ftable,
"static const char * const yytname[] = { \"%s\"", tags[0]); "static const char *const yytname[] =\n{\n ");
j = strlen (tags[0]) + 44; j = 0;
for (i = 1; i < nsyms; i++) for (i = 0; i < nsyms; i++)
/* this used to be i<=nsyms, but that output a final "" symbol /* this used to be i<=nsyms, but that output a final "" symbol
almost by accident */ almost by accident */
{ {
/* Width of the next token, including the two quotes, the coma
and the space. */
int strsize = 4;
char *p; 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); fputs ("\n ", ftable);
j = 0; j = 2;
} }
putc ('\"', ftable); putc ('\"', ftable);
j++;
for (p = tags[i]; p && *p; p++) for (p = tags[i]; p && *p; p++)
{ {
if (*p == '"' || *p == '\\') if (*p == '"' || *p == '\\')
{ {
fprintf (ftable, "\\%c", *p); fprintf (ftable, "\\%c", *p);
j += 2;
} }
else if (*p == '\n') else if (*p == '\n')
{ {
fprintf (ftable, "\\n"); fprintf (ftable, "\\n");
j += 2;
} }
else if (*p == '\t') else if (*p == '\t')
{ {
fprintf (ftable, "\\t"); fprintf (ftable, "\\t");
j += 2;
} }
else if (*p == '\b') else if (*p == '\b')
{ {
fprintf (ftable, "\\b"); fprintf (ftable, "\\b");
j += 2;
} }
else if (*p < 040 || *p >= 0177) else if (*p < 040 || *p >= 0177)
{ {
fprintf (ftable, "\\%03o", *p); fprintf (ftable, "\\%03o", *p);
j += 4;
} }
else else
{ {
putc (*p, ftable); putc (*p, ftable);
j++;
} }
} }
putc ('\"', ftable); fputs ("\", ", ftable);
j++; j += strsize;
} }
/* add a NULL entry to list of tokens */ /* 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) if (!token_table_flag && !no_parser_flag)
fprintf (ftable, "#endif\n\n"); fprintf (ftable, "#endif\n\n");
@@ -442,47 +441,34 @@ output_rule_data (void)
/* Output YYTOKNUM. */ /* Output YYTOKNUM. */
if (token_table_flag) 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); 0, 1, ntokens + 1);
} }
/* Output YYR1. */ /* Output YYR1. */
fputs ("\ output_short_table (ftable,
/* YYR1[YYN]: Symbol number of symbol that rule YYN derives. */\n", ftable); "YYR1[YYN] -- Symbol number of symbol that rule YYN derives",
"yyr1", rlhs,
output_short_table (ftable, "yyr1", rlhs,
0, 1, nrules + 1); 0, 1, nrules + 1);
XFREE (rlhs + 1); XFREE (rlhs + 1);
putc ('\n', ftable); putc ('\n', ftable);
/* Output YYR2. */ /* Output YYR2. */
fputs ("\ short_tab = XMALLOC (short, nrules + 1);
/* YYR2[YYN]: Number of symbols composing right hand side of rule YYN. */\n\
static const short yyr2[] = { 0", ftable);
j = 10;
for (i = 1; i < nrules; i++) for (i = 1; i < nrules; i++)
{ short_tab[i] = rrhs[i + 1] - rrhs[i] - 1;
putc (',', ftable); 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);
if (j >= 10) XFREE (short_tab);
{
putc ('\n', ftable);
j = 1;
}
else
{
j++;
}
fprintf (ftable, "%6d", rrhs[i + 1] - rrhs[i] - 1);
}
putc (',', ftable);
if (j >= 10)
putc ('\n', ftable);
fprintf (ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1);
XFREE (rrhs + 1); XFREE (rrhs + 1);
} }
@@ -738,7 +724,7 @@ token_actions (void)
} }
XFREE (actrow); XFREE (actrow);
output_short_table (ftable, "yydefact", yydefact, output_short_table (ftable, NULL, "yydefact", yydefact,
yydefact[0], 1, nstates); yydefact[0], 1, nstates);
XFREE (yydefact); XFREE (yydefact);
} }
@@ -1095,12 +1081,12 @@ pack_table (void)
static void static void
output_base (void) output_base (void)
{ {
output_short_table (ftable, "yypact", base, output_short_table (ftable, NULL, "yypact", base,
base[0], 1, nstates); base[0], 1, nstates);
putc ('\n', ftable); putc ('\n', ftable);
output_short_table (ftable, "yypgoto", base, output_short_table (ftable, NULL, "yypgoto", base,
base[nstates], nstates + 1, nvectors); base[nstates], nstates + 1, nvectors);
XFREE (base); XFREE (base);
@@ -1111,7 +1097,7 @@ static void
output_table (void) output_table (void)
{ {
fprintf (ftable, "\n\n#define\tYYLAST\t\t%d\n\n\n", high); 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); table[0], 1, high + 1);
XFREE (table); XFREE (table);
} }
@@ -1120,7 +1106,7 @@ output_table (void)
static void static void
output_check (void) output_check (void)
{ {
output_short_table (ftable, "yycheck", check, output_short_table (ftable, NULL, "yycheck", check,
check[0], 1, high + 1); check[0], 1, high + 1);
XFREE (check); XFREE (check);
} }

View File

@@ -462,12 +462,18 @@ parse_token_decl (symbol_class what_is, symbol_class what_is_not)
{ {
int token = 0; int token = 0;
char *typename = 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 (;;) for (;;)
{ {
int tmp_char = ungetc (skip_white_space (), finput); 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 == '%') if (tmp_char == '%')
return; return;
if (tmp_char == EOF) if (tmp_char == EOF)

View File

@@ -43,6 +43,31 @@ hash (const char *key)
return k % TABSIZE; 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 void
tabinit (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 * bucket *
getsym (const char *key) getsym (const char *key)
{ {
@@ -75,13 +105,7 @@ getsym (const char *key)
if (found == 0) if (found == 0)
{ {
nsyms++; bp = bucket_new (key, hashval);
bp = XCALLOC (bucket, 1);
bp->link = symtab[hashval];
bp->next = NULL;
bp->tag = xstrdup (key);
bp->class = unknown_sym;
if (firstsymbol == NULL) if (firstsymbol == NULL)
{ {

View File

@@ -37,6 +37,7 @@ typedef enum
typedef struct bucket typedef struct bucket
{ {
/* Needed for the hash table. */
struct bucket *link; struct bucket *link;
struct bucket *next; struct bucket *next;
@@ -48,10 +49,10 @@ typedef struct bucket
short prec; short prec;
associativity assoc; associativity assoc;
short user_token_number; short user_token_number;
/* special value SALIAS in the identifier half of the /* Points to the other in the identifier-symbol pair for an
identifier-symbol pair for an alias */ alias. Special value SALIAS in the identifier half of the
identifier-symbol pair for an alias. */
struct bucket *alias; struct bucket *alias;
/* points to the other in the identifier-symbol pair for an alias */
symbol_class class; symbol_class class;
} }
bucket; bucket;