(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>
+
+ * 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>
* src/lex.c (read_typename): New function.
2000-10-16 Akim Demaille <akim@epita.fr>
* src/lex.c (read_typename): New function.
static inline void
output_short_table (FILE *out,
static inline void
output_short_table (FILE *out,
const char *table_name,
short *short_table,
short first_value,
const char *table_name,
short *short_table,
short first_value,
- fprintf (out, "static const short %s[] = {%6d", table_name, first_value);
+ if (comment)
+ fprintf (out, "/* %s. */\n", comment);
+ 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)
{
for (i = begin; i < end; i++)
{
putc (',', out);
if (j >= 10)
{
static void
output_gram (void)
{
static void
output_gram (void)
{
/* 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");
/* 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,
- 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");
if (!semantic_parser && !no_parser_flag)
fprintf (ftable, "\n#endif\n");
static void
output_stos (void)
{
static void
output_stos (void)
{
- output_short_table (ftable, "yystos", accessing_symbol,
+ output_short_table (ftable, NULL, "yystos", accessing_symbol,
+ short *short_tab = NULL;
-#if YYDEBUG != 0\n\
-/* YYRLINE[yyn]: source line where rule number YYN was defined. */\n",
- 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);
0, 1, nrules + 1);
fputs ("#endif\n\n", ftable);
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);
- "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 */
{
/* 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;
- putc (',', ftable);
- j++;
+ 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;
for (p = tags[i]; p && *p; p++)
{
if (*p == '"' || *p == '\\')
{
fprintf (ftable, "\\%c", *p);
for (p = tags[i]; p && *p; p++)
{
if (*p == '"' || *p == '\\')
{
fprintf (ftable, "\\%c", *p);
}
else if (*p == '\n')
{
fprintf (ftable, "\\n");
}
else if (*p == '\n')
{
fprintf (ftable, "\\n");
}
else if (*p == '\t')
{
fprintf (ftable, "\\t");
}
else if (*p == '\t')
{
fprintf (ftable, "\\t");
}
else if (*p == '\b')
{
fprintf (ftable, "\\b");
}
else if (*p == '\b')
{
fprintf (ftable, "\\b");
}
else if (*p < 040 || *p >= 0177)
{
fprintf (ftable, "\\%03o", *p);
}
else if (*p < 040 || *p >= 0177)
{
fprintf (ftable, "\\%03o", *p);
}
else
{
putc (*p, ftable);
}
else
{
putc (*p, ftable);
- putc ('\"', ftable);
- j++;
+ fputs ("\", ", ftable);
+ 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)
fprintf (ftable, "#endif\n\n");
if (!token_table_flag && !no_parser_flag)
fprintf (ftable, "#endif\n\n");
/* Output YYTOKNUM. */
if (token_table_flag)
{
/* 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. */
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. */
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++)
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);
- fprintf (ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1);
- output_short_table (ftable, "yydefact", yydefact,
+ output_short_table (ftable, NULL, "yydefact", yydefact,
yydefact[0], 1, nstates);
XFREE (yydefact);
}
yydefact[0], 1, nstates);
XFREE (yydefact);
}
static void
output_base (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);
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);
base[nstates], nstates + 1, nvectors);
XFREE (base);
output_table (void)
{
fprintf (ftable, "\n\n#define\tYYLAST\t\t%d\n\n\n", high);
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);
}
table[0], 1, high + 1);
XFREE (table);
}
static void
output_check (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);
}
check[0], 1, high + 1);
XFREE (check);
}
{
int token = 0;
char *typename = 0;
{
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 (;;)
{
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)
if (tmp_char == '%')
return;
if (tmp_char == EOF)
+/*--------------------------------------------------------------.
+| 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;
+}
+
+/*----------------------------------------------------------------.
+| Find the symbol named KEY, and return it. If it does not exist |
+| yet, create it. |
+`----------------------------------------------------------------*/
+
bucket *
getsym (const char *key)
{
bucket *
getsym (const char *key)
{
- 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)
{
if (firstsymbol == NULL)
{
+ /* Needed for the hash table. */
struct bucket *link;
struct bucket *next;
struct bucket *link;
struct bucket *next;
short prec;
associativity assoc;
short user_token_number;
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. */
- /* points to the other in the identifier-symbol pair for an alias */
symbol_class class;
}
bucket;
symbol_class class;
}
bucket;