1991-11-23 10:41:43 +00:00
|
|
|
/* Copyright (c) 1991 by the Vrije Universiteit, Amsterdam, the Netherlands.
|
1995-07-31 09:17:14 +00:00
|
|
|
* For full copyright and restrictions on use see the file COPYING in the top
|
1995-07-31 09:10:42 +00:00
|
|
|
* level of the LLgen tree.
|
1985-10-02 22:20:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* L L G E N
|
|
|
|
*
|
|
|
|
* An Extended LL(1) Parser Generator
|
|
|
|
*
|
|
|
|
* Author : Ceriel J.H. Jacobs
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compute.c
|
|
|
|
* Defines routines to compute FIRST, FOLLOW etc.
|
|
|
|
* Also checks the continuation grammar from the specified grammar.
|
|
|
|
*/
|
|
|
|
|
2013-05-12 19:45:55 +00:00
|
|
|
# include <stdlib.h>
|
2019-02-18 16:39:05 +00:00
|
|
|
# include <stdio.h>
|
1985-10-02 22:20:04 +00:00
|
|
|
# include "types.h"
|
|
|
|
# include "extern.h"
|
|
|
|
# include "sets.h"
|
|
|
|
# include "assert.h"
|
|
|
|
# include "io.h"
|
|
|
|
|
|
|
|
# ifndef NORCSID
|
1994-06-24 11:31:16 +00:00
|
|
|
static string rcsid = "$Id$";
|
1985-10-02 22:20:04 +00:00
|
|
|
# endif
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
p_set get_set();
|
|
|
|
typedef struct lngth
|
|
|
|
{
|
|
|
|
/* Structure used to compute the shortest possible
|
|
|
|
* length of a terminal production of a rule.
|
|
|
|
* In case of a tie, the second field is used.
|
|
|
|
*/
|
1985-11-25 15:47:51 +00:00
|
|
|
int cnt;
|
|
|
|
int val;
|
|
|
|
} t_length, *p_length;
|
1985-10-02 22:20:04 +00:00
|
|
|
|
|
|
|
/* Defined in this file : */
|
2019-02-18 16:39:05 +00:00
|
|
|
|
|
|
|
void do_compute(void);
|
|
|
|
STATIC void createsets(void);
|
|
|
|
STATIC void walk(p_set u, register p_gram p);
|
|
|
|
STATIC void co_trans(int (*fc)());
|
|
|
|
STATIC int nempty(register p_nont p);
|
|
|
|
int empty(register p_gram p);
|
|
|
|
STATIC int nfirst(register p_nont p);
|
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
STATIC int nc_nfirst(register p_nont p);
|
|
|
|
#endif
|
|
|
|
STATIC int first(p_set setp, register p_gram p, int flag);
|
1997-02-21 11:27:57 +00:00
|
|
|
#ifdef NON_CORRECTING
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC int nc_first(p_set setp,register p_gram p,int flag);
|
1997-02-21 11:27:57 +00:00
|
|
|
#endif
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC int nfollow(register p_nont p);
|
|
|
|
STATIC int follow(p_set setp, register p_gram p);
|
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
STATIC int nc_nfollow(register p_nont p);
|
|
|
|
#endif
|
|
|
|
STATIC void co_dirsymb(p_set setp, register p_gram p);
|
|
|
|
STATIC void co_others(p_gram p);
|
|
|
|
STATIC int ncomplength(p_nont p);
|
|
|
|
STATIC void do_lengthcomp(void);
|
|
|
|
STATIC void complength(register p_gram p, p_length le);
|
|
|
|
STATIC void add(register p_length a, int c, int v);
|
|
|
|
STATIC int compare(p_length a, p_length b);
|
|
|
|
STATIC void setdefaults(register p_gram p);
|
|
|
|
STATIC void do_contains(register p_nont n);
|
|
|
|
STATIC void contains(register p_gram p, register p_set set);
|
|
|
|
STATIC int nsafes(register p_nont p);
|
|
|
|
STATIC int do_safes(register p_gram p, int safe, register int *ch);
|
|
|
|
int t_safety(int rep, int count, int persistent, int safety);
|
|
|
|
int t_after(int rep, int count, int outsafety);
|
1985-10-02 22:20:04 +00:00
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
/*
|
|
|
|
* Does all the work, by calling other routines (divide and conquer)
|
|
|
|
*/
|
|
|
|
void do_compute(void)
|
|
|
|
{
|
|
|
|
register p_nont p;
|
1985-11-25 15:47:51 +00:00
|
|
|
register p_start st;
|
|
|
|
|
|
|
|
createsets();
|
2019-02-18 16:39:05 +00:00
|
|
|
co_trans(nempty); /* Which nonterminals produce empty? */
|
|
|
|
co_trans(nfirst); /* Computes first sets */
|
1985-11-25 15:47:51 +00:00
|
|
|
/*
|
|
|
|
* Compute FOLLOW sets.
|
|
|
|
* First put EOFILE in the follow set of the start nonterminals.
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
for (st = start; st; st = st->ff_next)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
p = &nonterms[st->ff_nont];
|
2019-02-18 16:39:05 +00:00
|
|
|
PUTIN(p->n_follow, 0);
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
|
|
|
co_trans(nfollow);
|
|
|
|
/*
|
|
|
|
* Compute the sets which determine which alternative to choose
|
|
|
|
* in case of a choice
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
for (p = nonterms; p < maxnt; p++)
|
|
|
|
{
|
|
|
|
co_dirsymb(p->n_follow, p->n_rule);
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Compute the minimum length of productions of nonterminals,
|
|
|
|
* and then determine the default choices
|
|
|
|
*/
|
|
|
|
do_lengthcomp();
|
|
|
|
/*
|
|
|
|
* Compute the contains sets
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
for (p = nonterms; p < maxnt; p++)
|
|
|
|
do_contains(p);
|
|
|
|
for (p = nonterms; p < maxnt; p++)
|
|
|
|
contains(p->n_rule, (p_set) 0);
|
1985-11-25 15:47:51 +00:00
|
|
|
/*
|
|
|
|
* Compute the safety of each nonterminal and term.
|
|
|
|
* The safety gives an answer to the question whether a scan is done,
|
|
|
|
* and how it should be handled.
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
for (p = nonterms; p < maxnt; p++)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
/*
|
|
|
|
* Don't know anything yet
|
|
|
|
*/
|
|
|
|
setntsafe(p, NOSAFETY);
|
|
|
|
setntout(p, NOSAFETY);
|
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
for (st = start; st; st = st->ff_next)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
/*
|
|
|
|
* But start symbols are called with lookahead done
|
|
|
|
*/
|
|
|
|
p = &nonterms[st->ff_nont];
|
2019-02-18 16:39:05 +00:00
|
|
|
setntsafe(p, SCANDONE);
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
|
|
|
co_trans(nsafes);
|
1997-02-21 11:27:57 +00:00
|
|
|
|
|
|
|
#ifdef NON_CORRECTING
|
2019-02-18 16:39:05 +00:00
|
|
|
if (subpars_sim)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
/* compute the union of the first sets of all start symbols
|
|
|
|
Used to compute the nc-first-sets when -s option is given */
|
|
|
|
start_firsts = get_set();
|
|
|
|
for (st = start; st; st = st->ff_next)
|
|
|
|
{
|
|
|
|
s = setunion(start_firsts, (&nonterms[st->ff_nont])->n_first);
|
|
|
|
}
|
1997-02-21 11:27:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
if (non_corr)
|
|
|
|
{
|
|
|
|
/* compute the non_corr first sets for all nonterminals and terms */
|
1997-02-21 11:27:57 +00:00
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
co_trans(nc_nfirst);
|
|
|
|
for (st = start; st; st = st->ff_next)
|
|
|
|
{
|
|
|
|
p = &nonterms[st->ff_nont];
|
|
|
|
PUTIN(p->n_nc_follow,0);
|
|
|
|
}
|
|
|
|
co_trans(nc_nfollow);
|
1997-02-21 11:27:57 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
# ifndef NDEBUG
|
2019-02-18 16:39:05 +00:00
|
|
|
if (debug)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
fputs("Safeties:\n", stderr);
|
2019-02-18 16:39:05 +00:00
|
|
|
for (p = nonterms; p < maxnt; p++)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s\t%d\t%d\n", p->n_name, getntsafe(p),
|
|
|
|
getntout(p));
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
/*
|
|
|
|
* Allocate space for the sets. Also determine which files use
|
|
|
|
* which nonterminals, and determine which nonterminals can be
|
|
|
|
* made static.
|
|
|
|
*/STATIC void createsets(void)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_nont p;
|
1991-11-26 15:38:41 +00:00
|
|
|
register p_file f;
|
|
|
|
register p_start st;
|
|
|
|
register int i;
|
|
|
|
int n = NINTS(NBYTES(nnonterms));
|
|
|
|
p_mem alloc();
|
1985-10-02 22:20:04 +00:00
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
for (f = files; f < maxfiles; f++)
|
|
|
|
{
|
1991-11-26 15:38:41 +00:00
|
|
|
register p_set s;
|
LLgen: use size_t, reduce warnings, other small changes
Use C89 size_t for sizes from sizeof() or to malloc() or realloc().
Remove obsolete (unsigned) casts. Sizes were unsigned int in
traditional C but are size_t in C89.
Silence some clang warnings. Add the second pair of round brackets in
`while ((ff = ff->ff_next))` to silence -Wparentheses. Change
`if (nc_first(...))/*nothing*/;` to `(void)nc_first(...);` to silence
-Wempty-body. The code in compute.c nc_first() had the form
`if (x) if (y) s; else t;`. The old indentation (before 10717cc)
suggests that the "else" belongs to the 2nd "if", so add braces like
`if (x) { if (y) s; else t; }` to silence -Wdangling-else.
Shuffle extern function declarations. Add missing declaration for
LLparse(). Stop declaring RENAME(); it doesn't exist. Move some
declarations from main.c to extern.h, so the C compiler may check that
the declarations are compatible with the function definitions.
Assume that standard C89 remove() is available and doesn't need the
UNLINK() wrapper.
In lib/incl, don't need to include <stdio.h> nor <stdlib.h> to use
assert().
Remove alloc.h. If you don't clean your build, then an outdated
BUILDDIR/obj/util/LLgen/headers/alloc.h will survive but should not
cause harm, because nothing includes it. Don't need to remove alloc.h
from util/LLgen/distr.sh, because it isn't there.
Run the bootstrap to rebuild LLgen.c, Lpars.c, tokens.c.
2019-10-22 16:56:50 +00:00
|
|
|
f->f_used = s = (p_set) alloc(n * sizeof(*(f->f_used)));
|
2019-02-18 16:39:05 +00:00
|
|
|
for (i = n; i; i--)
|
|
|
|
*s++ = 0;
|
|
|
|
for (i = f->f_nonterminals; i != -1; i = p->n_next)
|
|
|
|
{
|
1991-11-26 15:38:41 +00:00
|
|
|
p = &nonterms[i];
|
|
|
|
p->n_flags |= GENSTATIC;
|
|
|
|
p->n_first = get_set();
|
1997-02-21 11:27:57 +00:00
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
p->n_nc_first = get_set();
|
|
|
|
p->n_nc_follow = get_set();
|
|
|
|
#endif
|
1991-11-26 15:38:41 +00:00
|
|
|
p->n_follow = get_set();
|
|
|
|
walk(f->f_used, p->n_rule);
|
|
|
|
}
|
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
for (f = files; f < maxfiles; f++)
|
|
|
|
{
|
|
|
|
for (i = f->f_nonterminals; i != -1; i = p->n_next)
|
|
|
|
{
|
1991-11-26 15:38:41 +00:00
|
|
|
register p_file f2;
|
|
|
|
|
|
|
|
p = &nonterms[i];
|
2019-02-18 16:39:05 +00:00
|
|
|
for (f2 = files; f2 < maxfiles; f2++)
|
|
|
|
{
|
|
|
|
if (f2 != f && IN(f2->f_used, i))
|
|
|
|
{
|
1991-11-26 15:38:41 +00:00
|
|
|
p->n_flags &= ~GENSTATIC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
for (st = start; st; st = st->ff_next)
|
|
|
|
{
|
1991-11-26 15:38:41 +00:00
|
|
|
nonterms[st->ff_nont].n_flags &= ~GENSTATIC;
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
/*
|
|
|
|
* Walk through the grammar rule p, allocating sets
|
|
|
|
*/
|
|
|
|
STATIC void walk(p_set u, register p_gram p)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
switch (g_gettype(p))
|
|
|
|
{
|
|
|
|
case TERM:
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_term q;
|
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
q = g_getterm(p);
|
1985-11-25 15:47:51 +00:00
|
|
|
q->t_first = get_set();
|
1997-02-21 11:27:57 +00:00
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
q->t_nc_first = get_set();
|
|
|
|
q->t_nc_follow = get_set();
|
|
|
|
#endif
|
1985-11-25 15:47:51 +00:00
|
|
|
q->t_follow = get_set();
|
1991-11-26 15:38:41 +00:00
|
|
|
walk(u, q->t_rule);
|
2019-02-18 16:39:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ALTERNATION:
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_link l;
|
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
l = g_getlink(p);
|
1985-11-25 15:47:51 +00:00
|
|
|
l->l_symbs = get_set();
|
1997-02-21 11:27:57 +00:00
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
l->l_nc_symbs = get_set();
|
|
|
|
#endif
|
1985-11-25 15:47:51 +00:00
|
|
|
l->l_others = get_set();
|
1991-11-26 15:38:41 +00:00
|
|
|
walk(u, l->l_rule);
|
2019-02-18 16:39:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NONTERM:
|
|
|
|
{
|
1991-11-26 15:38:41 +00:00
|
|
|
register int i = g_getcont(p);
|
|
|
|
|
|
|
|
PUTIN(u, i);
|
2019-02-18 16:39:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EORULE:
|
1985-10-02 22:20:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC void co_trans(int (*fc)())
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
register p_nont p;
|
|
|
|
register int change;
|
1985-10-02 22:20:04 +00:00
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
do
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
change = 0;
|
2019-02-18 16:39:05 +00:00
|
|
|
for (p = nonterms; p < maxnt; p++)
|
|
|
|
{
|
|
|
|
if ((*fc)(p))
|
|
|
|
change = 1;
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
1985-11-25 15:47:51 +00:00
|
|
|
} while (change);
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC int nempty(register p_nont p)
|
|
|
|
{
|
|
|
|
if (!(p->n_flags & EMPTY) && empty(p->n_rule))
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
p->n_flags |= EMPTY;
|
|
|
|
return 1;
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
1985-11-25 15:47:51 +00:00
|
|
|
return 0;
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
/*
|
|
|
|
* Does the rule pointed to by p produce empty ?
|
|
|
|
*/
|
|
|
|
int empty(register p_gram p)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
switch (g_gettype(p))
|
|
|
|
{
|
|
|
|
case EORULE:
|
1985-10-02 22:20:04 +00:00
|
|
|
return 1;
|
2019-02-18 16:39:05 +00:00
|
|
|
case TERM:
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_term q;
|
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
q = g_getterm(p);
|
2019-02-18 16:39:05 +00:00
|
|
|
if (r_getkind(q) == STAR || r_getkind(q) == OPT || empty(q->t_rule))
|
|
|
|
break;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case ALTERNATION:
|
|
|
|
if (empty(g_getlink(p)->l_rule))
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
if (g_gettype(p+1) == EORULE)
|
|
|
|
return 0;
|
1985-10-02 22:20:04 +00:00
|
|
|
break;
|
2019-02-18 16:39:05 +00:00
|
|
|
case NONTERM:
|
|
|
|
if (nonterms[g_getcont(p)].n_flags & EMPTY)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Fall through */
|
2019-02-18 16:39:05 +00:00
|
|
|
case LITERAL:
|
|
|
|
case TERMINAL:
|
1985-10-02 22:20:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC int nfirst(register p_nont p)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
return first(p->n_first, p->n_rule, 0);
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
|
1997-02-21 11:27:57 +00:00
|
|
|
#ifdef NON_CORRECTING
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC int nc_nfirst(register p_nont p)
|
|
|
|
{
|
1997-02-21 11:27:57 +00:00
|
|
|
return nc_first(p->n_nc_first, p->n_rule, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
/*
|
|
|
|
* Compute the FIRST set of rule p.
|
|
|
|
* If flag = 0, also the first sets for terms and alternations in
|
|
|
|
* the rule p are computed.
|
|
|
|
* The FIRST set is put in setp.
|
|
|
|
* return 1 if the set refered to by "setp" changed
|
|
|
|
*/
|
|
|
|
STATIC int first(p_set setp, register p_gram p, int flag)
|
|
|
|
{
|
|
|
|
register int s; /* Will gather return value */
|
|
|
|
int noenter;/* when set, unables entering of elements into
|
|
|
|
* setp. This is necessary to walk through the
|
|
|
|
* rest of rule p.
|
1985-10-02 22:20:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
s = 0;
|
|
|
|
noenter = 0;
|
2019-02-18 16:39:05 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
switch (g_gettype(p))
|
|
|
|
{
|
|
|
|
case EORULE:
|
1985-10-02 22:20:04 +00:00
|
|
|
return s;
|
2019-02-18 16:39:05 +00:00
|
|
|
case TERM:
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_term q;
|
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
q = g_getterm(p);
|
2019-02-18 16:39:05 +00:00
|
|
|
if (flag == 0)
|
|
|
|
{
|
|
|
|
if (first(q->t_first, q->t_rule, 0))/*nothing*/
|
|
|
|
;
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
if (!noenter)
|
|
|
|
s |= setunion(setp, q->t_first);
|
1985-10-02 22:20:04 +00:00
|
|
|
p++;
|
2019-02-18 16:39:05 +00:00
|
|
|
if (r_getkind(q) == STAR || r_getkind(q) == OPT || empty(q->t_rule))
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ALTERNATION:
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_link l;
|
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
l = g_getlink(p);
|
2019-02-18 16:39:05 +00:00
|
|
|
if (flag == 0)
|
|
|
|
{
|
|
|
|
if (first(l->l_symbs, l->l_rule, 0))/*nothing*/
|
|
|
|
;
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
if (noenter == 0)
|
|
|
|
{
|
|
|
|
s |= setunion(setp, l->l_symbs);
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
if (g_gettype(p+1) == EORULE)
|
|
|
|
return s;
|
|
|
|
}
|
1985-10-02 22:20:04 +00:00
|
|
|
/* Fall Through */
|
2019-02-18 16:39:05 +00:00
|
|
|
case ACTION:
|
1985-10-02 22:20:04 +00:00
|
|
|
p++;
|
|
|
|
continue;
|
2019-02-18 16:39:05 +00:00
|
|
|
case LITERAL:
|
|
|
|
case TERMINAL:
|
|
|
|
if ((noenter == 0) && !IN(setp,g_getcont(p)))
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
s = 1;
|
2019-02-18 16:39:05 +00:00
|
|
|
PUTIN(setp, g_getcont(p));
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
break;
|
2019-02-18 16:39:05 +00:00
|
|
|
case NONTERM:
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_nont n;
|
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
n = &nonterms[g_getcont(p)];
|
2019-02-18 16:39:05 +00:00
|
|
|
if (noenter == 0)
|
|
|
|
{
|
|
|
|
s |= setunion(setp, n->n_first);
|
|
|
|
if (ntneeded)
|
|
|
|
NTPUTIN(setp, g_getcont(p));
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
p++;
|
2019-02-18 16:39:05 +00:00
|
|
|
if (n->n_flags & EMPTY)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
if (flag == 0)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
noenter = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-02-21 11:27:57 +00:00
|
|
|
#ifdef NON_CORRECTING
|
2019-02-18 16:39:05 +00:00
|
|
|
/*
|
|
|
|
* Compute the non_corr FIRST set of rule p.
|
|
|
|
* If flag = 0, also the non_corr first sets for terms and
|
|
|
|
* alternations in the rule p are computed.
|
|
|
|
* The non_corr FIRST set is put in setp.
|
|
|
|
* return 1 if the set refered to by "setp" changed
|
|
|
|
* If the -s flag was given, the union of the first-sets of all
|
|
|
|
* start symbols is used whenever an action occurs. Else, only the
|
|
|
|
* first-sets of startsynbols in the %substart are used
|
|
|
|
*/
|
|
|
|
STATIC int nc_first(p_set setp,register p_gram p,int flag)
|
|
|
|
{
|
1997-02-21 11:27:57 +00:00
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
register int s; /* Will gather return value */
|
|
|
|
int noenter;/* when set, unables entering of elements into
|
|
|
|
* setp. This is necessary to walk through the
|
|
|
|
* rest of rule p.
|
|
|
|
*/
|
1997-02-21 11:27:57 +00:00
|
|
|
|
|
|
|
s = 0;
|
|
|
|
noenter = 0;
|
2019-02-18 16:39:05 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
switch (g_gettype(p))
|
|
|
|
{
|
|
|
|
case EORULE :
|
1997-02-21 11:27:57 +00:00
|
|
|
return s;
|
2019-02-18 16:39:05 +00:00
|
|
|
case TERM :
|
|
|
|
{
|
|
|
|
register p_term q;
|
|
|
|
|
|
|
|
q = g_getterm(p);
|
|
|
|
if (flag == 0)
|
LLgen: use size_t, reduce warnings, other small changes
Use C89 size_t for sizes from sizeof() or to malloc() or realloc().
Remove obsolete (unsigned) casts. Sizes were unsigned int in
traditional C but are size_t in C89.
Silence some clang warnings. Add the second pair of round brackets in
`while ((ff = ff->ff_next))` to silence -Wparentheses. Change
`if (nc_first(...))/*nothing*/;` to `(void)nc_first(...);` to silence
-Wempty-body. The code in compute.c nc_first() had the form
`if (x) if (y) s; else t;`. The old indentation (before 10717cc)
suggests that the "else" belongs to the 2nd "if", so add braces like
`if (x) { if (y) s; else t; }` to silence -Wdangling-else.
Shuffle extern function declarations. Add missing declaration for
LLparse(). Stop declaring RENAME(); it doesn't exist. Move some
declarations from main.c to extern.h, so the C compiler may check that
the declarations are compatible with the function definitions.
Assume that standard C89 remove() is available and doesn't need the
UNLINK() wrapper.
In lib/incl, don't need to include <stdio.h> nor <stdlib.h> to use
assert().
Remove alloc.h. If you don't clean your build, then an outdated
BUILDDIR/obj/util/LLgen/headers/alloc.h will survive but should not
cause harm, because nothing includes it. Don't need to remove alloc.h
from util/LLgen/distr.sh, because it isn't there.
Run the bootstrap to rebuild LLgen.c, Lpars.c, tokens.c.
2019-10-22 16:56:50 +00:00
|
|
|
(void)nc_first(q->t_nc_first,q->t_rule,0);
|
2019-02-18 16:39:05 +00:00
|
|
|
if (!noenter) s |= setunion(setp,q->t_nc_first);
|
|
|
|
p++;
|
|
|
|
if (r_getkind(q) == STAR ||
|
|
|
|
r_getkind(q) == OPT ||
|
|
|
|
empty(q->t_rule)) continue;
|
|
|
|
break;}
|
|
|
|
case ALTERNATION :
|
|
|
|
{
|
|
|
|
register p_link l;
|
1997-02-21 11:27:57 +00:00
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
l = g_getlink(p);
|
|
|
|
if (flag == 0)
|
LLgen: use size_t, reduce warnings, other small changes
Use C89 size_t for sizes from sizeof() or to malloc() or realloc().
Remove obsolete (unsigned) casts. Sizes were unsigned int in
traditional C but are size_t in C89.
Silence some clang warnings. Add the second pair of round brackets in
`while ((ff = ff->ff_next))` to silence -Wparentheses. Change
`if (nc_first(...))/*nothing*/;` to `(void)nc_first(...);` to silence
-Wempty-body. The code in compute.c nc_first() had the form
`if (x) if (y) s; else t;`. The old indentation (before 10717cc)
suggests that the "else" belongs to the 2nd "if", so add braces like
`if (x) { if (y) s; else t; }` to silence -Wdangling-else.
Shuffle extern function declarations. Add missing declaration for
LLparse(). Stop declaring RENAME(); it doesn't exist. Move some
declarations from main.c to extern.h, so the C compiler may check that
the declarations are compatible with the function definitions.
Assume that standard C89 remove() is available and doesn't need the
UNLINK() wrapper.
In lib/incl, don't need to include <stdio.h> nor <stdlib.h> to use
assert().
Remove alloc.h. If you don't clean your build, then an outdated
BUILDDIR/obj/util/LLgen/headers/alloc.h will survive but should not
cause harm, because nothing includes it. Don't need to remove alloc.h
from util/LLgen/distr.sh, because it isn't there.
Run the bootstrap to rebuild LLgen.c, Lpars.c, tokens.c.
2019-10-22 16:56:50 +00:00
|
|
|
(void)nc_first(l->l_nc_symbs,l->l_rule,0);
|
2019-02-18 16:39:05 +00:00
|
|
|
if (noenter == 0)
|
|
|
|
{
|
|
|
|
s |= setunion(setp,l->l_nc_symbs);
|
|
|
|
}
|
|
|
|
if (g_gettype(p+1) == EORULE) return s;
|
1997-02-21 11:27:57 +00:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
continue;
|
2019-02-18 16:39:05 +00:00
|
|
|
case ACTION :
|
|
|
|
{
|
|
|
|
register p_start subp;
|
|
|
|
|
|
|
|
if (!noenter)
|
|
|
|
{
|
LLgen: use size_t, reduce warnings, other small changes
Use C89 size_t for sizes from sizeof() or to malloc() or realloc().
Remove obsolete (unsigned) casts. Sizes were unsigned int in
traditional C but are size_t in C89.
Silence some clang warnings. Add the second pair of round brackets in
`while ((ff = ff->ff_next))` to silence -Wparentheses. Change
`if (nc_first(...))/*nothing*/;` to `(void)nc_first(...);` to silence
-Wempty-body. The code in compute.c nc_first() had the form
`if (x) if (y) s; else t;`. The old indentation (before 10717cc)
suggests that the "else" belongs to the 2nd "if", so add braces like
`if (x) { if (y) s; else t; }` to silence -Wdangling-else.
Shuffle extern function declarations. Add missing declaration for
LLparse(). Stop declaring RENAME(); it doesn't exist. Move some
declarations from main.c to extern.h, so the C compiler may check that
the declarations are compatible with the function definitions.
Assume that standard C89 remove() is available and doesn't need the
UNLINK() wrapper.
In lib/incl, don't need to include <stdio.h> nor <stdlib.h> to use
assert().
Remove alloc.h. If you don't clean your build, then an outdated
BUILDDIR/obj/util/LLgen/headers/alloc.h will survive but should not
cause harm, because nothing includes it. Don't need to remove alloc.h
from util/LLgen/distr.sh, because it isn't there.
Run the bootstrap to rebuild LLgen.c, Lpars.c, tokens.c.
2019-10-22 16:56:50 +00:00
|
|
|
if (subpars_sim)
|
|
|
|
s |= setunion(setp, start_firsts);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (subp = g_getsubparse(p); subp;
|
2019-02-18 16:39:05 +00:00
|
|
|
subp = subp->ff_next)
|
LLgen: use size_t, reduce warnings, other small changes
Use C89 size_t for sizes from sizeof() or to malloc() or realloc().
Remove obsolete (unsigned) casts. Sizes were unsigned int in
traditional C but are size_t in C89.
Silence some clang warnings. Add the second pair of round brackets in
`while ((ff = ff->ff_next))` to silence -Wparentheses. Change
`if (nc_first(...))/*nothing*/;` to `(void)nc_first(...);` to silence
-Wempty-body. The code in compute.c nc_first() had the form
`if (x) if (y) s; else t;`. The old indentation (before 10717cc)
suggests that the "else" belongs to the 2nd "if", so add braces like
`if (x) { if (y) s; else t; }` to silence -Wdangling-else.
Shuffle extern function declarations. Add missing declaration for
LLparse(). Stop declaring RENAME(); it doesn't exist. Move some
declarations from main.c to extern.h, so the C compiler may check that
the declarations are compatible with the function definitions.
Assume that standard C89 remove() is available and doesn't need the
UNLINK() wrapper.
In lib/incl, don't need to include <stdio.h> nor <stdlib.h> to use
assert().
Remove alloc.h. If you don't clean your build, then an outdated
BUILDDIR/obj/util/LLgen/headers/alloc.h will survive but should not
cause harm, because nothing includes it. Don't need to remove alloc.h
from util/LLgen/distr.sh, because it isn't there.
Run the bootstrap to rebuild LLgen.c, Lpars.c, tokens.c.
2019-10-22 16:56:50 +00:00
|
|
|
s |= setunion(setp, (&nonterms[subp->ff_nont])->n_nc_first);
|
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
continue;
|
1997-02-21 11:27:57 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
case LITERAL :
|
|
|
|
case TERMINAL :
|
|
|
|
if (g_getcont(p) == g_getcont(illegal_gram))
|
|
|
|
{
|
1997-02-21 11:27:57 +00:00
|
|
|
/* Ignore for this set. */
|
|
|
|
p++;
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
if ((noenter == 0) && !IN(setp,g_getcont(p)))
|
|
|
|
{
|
1997-02-21 11:27:57 +00:00
|
|
|
s = 1;
|
|
|
|
PUTIN(setp,g_getcont(p));
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
break;
|
2019-02-18 16:39:05 +00:00
|
|
|
case NONTERM :
|
|
|
|
{
|
|
|
|
register p_nont n;
|
|
|
|
|
|
|
|
n = &nonterms[g_getcont(p)];
|
|
|
|
if (noenter == 0)
|
|
|
|
{
|
|
|
|
s |= setunion(setp,n->n_nc_first);
|
|
|
|
if (ntneeded) NTPUTIN(setp,g_getcont(p));
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
if (n->n_flags & EMPTY) continue;
|
|
|
|
break;}
|
1997-02-21 11:27:57 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
if (flag == 0)
|
|
|
|
{
|
1997-02-21 11:27:57 +00:00
|
|
|
noenter = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC int nfollow(register p_nont p)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
return follow(p->n_follow, p->n_rule);
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
/*
|
|
|
|
* setp is the follow set for the rule p.
|
|
|
|
* Compute the follow sets in the rule p from this set.
|
|
|
|
* Return 1 if a follow set of a nonterminal changed.
|
|
|
|
*/
|
|
|
|
STATIC int follow(p_set setp, register p_gram p)
|
|
|
|
{
|
|
|
|
register int s; /* Will gather return value */
|
1985-10-02 22:20:04 +00:00
|
|
|
|
|
|
|
s = 0;
|
2019-02-18 16:39:05 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
switch (g_gettype(p))
|
|
|
|
{
|
|
|
|
case EORULE:
|
1985-10-02 22:20:04 +00:00
|
|
|
return s;
|
2019-02-18 16:39:05 +00:00
|
|
|
case TERM:
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_term q;
|
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
q = g_getterm(p);
|
2019-02-18 16:39:05 +00:00
|
|
|
if (empty(p + 1))
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
/*
|
|
|
|
* If what follows the term can be empty,
|
|
|
|
* everything that can follow the whole
|
|
|
|
* rule can also follow the term
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
s |= setunion(q->t_follow, setp);
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Everything that can start the rest of the rule
|
|
|
|
* can follow the term
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
s |= first(q->t_follow, p + 1, 1);
|
|
|
|
if (r_getkind(q) == STAR || r_getkind(q) == PLUS || r_getnum(q))
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
/*
|
|
|
|
* If the term involves a repetition
|
|
|
|
* of possibly more than one,
|
|
|
|
* everything that can start the term
|
|
|
|
* can also follow it.
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
s |= follow(q->t_first, q->t_rule);
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Now propagate the set computed sofar
|
|
|
|
*/
|
1985-11-25 15:47:51 +00:00
|
|
|
s |= follow(q->t_follow, q->t_rule);
|
2019-02-18 16:39:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ALTERNATION:
|
1985-10-02 22:20:04 +00:00
|
|
|
/*
|
|
|
|
* Just propagate setp
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
s |= follow(setp, g_getlink(p)->l_rule);
|
1985-10-02 22:20:04 +00:00
|
|
|
break;
|
2019-02-18 16:39:05 +00:00
|
|
|
case NONTERM:
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_nont n;
|
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
n = &nonterms[g_getcont(p)];
|
2019-02-18 16:39:05 +00:00
|
|
|
s |= first(n->n_follow, p + 1, 1);
|
|
|
|
if (empty(p + 1))
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
/*
|
|
|
|
* If the rest of p can produce empty,
|
|
|
|
* everything that follows p can follow
|
|
|
|
* the nonterminal
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
s |= setunion(n->n_follow, setp);
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
break;
|
|
|
|
}
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
return 0;
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
|
1997-02-21 11:27:57 +00:00
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC int nc_nfollow(register p_nont p)
|
|
|
|
{
|
1997-02-21 11:27:57 +00:00
|
|
|
return follow(p->n_nc_follow, p->n_rule);
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC int nc_follow(p_set setp, register p_gram p)
|
|
|
|
{
|
1997-02-21 11:27:57 +00:00
|
|
|
/*
|
|
|
|
* setp is the follow set for the rule p.
|
|
|
|
* Compute the follow sets in the rule p from this set.
|
|
|
|
* Return 1 if a follow set of a nonterminal changed.
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
register int s; /* Will gather return value */
|
1997-02-21 11:27:57 +00:00
|
|
|
|
|
|
|
s = 0;
|
2019-02-18 16:39:05 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
switch (g_gettype(p))
|
|
|
|
{
|
|
|
|
case EORULE :
|
1997-02-21 11:27:57 +00:00
|
|
|
return s;
|
2019-02-18 16:39:05 +00:00
|
|
|
case TERM :
|
|
|
|
{
|
|
|
|
register p_term q;
|
|
|
|
|
|
|
|
q = g_getterm(p);
|
|
|
|
if (empty(p+1))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If what follows the term can be empty,
|
|
|
|
* everything that can follow the whole
|
|
|
|
* rule can also follow the term
|
|
|
|
*/
|
|
|
|
s |= setunion(q->t_nc_follow,setp);
|
|
|
|
}
|
1997-02-21 11:27:57 +00:00
|
|
|
/*
|
2019-02-18 16:39:05 +00:00
|
|
|
* Everything that can start the rest of the rule
|
|
|
|
* can follow the term
|
1997-02-21 11:27:57 +00:00
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
s |= nc_first(q->t_nc_follow,p+1,1);
|
|
|
|
if (r_getkind(q) == STAR ||
|
|
|
|
r_getkind(q) == PLUS ||
|
|
|
|
r_getnum(q) )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the term involves a repetition
|
|
|
|
* of possibly more than one,
|
|
|
|
* everything that can start the term
|
|
|
|
* can also follow it.
|
|
|
|
*/
|
|
|
|
s |= nc_follow(q->t_nc_first,q->t_rule);
|
|
|
|
}
|
1997-02-21 11:27:57 +00:00
|
|
|
/*
|
2019-02-18 16:39:05 +00:00
|
|
|
* Now propagate the set computed sofar
|
1997-02-21 11:27:57 +00:00
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
s |= nc_follow(q->t_nc_follow, q->t_rule);
|
|
|
|
break;}
|
|
|
|
case ALTERNATION :
|
1997-02-21 11:27:57 +00:00
|
|
|
/*
|
|
|
|
* Just propagate setp
|
|
|
|
*/
|
|
|
|
s |= nc_follow(setp,g_getlink(p)->l_rule);
|
|
|
|
break;
|
2019-02-18 16:39:05 +00:00
|
|
|
case NONTERM :
|
|
|
|
{
|
|
|
|
register p_nont n;
|
|
|
|
|
|
|
|
n = &nonterms[g_getcont(p)];
|
|
|
|
s |= nc_first(n->n_nc_follow,p+1,1);
|
|
|
|
if (empty(p+1))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the rest of p can produce empty,
|
|
|
|
* everything that follows p can follow
|
|
|
|
* the nonterminal
|
|
|
|
*/
|
|
|
|
s |= setunion(n->n_nc_follow,setp);
|
|
|
|
}
|
|
|
|
break;}
|
1997-02-21 11:27:57 +00:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
return 0;
|
1997-02-21 11:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC void co_dirsymb(p_set setp, register p_gram p)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
/*
|
|
|
|
* Walk the rule p, doing the work for alternations
|
|
|
|
*/
|
|
|
|
register p_gram s = 0;
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
switch (g_gettype(p))
|
|
|
|
{
|
|
|
|
case EORULE:
|
1985-10-02 22:20:04 +00:00
|
|
|
return;
|
2019-02-18 16:39:05 +00:00
|
|
|
case TERM:
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_term q;
|
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
q = g_getterm(p);
|
2019-02-18 16:39:05 +00:00
|
|
|
co_dirsymb(q->t_follow, q->t_rule);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ALTERNATION:
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_link l;
|
|
|
|
/*
|
|
|
|
* Save first alternative
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
if (!s)
|
|
|
|
s = p;
|
1987-05-12 18:23:09 +00:00
|
|
|
l = g_getlink(p);
|
2019-02-18 16:39:05 +00:00
|
|
|
co_dirsymb(setp, l->l_rule);
|
|
|
|
if (empty(l->l_rule))
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
/*
|
|
|
|
* If the rule can produce empty, everything
|
|
|
|
* that follows it can also start it
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
setunion(l->l_symbs, setp);
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
if (g_gettype(p+1) == EORULE)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
/*
|
|
|
|
* Every alternation is implemented as a
|
|
|
|
* choice between two alternatives :
|
|
|
|
* this one or one of the following.
|
|
|
|
* The l_others set will contain the starters
|
|
|
|
* of the other alternatives
|
|
|
|
*/
|
|
|
|
co_others(s);
|
|
|
|
return;
|
2019-02-18 16:39:05 +00:00
|
|
|
}
|
|
|
|
}
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC void co_others(p_gram p)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
/*
|
|
|
|
* compute the l_others-sets for the list of alternatives
|
|
|
|
* indicated by p
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
register p_link l1, l2;
|
1985-10-02 22:20:04 +00:00
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
l1 = g_getlink(p);
|
1985-10-02 22:20:04 +00:00
|
|
|
p++;
|
1987-05-12 18:23:09 +00:00
|
|
|
l2 = g_getlink(p);
|
2019-02-18 16:39:05 +00:00
|
|
|
setunion(l1->l_others, l2->l_symbs);
|
|
|
|
if (g_gettype(p+1) != EORULE)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
/*
|
|
|
|
* First compute l2->l_others
|
|
|
|
*/
|
|
|
|
co_others(p);
|
|
|
|
/*
|
|
|
|
* and then l1->l_others
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
setunion(l1->l_others, l2->l_others);
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
static p_length length;
|
|
|
|
# define INFINITY 32767
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC int ncomplength(p_nont p)
|
1987-03-16 22:26:03 +00:00
|
|
|
{
|
|
|
|
register p_length pl = &length[p - nonterms];
|
|
|
|
int x = pl->cnt;
|
|
|
|
|
|
|
|
pl->cnt = -1;
|
|
|
|
complength(p->n_rule, pl);
|
|
|
|
return pl->cnt < INFINITY && x == INFINITY;
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
/*
|
|
|
|
* Compute the minimum length of a terminal production for each
|
|
|
|
* nonterminal.
|
|
|
|
* This length consists of two fields: the number of terminals,
|
|
|
|
* and a number that is composed of
|
|
|
|
* - the number of this alternative
|
|
|
|
* - a crude measure of the number of terms and nonterminals in the
|
|
|
|
* production of this shortest string.
|
|
|
|
*/
|
|
|
|
STATIC void do_lengthcomp(void)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
register p_length pl;
|
|
|
|
register p_nont p;
|
|
|
|
p_mem alloc();
|
|
|
|
|
LLgen: use size_t, reduce warnings, other small changes
Use C89 size_t for sizes from sizeof() or to malloc() or realloc().
Remove obsolete (unsigned) casts. Sizes were unsigned int in
traditional C but are size_t in C89.
Silence some clang warnings. Add the second pair of round brackets in
`while ((ff = ff->ff_next))` to silence -Wparentheses. Change
`if (nc_first(...))/*nothing*/;` to `(void)nc_first(...);` to silence
-Wempty-body. The code in compute.c nc_first() had the form
`if (x) if (y) s; else t;`. The old indentation (before 10717cc)
suggests that the "else" belongs to the 2nd "if", so add braces like
`if (x) { if (y) s; else t; }` to silence -Wdangling-else.
Shuffle extern function declarations. Add missing declaration for
LLparse(). Stop declaring RENAME(); it doesn't exist. Move some
declarations from main.c to extern.h, so the C compiler may check that
the declarations are compatible with the function definitions.
Assume that standard C89 remove() is available and doesn't need the
UNLINK() wrapper.
In lib/incl, don't need to include <stdio.h> nor <stdlib.h> to use
assert().
Remove alloc.h. If you don't clean your build, then an outdated
BUILDDIR/obj/util/LLgen/headers/alloc.h will survive but should not
cause harm, because nothing includes it. Don't need to remove alloc.h
from util/LLgen/distr.sh, because it isn't there.
Run the bootstrap to rebuild LLgen.c, Lpars.c, tokens.c.
2019-10-22 16:56:50 +00:00
|
|
|
length = (p_length) alloc(nnonterms * sizeof(*length));
|
2019-02-18 16:39:05 +00:00
|
|
|
for (pl = &length[nnonterms - 1]; pl >= length; pl--)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
pl->val = pl->cnt = INFINITY;
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
1987-03-16 22:26:03 +00:00
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
co_trans(ncomplength);
|
1987-03-16 22:26:03 +00:00
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
pl = length;
|
2019-02-18 16:39:05 +00:00
|
|
|
for (p = nonterms; p < maxnt; p++, pl++)
|
|
|
|
{
|
|
|
|
if (pl->cnt == INFINITY)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
p->n_flags |= RECURSIVE;
|
|
|
|
}
|
|
|
|
setdefaults(p->n_rule);
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
free((p_mem) length);
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC void complength(register p_gram p, p_length le)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
/*
|
1985-11-25 15:47:51 +00:00
|
|
|
* Walk grammar rule p, computing minimum lengths
|
1985-10-02 22:20:04 +00:00
|
|
|
*/
|
|
|
|
register p_link l;
|
|
|
|
register p_term q;
|
1985-11-25 15:47:51 +00:00
|
|
|
t_length i;
|
1987-03-16 22:26:03 +00:00
|
|
|
t_length X;
|
1987-11-27 11:04:07 +00:00
|
|
|
int cnt = 0;
|
1985-10-02 22:20:04 +00:00
|
|
|
|
1987-03-16 22:26:03 +00:00
|
|
|
X.cnt = 0;
|
|
|
|
X.val = 0;
|
2019-02-18 16:39:05 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
switch (g_gettype(p))
|
|
|
|
{
|
|
|
|
case LITERAL:
|
|
|
|
case TERMINAL:
|
1997-02-21 11:27:57 +00:00
|
|
|
#ifdef NON_CORRECTING
|
2019-02-18 16:39:05 +00:00
|
|
|
if (g_getcont(p) == g_getcont(illegal_gram))
|
|
|
|
{
|
1997-02-21 11:27:57 +00:00
|
|
|
add(&X, INFINITY, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
1987-11-27 11:04:07 +00:00
|
|
|
add(&X, 1, 0);
|
1985-11-25 15:47:51 +00:00
|
|
|
break;
|
2019-02-18 16:39:05 +00:00
|
|
|
case ALTERNATION:
|
1985-11-25 15:47:51 +00:00
|
|
|
|
1987-03-16 22:26:03 +00:00
|
|
|
X.cnt = INFINITY;
|
|
|
|
X.val = INFINITY;
|
2019-02-18 16:39:05 +00:00
|
|
|
while (g_gettype(p) != EORULE)
|
|
|
|
{
|
1987-11-27 11:04:07 +00:00
|
|
|
cnt++;
|
1987-05-12 18:23:09 +00:00
|
|
|
l = g_getlink(p);
|
1997-02-21 11:27:57 +00:00
|
|
|
p++;
|
2019-02-18 16:39:05 +00:00
|
|
|
complength(l->l_rule, &i);
|
1987-11-27 11:04:07 +00:00
|
|
|
i.val += cnt;
|
2019-02-18 16:39:05 +00:00
|
|
|
if (l->l_flag & DEF)
|
|
|
|
{
|
1987-03-16 22:26:03 +00:00
|
|
|
X = i;
|
|
|
|
break;
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
if (compare(&i, &X) < 0)
|
|
|
|
{
|
1987-03-16 22:26:03 +00:00
|
|
|
X = i;
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
1987-03-16 22:26:03 +00:00
|
|
|
/* Fall through */
|
2019-02-18 16:39:05 +00:00
|
|
|
case EORULE:
|
1987-03-16 22:26:03 +00:00
|
|
|
le->cnt = X.cnt;
|
|
|
|
le->val = X.val;
|
1985-11-25 15:47:51 +00:00
|
|
|
return;
|
2019-02-18 16:39:05 +00:00
|
|
|
case TERM:
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
register int rep;
|
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
q = g_getterm(p);
|
1985-11-25 15:47:51 +00:00
|
|
|
rep = r_getkind(q);
|
1987-11-27 11:04:07 +00:00
|
|
|
X.val += 1;
|
2019-02-18 16:39:05 +00:00
|
|
|
if ((q->t_flags & PERSISTENT) || rep == FIXED || rep == PLUS)
|
|
|
|
{
|
|
|
|
complength(q->t_rule, &i);
|
1987-03-16 22:26:03 +00:00
|
|
|
add(&X, i.cnt, i.val);
|
2019-02-18 16:39:05 +00:00
|
|
|
if (rep == FIXED && r_getnum(q) > 0)
|
|
|
|
{
|
|
|
|
for (rep = r_getnum(q) - 1; rep > 0; rep--)
|
|
|
|
{
|
1987-03-16 22:26:03 +00:00
|
|
|
add(&X, i.cnt, i.val);
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
|
|
|
}
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NONTERM:
|
|
|
|
{
|
1987-05-12 18:23:09 +00:00
|
|
|
int nn = g_getcont(p);
|
1987-03-16 22:26:03 +00:00
|
|
|
register p_length pl = &length[nn];
|
|
|
|
int x = pl->cnt;
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
if (x == INFINITY)
|
|
|
|
{
|
1987-03-16 22:26:03 +00:00
|
|
|
pl->cnt = -1;
|
2019-02-18 16:39:05 +00:00
|
|
|
complength(nonterms[nn].n_rule, pl);
|
1987-03-16 22:26:03 +00:00
|
|
|
x = pl->cnt;
|
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
else if (x == -1)
|
|
|
|
x = INFINITY;
|
1987-03-16 22:26:03 +00:00
|
|
|
add(&X, x, pl->val);
|
1987-11-27 11:04:07 +00:00
|
|
|
X.val += 1;
|
2019-02-18 16:39:05 +00:00
|
|
|
}
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC void add(register p_length a, int c, int v)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
if (a->cnt == INFINITY || c == INFINITY)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
a->cnt = INFINITY;
|
|
|
|
return;
|
|
|
|
}
|
1987-11-27 11:04:07 +00:00
|
|
|
a->val += v;
|
1985-11-25 15:47:51 +00:00
|
|
|
a->cnt += c;
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC int compare(p_length a, p_length b)
|
|
|
|
{
|
|
|
|
if (a->cnt != b->cnt)
|
|
|
|
return a->cnt - b->cnt;
|
1985-11-25 15:47:51 +00:00
|
|
|
return a->val - b->val;
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC void setdefaults(register p_gram p)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
switch (g_gettype(p))
|
|
|
|
{
|
|
|
|
case EORULE:
|
1985-11-25 15:47:51 +00:00
|
|
|
return;
|
2019-02-18 16:39:05 +00:00
|
|
|
case TERM:
|
1987-05-12 18:23:09 +00:00
|
|
|
setdefaults(g_getterm(p)->t_rule);
|
1985-11-25 15:47:51 +00:00
|
|
|
break;
|
2019-02-18 16:39:05 +00:00
|
|
|
case ALTERNATION:
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
register p_link l, l1;
|
1987-11-27 11:04:07 +00:00
|
|
|
int temp = 0, temp1, cnt = 0;
|
1985-11-25 15:47:51 +00:00
|
|
|
t_length count, i;
|
|
|
|
|
|
|
|
count.cnt = INFINITY;
|
|
|
|
count.val = INFINITY;
|
1987-05-12 18:23:09 +00:00
|
|
|
l1 = g_getlink(p);
|
2019-02-18 16:39:05 +00:00
|
|
|
do
|
|
|
|
{
|
1987-11-27 11:04:07 +00:00
|
|
|
cnt++;
|
1987-05-12 18:23:09 +00:00
|
|
|
l = g_getlink(p);
|
1997-02-21 11:27:57 +00:00
|
|
|
p++;
|
2019-02-18 16:39:05 +00:00
|
|
|
complength(l->l_rule, &i);
|
1987-11-27 11:04:07 +00:00
|
|
|
i.val += cnt;
|
2019-02-18 16:39:05 +00:00
|
|
|
if (l->l_flag & DEF)
|
|
|
|
temp = 1;
|
1985-11-25 15:47:51 +00:00
|
|
|
temp1 = compare(&i, &count);
|
2019-02-18 16:39:05 +00:00
|
|
|
if (temp1 < 0 || (temp1 == 0 && l1->l_flag & AVOIDING))
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
l1 = l;
|
|
|
|
count = i;
|
|
|
|
}
|
|
|
|
setdefaults(l->l_rule);
|
|
|
|
} while (g_gettype(p) != EORULE);
|
2019-02-18 16:39:05 +00:00
|
|
|
if (!temp)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
/* No user specified default */
|
|
|
|
l1->l_flag |= DEF;
|
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
return;
|
|
|
|
}
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC void do_contains(register p_nont n)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
/*
|
|
|
|
* Compute the total set of symbols that nonterminal n can
|
1997-02-21 11:27:57 +00:00
|
|
|
* produce
|
1985-10-02 22:20:04 +00:00
|
|
|
*/
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
if (n->n_contains == 0)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
n->n_contains = get_set();
|
2019-02-18 16:39:05 +00:00
|
|
|
contains(n->n_rule, n->n_contains);
|
1985-10-02 22:20:04 +00:00
|
|
|
/*
|
|
|
|
* If the rule can produce empty, delete all symbols that
|
|
|
|
* can follow the rule as well as be in the rule.
|
|
|
|
* This is needed because the contains-set may only contain
|
|
|
|
* symbols that are guaranteed to be eaten by the rule!
|
|
|
|
* Otherwise, the generated parser may loop forever
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
if (n->n_flags & EMPTY)
|
|
|
|
{
|
|
|
|
setminus(n->n_contains, n->n_follow);
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* But the symbols that can start the rule are always
|
|
|
|
* eaten
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
setunion(n->n_contains, n->n_first);
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC void contains(register p_gram p, register p_set set)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
/*
|
|
|
|
* Does the real computation of the contains-sets
|
|
|
|
*/
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
switch (g_gettype(p))
|
|
|
|
{
|
|
|
|
case EORULE:
|
1985-10-02 22:20:04 +00:00
|
|
|
return;
|
2019-02-18 16:39:05 +00:00
|
|
|
case TERM:
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_term q;
|
1985-11-25 15:47:51 +00:00
|
|
|
int rep;
|
1985-10-02 22:20:04 +00:00
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
q = g_getterm(p);
|
1985-11-25 15:47:51 +00:00
|
|
|
rep = r_getkind(q);
|
2019-02-18 16:39:05 +00:00
|
|
|
if ((q->t_flags & PERSISTENT) || rep == PLUS || rep == FIXED)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
/*
|
|
|
|
* In these cases, the term belongs to the
|
|
|
|
* continuation grammar.
|
|
|
|
* Otherwise, q->t_contains is just
|
|
|
|
* q->t_first
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
if (!q->t_contains)
|
|
|
|
{
|
|
|
|
q->t_contains = get_set();
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
contains(q->t_rule, q->t_contains);
|
|
|
|
if (rep != FIXED || empty(q->t_rule))
|
|
|
|
{
|
|
|
|
setminus(q->t_contains, q->t_follow);
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
setunion(q->t_contains, q->t_first);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
contains(q->t_rule, (p_set) 0);
|
|
|
|
q->t_contains = q->t_first;
|
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
if (set)
|
|
|
|
setunion(set, q->t_contains);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NONTERM:
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_nont n;
|
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
n = &nonterms[g_getcont(p)];
|
1985-10-02 22:20:04 +00:00
|
|
|
do_contains(n);
|
2019-02-18 16:39:05 +00:00
|
|
|
if (set)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
setunion(set, n->n_contains);
|
2019-02-18 16:39:05 +00:00
|
|
|
if (ntneeded)
|
|
|
|
NTPUTIN(set, g_getcont(p));
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ALTERNATION:
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_link l;
|
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
l = g_getlink(p);
|
2019-02-18 16:39:05 +00:00
|
|
|
contains(l->l_rule, (l->l_flag & DEF) ? set : (p_set) 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LITERAL:
|
|
|
|
case TERMINAL:
|
|
|
|
{
|
|
|
|
register int hulp;
|
|
|
|
|
|
|
|
if (set)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
hulp = g_getcont(p);
|
1985-11-25 15:47:51 +00:00
|
|
|
assert(hulp < ntokens);
|
2019-02-18 16:39:05 +00:00
|
|
|
PUTIN(set, hulp);
|
|
|
|
}
|
|
|
|
}
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC int nsafes(register p_nont p)
|
|
|
|
{
|
|
|
|
int ch;
|
1985-11-25 15:47:51 +00:00
|
|
|
register int i;
|
|
|
|
|
|
|
|
ch = 0;
|
|
|
|
i = getntsafe(p);
|
2019-02-18 16:39:05 +00:00
|
|
|
if (i != NOSAFETY)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
i = do_safes(p->n_rule, i, &ch);
|
2019-02-18 16:39:05 +00:00
|
|
|
if (i < SCANDONE)
|
|
|
|
i = SCANDONE;
|
1986-10-29 11:16:55 +00:00
|
|
|
/* After a nonterminal, we only know whether a scan was done
|
2019-02-18 16:39:05 +00:00
|
|
|
or not
|
|
|
|
*/
|
|
|
|
if (getntout(p) != i)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
ch = 1;
|
2019-02-18 16:39:05 +00:00
|
|
|
setntout(p, i);
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
1985-11-25 15:47:51 +00:00
|
|
|
return ch;
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
STATIC int do_safes(register p_gram p, int safe, register int *ch)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
/*
|
|
|
|
* Walk the grammar rule, doing the computation described in the
|
|
|
|
* comment of the procedure above this one.
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
int retval;
|
1985-10-02 22:20:04 +00:00
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
switch (g_gettype(p))
|
|
|
|
{
|
|
|
|
case ACTION:
|
1997-02-21 11:27:57 +00:00
|
|
|
p++;
|
1985-10-02 22:20:04 +00:00
|
|
|
continue;
|
2019-02-18 16:39:05 +00:00
|
|
|
case LITERAL:
|
|
|
|
case TERMINAL:
|
1985-10-02 22:49:16 +00:00
|
|
|
safe = NOSCANDONE;
|
1985-10-02 22:20:04 +00:00
|
|
|
break;
|
2019-02-18 16:39:05 +00:00
|
|
|
case TERM:
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_term q;
|
2019-02-18 16:39:05 +00:00
|
|
|
int i, rep;
|
1985-10-02 22:20:04 +00:00
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
q = g_getterm(p);
|
1985-11-25 15:47:51 +00:00
|
|
|
i = r_getnum(q);
|
|
|
|
rep = r_getkind(q);
|
1985-10-02 22:20:04 +00:00
|
|
|
retval = do_safes(q->t_rule,
|
2019-02-18 16:39:05 +00:00
|
|
|
t_safety(rep, i, q->t_flags & PERSISTENT, safe), ch);
|
1985-11-25 15:47:51 +00:00
|
|
|
settout(q, retval);
|
|
|
|
safe = t_after(rep, i, retval);
|
2019-02-18 16:39:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ALTERNATION:
|
|
|
|
{
|
1997-02-21 11:27:57 +00:00
|
|
|
register p_link l;
|
1991-11-01 10:37:26 +00:00
|
|
|
register int i;
|
1985-10-02 22:20:04 +00:00
|
|
|
|
1991-11-01 10:37:26 +00:00
|
|
|
retval = -1;
|
2019-02-18 16:39:05 +00:00
|
|
|
while (g_gettype(p) == ALTERNATION)
|
|
|
|
{
|
1987-05-12 18:23:09 +00:00
|
|
|
l = g_getlink(p);
|
1997-02-21 11:27:57 +00:00
|
|
|
p++;
|
2019-02-18 16:39:05 +00:00
|
|
|
if (safe > SAFE && (l->l_flag & DEF))
|
|
|
|
{
|
|
|
|
i = do_safes(l->l_rule, SAFESCANDONE, ch);
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
else
|
|
|
|
i = do_safes(l->l_rule, SAFE, ch);
|
|
|
|
if (retval == -1)
|
|
|
|
retval = i;
|
|
|
|
else if (i != retval)
|
|
|
|
{
|
|
|
|
if (i == NOSCANDONE || retval == NOSCANDONE)
|
|
|
|
{
|
1985-10-03 12:38:55 +00:00
|
|
|
retval = SCANDONE;
|
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
else if (i > retval)
|
|
|
|
retval = i;
|
1985-10-03 12:38:55 +00:00
|
|
|
}
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
case NONTERM:
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
register p_nont n;
|
1985-11-25 15:47:51 +00:00
|
|
|
register int nsafe, osafe;
|
1985-10-02 22:20:04 +00:00
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
n = &nonterms[g_getcont(p)];
|
1985-10-03 12:38:55 +00:00
|
|
|
nsafe = getntsafe(n);
|
1985-10-02 22:49:16 +00:00
|
|
|
osafe = safe;
|
1985-10-03 12:38:55 +00:00
|
|
|
safe = getntout(n);
|
2019-02-18 16:39:05 +00:00
|
|
|
if (safe == NOSAFETY)
|
|
|
|
safe = SCANDONE;
|
|
|
|
if (osafe == nsafe)
|
|
|
|
break;
|
|
|
|
if (nsafe == NOSAFETY)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
*ch = 1;
|
1985-10-03 12:38:55 +00:00
|
|
|
setntsafe(n, osafe);
|
1985-10-02 22:20:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
if (osafe == NOSCANDONE || nsafe == NOSCANDONE)
|
|
|
|
{
|
|
|
|
if (nsafe != SCANDONE)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
*ch = 1;
|
1985-10-03 12:38:55 +00:00
|
|
|
setntsafe(n, SCANDONE);
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
if (osafe > nsafe)
|
|
|
|
{
|
1985-10-03 12:38:55 +00:00
|
|
|
setntsafe(n, osafe);
|
1985-11-25 15:47:51 +00:00
|
|
|
*ch = 1;
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EORULE:
|
1985-10-03 12:38:55 +00:00
|
|
|
return safe;
|
1985-10-02 22:20:04 +00:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
int t_safety(int rep, int count, int persistent, int safety)
|
|
|
|
{
|
1985-10-02 22:20:04 +00:00
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
if (safety == NOSCANDONE)
|
|
|
|
safety = SCANDONE;
|
|
|
|
switch (rep)
|
|
|
|
{
|
|
|
|
default:
|
1985-10-02 22:20:04 +00:00
|
|
|
assert(0);
|
2019-02-18 16:39:05 +00:00
|
|
|
case OPT:
|
|
|
|
if (!persistent || safety < SAFESCANDONE)
|
|
|
|
return SAFE;
|
1985-10-02 22:20:04 +00:00
|
|
|
return SAFESCANDONE;
|
2019-02-18 16:39:05 +00:00
|
|
|
case STAR:
|
|
|
|
if (persistent)
|
|
|
|
return SAFESCANDONE;
|
1985-10-02 22:20:04 +00:00
|
|
|
return SAFE;
|
2019-02-18 16:39:05 +00:00
|
|
|
case PLUS:
|
|
|
|
if (persistent)
|
|
|
|
{
|
|
|
|
if (safety > SAFESCANDONE)
|
|
|
|
return safety;
|
1985-10-02 22:20:04 +00:00
|
|
|
return SAFESCANDONE;
|
|
|
|
}
|
1985-11-25 15:47:51 +00:00
|
|
|
return safety;
|
2019-02-18 16:39:05 +00:00
|
|
|
case FIXED:
|
|
|
|
if (!count)
|
|
|
|
return safety;
|
1985-10-02 22:20:04 +00:00
|
|
|
return SCANDONE;
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1985-10-02 22:49:16 +00:00
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
int t_after(int rep, int count, int outsafety)
|
|
|
|
{
|
|
|
|
if (count == 0 && (rep == STAR || rep == PLUS))
|
|
|
|
{
|
1985-10-02 22:49:16 +00:00
|
|
|
return SAFESCANDONE;
|
|
|
|
}
|
2019-02-18 16:39:05 +00:00
|
|
|
if (rep != FIXED)
|
|
|
|
{
|
|
|
|
if (outsafety <= SAFESCANDONE)
|
|
|
|
return SAFESCANDONE;
|
1985-10-03 12:38:55 +00:00
|
|
|
return SCANDONE;
|
|
|
|
}
|
|
|
|
return outsafety;
|
1985-10-02 22:49:16 +00:00
|
|
|
}
|