Part 1 of warning/old k&r inconsistency correction.

This commit is contained in:
Manoel Trapier 2013-03-05 16:24:29 +01:00 committed by Manoël Trapier
parent 7eff32c40d
commit 9f7ae734db
8 changed files with 420 additions and 402 deletions

View File

@ -7,13 +7,11 @@
#include "system.h"
#include <unistd.h>
char *
sys_break(incr)
int incr;
char *sys_break(int incr)
{
register char *brk = sbrk(incr);
char *brk = (char*)sbrk(incr);
if (brk == (char *) 0 || brk == (char *)-1)
if ((brk == (char *) 0) || (brk == (char *)-1))
return ILL_BREAK;
return brk;
}

160
util/LLgen/src/LLgen.h Normal file
View File

@ -0,0 +1,160 @@
#ifndef LLGEN_H
#define LLGEN_H
/* alloc.c */
p_mem alloc(unsigned size);
p_mem ralloc(p_mem p, unsigned size);
p_mem new_mem(p_info p);
/* check.c */
void conflchecks(void);
void prline(char *s);
void printset(p_set p, char * s);
int check(p_gram p);
void moreverbose(p_set t);
void prrule(p_gram p);
void cfcheck(p_set s1, p_set s2, int flag);
void resolve(p_gram p);
void propagate(p_set set, p_gram p);
void spaces(void);
/* compute.c */
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.
*/
int cnt;
int val;
} t_length, *p_length;
void do_compute(void);
void createsets(void);
void walk(p_set u, p_gram p);
void co_trans(int (*fc)(p_nont));
int nempty(p_nont p);
int empty(p_gram p);
int nfirst(p_nont p);
int first(p_set setp, p_gram p, int flag);
int nfollow(p_nont p);
int follow(p_set setp, p_gram p);
void co_dirsymb(p_set setp, p_gram p);
void co_others(p_gram p);
int ncomplength(p_nont p);
int nc_nfirst(p_nont p);
STATIC int nc_nfollow(p_nont p);
void do_lengthcomp(void);
void complength(p_gram p, p_length le);
void add(p_length a, int c, int v);
int compare(p_length a, p_length b);
void setdefaults(p_gram p);
void do_contains(p_nont n);
void contains(p_gram p, p_set set);
int nsafes(p_nont p);
int do_safes(p_gram p, int safe, int *ch);
int t_safety(int rep, int count, int persistent, int safety);
int t_after(int rep, int count, int outsafety);
/* gencode.c */
void doclose(FILE *f);
int *mk_tokenlist(void);
void genhdr(void);
void gencode(int argc);
void opentemp(char * str);
void geninclude(void);
void genrecovery(void);
void generate(p_file f);
void prset(p_set p);
void macro(char * s, p_nont n);
void controlline(void);
void getparams(void);
void genprototypes(p_file f);
void getansiparams(int mkdef);
int gettok(void);
void rulecode(p_gram p, int safety, int mustscan, int mustpop);
void alternation(p_gram pp, int safety, int mustscan, int mustpop, int lb);
void genncrecovery(void);
int *dopush(p_gram p, int safety, int toplevel, int **pp);
void getaction(int flag);
int codeforterm(p_term q, int safety, int toplevel);
void genswhead(p_term q, int rep_kind, int rep_count, int safety, int ispushed);
void gencases(int *tokenlist, int caseno, int compacted);
char * genname(char * s);
void genpush(int d);
void genincrdecr(char * s, int d);
void genpop(int d);
int analyze_switch(int *tokenlist);
void add_cases(p_set s, int *tokenlist, int caseno);
void out_list(int *tokenlist, int listno, int casecnt);
void genextname(int d, char *s, FILE *f);
void correct_prefix(void);
/* LLgen.c */
void newnorder(int index);
void newtorder(int index);
int p_init(void);
void LL0_spec(void);
void mkalt(p_gram prod, int condition, int lc, p_gram res);
void mkterm(p_gram prod, int flags, int lc, p_gram result);
p_gram copyrule(p_gram p, int length);
/* Lpars.c */
void LLparse(void);
void LLscan(int t);
void LLread(void);
void LLerror(int t);
void LLsafeerror(int t);
int LLnext(int n);
int LLskip(void);
void LLnewlevel(unsigned int *LLsinfo);
void LLoldlevel(unsigned int *LLsinfo);
/* machdep.c */
void UNLINK(char * x);
void RENAME(char * x, char * y);
char * libpath(char * s);
/* main.c */
int main(int argc, char *argv[]);
void readgrammar(int argc, char *argv[]);
void doparse(p_file p);
void error(int lineno, char *s, char *t, char *u);
void warning(int lineno, char *s, char *t, char *u);
void fatal(int lineno, char *s, char *t, char *u);
void comfatal(void);
void copyfile(char *file);
void install(char *target, char *source);
/* name.c */
int name_init(void);
p_entry newentry(char * str, p_entry next);
char * store(char * s);
int hash(char * str);
p_gram search(int type, char * str, int option);
/* reach.c */
int co_reach(void);
void reachable(p_nont p);
void reachwalk(p_gram p);
/* sets.c */
int setinit(int nt_needed);
p_set get_set(void);
p_set setalloc(void);
int setunion(p_set a, p_set b);
int setintersect(p_set a, p_set b);
int setminus(p_set a, p_set b);
int setempty(p_set p);
int findindex(p_set set);
int setcount(p_set set, int *saved);
/* tokens.c */
void copyact(int ch1, int ch2, int flag, int level);
int scanner(void);
int input(void);
int unput(int c);
int skipcomment(int flag);
char * vallookup(int s);
char * cpy(int s, char * p, int inserted);
int LLmessage(int d);
#endif /* LLGEN_H */

View File

