#include "system.h"
-#include "obstack.h"
#include "quotearg.h"
#include "quote.h"
#include "getargs.h"
struct symbol_list *next;
bucket *sym;
int line;
+
/* The action is attached to the LHS of a rule. */
const char *action;
int action_line;
+
+ /* The guard is attached to the LHS of a rule. */
+ const char *guard;
+ int guard_line;
bucket *ruleprec;
-}
-symbol_list;
+} symbol_list;
int lineno;
char **tags;
res->next = NULL;
res->sym = sym;
res->line = lineno;
+ res->action = NULL;
+ res->action_line = 0;
+ res->guard = NULL;
+ res->guard_line = 0;
res->ruleprec = NULL;
return res;
}
ungetc (c, fin);
n = read_signed_integer (fin);
-
- obstack_fgrow1 (oout, "yylsp[%d]", n - stack_offset);
- locations_flag = 1;
+ if (n > stack_offset)
+ complain (_("invalid value: %s%d"), "@", n);
+ else
+ {
+ /* Offset is always 0 if parser has already popped the stack
+ pointer. */
+ obstack_fgrow1 (oout, "yylsp[%d]",
+ n - (semantic_parser ? 0 : stack_offset));
+ locations_flag = 1;
+ }
}
else
{
ungetc (c, fin);
n = read_signed_integer (fin);
- if (!type_name && n > 0)
- type_name = get_type_name (n, rule);
-
- obstack_fgrow1 (oout, "yyvsp[%d]", n - stack_offset);
-
- if (type_name)
- obstack_fgrow1 (oout, ".%s", type_name);
- if (!type_name && typed)
- complain (_("$%d of `%s' has no declared type"),
- n, rule->sym->tag);
+ if (n > stack_offset)
+ complain (_("invalid value: %s%d"), "$", n);
+ else
+ {
+ if (!type_name && n > 0)
+ type_name = get_type_name (n, rule);
+
+ /* Offset is always 0 if parser has already popped the stack
+ pointer. */
+ obstack_fgrow1 (oout, "yyvsp[%d]",
+ n - (semantic_parser ? 0 : stack_offset));
+
+ if (type_name)
+ obstack_fgrow1 (oout, ".%s", type_name);
+ if (!type_name && typed)
+ complain (_("$%d of `%s' has no declared type"),
+ n, rule->sym->tag);
+ }
}
else
{
ungetc (c, finput);
obstack_1grow (&union_obstack, 0);
muscle_insert ("stype", obstack_finish (&union_obstack));
-
- if (defines_flag)
- obstack_fgrow1 (&defines_obstack, "\
-#ifndef YYSTYPE\n\
-typedef %s
-yystype;\n\
-# define YYSTYPE yystype\n\
-#endif\n",
- muscle_find ("stype"));
}
nsyms--;
}
+
static void
parse_muscle_decl (void)
{
| matching `}' into the actions file. STACK_OFFSET is the number of |
| values in the current rule so far, which says where to find `$0' |
| with respect to the top of the stack. |
+| |
+| This routine is used both for actions and guards. Only |
+| ACTION_OBSTACK is used, but this is fine, since we use only |
+| pointers to relevant portions inside this obstack. |
`-------------------------------------------------------------------*/
static void
-copy_action (symbol_list *rule, int stack_offset)
+parse_braces (symbol_list *rule, int stack_offset)
{
int c;
int count;
- /* offset is always 0 if parser has already popped the stack pointer */
- if (semantic_parser)
- stack_offset = 0;
-
count = 1;
- c = getc (finput);
-
while (count > 0)
{
- while (c != '}')
- {
- switch (c)
- {
- case '\n':
- obstack_1grow (&action_obstack, c);
- lineno++;
- break;
-
- case '{':
- obstack_1grow (&action_obstack, c);
- count++;
- break;
-
- case '\'':
- case '"':
- copy_string (finput, &action_obstack, c);
- break;
+ while ((c = getc (finput)) != '}')
+ switch (c)
+ {
+ case '\n':
+ obstack_1grow (&action_obstack, c);
+ lineno++;
+ break;
- case '/':
- copy_comment (finput, &action_obstack);
- break;
+ case '{':
+ obstack_1grow (&action_obstack, c);
+ count++;
+ break;
- case '$':
- copy_dollar (finput, &action_obstack,
- rule, stack_offset);
- break;
+ case '\'':
+ case '"':
+ copy_string (finput, &action_obstack, c);
+ break;
- case '@':
- copy_at (finput, &action_obstack,
- stack_offset);
- break;
+ case '/':
+ copy_comment (finput, &action_obstack);
+ break;
- case EOF:
- fatal (_("unmatched %s"), "`{'");
+ case '$':
+ copy_dollar (finput, &action_obstack,
+ rule, stack_offset);
+ break;
- default:
- obstack_1grow (&action_obstack, c);
- }
+ case '@':
+ copy_at (finput, &action_obstack,
+ stack_offset);
+ break;
- c = getc (finput);
- }
+ case EOF:
+ fatal (_("unmatched %s"), "`{'");
- /* above loop exits when c is '}' */
+ default:
+ obstack_1grow (&action_obstack, c);
+ }
+ /* Above loop exits when C is '}'. */
if (--count)
{
obstack_1grow (&action_obstack, c);
}
obstack_1grow (&action_obstack, '\0');
- rule->action = obstack_finish (&action_obstack);
- rule->action_line = lineno;
}
-\f
-/*-------------------------------------------------------------------.
-| After `%guard' is seen in the input file, copy the actual guard |
-| into the guards file. If the guard is followed by an action, copy |
-| that into the actions file. STACK_OFFSET is the number of values |
-| in the current rule so far, which says where to find `$0' with |
-| respect to the top of the stack, for the simple parser in which |
-| the stack is not popped until after the guard is run. |
-`-------------------------------------------------------------------*/
+
static void
-copy_guard (symbol_list *rule, int stack_offset)
+parse_action (symbol_list *rule, int stack_offset)
{
- int c;
- int count;
- int brace_flag = 0;
-
- /* offset is always 0 if parser has already popped the stack pointer */
- if (semantic_parser)
- stack_offset = 0;
-
- obstack_fgrow1 (&guard_obstack, "\ncase %d:\n", nrules);
- if (!no_lines_flag)
- obstack_fgrow2 (&guard_obstack, muscle_find ("linef"),
- lineno, quotearg_style (c_quoting_style,
- muscle_find ("filename")));
- obstack_1grow (&guard_obstack, '{');
-
- count = 0;
- c = getc (finput);
-
- while (brace_flag ? (count > 0) : (c != ';'))
- {
- switch (c)
- {
- case '\n':
- obstack_1grow (&guard_obstack, c);
- lineno++;
- break;
-
- case '{':
- obstack_1grow (&guard_obstack, c);
- brace_flag = 1;
- count++;
- break;
-
- case '}':
- obstack_1grow (&guard_obstack, c);
- if (count > 0)
- count--;
- else
- {
- complain (_("unmatched %s"), "`}'");
- c = getc (finput); /* skip it */
- }
- break;
-
- case '\'':
- case '"':
- copy_string (finput, &guard_obstack, c);
- break;
-
- case '/':
- copy_comment (finput, &guard_obstack);
- break;
-
- case '$':
- copy_dollar (finput, &guard_obstack, rule, stack_offset);
- break;
-
- case '@':
- copy_at (finput, &guard_obstack, stack_offset);
- break;
-
- case EOF:
- fatal ("%s", _("unterminated %guard clause"));
-
- default:
- obstack_1grow (&guard_obstack, c);
- }
-
- if (c != '}' || count != 0)
- c = getc (finput);
- }
+ rule->action_line = lineno;
+ parse_braces (rule, stack_offset);
+ rule->action = obstack_finish (&action_obstack);
+}
- c = skip_white_space ();
- obstack_sgrow (&guard_obstack, ";\n break;}");
- if (c == '{')
- copy_action (rule, stack_offset);
- else if (c == '=')
- {
- c = getc (finput); /* why not skip_white_space -wjh */
- if (c == '{')
- copy_action (rule, stack_offset);
- }
- else
- ungetc (c, finput);
+static void
+parse_guard (symbol_list *rule, int stack_offset)
+{
+ token_t t = lex ();
+ if (t != tok_left_curly)
+ complain (_("invalid %s declaration"), "%guard");
+ rule->guard_line = lineno;
+ parse_braces (rule, stack_offset);
+ rule->guard = obstack_finish (&action_obstack);
}
+
\f
/*-------------------------------------------------------------------.
}
else /* handle an action. */
{
- copy_action (crule, rulelength);
+ parse_action (crule, rulelength);
action_flag = 1;
xactions++; /* JF */
}
crule->ruleprec = symval;
t = lex ();
}
+
if (t == tok_guard)
{
if (!semantic_parser)
complain (_("%%guard present but %%semantic_parser not specified"));
- copy_guard (crule, rulelength);
+ parse_guard (crule, rulelength);
t = lex ();
}
- else if (t == tok_left_curly)
+
+ if (t == tok_left_curly)
{
/* This case never occurs -wjh */
if (action_flag)
complain (_("two actions at end of one rule"));
- copy_action (crule, rulelength);
+ parse_action (crule, rulelength);
action_flag = 1;
xactions++; /* -wjh */
t = lex ();
}
\f
-/*--------------------------------------------------------------.
-| For named tokens, but not literal ones, define the name. The |
-| value is the user token number. |
-`--------------------------------------------------------------*/
-
-static void
-output_token_defines (struct obstack *oout)
-{
- bucket *bp;
- char *cp, *symbol;
- char c;
-
- for (bp = firstsymbol; bp; bp = bp->next)
- {
- symbol = bp->tag; /* get symbol */
-
- if (bp->value >= ntokens)
- continue;
- if (bp->user_token_number == SALIAS)
- continue;
- if ('\'' == *symbol)
- continue; /* skip literal character */
- if (bp == errtoken)
- continue; /* skip error token */
- if ('\"' == *symbol)
- {
- /* use literal string only if given a symbol with an alias */
- if (bp->alias)
- symbol = bp->alias->tag;
- else
- continue;
- }
-
- /* Don't #define nonliteral tokens whose names contain periods. */
- cp = symbol;
- while ((c = *cp++) && c != '.');
- if (c != '\0')
- continue;
-
- obstack_fgrow2 (oout, "# define\t%s\t%d\n",
- symbol, bp->user_token_number);
- if (semantic_parser)
- /* FIXME: This is certainly dead wrong, and should be just as
- above. --akim. */
- obstack_fgrow2 (oout, "# define\tT%s\t%d\n", symbol, bp->value);
- }
-}
-
-
/*------------------------------------------------------------------.
| Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
| number. |
}
-/*-----------------------------------.
-| Output definition of token names. |
-`-----------------------------------*/
+/*---------------------------------------------------------------.
+| Save the definition of token names in the `TOKENDEFS' muscle. |
+`---------------------------------------------------------------*/
static void
-symbols_output (void)
+symbols_save (void)
{
- {
- struct obstack tokendefs;
- obstack_init (&tokendefs);
- output_token_defines (&tokendefs);
- obstack_1grow (&tokendefs, 0);
- muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs)));
- obstack_free (&tokendefs, NULL);
- }
-
- if (defines_flag)
+ struct obstack tokendefs;
+ bucket *bp;
+ char *cp, *symbol;
+ char c;
+ obstack_init (&tokendefs);
+
+ for (bp = firstsymbol; bp; bp = bp->next)
{
- output_token_defines (&defines_obstack);
+ symbol = bp->tag; /* get symbol */
- if (!pure_parser)
- obstack_fgrow1 (&defines_obstack, "\nextern YYSTYPE %slval;\n",
- spec_name_prefix);
- if (semantic_parser)
+ if (bp->value >= ntokens)
+ continue;
+ if (bp->user_token_number == SALIAS)
+ continue;
+ if ('\'' == *symbol)
+ continue; /* skip literal character */
+ if (bp == errtoken)
+ continue; /* skip error token */
+ if ('\"' == *symbol)
{
- int i;
-
- for (i = ntokens; i < nsyms; i++)
- {
- /* don't make these for dummy nonterminals made by gensym. */
- if (*tags[i] != '@')
- obstack_fgrow2 (&defines_obstack,
- "# define\tNT%s\t%d\n", tags[i], i);
- }
-#if 0
- /* `fdefines' is now a temporary file, so we need to copy its
- contents in `done', so we can't close it here. */
- fclose (fdefines);
- fdefines = NULL;
-#endif
+ /* use literal string only if given a symbol with an alias */
+ if (bp->alias)
+ symbol = bp->alias->tag;
+ else
+ continue;
}
+
+ /* Don't #define nonliteral tokens whose names contain periods. */
+ cp = symbol;
+ while ((c = *cp++) && c != '.');
+ if (c != '\0')
+ continue;
+
+ obstack_fgrow2 (&tokendefs, "# define %s\t%d\n",
+ symbol, bp->user_token_number);
+ if (semantic_parser)
+ /* FIXME: This is probably wrong, and should be just as
+ above. --akim. */
+ obstack_fgrow2 (&tokendefs, "# define T%s\t%d\n", symbol, bp->value);
}
+
+ obstack_1grow (&tokendefs, 0);
+ muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs)));
+ obstack_free (&tokendefs, NULL);
}
int ruleno;
symbol_list *p;
+ /* We use short to index items. */
+ if (nitems >= MAXSHORT)
+ fatal (_("too many items (max %d)"), MAXSHORT);
+
ritem = XCALLOC (short, nitems + 1);
rule_table = XCALLOC (rule_t, nrules) - 1;
rule_table[ruleno].useful = TRUE;
rule_table[ruleno].action = p->action;
rule_table[ruleno].action_line = p->action_line;
+ rule_table[ruleno].guard = p->guard;
+ rule_table[ruleno].guard_line = p->guard_line;
p = p->next;
while (p && p->sym)
undeftoken->class = token_sym;
undeftoken->user_token_number = 2;
+ /* Initialize the obstacks. */
+ obstack_init (&action_obstack);
+ obstack_init (&attrs_obstack);
+ obstack_init (&output_obstack);
+
+ finput = xfopen (infile, "r");
+
/* Read the declaration section. Copy %{ ... %} groups to
TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
etc. found there. */
/* Some C code is given at the end of the grammar file. */
read_additionnal_code ();
- /* Now we know whether we need the line-number stack. If we do,
- write its type into the .tab.h file.
- This is no longer need with header skeleton. */
+ lex_free ();
+ xfclose (finput);
/* Assign the symbols their symbol numbers. Write #defines for the
token symbols into FDEFINES if requested. */
packsymbols ();
+ /* Save them. */
+ symbols_save ();
+
/* Convert the grammar into the format described in gram.h. */
packgram ();
- /* Output the headers. */
- symbols_output ();
}