- register int t;
- register bucket *lhs;
- register symbol_list *p;
- register symbol_list *p1;
- register bucket *bp;
-
- symbol_list *crule; /* points to first symbol_list of current rule. */
- /* its symbol is the lhs of the rule. */
- symbol_list *crule1; /* points to the symbol_list preceding crule. */
-
- p1 = NULL;
-
- t = lex();
-
- while (t != TWO_PERCENTS && t != ENDFILE)
- {
- if (t == IDENTIFIER || t == BAR)
- {
- register int actionflag = 0;
- int rulelength = 0; /* number of symbols in rhs of this rule so far */
- int xactions = 0; /* JF for error checking */
- bucket *first_rhs = 0;
-
- if (t == IDENTIFIER)
- {
- lhs = symval;
-
- t = lex();
- if (t != COLON)
- fatal("ill-formed rule");
- }
-
- if (nrules == 0)
- {
- if (t == BAR)
- fatal("grammar starts with vertical bar");
-
- if (!start_flag)
- startval = lhs;
- }
-
- /* start a new rule and record its lhs. */
-
- nrules++;
- nitems++;
-
- record_rule_line ();
-
- p = NEW(symbol_list);
- p->sym = lhs;
-
- crule1 = p1;
- if (p1)
- p1->next = p;
- else
- grammar = p;
-
- p1 = p;
- crule = p;
-
- /* mark the rule's lhs as a nonterminal if not already so. */
-
- if (lhs->class == SUNKNOWN)
- {
- lhs->class = SNTERM;
- lhs->value = nvars;
- nvars++;
- }
- else if (lhs->class == STOKEN)
- fatals("rule given for %s, which is a token", lhs->tag);
-
- /* read the rhs of the rule. */
-
- for (;;)
- {
- t = lex();
-
- if (! (t == IDENTIFIER || t == LEFT_CURLY)) break;
-
- /* If next token is an identifier, see if a colon follows it.
- If one does, exit this rule now. */
- if (t == IDENTIFIER)
- {
- register bucket *ssave;
- register int t1;
-
- ssave = symval;
- t1 = lex();
- unlex(t1);
- symval = ssave;
- if (t1 == COLON) break;
-
- if(!first_rhs) /* JF */
- first_rhs = symval;
- /* Not followed by colon =>
- process as part of this rule's rhs. */
- }
-
- /* If we just passed an action, that action was in the middle
- of a rule, so make a dummy rule to reduce it to a
- non-terminal. */
- if (actionflag)
- {
- register bucket *sdummy;
-
- /* Since the action was written out with this rule's */
- /* number, we must write give the new rule this number */
- /* by inserting the new rule before it. */
-
- /* Make a dummy nonterminal, a gensym. */
- sdummy = gensym();
-
- /* Make a new rule, whose body is empty,
- before the current one, so that the action
- just read can belong to it. */
- nrules++;
- nitems++;
- record_rule_line ();
- p = NEW(symbol_list);
- if (crule1)
- crule1->next = p;
- else grammar = p;
- p->sym = sdummy;
- crule1 = NEW(symbol_list);
- p->next = crule1;
- crule1->next = crule;
-
- /* insert the dummy generated by that rule into this rule. */
- nitems++;
- p = NEW(symbol_list);
- p->sym = sdummy;
- p1->next = p;
- p1 = p;
-
- actionflag = 0;
- }
-
- if (t == IDENTIFIER)
- {
- nitems++;
- p = NEW(symbol_list);
- p->sym = symval;
- p1->next = p;
- p1 = p;
- }
- else /* handle an action. */
- {
- copy_action(crule, rulelength);
- actionflag = 1;
- xactions++; /* JF */
- }
- rulelength++;
- }
-
- /* Put an empty link in the list to mark the end of this rule */
- p = NEW(symbol_list);
- p1->next = p;
- p1 = p;
-
- if (t == PREC)
- {
- t = lex();
- crule->ruleprec = symval;
- t = lex();
- }
- if (t == GUARD)
- {
- if (! semantic_parser)
- fatal("%guard present but %semantic_parser not specified");
-
- copy_guard(crule, rulelength);
- t = lex();
- }
- else if (t == LEFT_CURLY)
- {
- if (actionflag) fatal("two actions at end of one rule");
- copy_action(crule, rulelength);
- t = lex();
- }
- /* If $$ is being set in default way,
- warn if any type mismatch. */
- else if (!xactions && first_rhs && lhs->type_name != first_rhs->type_name)
- {
- if (lhs->type_name == 0 || first_rhs->type_name == 0
- || strcmp(lhs->type_name,first_rhs->type_name))
- fprintf(stderr, "%s:%d: warning: type clash ('%s' '%s') on default action\n",
- infile,
- lineno,
- lhs->type_name ? lhs->type_name : "",
- first_rhs->type_name ? first_rhs->type_name : "");
- }
- /* Warn if there is no default for $$ but we need one. */
- else if (!xactions && !first_rhs && lhs->type_name != 0)
- fprintf(stderr,
- "%s:%d: warning: empty rule for typed nonterminal, and no action\n",
- infile,
- lineno);
- if (t == SEMICOLON)
- t = lex();
- }
- /* these things can appear as alternatives to rules. */
- else if (t == TOKEN)
- {
- parse_token_decl(STOKEN, SNTERM);
- t = lex();
- }
- else if (t == NTERM)
- {
- parse_token_decl(SNTERM, STOKEN);
- t = lex();
- }
- else if (t == TYPE)
- {
- t = get_type();
- }
- else if (t == UNION)
- {
- parse_union_decl();
- t = lex();
- }
- else if (t == EXPECT)
- {
- parse_expect_decl();
- t = lex();
- }
- else if (t == START)
- {
- parse_start_decl();
- t = lex();
- }
- else
- fatal("invalid input");
- }
-
- if (nsyms > MAXSHORT)
- fatals("too many symbols (tokens plus nonterminals); maximum %d",
- MAXSHORT);
- if (nrules == 0)
- fatal("no input grammar");
-
- if (typed == 0 /* JF put out same default YYSTYPE as YACC does */
- && !value_components_used)
- {
- /* We used to use `unsigned long' as YYSTYPE on MSDOS,
- but it seems better to be consistent.
- Most programs should declare their own type anyway. */
- fprintf(fattrs, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
- if (fdefines)
- fprintf(fdefines, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
- }
-
- /* Report any undefined symbols and consider them nonterminals. */
-
- for (bp = firstsymbol; bp; bp = bp->next)
- if (bp->class == SUNKNOWN)
- {
- fprintf(stderr, "symbol %s used, not defined as token, and no rules for it\n",
- bp->tag);
- failure = 1;
- bp->class = SNTERM;
- bp->value = nvars++;
- }
-
- ntokens = nsyms - nvars;