@ -24,29 +24,15 @@
# include "sets.h"
# include "assert.h"
# ifndef NORCSID
static string rcsid1 = "$Id$";
# endif
#include "LLgen.h"
static string c_first = "> firstset ";
static string c_contains = "> containset ";
static string c_follow = "> followset ";
p_set setalloc();
static int level;
/* In this file are defined : */
extern conflchecks();
STATIC prline();
STATIC printset();
STATIC int check();
STATIC moreverbose();
STATIC prrule();
STATIC cfcheck();
STATIC resolve();
STATIC propagate();
STATIC spaces();
conflchecks() {
void conflchecks() {
/*
* Check for conflicts, that is,
* in a repeating term, the FIRST and FOLLOW must be disjunct,
@ -63,7 +49,7 @@ conflchecks() {
for (p = nonterms; p < maxnt; p++) p->n_flags |= VERBOSE;
}
if (verbose) {
if ((fout = fopen(f_out,"w")) == NULL) fatal(1,e_noopen,f_out);
if ((fout = fopen(f_out,"w")) == NULL) fatal(1,e_noopen,f_out, NULL);
}
/*
* Check the rules in the order in which they are declared,

View File

@ -17,61 +17,23 @@
* Also checks the continuation grammar from the specified grammar.
*/
#include <stdlib.h>
# include "types.h"
# include "extern.h"
# include "sets.h"
# include "assert.h"
# include "io.h"
# ifndef NORCSID
static string rcsid = "$Id$";
# endif
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.
*/
int cnt;
int val;
} t_length, *p_length;
#include "LLgen.h"
/* Defined in this file : */
extern do_compute();
STATIC createsets();
STATIC walk();
STATIC co_trans();
STATIC int nempty();
extern empty();
STATIC int nfirst();
STATIC first();
STATIC int nfollow();
STATIC follow();
STATIC co_dirsymb();
STATIC co_others();
STATIC do_lengthcomp();
STATIC complength();
STATIC add();
STATIC int compare();
STATIC setdefaults();
STATIC do_contains();
STATIC contains();
STATIC int nsafes();
STATIC int do_safes();
#ifdef NON_CORRECTING
STATIC int nc_nfirst();
STATIC nc_first();
STATIC int nc_nfollow();
STATIC nc_follow();
#endif
do_compute() {
void do_compute(void) {
/*
* Does all the work, by calling other routines (divide and conquer)
*/
register p_nont p;
register p_start st;
p_nont p;
p_start st;
createsets();
co_trans(nempty); /* Which nonterminals produce empty? */
@ -160,22 +122,21 @@ do_compute() {
# endif
}
STATIC
createsets() {
STATIC void createsets() {
/*
* Allocate space for the sets. Also determine which files use
* which nonterminals, and determine which nonterminals can be
* made static.
*/
register p_nont p;
register p_file f;
register p_start st;
register int i;
p_nont p;
p_file f;
p_start st;
int i;
int n = NINTS(NBYTES(nnonterms));
p_mem alloc();
for (f = files; f < maxfiles; f++) {
register p_set s;
p_set s;
f->f_used = s = (p_set) alloc((unsigned)n*sizeof(*(f->f_used)));
for (i = n; i; i--) *s++ = 0;
for (i = f->f_nonterminals; i != -1; i = p->n_next) {
@ -192,7 +153,7 @@ createsets() {
}
for (f = files; f < maxfiles; f++) {
for (i = f->f_nonterminals; i != -1; i = p->n_next) {
register p_file f2;
p_file f2;
p = &nonterms[i];
for (f2 = files; f2 < maxfiles; f2++) {
@ -207,8 +168,7 @@ createsets() {
}
}
STATIC
walk(u, p) p_set u; register p_gram p; {
STATIC void walk(p_set u, p_gram p) {
/*
* Walk through the grammar rule p, allocating sets
*/
@ -216,7 +176,7 @@ walk(u, p) p_set u; register p_gram p; {
for (;;) {
switch (g_gettype(p)) {
case TERM : {
register p_term q;
p_term q;
q = g_getterm(p);
q->t_first = get_set();
@ -228,7 +188,7 @@ walk(u, p) p_set u; register p_gram p; {
walk(u, q->t_rule);
break; }
case ALTERNATION : {
register p_link l;
p_link l;
l = g_getlink(p);
l->l_symbs = get_set();
@ -239,7 +199,7 @@ walk(u, p) p_set u; register p_gram p; {
walk(u, l->l_rule);
break; }
case NONTERM : {
register int i = g_getcont(p);
int i = g_getcont(p);
PUTIN(u, i);
break; }
@ -250,10 +210,9 @@ walk(u, p) p_set u; register p_gram p; {
}
}
STATIC
co_trans(fc) int (*fc)(); {
register p_nont p;
register int change;
STATIC void co_trans(int (*fc)(p_nont)) {
p_nont p;
int change;
do {
change = 0;
@ -263,8 +222,7 @@ co_trans(fc) int (*fc)(); {
} while (change);
}
STATIC int
nempty(p) register p_nont p; {
STATIC int nempty(p_nont p) {
if (!(p->n_flags & EMPTY) && empty(p->n_rule)) {
p->n_flags |= EMPTY;
return 1;
@ -272,7 +230,7 @@ nempty(p) register p_nont p; {
return 0;
}
empty(p) register p_gram p; {
int empty(p_gram p) {
/*
* Does the rule pointed to by p produce empty ?
*/
@ -282,7 +240,7 @@ empty(p) register p_gram p; {
case EORULE :
return 1;
case TERM : {
register p_term q;
p_term q;
q = g_getterm(p);
if (r_getkind(q) == STAR
@ -308,19 +266,17 @@ empty(p) register p_gram p; {
}
}
STATIC int
nfirst(p) register p_nont p; {
STATIC int nfirst(p_nont p) {
return first(p->n_first, p->n_rule, 0);
}
#ifdef NON_CORRECTING
STATIC int nc_nfirst(p) register p_nont p; {
STATIC int nc_nfirst(p_nont p) {
return nc_first(p->n_nc_first, p->n_rule, 0);
}
#endif
STATIC
first(setp,p,flag) p_set setp; register p_gram p; {
STATIC int first(p_set setp, p_gram p, int flag) {
/*
* Compute the FIRST set of rule p.
* If flag = 0, also the first sets for terms and alternations in
@ -328,8 +284,8 @@ first(setp,p,flag) p_set setp; register p_gram p; {
* The FIRST set is put in setp.
* return 1 if the set refered to by "setp" changed
*/
register s; /* Will gather return value */
int noenter;/* when set, unables entering of elements into
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.
*/
@ -341,11 +297,11 @@ first(setp,p,flag) p_set setp; register p_gram p; {
case EORULE :
return s;
case TERM : {
register p_term q;
p_term q;
q = g_getterm(p);
if (flag == 0) {
if (first(q->t_first,q->t_rule,0))/*nothing*/;
first(q->t_first,q->t_rule,0);
}
if (!noenter) s |= setunion(setp,q->t_first);
p++;
@ -354,11 +310,11 @@ first(setp,p,flag) p_set setp; register p_gram p; {
empty(q->t_rule)) continue;
break; }
case ALTERNATION : {
register p_link l;
p_link l;
l = g_getlink(p);
if (flag == 0) {
if (first(l->l_symbs,l->l_rule,0))/*nothing*/;
first(l->l_symbs,l->l_rule,0);
}
if (noenter == 0) {
s |= setunion(setp,l->l_symbs);
@ -378,7 +334,7 @@ first(setp,p,flag) p_set setp; register p_gram p; {
p++;
break;
case NONTERM : {
register p_nont n;
p_nont n;
n = &nonterms[g_getcont(p)];
if (noenter == 0) {
@ -398,8 +354,7 @@ first(setp,p,flag) p_set setp; register p_gram p; {
}
#ifdef NON_CORRECTING
STATIC
nc_first(setp,p,flag) p_set setp; register p_gram p; {
STATIC int nc_first(p_set setp, p_gram p, int flag) {
/*
* Compute the non_corr FIRST set of rule p.
* If flag = 0, also the non_corr first sets for terms and
@ -424,11 +379,11 @@ nc_first(setp,p,flag) p_set setp; register p_gram p; {
case EORULE :
return s;
case TERM : {
register p_term q;
p_term q;
q = g_getterm(p);
if (flag == 0) {
if (nc_first(q->t_nc_first,q->t_rule,0))/*nothing*/;
nc_first(q->t_nc_first,q->t_rule,0);
}
if (!noenter) s |= setunion(setp,q->t_nc_first);
p++;
@ -437,11 +392,11 @@ nc_first(setp,p,flag) p_set setp; register p_gram p; {
empty(q->t_rule)) continue;
break; }
case ALTERNATION : {
register p_link l;
p_link l;
l = g_getlink(p);
if (flag == 0) {
if (nc_first(l->l_nc_symbs,l->l_rule,0))/*nothing*/;
nc_first(l->l_nc_symbs,l->l_rule,0);
}
if (noenter == 0) {
s |= setunion(setp,l->l_nc_symbs);
@ -451,17 +406,23 @@ nc_first(setp,p,flag) p_set setp; register p_gram p; {
p++;
continue;
case ACTION : {
register p_start subp;
p_start subp;
if (!noenter)
{
if (subpars_sim)
{
s |= setunion(setp, start_firsts);
else {
for (subp = g_getsubparse(p); subp;
subp = subp->ff_next)
s |= setunion(setp, (&nonterms[subp->ff_nont])->n_nc_first);
}
else
{
for (subp = g_getsubparse(p); subp; subp = subp->ff_next)
{
s |= setunion(setp, (&nonterms[subp->ff_nont])->n_nc_first);
}
}
}
p++;
continue;
}
@ -479,7 +440,7 @@ nc_first(setp,p,flag) p_set setp; register p_gram p; {
p++;
break;
case NONTERM : {
register p_nont n;
p_nont n;
n = &nonterms[g_getcont(p)];
if (noenter == 0) {
@ -499,19 +460,17 @@ nc_first(setp,p,flag) p_set setp; register p_gram p; {
}
#endif
STATIC int
nfollow(p) register p_nont p; {
STATIC int nfollow(p_nont p) {
return follow(p->n_follow, p->n_rule);
}
STATIC
follow(setp,p) p_set setp; register p_gram p; {
STATIC int follow(p_set setp, p_gram p) {
/*
* 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.
*/
register s; /* Will gather return value */
int s; /* Will gather return value */
s = 0;
for (;;) {
@ -519,7 +478,7 @@ follow(setp,p) p_set setp; register p_gram p; {
case EORULE :
return s;
case TERM : {
register p_term q;
p_term q;
q = g_getterm(p);
if (empty(p+1)) {
@ -558,7 +517,7 @@ follow(setp,p) p_set setp; register p_gram p; {
s |= follow(setp,g_getlink(p)->l_rule);
break;
case NONTERM : {
register p_nont n;
p_nont n;
n = &nonterms[g_getcont(p)];
s |= first(n->n_follow,p+1,1);
@ -578,19 +537,17 @@ follow(setp,p) p_set setp; register p_gram p; {
#ifdef NON_CORRECTING
STATIC int
nc_nfollow(p) register p_nont p; {
STATIC int nc_nfollow(p_nont p) {
return follow(p->n_nc_follow, p->n_rule);
}
STATIC
nc_follow(setp,p) p_set setp; register p_gram p; {
STATIC int nc_follow(p_set setp, p_gram p) {
/*
* 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.
*/
register s; /* Will gather return value */
int s; /* Will gather return value */
s = 0;
for (;;) {
@ -598,7 +555,7 @@ nc_follow(setp,p) p_set setp; register p_gram p; {
case EORULE :
return s;
case TERM : {
register p_term q;
p_term q;
q = g_getterm(p);
if (empty(p+1)) {
@ -637,7 +594,7 @@ nc_follow(setp,p) p_set setp; register p_gram p; {
s |= nc_follow(setp,g_getlink(p)->l_rule);
break;
case NONTERM : {
register p_nont n;
p_nont n;
n = &nonterms[g_getcont(p)];
s |= nc_first(n->n_nc_follow,p+1,1);
@ -657,25 +614,24 @@ nc_follow(setp,p) p_set setp; register p_gram p; {
#endif
STATIC
co_dirsymb(setp,p) p_set setp; register p_gram p; {
STATIC void co_dirsymb(p_set setp, p_gram p) {
/*
* Walk the rule p, doing the work for alternations
*/
register p_gram s = 0;
p_gram s = 0;
for (;;) {
switch (g_gettype(p)) {
case EORULE :
return;
case TERM : {
register p_term q;
p_term q;
q = g_getterm(p);
co_dirsymb(q->t_follow,q->t_rule);
break; }
case ALTERNATION : {
register p_link l;
p_link l;
/*
* Save first alternative
*/
@ -705,13 +661,12 @@ co_dirsymb(setp,p) p_set setp; register p_gram p; {
}
}
STATIC
co_others(p) register p_gram p; {
STATIC void co_others(p_gram p) {
/*
* compute the l_others-sets for the list of alternatives
* indicated by p
*/
register p_link l1,l2;
p_link l1,l2;
l1 = g_getlink(p);
p++;
@ -732,11 +687,9 @@ co_others(p) register p_gram p; {
static p_length length;
# define INFINITY 32767
STATIC
ncomplength(p)
register p_nont p;
STATIC int ncomplength(p_nont p)
{
register p_length pl = &length[p - nonterms];
p_length pl = &length[p - nonterms];
int x = pl->cnt;
pl->cnt = -1;
@ -744,8 +697,8 @@ ncomplength(p)
return pl->cnt < INFINITY && x == INFINITY;
}
STATIC
do_lengthcomp() {
STATIC void do_lengthcomp()
{
/*
* Compute the minimum length of a terminal production for each
* nonterminal.
@ -755,8 +708,8 @@ do_lengthcomp() {
* - a crude measure of the number of terms and nonterminals in the
* production of this shortest string.
*/
register p_length pl;
register p_nont p;
p_length pl;
p_nont p;
p_mem alloc();
length = (p_length) alloc((unsigned) (nnonterms * sizeof(*length)));
@ -776,13 +729,12 @@ do_lengthcomp() {
free ((p_mem) length);
}
STATIC
complength(p,le) register p_gram p; p_length le; {
STATIC void complength(p_gram p, p_length le) {
/*
* Walk grammar rule p, computing minimum lengths
*/
register p_link l;
register p_term q;
p_link l;
p_term q;
t_length i;
t_length X;
int cnt = 0;
@ -825,7 +777,7 @@ complength(p,le) register p_gram p; p_length le; {
le->val = X.val;
return;
case TERM : {
register int rep;
int rep;
q = g_getterm(p);
rep = r_getkind(q);
@ -844,7 +796,7 @@ complength(p,le) register p_gram p; p_length le; {
break; }
case NONTERM : {
int nn = g_getcont(p);
register p_length pl = &length[nn];
p_length pl = &length[nn];
int x = pl->cnt;
if (x == INFINITY) {
@ -861,9 +813,7 @@ complength(p,le) register p_gram p; p_length le; {
}
}
STATIC
add(a, c, v) register p_length a; {
STATIC void add(p_length a, int c, int v) {
if (a->cnt == INFINITY || c == INFINITY) {
a->cnt = INFINITY;
return;
@ -872,14 +822,12 @@ add(a, c, v) register p_length a; {
a->cnt += c;
}
STATIC int
compare(a, b) register p_length a, b; {
STATIC int compare(p_length a, p_length b) {
if (a->cnt != b->cnt) return a->cnt - b->cnt;
return a->val - b->val;
}
STATIC
setdefaults(p) register p_gram p; {
STATIC void setdefaults(p_gram p) {
for (;;) {
switch(g_gettype(p)) {
case EORULE:
@ -888,7 +836,7 @@ setdefaults(p) register p_gram p; {
setdefaults(g_getterm(p)->t_rule);
break;
case ALTERNATION: {
register p_link l, l1;
p_link l, l1;
int temp = 0, temp1, cnt = 0;
t_length count, i;
@ -920,8 +868,7 @@ setdefaults(p) register p_gram p; {
}
}
STATIC
do_contains(n) register p_nont n; {
STATIC void do_contains(p_nont n) {
/*
* Compute the total set of symbols that nonterminal n can
* produce
@ -948,8 +895,7 @@ do_contains(n) register p_nont n; {
}
}
STATIC
contains(p,set) register p_gram p; register p_set set; {
STATIC void contains(p_gram p, p_set set) {
/*
* Does the real computation of the contains-sets
*/
@ -959,7 +905,7 @@ contains(p,set) register p_gram p; register p_set set; {
case EORULE :
return;
case TERM : {
register p_term q;
p_term q;
int rep;
q = g_getterm(p);
@ -987,7 +933,7 @@ contains(p,set) register p_gram p; register p_set set; {
if (set) setunion(set,q->t_contains);
break; }
case NONTERM : {
register p_nont n;
p_nont n;
n = &nonterms[g_getcont(p)];
do_contains(n);
@ -997,7 +943,7 @@ contains(p,set) register p_gram p; register p_set set; {
}
break; }
case ALTERNATION : {
register p_link l;
p_link l;
l = g_getlink(p);
contains(l->l_rule,
@ -1005,7 +951,7 @@ contains(p,set) register p_gram p; register p_set set; {
break; }
case LITERAL :
case TERMINAL : {
register hulp;
int hulp;
if (set) {
hulp = g_getcont(p);
@ -1017,9 +963,9 @@ contains(p,set) register p_gram p; register p_set set; {
}
}
STATIC int nsafes(p) register p_nont p; {
STATIC int nsafes(p) p_nont p; {
int ch;
register int i;
int i;
ch = 0;
i = getntsafe(p);
@ -1038,7 +984,7 @@ STATIC int nsafes(p) register p_nont p; {
}
STATIC int
do_safes(p,safe,ch) register p_gram p; register int *ch; {
do_safes(p,safe,ch) p_gram p; int *ch; {
/*
* Walk the grammar rule, doing the computation described in the
* comment of the procedure above this one.
@ -1055,7 +1001,7 @@ do_safes(p,safe,ch) register p_gram p; register int *ch; {
safe = NOSCANDONE;
break;
case TERM : {
register p_term q;
p_term q;
int i,rep;
q = g_getterm(p);
@ -1067,8 +1013,8 @@ do_safes(p,safe,ch) register p_gram p; register int *ch; {
safe = t_after(rep, i, retval);
break; }
case ALTERNATION : {
register p_link l;
register int i;
p_link l;
int i;
retval = -1;
while (g_gettype(p) == ALTERNATION) {
@ -1089,8 +1035,8 @@ do_safes(p,safe,ch) register p_gram p; register int *ch; {
}
return retval; }
case NONTERM : {
register p_nont n;
register int nsafe, osafe;
p_nont n;
int nsafe, osafe;
n = &nonterms[g_getcont(p)];
nsafe = getntsafe(n);
@ -1122,7 +1068,7 @@ do_safes(p,safe,ch) register p_gram p; register int *ch; {
}
}
t_safety(rep, count, persistent, safety) {
int t_safety(int rep, int count, int persistent, int safety) {
if (safety == NOSCANDONE) safety = SCANDONE;
switch(rep) {
@ -1147,7 +1093,7 @@ t_safety(rep, count, persistent, safety) {
/* NOTREACHED */
}
t_after(rep, count, outsafety) {
int t_after(int rep, int count, int outsafety) {
if (count == 0 && (rep == STAR || rep == PLUS)) {
return SAFESCANDONE;
}

View File

@ -18,6 +18,8 @@
* This file is a mess, it should be cleaned up some time.
*/
#include <stdlib.h>
# include "types.h"
# include "io.h"
# include "extern.h"
@ -25,18 +27,16 @@
# include "assert.h"
# include "cclass.h"
# ifndef NORCSID
static string rcsid3 = "$Id$";
#endif /* NORCSID */
#include "LLgen.h"
/*
* Some codestrings used more than once
* Some codechar *s used more than once
*/
static string c_arrend = "0 };\n";
static string c_close = "}\n";
static string c_break = "break;\n";
static string c_read = "LLread();\n";
static char * c_arrend = "0 };\n";
static char * c_close = "}\n";
static char * c_break = "break;\n";
static char * c_read = "LLread();\n";
/* Some constants used for reading from the action file */
# define ENDDECL 0400
@ -47,65 +47,28 @@ static int firsts; /* are there any? */
static int listcount;
/* In this file the following routines are defined: */
extern gencode();
STATIC opentemp();
STATIC geninclude();
STATIC genrecovery();
#ifdef NON_CORRECTING
STATIC genncrecovery();
#endif
STATIC string genname();
STATIC generate();
STATIC prset();
STATIC macro();
STATIC controlline();
STATIC getparams();
STATIC getansiparams();
STATIC genprototypes();
STATIC gettok();
STATIC rulecode();
STATIC int * dopush();
STATIC int * mk_tokenlist();
STATIC getaction();
STATIC alternation();
STATIC codeforterm();
STATIC genswhead();
STATIC gencases();
STATIC genpush();
STATIC genpop();
STATIC genincrdecr();
STATIC add_cases();
STATIC int analyze_switch();
STATIC out_list();
STATIC genextname();
STATIC correct_prefix();
# define NOPOP -20000
p_mem alloc(), ralloc();
doclose(f)
FILE *f;
void doclose(FILE *f)
{
if (ferror(f) != 0) {
fatal(0,"Write error on temporary");
fatal(0,"Write error on temporary", NULL, NULL);
}
fclose(f);
}
STATIC int *
mk_tokenlist()
STATIC int *mk_tokenlist()
{
register int i = ntokens;
register int *p = (int *)alloc((unsigned)(i * sizeof(int))) + i;
int i = ntokens;
int *p = (int *)alloc((unsigned)(i * sizeof(int))) + i;
while (i--) *--p = -1;
return p;
}
STATIC
genhdr()
STATIC void genhdr()
{
if (!firsts) fputs("#define LLNOFIRSTS\n", fpars);
if (ansi_c) fputs("#define LL_ANSI_C 1\n", fpars);
@ -119,12 +82,12 @@ genhdr()
copyfile(incl_file);
}
gencode(argc) {
register p_file p = files;
void gencode(int argc) {
p_file p = files;
/* Set up for code generation */
if ((fact = fopen(f_temp,"r")) == NULL) {
fatal(0,e_noopen,f_temp);
fatal(0,e_noopen,f_temp, NULL);
}
#ifdef NON_CORRECTING
@ -157,22 +120,20 @@ gencode(argc) {
fclose(fact);
}
STATIC
opentemp(str) string str; {
STATIC void opentemp(char *str) {
if ((fpars = fopen(f_pars,"w")) == NULL) {
fatal(0,e_noopen,f_pars);
fatal(0,e_noopen,f_pars, NULL);
}
if (!str) str = ".";
fprintf(fpars,LLgenid,str);
}
STATIC
geninclude() {
register p_token p;
STATIC void geninclude() {
p_token p;
int maxno = 0;
opentemp((string) 0);
opentemp((char *) 0);
for (p = tokens; p < maxt; p++) {
if (p->t_tokno > maxno) maxno = p->t_tokno;
if (p->t_tokno >= 0400) {
@ -192,18 +153,17 @@ geninclude() {
install(f_include, ".");
}
STATIC
genrecovery() {
register FILE *f;
register p_token t;
register int *q;
register p_nont p;
register p_set *psetl;
STATIC void genrecovery() {
FILE *f;
p_token t;
int *q;
p_nont p;
p_set *psetl;
int *index;
int i;
register p_start st;
p_start st;
opentemp((string) 0);
opentemp((char *) 0);
f = fpars;
correct_prefix();
genhdr();
@ -304,16 +264,15 @@ genrecovery() {
}
#ifdef NON_CORRECTING
STATIC
genncrecovery() {
register FILE *f;
register p_token t;
register int *q;
STATIC void genncrecovery() {
FILE *f;
p_token t;
int *q;
int *index;
/* Generate the non-correcting error recovery file */
opentemp((string) 0);
opentemp((char *) 0);
f = fpars;
genhdr();
@ -343,15 +302,14 @@ genncrecovery() {
}
#endif
STATIC
generate(f) p_file f; {
STATIC void generate(p_file f) {
/*
* Generates a parsing routine for every nonterminal
*/
register int s;
register p_nont p;
int s;
p_nont p;
int i;
register p_first ff;
p_first ff;
int mustpop;
int is_first = 1;
@ -413,10 +371,9 @@ generate(f) p_file f; {
}
}
STATIC
prset(p) p_set p; {
register int k;
register unsigned i;
STATIC void prset(p_set p) {
int k;
unsigned i;
int j;
j = nbytes;
@ -434,8 +391,7 @@ prset(p) p_set p; {
/* NOTREACHED */
}
STATIC
macro(s,n) string s; p_nont n; {
STATIC void macro(char * s, p_nont n) {
int i;
i = findindex(n->n_first);
@ -449,29 +405,27 @@ macro(s,n) string s; p_nont n; {
fprintf(fpars,"#define %s(x) LLfirst((x), %d)\n", s, i);
}
STATIC
controlline() {
STATIC void controlline() {
/* Copy a compiler control line */
register int l;
register FILE *f1,*f2;
int l;
FILE *f1,*f2;
f1 = fact; f2 = fpars;
l = getc(f1);
assert(l == '\0');
do {
l = getc(f1);
if (l == EOF) fatal(0, "temp file mangled");
if (l == EOF) fatal(0, "temp file mangled", NULL, NULL);
putc(l,f2);
} while ( l != '\n' ) ;
}
STATIC
getparams() {
STATIC void getparams() {
/* getparams is called if a nonterminal has parameters. The names
* of the parameters have to be found, and they should be declared
*/
long off;
register int l;
int l;
long ftell();
char first;
char add_semi = ' ';
@ -508,15 +462,13 @@ getparams() {
fprintf(fpars, "%c\n",add_semi);
}
STATIC
genprototypes(f)
register p_file f;
STATIC void genprototypes(p_file f)
{
/*
* Generate prototypes for all nonterminals
*/
register int i;
register p_nont p;
int i;
p_nont p;
long off = ftell(fact);
fputs("#if LL_ANSI_C\n", fpars);
@ -555,13 +507,12 @@ genprototypes(f)
fputs("#endif\n", fpars);
}
STATIC
getansiparams(mkdef) {
STATIC void getansiparams(int mkdef) {
/* getansiparams is called if a nonterminal has parameters
* and an ANSI C function definition/declaration has to be produced.
* If a definition has to be produced, "mkdef" is set to 1.
*/
register int l;
int l;
int delayed = 0;
ltext[0] = '\0';
@ -586,12 +537,11 @@ getansiparams(mkdef) {
fprintf(fpars, ") %c\n", mkdef ? ' ' : ';');
}
STATIC
gettok() {
STATIC int gettok() {
/* Read from the action file. */
register int ch;
register string c;
register FILE *f;
int ch;
char *c;
FILE *f;
f = fact;
ch = getc(f);
@ -624,15 +574,14 @@ gettok() {
}
}
STATIC
rulecode(p,safety,mustscan,mustpop) register p_gram p; {
STATIC void rulecode(p_gram p, int safety, int mustscan, int mustpop) {
/*
* Code for a production rule.
*/
register int toplevel = 1;
register FILE *f;
register int *ppu;
int toplevel = 1;
FILE *f;
int *ppu;
int *pushlist;
int *ppushlist;
@ -663,8 +612,8 @@ rulecode(p,safety,mustscan,mustpop) register p_gram p; {
return;
case LITERAL :
case TERMINAL : {
register p_token t;
string s;
p_token t;
char * s;
t = &tokens[g_getcont(p)];
if (toplevel == 0) {
@ -694,7 +643,7 @@ rulecode(p,safety,mustscan,mustpop) register p_gram p; {
safety = NOSCANDONE;
break; }
case NONTERM : {
register p_nont n = &nonterms[g_getcont(p)];
p_nont n = &nonterms[g_getcont(p)];
if (safety == NOSCANDONE &&
getntsafe(n) < NOSCANDONE) {
@ -734,13 +683,11 @@ rulecode(p,safety,mustscan,mustpop) register p_gram p; {
}
}
STATIC
alternation(pp, safety, mustscan, mustpop, lb)
p_gram pp;
STATIC void alternation(p_gram pp, int safety, int mustscan, int mustpop, int lb)
{
register p_gram p = pp;
register FILE *f = fpars;
register p_link l;
p_gram p = pp;
FILE *f = fpars;
p_link l;
int hulp, hulp1,hulp2;
int haddefault = 0;
int nsafe;
@ -891,12 +838,12 @@ alternation(pp, safety, mustscan, mustpop, lb)
}
STATIC int *
dopush(p,safety,toplevel,pp) register p_gram p; int **pp; {
dopush(p,safety,toplevel,pp) p_gram p; int **pp; {
/*
* The safety only matters if toplevel != 0
*/
unsigned int i = 100;
register int *ip = (int *) alloc(100 * sizeof(int));
int *ip = (int *) alloc(100 * sizeof(int));
*pp = ip;
@ -912,7 +859,7 @@ dopush(p,safety,toplevel,pp) register p_gram p; int **pp; {
case ALTERNATION :
return ip;
case TERM : {
register p_term q;
p_term q;
int rep_kind, rep_count;
q = g_getterm(p);
@ -934,7 +881,7 @@ dopush(p,safety,toplevel,pp) register p_gram p; int **pp; {
if (toplevel == 0) *ip++ = -(g_getcont(p)+1);
break;
case NONTERM : {
register p_nont n;
p_nont n;
n = &nonterms[g_getcont(p)];
if (toplevel == 0 ||
@ -955,14 +902,13 @@ dopush(p,safety,toplevel,pp) register p_gram p; int **pp; {
# define max(a,b) ((a) < (b) ? (b) : (a))
STATIC
getaction(flag) {
STATIC void getaction(int flag) {
/* Read an action from the action file.
* flag = 1 if it is an action,
* 0 when reading parameters
*/
register int ch;
register FILE *f;
int ch;
FILE *f;
int mark = 0;
if (flag == 1) {
@ -991,14 +937,13 @@ getaction(flag) {
if (flag) fputs("\n",f);
}
STATIC
codeforterm(q,safety,toplevel) register p_term q; {
STATIC int codeforterm(p_term q, int safety, int toplevel) {
/*
* Generate code for a term
*/
register FILE *f = fpars;
register int rep_count = r_getnum(q);
register int rep_kind = r_getkind(q);
FILE *f = fpars;
int rep_count = r_getnum(q);
int rep_kind = r_getkind(q);
int term_is_persistent = (q->t_flags & PERSISTENT);
int ispushed = NOPOP;
@ -1024,7 +969,7 @@ codeforterm(q,safety,toplevel) register p_term q; {
}
if (rep_count) {
/* N > 0, so generate fixed forloop */
fputs("{\nregister LL_i;\n", f);
fputs("{\nLL_i;\n", f);
assert(ispushed != NOPOP);
fprintf(f, "for (LL_i = %d; LL_i >= 0; LL_i--) {\n", rep_count - 1);
if (rep_kind == FIXED) {
@ -1075,18 +1020,17 @@ codeforterm(q,safety,toplevel) register p_term q; {
if (rep_kind != OPT && (rep_kind != FIXED || rep_count > 0)) {
fputs(c_close, f); /* Close for */
if (rep_count > 0) {
fputs(c_close, f);/* Close Register ... */
fputs(c_close, f);/* Close ... */
}
}
return t_after(rep_kind, rep_count, gettout(q));
}
STATIC
genswhead(q, rep_kind, rep_count, safety, ispushed) register p_term q; {
STATIC void genswhead(p_term q, int rep_kind, int rep_count, int safety, int ispushed) {
/*
* Generate switch statement for term q
*/
register FILE *f = fpars;
FILE *f = fpars;
p_set p1;
p_set setalloc();
int hulp1 = 0, hulp2;
@ -1180,9 +1124,7 @@ genswhead(q, rep_kind, rep_count, safety, ispushed) register p_term q; {
free((p_mem) tokenlist);
}
STATIC
gencases(tokenlist, caseno, compacted)
int *tokenlist;
STATIC void gencases(int *tokenlist, int caseno, int compacted)
{
/*
* setp points to a bitset indicating which cases must
@ -1199,32 +1141,33 @@ gencases(tokenlist, caseno, compacted)
* labeledstatement : labels statement ;
* labels : labels label | ;
*/
register p_token p;
register int i;
p_token p;
int i;
if (compacted) fprintf(fpars, "case %d :\n", caseno);
for (i = 0, p = tokens; i < ntokens; i++, p++) {
if (tokenlist[i] == caseno) {
fprintf(fpars,
compacted ?
(p->t_tokno < 0400 ? "/* case '%s' */\n" :
"/* case %s */\n") :
p->t_tokno<0400 ? "case /* '%s' */ %d : ;\n"
: "case /* %s */ %d : ;\n",
p->t_string, i);
if (tokenlist[i] == caseno)
{
if (compacted)
fprintf(fpars, p->t_tokno < 0400 ? "/* case '%s' */\n" :
"/* case %s */\n",
p->t_string);
else
fprintf(fpars, p->t_tokno<0400 ? "case /* '%s' */ %d : ;\n" :
"case /* %s */ %d : ;\n",
p->t_string, i);
}
}
}
static char namebuf[20];
STATIC string
genname(s) string s; {
STATIC char *genname(char * s) {
/*
* Generate a target file name from the
* source file name s.
*/
register string c,d;
char *c, *d;
c = namebuf;
while (*s) {
@ -1238,7 +1181,7 @@ genname(s) string s; {
for (d = c; --d > namebuf;) if (*d == '.') break;
if (d == namebuf) d = c;
if (d >= &namebuf[12]) {
fatal(0,"%s : filename too long",namebuf);
fatal(0,"%s : filename too long",namebuf, NULL);
}
*d++ = '.';
*d++ = 'c';
@ -1246,13 +1189,11 @@ genname(s) string s; {
return namebuf;
}
STATIC
genpush(d) {
STATIC void genpush(int d) {
genincrdecr("incr", d);
}
STATIC
genincrdecr(s, d) string s; {
STATIC void genincrdecr(char *s, int d) {
if (d == NOPOP) return;
if (d >= 0) {
fprintf(fpars, "LLs%s(%d);\n", s, d / nbytes);
@ -1261,16 +1202,13 @@ genincrdecr(s, d) string s; {
fprintf(fpars, "LLt%s(%d);\n", s, -(d + 1));
}
STATIC
genpop(d) {
STATIC void genpop(int d) {
genincrdecr("decr", d);
}
STATIC int
analyze_switch(tokenlist)
int *tokenlist;
STATIC int analyze_switch(int *tokenlist)
{
register int i;
int i;
int ncases = 0;
int percentage;
int maxcase = 0, mincase = 0;
@ -1290,12 +1228,9 @@ analyze_switch(tokenlist)
return percentage >= low_percentage && percentage <= high_percentage;
}
STATIC
add_cases(s, tokenlist, caseno)
p_set s;
int *tokenlist;
STATIC void add_cases(p_set s, int *tokenlist, int caseno)
{
register int i;
int i;
for (i = 0; i < ntokens; i++) {
if (IN(s, i)) {
@ -1304,12 +1239,10 @@ add_cases(s, tokenlist, caseno)
}
}
STATIC
out_list(tokenlist, listno, casecnt)
int *tokenlist;
STATIC void out_list(int *tokenlist, int listno, int casecnt)
{
register int i;
register FILE *f = fpars;
int i;
FILE *f = fpars;
fprintf(f, "static %s LL%d_tklist[] = {",
casecnt <= 127 ? "char" : "short",
@ -1322,19 +1255,15 @@ out_list(tokenlist, listno, casecnt)
fprintf(f, "switch(LL%d_tklist[LLcsymb]) {\n", listno);
}
STATIC
genextname(d, s, f)
char *s;
FILE *f;
STATIC void genextname(int d, char *s, FILE *f)
{
fprintf(f, "%s%d_%s", prefix ? prefix : "LL", d, s);
}
STATIC
correct_prefix()
STATIC void correct_prefix()
{
register FILE *f = fpars;
register char *s = prefix;
FILE *f = fpars;
char *s = prefix;
if (s) {
fprintf(f, "#define LLsymb %ssymb\n", s);

View File

@ -16,20 +16,24 @@
* Machine dependant things
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
# include "types.h"
#include "LLgen.h"
# ifndef NORCSID
static string rcsid5 = "$Id$";
# endif
/* In this file the following routines are defined: */
//extern UNLINK();
//extern RENAME();
//extern string libpath();
/* extern UNLINK(); */
/* extern RENAME(); */
/* extern string libpath(); */
UNLINK(x) string x; {
void UNLINK(string x) {
/* Must remove the file "x" */
#ifdef USE_SYS
@ -39,14 +43,14 @@ UNLINK(x) string x; {
#endif
}
RENAME(x,y) string x,y; {
void RENAME(char *x, char *y) {
/* Must move the file "x" to the file "y" */
#ifdef USE_SYS
if(! sys_rename(x,y)) fatal(1,"Cannot rename to %s",y);
#else
if (rename(x, y) == -1)
fatal(1, "Cannot rename to %s", y);
fatal(1, "Cannot rename to %s", y, NULL);
#endif
}
@ -55,9 +59,9 @@ libpath(s) string s; {
/* Must deliver a full pathname to the library file "s" */
register string p;
register length;
int length;
p_mem alloc();
// string strcpy(), strcat();
/* string strcpy(), strcat(); */
char* libdir = getenv("LLGEN_LIB_DIR");
if (!libdir)

View File

@ -16,31 +16,27 @@
* Contains main program, and some error message routines
*/
#include "types.h"
#include "io.h"
#include "extern.h"
#include "sets.h"
#include "assert.h"
#include "LLgen.h"
#include <stdlib.h>
#include <string.h>
# include "types.h"
# include "io.h"
# include "extern.h"
# include "sets.h"
# include "assert.h"
#include <unistd.h>
# ifndef NORCSID
void *sbrk(void *addr);
char *mktemp(char *template);
#ifndef NORCSID
static string rcsid6 = "$Id$";
# endif
#endif
/* In this file the following routines are defined: */
extern int main();
STATIC readgrammar();
STATIC doparse();
extern error();
extern fatal();
extern comfatal();
extern copyfile();
extern install();
extern char *mktemp();
extern char *sbrk();
main(argc,argv) register string argv[]; {
int main(int argc, char *argv[]) {
register string arg;
string libpath();
char *beg_sbrk = 0;
@ -130,7 +126,7 @@ main(argc,argv) register string argv[]; {
argc--;
}
if (verbose) beg_sbrk = sbrk(0);
if (verbose) beg_sbrk = sbrk(NULL);
#ifdef NON_CORRECTING
if ((subpars_sim) && (!non_corr)) {
@ -207,13 +203,12 @@ main(argc,argv) register string argv[]; {
fprintf(stderr, "number of tokens: %d\n", ntokens);
fprintf(stderr, "number of term structures: %d\n", nterms);
fprintf(stderr, "number of alternation structures: %d\n", nalts);
fprintf(stderr, "total memory used: %ld\n", (long)(sbrk(0) - beg_sbrk));
fprintf(stderr, "total memory used: %ld\n", (long)((long)sbrk(0) - (long)beg_sbrk));
}
exit(0);
}
STATIC
readgrammar(argc,argv) char *argv[]; {
STATIC void readgrammar(int argc, char *argv[]) {
/*
* Do just what the name suggests : read the grammar
*/
@ -233,7 +228,7 @@ readgrammar(argc,argv) char *argv[]; {
} else {
while (argc--) {
if ((finput = fopen(f_input=argv[1],"r")) == NULL) {
fatal(0,e_noopen,f_input);
fatal(0, e_noopen, f_input, NULL);
}
doparse(p++);
argv++;
@ -246,13 +241,12 @@ readgrammar(argc,argv) char *argv[]; {
* There must be a start symbol!
*/
if (! nerrors && start == 0) {
fatal(linecount,"Missing %%start");
fatal(linecount, "Missing %%start", NULL, NULL);
}
if (nerrors) comfatal();
}
STATIC
doparse(p) register p_file p; {
STATIC void doparse(p_file p) {
linecount = 0;
p->f_name = f_input;
@ -266,7 +260,7 @@ doparse(p) register p_file p; {
}
/* VARARGS1 */
error(lineno,s,t,u) string s,t,u; {
void error(int lineno, char *s, char *t, char *u) {
/*
* Just an error message
*/
@ -279,7 +273,7 @@ error(lineno,s,t,u) string s,t,u; {
}
/* VARARGS1 */
warning(lineno,s,t,u) string s,t,u; {
void warning(int lineno, char *s, char *t, char *u) {
/*
* Just a warning
*/
@ -292,7 +286,7 @@ warning(lineno,s,t,u) string s,t,u; {
}
/* VARARGS1 */
fatal(lineno,s,t,u) string s,t,u; {
void fatal(int lineno, char *s, char *t, char *u) {
/*
* Fatal error
*/
@ -300,7 +294,7 @@ fatal(lineno,s,t,u) string s,t,u; {
comfatal();
}
comfatal() {
void comfatal(void) {
/*
* Some common code for exit on errors
*/
@ -313,7 +307,7 @@ comfatal() {
exit(1);
}
copyfile(file) string file; {
void copyfile(char *file) {
/*
* Copies a file indicated by the parameter to filedescriptor fpars.
*/
@ -321,13 +315,13 @@ copyfile(file) string file; {
register FILE *f;
if ((f = fopen(file,"r")) == NULL) {
fatal(0,"Cannot open library file %s, call an expert",file);
fatal(0,"Cannot open library file %s, call an expert",file, NULL);
}
while ((c = getc(f)) != EOF) putc(c,fpars);
fclose(f);
}
install(target, source) string target, source; {
void install(char *target, char *source) {
/*
* Copy the temporary file generated from source to target
* if allowed (which means that the target must be generated
@ -341,7 +335,7 @@ install(target, source) string target, source; {
* First open temporary, generated for source
*/
if ((f1 = fopen(f_pars,"r")) == NULL) {
fatal(0,e_noopen,f_pars);
fatal(0,e_noopen,f_pars, NULL);
}
/*
* Now open target for reading
@ -371,7 +365,7 @@ install(target, source) string target, source; {
*/
if (c1 != c2) {
if (cnt >= 0) {
fatal(0,"%s : not a file generated by LLgen",target);
fatal(0,"%s : not a file generated by LLgen",target, NULL);
}
RENAME(f_pars,target);
}

View File

@ -24,6 +24,7 @@
* [B | C]* becomes X; X: B X | C X | {empty} etc.
*/
#include <stdlib.h>
# include "types.h"
# include "extern.h"