<commit_before>/*
 * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
 *
 *          This product is part of the Amsterdam Compiler Kit.
 *
 * Permission to use, sell, duplicate or disclose this software must be
 * obtained in writing. Requests for such permissions may be sent to
 *
 *      Dr. <NAME>
 *      Wiskundig Seminarium
 *      Vrije Universiteit
 *      Postbox 7161
 *      1007 MC Amsterdam
 *      The Netherlands
 *
 */

/*
 *  L L G E N
 *
 *  An Extended LL(1) Parser Generator
 *
 *  Author : <NAME>
 */

/*
 * reach.c
 * Determine which nonterminals are reachable, and also check that they
 * are all defined.
 */

# include "tunable.h"
# include "types.h"
# include "extern.h"
# include "io.h"
# include "assert.h"

# ifndef NORCSID
static string rcsid8 = "$Header$";
# endif

/* In this file the following routines are defined: */
extern co_reach();
STATIC reachable();
STATIC reachwalk();

co_reach() {
	/*
	 * Check for undefined or unreachable nonterminals.
	 * An undefined nonterminal is a fatal error!
	 */
	register p_nont		p;
	register p_start	st;
	register p_file		x = files;
	register int		*s;

	/* Check for undefined nonterminals */
	for (p = nonterms; p < maxnt; p++) {
		if (! p->n_rule) {
			f_input = p->n_string;
			fatal(p->n_lineno,"nonterminal %s not defined",
				(min_nt_ent + (p - nonterms))->h_name);
		}
	}
	/*
	 * Walk the grammar rules, starting with the startsymbols
	 * Mark the nonterminals that are encountered with the flag
	 * REACHABLE, and walk their rules, if not done before
	 */
	for (st = start; st; st = st->ff_next) reachable(st->ff_nont);
	/*
	 * Now check for unreachable nonterminals
	 */
	for (; x->f_end < maxorder; x++) {
	    f_input = x->f_name;
	    for (s = x->f_start; s <= x->f_end; s++) {
		p = &nonterms[*s];
		if (! (p->n_flags & REACHABLE)) {
			error(p->n_lineno,"nonterminal %s unreachable",
				(min_nt_ent + (p - nonterms))->h_name);
		}
	    }
	}
}

STATIC
reachable(p) register p_nont p; {
	/*
	 * Enter the fact that p is reachable, and look for implications
	 */
	if (! (p->n_flags & REACHABLE)) {
		p->n_flags |= REACHABLE;
		/*
		 * Now walk its grammar rule
		 */
		reachwalk(p->n_rule);
	}
}

STATIC
reachwalk(p) register p_gram p; {
	/*
	 * Walk through rule p, looking for nonterminals.
	 * The nonterminals found are entered as reachable
	 */

	for (;;) {
		switch(g_gettype(p)) {
		  case ALTERNATION :
			reachwalk(((p_link) pentry[g_getcont(p)])->l_rule);
			break;
		  case TERM :
			reachwalk(((p_term) pentry[g_getcont(p)])->t_rule);
			break;
		  case NONTERM :
			reachable(&nonterms[g_getnont(p)]);
			break;
		  case EORULE :
			return;
		}
		p++;
	}
}
<commit_msg>The former version gave a fatal error message at the first undefined nonterminal. The new version checks all nonterminals<commit_after>/*
 * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
 *
 *          This product is part of the Amsterdam Compiler Kit.
 *
 * Permission to use, sell, duplicate or disclose this software must be
 * obtained in writing. Requests for such permissions may be sent to
 *
 *      Dr. <NAME>
 *      Wiskundig Seminarium
 *      Vrije Universiteit
 *      Postbox 7161
 *      1007 MC Amsterdam
 *      The Netherlands
 *
 */

/*
 *  L L G E N
 *
 *  An Extended LL(1) Parser Generator
 *
 *  Author : <NAME>
 */

/*
 * reach.c
 * Determine which nonterminals are reachable, and also check that they
 * are all defined.
 */

# include "tunable.h"
# include "types.h"
# include "extern.h"
# include "io.h"
# include "assert.h"

# ifndef NORCSID
static string rcsid8 = "$Header$";
# endif

/* In this file the following routines are defined: */
extern co_reach();
STATIC reachable();
STATIC reachwalk();

co_reach() {
	/*
	 * Check for undefined or unreachable nonterminals.
	 * An undefined nonterminal is a fatal error!
	 */
	register p_nont		p;
	register p_start	st;
	register p_file		x = files;
	register int		*s;

	/* Check for undefined nonterminals */
	for (p = nonterms; p < maxnt; p++) {
		if (! p->n_rule) {
			f_input = p->n_string;
			error(p->n_lineno,"nonterminal %s not defined",
				(min_nt_ent + (p - nonterms))->h_name);
		}
	}
	/*
	 * Walk the grammar rules, starting with the startsymbols
	 * Mark the nonterminals that are encountered with the flag
	 * REACHABLE, and walk their rules, if not done before
	 */
	for (st = start; st; st = st->ff_next) reachable(st->ff_nont);
	/*
	 * Now check for unreachable nonterminals
	 */
	for (; x->f_end < maxorder; x++) {
	    f_input = x->f_name;
	    for (s = x->f_start; s <= x->f_end; s++) {
		p = &nonterms[*s];
		if (! (p->n_flags & REACHABLE)) {
			error(p->n_lineno,"nonterminal %s unreachable",
				(min_nt_ent + (p - nonterms))->h_name);
		}
	    }
	}
}

STATIC
reachable(p) register p_nont p; {
	/*
	 * Enter the fact that p is reachable, and look for implications
	 */
	if (! (p->n_flags & REACHABLE)) {
		p->n_flags |= REACHABLE;
		/*
		 * Now walk its grammar rule
		 */
		if (p->n_rule) reachwalk(p->n_rule);
	}
}

STATIC
reachwalk(p) register p_gram p; {
	/*
	 * Walk through rule p, looking for nonterminals.
	 * The nonterminals found are entered as reachable
	 */

	for (;;) {
		switch(g_gettype(p)) {
		  case ALTERNATION :
			reachwalk(((p_link) pentry[g_getcont(p)])->l_rule);
			break;
		  case TERM :
			reachwalk(((p_term) pentry[g_getcont(p)])->t_rule);
			break;
		  case NONTERM :
			reachable(&nonterms[g_getnont(p)]);
			break;
		  case EORULE :
			return;
		}
		p++;
	}
}

{'fraction_non_alphanumeric': 0.13078178849990932, 'fraction_numerical': 0.005078904407763468, 'mean_word_length': 3.0043572984749454, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4475628', 'n_tokens_mistral': 2169, 'n_tokens_neox': 1956, 'n_words': 594}