From da48891d6e43a89cdeaa86181ab3d231c0a302b4 Mon Sep 17 00:00:00 2001 From: ceriel Date: Mon, 29 Jan 1990 13:45:42 +0000 Subject: [PATCH] allow for multiple parsers within one program --- util/LLgen/src/LLgen.g | 21 ++++++++++-- util/LLgen/src/extern.h | 1 + util/LLgen/src/gencode.c | 72 ++++++++++++++++++++++++++++++---------- util/LLgen/src/global.c | 9 +++-- util/LLgen/src/io.h | 18 +++++----- util/LLgen/src/main.c | 10 ++++++ util/LLgen/src/tokens.g | 2 ++ 7 files changed, 101 insertions(+), 32 deletions(-) diff --git a/util/LLgen/src/LLgen.g b/util/LLgen/src/LLgen.g index b82df0c7f..afa8cc72f 100644 --- a/util/LLgen/src/LLgen.g +++ b/util/LLgen/src/LLgen.g @@ -140,6 +140,21 @@ def { register string p; } else error(linecount,"Duplicate %%lexical"); } ';' + | C_PREFIX C_IDENT + /* + * Prefix of external names (default: LL) + */ + { if (!prefix) { + prefix = store(lextoken.t_string); + if (strlen(prefix) > 6) { + error(linecount, + "%%prefix too long"); + prefix[6] = 0; + } + } + else error(linecount,"Duplicate %%prefix"); + } + ';' | C_ONERROR C_IDENT { if (! onerror) { onerror = store(lextoken.t_string); @@ -230,7 +245,7 @@ productions(p_gram *p;) { if (n_alts >= max_alts-2) { alt_table = (p_gram ) ralloc( (p_mem) alt_table, - (max_alts+=ALTINCR)*sizeof(t_gram)); + (unsigned)(max_alts+=ALTINCR)*sizeof(t_gram)); } if (t & DEF) { if (haddefault) { @@ -316,7 +331,7 @@ simpleproduction(p_gram *p; register int *conflres;) { if (n_rules >= max_rules-2) { rule_table = (p_gram) ralloc( (p_mem) rule_table, - (max_rules+=RULEINCR)*sizeof(t_gram)); + (unsigned)(max_rules+=RULEINCR)*sizeof(t_gram)); } kind = FIXED; cnt = 0; @@ -344,7 +359,7 @@ simpleproduction(p_gram *p; register int *conflres;) if (n_rules >= max_rules-2) { rule_table = (p_gram) ralloc( (p_mem) rule_table, - (max_rules+=RULEINCR)*sizeof(t_gram)); + (unsigned)(max_rules+=RULEINCR)*sizeof(t_gram)); } } elem = *--(q->t_rule); diff --git a/util/LLgen/src/extern.h b/util/LLgen/src/extern.h index ac078044b..217b5625f 100644 --- a/util/LLgen/src/extern.h +++ b/util/LLgen/src/extern.h @@ -55,6 +55,7 @@ extern string e_noopen; /* Error message string used often */ extern int verbose; /* Level of verbosity */ extern int wflag; /* warnings? */ extern string lexical; /* name of lexical analyser */ +extern string prefix; /* prefix of externals */ extern string onerror; /* name of user error handler */ extern int ntneeded; /* ntneeded = 1 if nonterminals are included * in the sets. diff --git a/util/LLgen/src/gencode.c b/util/LLgen/src/gencode.c index f1e063d43..c0da9d603 100644 --- a/util/LLgen/src/gencode.c +++ b/util/LLgen/src/gencode.c @@ -73,6 +73,8 @@ STATIC genincrdecr(); STATIC add_cases(); STATIC int analyze_switch(); STATIC out_list(); +STATIC genextname(); +STATIC correct_prefix(); # define NOPOP -20000 @@ -90,8 +92,8 @@ doclose(f) STATIC int * mk_tokenlist() { - register int *p = (int *)alloc(ntokens * sizeof(int)) + ntokens; register int i = ntokens; + register int *p = (int *)alloc((unsigned)(i * sizeof(int))) + i; while (i--) *--p = -1; @@ -111,6 +113,7 @@ gencode(argc) { /* Open temporary */ f_input = p->f_name; opentemp(f_input); + correct_prefix(); /* generate code ... */ copyfile(incl_file); generate(p); @@ -147,7 +150,7 @@ geninclude() { } } doclose(fpars); - install(HFILE, "."); + install(f_include, "."); } STATIC @@ -163,6 +166,7 @@ genrecovery() { opentemp((string) 0); f = fpars; + correct_prefix(); copyfile(incl_file); if (!firsts) fputs("#define LLNOFIRSTS\n", f); for (st = start; st; st = st->ff_next) { @@ -190,9 +194,8 @@ genrecovery() { if (g_gettype(p->n_rule) == ALTERNATION) { genpush(findindex(p->n_contains)); } - fprintf(f, "\tL%d_%s();\n", - st->ff_nont, - p->n_name); + genextname(st->ff_nont, p->n_name, f); + fputs("();\n", f); if (getntout(p) == NOSCANDONE) { fputs("\tLL_NOSCANDONE(EOFILE);\n",f); } @@ -224,7 +227,7 @@ genrecovery() { fputs("#define LL_NEWMESS\n", f); copyfile(rec_file); doclose(f); - install(RFILE, "."); + install(f_rec, "."); } STATIC @@ -257,12 +260,13 @@ generate(f) p_file f; { getntparams(p) == 0) { continue; } - fprintf(fpars,"L%d_%s (\n",s->o_index,p->n_name); + genextname(s->o_index, p->n_name, fpars); if (p->n_flags & PARAMS) { + fputs("(\n", fpars); controlline(); getparams(); } - else fputs(") {\n", fpars); + else fputs("() {\n", fpars); if (p->n_flags & LOCALS) getaction(1); i = getntsafe(p); mustpop = NOPOP; @@ -491,12 +495,14 @@ rulecode(p,safety,mustscan,mustpop) register p_gram p; { getntsafe(n) <= SAFESCANDONE)) { genpop(findindex(n->n_contains)); } - fprintf(f,"L%d_%s(\n",g_getcont(p), n->n_name); + genextname(g_getcont(p), n->n_name, f); if (g_getnpar(p)) { + fputs("(\n", f); controlline(); getaction(0); + fputs(");\n",f); } - fputs(");\n",f); + else fputs("();\n", f); safety = getntout(n); break; } case TERM : @@ -604,8 +610,7 @@ alternation(pp, safety, mustscan, mustpop, lb) compacted = analyze_switch(tokenlist); if (compacted) { fputs("{", f); - out_list(tokenlist, listcount, casecnt); - fprintf(f, "switch(LL%d_tklist[LLcsymb]) {\n", listcount++); + out_list(tokenlist, listcount++, casecnt); } else fputs("switch(LLcsymb) {\n", f); casecnt = 0; @@ -811,7 +816,6 @@ codeforterm(q,safety,toplevel) register p_term q; { register int rep_kind = r_getkind(q); int term_is_persistent = (q->t_flags & PERSISTENT); int ispushed = NOPOP; - int sw = SAFE; if (!(toplevel > 0 && (safety == 0 || (!onerror && safety <= SAFESCANDONE)) && @@ -858,7 +862,7 @@ codeforterm(q,safety,toplevel) register p_term q; { ispushed = NOPOP; } if (rep_kind == STAR || rep_kind == OPT) { - sw = genswhead(q, rep_kind, rep_count, safety, ispushed); + genswhead(q, rep_kind, rep_count, safety, ispushed); } rulecode(q->t_rule, t_safety(rep_kind,rep_count,term_is_persistent,safety), @@ -872,7 +876,7 @@ codeforterm(q,safety,toplevel) register p_term q; { if (rep_count) { fputs("if (!LL_i) break;\n", f); } - sw = genswhead(q, rep_kind, rep_count, safety, ispushed); + genswhead(q, rep_kind, rep_count, safety, ispushed); } if (rep_kind != OPT && rep_kind != FIXED) fputs("continue;\n", f); if (rep_kind != FIXED) { @@ -940,8 +944,7 @@ genswhead(q, rep_kind, rep_count, safety, ispushed) register p_term q; { compacted = analyze_switch(tokenlist); fputs("{", f); if (compacted) { - out_list(tokenlist, listcount, casecnt); - fprintf(f, "switch(LL%d_tklist[LLcsymb]) {\n", listcount++); + out_list(tokenlist, listcount++, casecnt); } else fputs("switch(LLcsymb) {\n", f); casecnt = 0; @@ -990,7 +993,6 @@ genswhead(q, rep_kind, rep_count, safety, ispushed) register p_term q; { genpop(ispushed); } free((p_mem) tokenlist); - return safeterm; } STATIC @@ -1132,4 +1134,38 @@ out_list(tokenlist, listno, casecnt) fprintf(f, "%c%d,", i % 10 == 0 ? '\n': ' ', tokenlist[i]); } fputs(c_arrend, f); + fprintf(f, "switch(LL%d_tklist[LLcsymb]) {\n", listcount); +} + +STATIC +genextname(d, s, f) + char *s; + FILE *f; +{ + fprintf(f, "%s%d_%s", prefix ? prefix : "LL", d, s); +} + +STATIC +correct_prefix() +{ + register FILE *f = fpars; + register char *s = prefix; + + if (s) { + fprintf(f, "#define LLsymb %ssymb\n", s); + fprintf(f, "#define LLerror %serror\n", s); + fprintf(f, "#ifndef LL_FASTER\n#define LLscan %sscan\n#endif\n", s); + fprintf(f, "#define LLscnt %sscnt\n", s); + fprintf(f, "#define LLtcnt %stcnt\n", s); + fprintf(f, "#define LLcsymb %scsymb\n", s); + fprintf(f, "#define LLread %sread\n", s); + fprintf(f, "#define LLskip %sskip\n", s); + fprintf(f, "#define LLnext %snext\n", s); + fprintf(f, "#define LLfirst %sfirst\n", s); + fprintf(f, "#define LLnewlevel %snewlevel\n", s); + fprintf(f, "#define LLoldlevel %soldlevel\n", s); + fprintf(f, "#define LLlex %slex\n", s); + fprintf(f, "#define LLmessage %smessage\n", s); + } + fprintf(f, "#include \"%s\"\n", f_include); } diff --git a/util/LLgen/src/global.c b/util/LLgen/src/global.c index a26338db4..2e563131b 100644 --- a/util/LLgen/src/global.c +++ b/util/LLgen/src/global.c @@ -41,14 +41,17 @@ FILE *fout; FILE *fpars; FILE *finput; FILE *fact; -string f_pars = PARSERFILE; -string f_out = OUTFILE; -string f_temp = ACTFILE; +char f_pars[] = PARSERFILE; +char f_temp[] = ACTFILE; +char f_out[20]; string f_input; +char f_include[20]; +char f_rec[20]; string e_noopen = "Cannot open %s"; int verbose; int wflag; string lexical; +string prefix; string onerror; int ntneeded; int ntprint; diff --git a/util/LLgen/src/io.h b/util/LLgen/src/io.h index 01f0cc438..6e2686df0 100644 --- a/util/LLgen/src/io.h +++ b/util/LLgen/src/io.h @@ -21,17 +21,19 @@ /* FILES */ -# define OUTFILE "LL.output" /* -v option */ -# define PARSERFILE "LL.xxx" /* This is what we want */ -# define ACTFILE "LL.temp" /* temporary file to save actions */ -# define HFILE "Lpars.h" /* file for "#define's " */ -# define RFILE "Lpars.c" /* Error recovery */ +# define OUTFILE "%s.output" /* -v option */ +# define PARSERFILE "xxxXXXXXX" /* This is what we want */ +# define ACTFILE "tempXXXXXX" /* temporary file to save actions */ +# define HFILE "%spars.h" /* file for "#define's " */ +# define RFILE "%spars.c" /* Error recovery */ extern FILE *finput; extern FILE *fpars; extern FILE *fact; extern FILE *fout; -extern string f_pars; -extern string f_temp; -extern string f_out; +extern char f_pars[]; +extern char f_temp[]; +extern char f_out[]; extern string f_input; +extern char f_include[]; +extern char f_rec[]; diff --git a/util/LLgen/src/main.c b/util/LLgen/src/main.c index 14b890780..d410fb4b6 100644 --- a/util/LLgen/src/main.c +++ b/util/LLgen/src/main.c @@ -36,6 +36,7 @@ extern fatal(); extern comfatal(); extern copyfile(); extern install(); +extern char *mktemp(); # ifndef NDEBUG extern badassertion(); # endif not NDEBUG @@ -129,12 +130,21 @@ main(argc,argv) register string argv[]; { # ifndef NDEBUG } # endif + mktemp(f_temp); + mktemp(f_pars); if ((fact = fopen(f_temp,"w")) == NULL) { fputs("Cannot create temporary\n",stderr); exit(1); } name_init(); readgrammar(argc,argv); + sprintf(f_out, OUTFILE, prefix ? prefix : "LL"); + + /* for the following two filenames only one L is used; historical + reasons ... + */ + sprintf(f_include, HFILE, prefix ? prefix : "L"); + sprintf(f_rec, RFILE, prefix ? prefix : "L"); setinit(ntneeded); maxnt = &nonterms[nnonterms]; maxt = &tokens[ntokens]; diff --git a/util/LLgen/src/tokens.g b/util/LLgen/src/tokens.g index 425ce6c35..8f1481c35 100644 --- a/util/LLgen/src/tokens.g +++ b/util/LLgen/src/tokens.g @@ -56,6 +56,7 @@ STATIC string vallookup(); %token C_PERSISTENT ; %token C_FIRST ; %token C_LEXICAL ; +%token C_PREFIX ; %token C_ONERROR ; %token C_AVOID ; %token C_PREFER ; @@ -91,6 +92,7 @@ static t_keyw resword[] = { { "start", C_START }, { "lexical", C_LEXICAL }, { "onerror", C_ONERROR }, + { "prefix", C_PREFIX }, { 0, 0 } }; -- 2.34.1