From 10717cc791cc789906d4a0ffed0406c0de739cf8 Mon Sep 17 00:00:00 2001 From: carl Date: Tue, 19 Feb 2019 00:39:05 +0800 Subject: [PATCH] Better ANSI C compatibility and portability: + Addition of function prototypes and include files. + Change function definitions to ANSI C style. - Remove support for generating K&R code. - Remove mkstemp and replace by tmpnam (more portable but less safe) --- util/LLgen/src/LLgen.c | 327 ++++----- util/LLgen/src/LLgen.g | 78 ++- util/LLgen/src/Lpars.c | 94 --- util/LLgen/src/alloc.c | 17 +- util/LLgen/src/alloc.h | 19 + util/LLgen/src/check.c | 532 ++++++++------- util/LLgen/src/compute.c | 1198 ++++++++++++++++++-------------- util/LLgen/src/extern.h | 23 +- util/LLgen/src/gencode.c | 1357 +++++++++++++++++++++---------------- util/LLgen/src/global.c | 6 +- util/LLgen/src/io.h | 2 - util/LLgen/src/machdep.c | 41 +- util/LLgen/src/main.c | 297 ++++---- util/LLgen/src/name.c | 187 ++--- util/LLgen/src/reach.c | 23 +- util/LLgen/src/savegram.c | 12 +- util/LLgen/src/sets.c | 54 +- util/LLgen/src/sets.h | 43 ++ util/LLgen/src/tokens.c | 103 ++- util/LLgen/src/tokens.g | 81 +-- util/LLgen/src/types.h | 4 + 21 files changed, 2435 insertions(+), 2063 deletions(-) create mode 100644 util/LLgen/src/alloc.h diff --git a/util/LLgen/src/LLgen.c b/util/LLgen/src/LLgen.c index 6bb52f174..8676a4ca4 100644 --- a/util/LLgen/src/LLgen.c +++ b/util/LLgen/src/LLgen.c @@ -2,9 +2,6 @@ #include "Lpars.h" #define LL_LEXI scanner #define LLNOFIRSTS -#if __STDC__ || __cplusplus -#define LL_ANSI_C 1 -#endif #define LL_LEXI scanner /* $Id$ */ #ifdef LL_DEBUG @@ -38,7 +35,6 @@ extern int LLstartsymb; #define LLsincr(d) LLscnt[d]++ #define LLtincr(d) LLtcnt[d]++ -#if LL_ANSI_C extern int LL_LEXI(void); extern void LLread(void); extern int LLskip(void); @@ -56,33 +52,15 @@ extern int LLfirst(int, int); #if LL_NON_CORR extern void LLnc_recover(void); #endif -#else /* not LL_ANSI_C */ -extern LLread(); -extern int LLskip(); -extern int LLnext(); -extern LLerror(); -extern LLsafeerror(); -extern LLnewlevel(); -extern LLoldlevel(); -#ifndef LL_FASTER -extern LLscan(); -#endif -#ifndef LLNOFIRSTS -extern int LLfirst(); -#endif -#if LL_NON_CORR -extern LLnc_recover(); -#endif -#endif /* not LL_ANSI_C */ # line 20 "LLgen.g" -# include -# include -# include "types.h" -# include "io.h" -# include "extern.h" -# include "assert.h" -# include "cclass.h" +#include +#include +#include "types.h" +#include "io.h" +#include "extern.h" +#include "assert.h" +#include "cclass.h" # ifndef NORCSID static string rcsid = "$Id$"; @@ -106,15 +84,14 @@ static int max_rules; #define RULEINCR 32 /* Here are defined : */ -STATIC newnorder(); -STATIC newtorder(); -STATIC mkalt(); -STATIC mkterm(); -STATIC p_gram copyrule(); +STATIC void newnorder(int index); +STATIC void newtorder(int index); +STATIC void mkalt(p_gram prod, int condition,int lc,register p_gram res); +STATIC void mkterm(p_gram prod, int flags,int lc,register p_gram result); +STATIC p_gram copyrule(register p_gram p,int length); /* and of course LLparse() */ -STATIC -newnorder(index) { +STATIC void newnorder(int index) { static int porder; if (norder != -1) { @@ -125,8 +102,7 @@ newnorder(index) { nonterms[porder].n_next = -1; } -STATIC -newtorder(index) { +STATIC void newtorder(int index) { static int porder; if (torder != -1) { @@ -137,7 +113,7 @@ newtorder(index) { tokens[porder].t_next = -1; } -p_init() +void p_init(void) { alt_table = (p_gram )alloc(ALTINCR*sizeof(t_gram)); n_alts = 0; @@ -147,47 +123,31 @@ p_init() max_rules = RULEINCR; } -#if LL_ANSI_C static void LL1_def(void); static void LL2_rule(void); static void LL3_listel(void); static void LL4_firsts(void); static void LL5_productions( -# line 246 "LLgen.g" +# line 244 "LLgen.g" p_gram *p) ; static void LL6_simpleproduction( -# line 332 "LLgen.g" +# line 330 "LLgen.g" p_gram *p ,register int *conflres) ; static void LL7_elem( -# line 480 "LLgen.g" +# line 478 "LLgen.g" register p_gram pres) ; static void LL8_repeats( -# line 602 "LLgen.g" +# line 600 "LLgen.g" int *kind ,int *cnt) ; static void LL9_number( -# line 619 "LLgen.g" +# line 617 "LLgen.g" int *t) ; -#else -static LL1_def(); -static LL2_rule(); -static LL3_listel(); -static LL4_firsts(); -static LL5_productions(); -static LL6_simpleproduction(); -static LL7_elem(); -static LL8_repeats(); -static LL9_number(); -#endif -#if LL_ANSI_C void -#endif LL0_spec( -#if LL_ANSI_C void -#endif ) { LLsincr(0); -# line 96 "LLgen.g" +# line 94 "LLgen.g" { acount = 0; p_init(); } for (;;) { goto L_1; @@ -215,7 +175,7 @@ continue; LLsdecr(0); break; } -# line 98 "LLgen.g" +# line 96 "LLgen.g" { /* * Put an endmarker in temporary file */ @@ -226,15 +186,11 @@ break; } } static -#if LL_ANSI_C void -#endif LL1_def( -#if LL_ANSI_C void -#endif ) { -# line 108 "LLgen.g" +# line 106 "LLgen.g" register string p; switch(LLcsymb) { case /* C_IDENT */ 2 : ; @@ -275,13 +231,13 @@ LLtincr(2); LLtincr(24); LL_SAFE(C_START); LL_NOSCANDONE(C_IDENT); -# line 118 "LLgen.g" +# line 116 "LLgen.g" { p = store(lextoken.t_string); } LLtdecr(23); LL_NOSCANDONE(','); LLtdecr(2); LL_NOSCANDONE(C_IDENT); -# line 123 "LLgen.g" +# line 121 "LLgen.g" { /* * Put the declaration in the list * of start symbols @@ -309,11 +265,11 @@ case /* C_LEXICAL */ 14 : ; LLtincr(24); LL_SAFE(C_LEXICAL); LL_NOSCANDONE(C_IDENT); -# line 149 "LLgen.g" +# line 147 "LLgen.g" { if (!lexical) { lexical = store(lextoken.t_string); } - else error(linecount,"Duplicate %%lexical"); + else error(linecount,"Duplicate %%lexical", NULL); } LLtdecr(24); LL_NOSCANDONE(';'); @@ -322,16 +278,16 @@ case /* C_PREFIX */ 15 : ; LLtincr(24); LL_SAFE(C_PREFIX); LL_NOSCANDONE(C_IDENT); -# line 159 "LLgen.g" +# line 157 "LLgen.g" { if (!prefix) { prefix = store(lextoken.t_string); if (strlen(prefix) > 6) { error(linecount, - "%%prefix too long"); + "%%prefix too long",NULL); prefix[6] = 0; } } - else error(linecount,"Duplicate %%prefix"); + else error(linecount,"Duplicate %%prefix", NULL); } LLtdecr(24); LL_NOSCANDONE(';'); @@ -340,25 +296,25 @@ case /* C_ONERROR */ 16 : ; LLtincr(24); LL_SAFE(C_ONERROR); LL_NOSCANDONE(C_IDENT); -# line 171 "LLgen.g" +# line 169 "LLgen.g" { #ifdef NON_CORRECTING if (non_corr) { - warning(linecount, "%%onerror conflicts with -n option"); + warning(linecount, "%%onerror conflicts with -n option", NULL); } else #endif if (! onerror) { onerror = store(lextoken.t_string); } - else error(linecount,"Duplicate %%onerror"); + else error(linecount,"Duplicate %%onerror", NULL); } LLtdecr(24); LL_NOSCANDONE(';'); break; default: LL_SSCANDONE(C_ACTION); -# line 184 "LLgen.g" +# line 182 "LLgen.g" { acount++; } break; case /* C_FIRST */ 13 : ; @@ -367,31 +323,23 @@ break; } } static -#if LL_ANSI_C void -#endif LL3_listel( -#if LL_ANSI_C void -#endif ) { LL_NOSCANDONE(C_IDENT); -# line 194 "LLgen.g" +# line 192 "LLgen.g" { p_gram temp = search(TERMINAL,lextoken.t_string,ENTERING); newtorder(g_getcont(temp)); tokens[g_getcont(temp)].t_lineno = linecount; } } static -#if LL_ANSI_C void -#endif LL2_rule( -#if LL_ANSI_C void -#endif ) { -# line 200 "LLgen.g" +# line 198 "LLgen.g" register p_nont p; p_gram rr; register p_gram temp; @@ -402,7 +350,7 @@ LLtincr(25); LLsincr(1); LLtincr(24); LL_SAFE(C_IDENT); -# line 207 "LLgen.g" +# line 205 "LLgen.g" { temp = search(NONTERM,lextoken.t_string,BOTH); p = &nonterms[g_getcont(temp)]; if (p->n_rule) { @@ -437,11 +385,11 @@ else if (LL_3 & 1) goto L_1;} case /* C_PARAMS */ 6 : ; LLtdecr(6); LL_SAFE(C_PARAMS); -# line 225 "LLgen.g" +# line 223 "LLgen.g" { if (lextoken.t_num > 0) { p->n_flags |= PARAMS; if (lextoken.t_num > 15) { - error(linecount,"Too many parameters"); + error(linecount,"Too many parameters", NULL); } else setntparams(p,lextoken.t_num); } @@ -463,41 +411,34 @@ else if (LL_4 & 1) goto L_2;} case /* C_ACTION */ 7 : ; LLtdecr(7); LL_SAFE(C_ACTION); -# line 234 "LLgen.g" +# line 232 "LLgen.g" { p->n_flags |= LOCALS; } LLread(); } } LLtdecr(25); LL_SCANDONE(':'); -# line 236 "LLgen.g" +# line 234 "LLgen.g" { in_production = 1; } LLread(); LLsdecr(1); LL5_productions( -# line 237 "LLgen.g" +# line 235 "LLgen.g" &rr); LLtdecr(24); LL_SCANDONE(';'); -# line 238 "LLgen.g" +# line 236 "LLgen.g" { in_production = 0; } -# line 243 "LLgen.g" +# line 241 "LLgen.g" { nonterms[g_getcont(temp)].n_rule = rr;} } static -#if LL_ANSI_C void -#endif LL5_productions( -#if LL_ANSI_C -# line 246 "LLgen.g" +# line 244 "LLgen.g" p_gram *p) -#else -# line 246 "LLgen.g" - p) p_gram *p; -#endif { -# line 250 "LLgen.g" +# line 248 "LLgen.g" p_gram prod; int conflres = 0; int t = 0; @@ -506,12 +447,12 @@ p_gram *p) int o_lc, n_lc; LLtincr(26); -# line 257 "LLgen.g" +# line 255 "LLgen.g" { o_lc = linecount; } LL6_simpleproduction( -# line 258 "LLgen.g" +# line 256 "LLgen.g" p,&conflres); -# line 259 "LLgen.g" +# line 257 "LLgen.g" { if (conflres & DEF) haddefault = 1; } goto L_2; /* so that the label is used for certain */ L_2: ; @@ -523,13 +464,13 @@ LLsdecr(1); LLtincr(26); for (;;) { LL_SAFE('|'); -# line 261 "LLgen.g" +# line 259 "LLgen.g" { n_lc = linecount; } LLread(); LL6_simpleproduction( -# line 262 "LLgen.g" +# line 260 "LLgen.g" &prod,&t); -# line 263 "LLgen.g" +# line 261 "LLgen.g" { if (n_alts >= max_alts-2) { alt_table = (p_gram ) ralloc( (p_mem) alt_table, @@ -538,7 +479,7 @@ LL6_simpleproduction( if (t & DEF) { if (haddefault) { error(n_lc, - "More than one %%default in alternation"); + "More than one %%default in alternation", NULL); } haddefault = 1; } @@ -566,10 +507,10 @@ continue; LLtdecr(26); break; } -# line 282 "LLgen.g" +# line 280 "LLgen.g" { if (conflres & (COND|PREFERING|AVOIDING)) { error(n_lc, - "Resolver on last alternative not allowed"); + "Resolver on last alternative not allowed", NULL); } mkalt(*p,conflres,n_lc,&alt_table[n_alts++]); altcnt++; @@ -582,15 +523,15 @@ case /* ']' */ 28 : ; goto L_3; L_3: ; LLtdecr(26); -# line 292 "LLgen.g" +# line 290 "LLgen.g" { if (conflres & (COND|PREFERING|AVOIDING)) { error(o_lc, - "No alternation conflict resolver allowed here"); + "No alternation conflict resolver allowed here", NULL); } /* if (conflres & DEF) { error(o_lc, - "No %%default allowed here"); + "No %%default allowed here", NULL); } */ } @@ -598,14 +539,14 @@ break; default: if (LLskip()) goto L_2; goto L_3; } -# line 304 "LLgen.g" +# line 302 "LLgen.g" { n_alts -= altcnt; } } -# line 306 "LLgen.g" +# line 304 "LLgen.g" -STATIC -mkalt(prod,condition,lc,res) p_gram prod; register p_gram res; { +STATIC void mkalt(p_gram prod, int condition,int lc,register p_gram res) +{ /* * Create an alternation and initialise it. */ @@ -627,19 +568,12 @@ mkalt(prod,condition,lc,res) p_gram prod; register p_gram res; { nalts++; } static -#if LL_ANSI_C void -#endif LL6_simpleproduction( -#if LL_ANSI_C -# line 332 "LLgen.g" +# line 330 "LLgen.g" p_gram *p ,register int *conflres) -#else -# line 332 "LLgen.g" - p,conflres) p_gram *p; register int *conflres; -#endif { -# line 333 "LLgen.g" +# line 331 "LLgen.g" t_gram elem; int elmcnt = 0; int cnt, kind; @@ -675,7 +609,7 @@ else if (LL_6 & 1) goto L_1;} case /* C_DEFAULT */ 19 : ; LLtdecr(19); LL_SAFE(C_DEFAULT); -# line 338 "LLgen.g" +# line 336 "LLgen.g" { *conflres |= DEF; } LLread(); } @@ -708,17 +642,17 @@ switch(LLcsymb) { case /* C_IF */ 10 : ; LL_SAFE(C_IF); LL_NOSCANDONE(C_EXPR); -# line 344 "LLgen.g" +# line 342 "LLgen.g" { *conflres |= COND; } break; default: LL_SAFE(C_PREFER); -# line 345 "LLgen.g" +# line 343 "LLgen.g" { *conflres |= PREFERING; } break; case /* C_AVOID */ 17 : ; LL_SAFE(C_AVOID); -# line 346 "LLgen.g" +# line 344 "LLgen.g" { *conflres |= AVOIDING; } break; } @@ -747,7 +681,7 @@ else if (LL_8 & 1) goto L_6;} case /* C_ILLEGAL */ 22 : ; LLtdecr(22); LL_SAFE(C_ILLEGAL); -# line 348 "LLgen.g" +# line 346 "LLgen.g" { #ifdef NON_CORRECTING if (n_rules >= max_rules-2) { @@ -759,7 +693,7 @@ LL_SAFE(C_ILLEGAL); rule_table[n_rules++] = *search(TERMINAL, "LLILLEGAL", BOTH); if (*conflres & DEF) { - error(linecount, "%%illegal not allowed in %%default rule"); + error(linecount, "%%illegal not allowed in %%default rule", NULL); } #endif } @@ -790,9 +724,9 @@ case /* '*' */ 30 : ; case /* '+' */ 31 : ; LLsincr(4); LL7_elem( -# line 364 "LLgen.g" +# line 362 "LLgen.g" &elem); -# line 365 "LLgen.g" +# line 363 "LLgen.g" { if (n_rules >= max_rules-2) { rule_table = (p_gram) ralloc( (p_mem) rule_table, @@ -810,9 +744,9 @@ case /* '*' */ 30 : ; case /* '+' */ 31 : ; LLsdecr(4); LL8_repeats( -# line 373 "LLgen.g" +# line 371 "LLgen.g" &kind, &cnt); -# line 374 "LLgen.g" +# line 372 "LLgen.g" { if (g_gettype(&elem) != TERM) { rule_table[n_rules] = elem; g_settype((&rule_table[n_rules+1]),EORULE); @@ -835,7 +769,7 @@ case /* ']' */ 28 : ; goto L_10; L_10: ; LLsdecr(4); -# line 384 "LLgen.g" +# line 382 "LLgen.g" { if (g_gettype(&elem) == TERM) { register p_term q = g_getterm(&elem); @@ -867,7 +801,7 @@ break; default: if (LLskip()) goto L_9; goto L_10; } -# line 411 "LLgen.g" +# line 409 "LLgen.g" { if (!termdeleted && g_gettype(&elem) == TERM) { register p_term q; @@ -877,7 +811,7 @@ goto L_10; if ((q->t_flags & RESOLVER) && (kind == PLUS || kind == FIXED)) { error(linecount, - "%%while not allowed in this term"); + "%%while not allowed in this term", NULL); } /* * A persistent fixed term is the same @@ -886,7 +820,7 @@ goto L_10; if ((q->t_flags & PERSISTENT) && kind == FIXED) { error(linecount, - "Illegal %%persistent"); + "Illegal %%persistent", NULL); } */ } @@ -900,7 +834,7 @@ continue; LLsdecr(3); break; } -# line 437 "LLgen.g" +# line 435 "LLgen.g" { register p_term q; g_settype((&rule_table[n_rules]),EORULE); @@ -918,11 +852,11 @@ break; elmcnt+1); } } -# line 454 "LLgen.g" +# line 452 "LLgen.g" -STATIC -mkterm(prod,flags,lc,result) p_gram prod; register p_gram result; { +STATIC void mkterm(p_gram prod, int flags,int lc,register p_gram result) +{ /* * Create a term, initialise it and return * a grammar element containing it @@ -944,19 +878,12 @@ mkterm(prod,flags,lc,result) p_gram prod; register p_gram result; { nterms++; } static -#if LL_ANSI_C void -#endif LL7_elem( -#if LL_ANSI_C -# line 480 "LLgen.g" +# line 478 "LLgen.g" register p_gram pres) -#else -# line 480 "LLgen.g" - pres) register p_gram pres; -#endif { -# line 481 "LLgen.g" +# line 479 "LLgen.g" register int t = 0; p_gram p1; int ln; @@ -972,7 +899,7 @@ LLtincr(12); LLsincr(1); LLtincr(28); LL_SAFE('['); -# line 489 "LLgen.g" +# line 487 "LLgen.g" { ln = linecount; } LLread(); goto L_4; @@ -1003,7 +930,7 @@ case /* C_WHILE */ 11 : ; LLtdecr(11); LL_SAFE(C_WHILE); LL_NOSCANDONE(C_EXPR); -# line 490 "LLgen.g" +# line 488 "LLgen.g" { t |= RESOLVER; } LLread(); } @@ -1034,18 +961,18 @@ else if (LL_11 & 1) goto L_5;} case /* C_PERSISTENT */ 12 : ; LLtdecr(12); LL_SAFE(C_PERSISTENT); -# line 492 "LLgen.g" +# line 490 "LLgen.g" { t |= PERSISTENT; } LLread(); } } LLsdecr(1); LL5_productions( -# line 494 "LLgen.g" +# line 492 "LLgen.g" &p1); LLtdecr(28); LL_SCANDONE(']'); -# line 495 "LLgen.g" +# line 493 "LLgen.g" { mkterm(p1,t,ln,pres); } @@ -1061,7 +988,7 @@ default: break; case /* C_ERRONEOUS */ 21 : ; LL_SAFE(C_ERRONEOUS); -# line 499 "LLgen.g" +# line 497 "LLgen.g" { #ifdef NON_CORRECTING erroneous = 1; @@ -1079,14 +1006,14 @@ L_9: ; LLsdecr(5); LLtincr(6); LL_SSCANDONE(C_IDENT); -# line 507 "LLgen.g" +# line 505 "LLgen.g" { pe = search(UNKNOWN,lextoken.t_string,BOTH); *pres = *pe; #ifdef NON_CORRECTING if (erroneous) { if (g_gettype(pres) != TERMINAL){ warning(linecount, - "Erroneous only allowed on terminal"); + "Erroneous only allowed on terminal", NULL); erroneous = 0; } else @@ -1122,13 +1049,13 @@ else if (LL_12 & 1) goto L_10;} case /* C_PARAMS */ 6 : ; LLtdecr(6); LL_SAFE(C_PARAMS); -# line 522 "LLgen.g" +# line 520 "LLgen.g" { if (lextoken.t_num > 15) { - error(linecount,"Too many parameters"); + error(linecount,"Too many parameters", NULL); } else g_setnpar(pres,lextoken.t_num); if (g_gettype(pres) == TERMINAL) { error(linecount, - "Terminal with parameters"); + "Terminal with parameters", NULL); } } LLread(); @@ -1140,7 +1067,7 @@ goto L_9; case /* C_LITERAL */ 4 : ; LLsdecr(5); LL_SAFE(C_LITERAL); -# line 531 "LLgen.g" +# line 529 "LLgen.g" { pe = search(LITERAL,lextoken.t_string,BOTH); *pres = *pe; #ifdef NON_CORRECTING @@ -1154,7 +1081,7 @@ break; break; default: LLtincr(7); -# line 539 "LLgen.g" +# line 537 "LLgen.g" { g_settype(pres,ACTION); pres->g_lineno = linecount; #ifdef NON_CORRECTING @@ -1169,14 +1096,14 @@ case /* C_SUBSTART */ 20 : ; LLtincr(23); LLtincr(24); LL_SAFE(C_SUBSTART); -# line 548 "LLgen.g" +# line 546 "LLgen.g" { #ifdef NON_CORRECTING nsubstarts++; #endif } LL_NOSCANDONE(C_IDENT); -# line 555 "LLgen.g" +# line 553 "LLgen.g" { #ifdef NON_CORRECTING register p_gram temp; @@ -1205,7 +1132,7 @@ else if (LL_13 & 1) goto L_12;} case /* ',' */ 23 : ; LL_SAFE(','); LL_NOSCANDONE(C_IDENT); -# line 571 "LLgen.g" +# line 569 "LLgen.g" { #ifdef NON_CORRECTING register p_gram temp; @@ -1249,24 +1176,17 @@ break; } } static -#if LL_ANSI_C void -#endif LL8_repeats( -#if LL_ANSI_C -# line 602 "LLgen.g" +# line 600 "LLgen.g" int *kind ,int *cnt) -#else -# line 602 "LLgen.g" - kind,cnt) int *kind; int *cnt; -#endif { -# line 602 "LLgen.g" +# line 600 "LLgen.g" int t1 = 0; switch(LLcsymb) { default: LL_SAFE('?'); -# line 604 "LLgen.g" +# line 602 "LLgen.g" { *kind = OPT; } LLread(); break; @@ -1276,12 +1196,12 @@ LLtincr(3); switch(LLcsymb) { default: LL_SAFE('*'); -# line 605 "LLgen.g" +# line 603 "LLgen.g" { *kind = STAR; } break; case /* '+' */ 31 : ; LL_SAFE('+'); -# line 606 "LLgen.g" +# line 604 "LLgen.g" { *kind = PLUS; } break; } @@ -1308,12 +1228,12 @@ else if (LL_14 & 1) goto L_7;} case /* C_NUMBER */ 3 : ; LLtdecr(3); LL9_number( -# line 608 "LLgen.g" +# line 606 "LLgen.g" &t1); LLread(); } } -# line 609 "LLgen.g" +# line 607 "LLgen.g" { if (t1 == 1) { t1 = 0; if (*kind == STAR) *kind = OPT; @@ -1323,52 +1243,41 @@ LLread(); break; case /* C_NUMBER */ 3 : ; LL9_number( -# line 615 "LLgen.g" +# line 613 "LLgen.g" &t1); LLread(); break; } -# line 616 "LLgen.g" +# line 614 "LLgen.g" { *cnt = t1; } } static -#if LL_ANSI_C void -#endif LL9_number( -#if LL_ANSI_C -# line 619 "LLgen.g" +# line 617 "LLgen.g" int *t) -#else -# line 619 "LLgen.g" - t) int *t; -#endif { LL_SAFE(C_NUMBER); -# line 621 "LLgen.g" +# line 619 "LLgen.g" { *t = lextoken.t_num; if (*t <= 0 || *t >= 8192) { - error(linecount,"Illegal number"); + error(linecount,"Illegal number", NULL); } } } static -#if LL_ANSI_C void -#endif LL4_firsts( -#if LL_ANSI_C void -#endif ) { -# line 628 "LLgen.g" +# line 626 "LLgen.g" register string p; LLtincr(23); LLtincr(2); LLtincr(24); LL_SAFE(C_FIRST); LL_NOSCANDONE(C_IDENT); -# line 630 "LLgen.g" +# line 628 "LLgen.g" { p = store(lextoken.t_string); } LLtdecr(23); LL_NOSCANDONE(','); @@ -1376,7 +1285,7 @@ LLtdecr(2); LL_NOSCANDONE(C_IDENT); LLtdecr(24); LL_NOSCANDONE(';'); -# line 632 "LLgen.g" +# line 630 "LLgen.g" { /* * Store this %first in the list belonging * to this input file @@ -1393,11 +1302,11 @@ LL_NOSCANDONE(';'); } } -# line 647 "LLgen.g" +# line 645 "LLgen.g" -STATIC p_gram -copyrule(p,length) register p_gram p; { +STATIC p_gram copyrule(register p_gram p,int length) +{ /* * Returns a pointer to a grammar rule that was created in * p. The space pointed to by p can now be reused diff --git a/util/LLgen/src/LLgen.g b/util/LLgen/src/LLgen.g index c64c15b33..d3dae69ed 100644 --- a/util/LLgen/src/LLgen.g +++ b/util/LLgen/src/LLgen.g @@ -18,13 +18,13 @@ */ { -# include -# include -# include "types.h" -# include "io.h" -# include "extern.h" -# include "assert.h" -# include "cclass.h" +#include +#include +#include "types.h" +#include "io.h" +#include "extern.h" +#include "assert.h" +#include "cclass.h" # ifndef NORCSID static string rcsid = "$Id$"; @@ -48,15 +48,14 @@ static int max_rules; #define RULEINCR 32 /* Here are defined : */ -STATIC newnorder(); -STATIC newtorder(); -STATIC mkalt(); -STATIC mkterm(); -STATIC p_gram copyrule(); +STATIC void newnorder(int index); +STATIC void newtorder(int index); +STATIC void mkalt(p_gram prod, int condition,int lc,register p_gram res); +STATIC void mkterm(p_gram prod, int flags,int lc,register p_gram result); +STATIC p_gram copyrule(register p_gram p,int length); /* and of course LLparse() */ -STATIC -newnorder(index) { +STATIC void newnorder(int index) { static int porder; if (norder != -1) { @@ -67,8 +66,7 @@ newnorder(index) { nonterms[porder].n_next = -1; } -STATIC -newtorder(index) { +STATIC void newtorder(int index) { static int porder; if (torder != -1) { @@ -79,7 +77,7 @@ newtorder(index) { tokens[porder].t_next = -1; } -p_init() +void p_init(void) { alt_table = (p_gram )alloc(ALTINCR*sizeof(t_gram)); n_alts = 0; @@ -149,7 +147,7 @@ def { register string p; } { if (!lexical) { lexical = store(lextoken.t_string); } - else error(linecount,"Duplicate %%lexical"); + else error(linecount,"Duplicate %%lexical", NULL); } ';' | C_PREFIX C_IDENT @@ -160,25 +158,25 @@ def { register string p; } prefix = store(lextoken.t_string); if (strlen(prefix) > 6) { error(linecount, - "%%prefix too long"); + "%%prefix too long",NULL); prefix[6] = 0; } } - else error(linecount,"Duplicate %%prefix"); + else error(linecount,"Duplicate %%prefix", NULL); } ';' | C_ONERROR C_IDENT { #ifdef NON_CORRECTING if (non_corr) { - warning(linecount, "%%onerror conflicts with -n option"); + warning(linecount, "%%onerror conflicts with -n option", NULL); } else #endif if (! onerror) { onerror = store(lextoken.t_string); } - else error(linecount,"Duplicate %%onerror"); + else error(linecount,"Duplicate %%onerror", NULL); } ';' | C_ACTION { acount++; } @@ -225,7 +223,7 @@ rule { register p_nont p; [ C_PARAMS { if (lextoken.t_num > 0) { p->n_flags |= PARAMS; if (lextoken.t_num > 15) { - error(linecount,"Too many parameters"); + error(linecount,"Too many parameters", NULL); } else setntparams(p,lextoken.t_num); } @@ -268,7 +266,7 @@ productions(p_gram *p;) if (t & DEF) { if (haddefault) { error(n_lc, - "More than one %%default in alternation"); + "More than one %%default in alternation", NULL); } haddefault = 1; } @@ -281,7 +279,7 @@ productions(p_gram *p;) } ]+ { if (conflres & (COND|PREFERING|AVOIDING)) { error(n_lc, - "Resolver on last alternative not allowed"); + "Resolver on last alternative not allowed", NULL); } mkalt(*p,conflres,n_lc,&alt_table[n_alts++]); altcnt++; @@ -291,12 +289,12 @@ productions(p_gram *p;) | { if (conflres & (COND|PREFERING|AVOIDING)) { error(o_lc, - "No alternation conflict resolver allowed here"); + "No alternation conflict resolver allowed here", NULL); } /* if (conflres & DEF) { error(o_lc, - "No %%default allowed here"); + "No %%default allowed here", NULL); } */ } @@ -305,8 +303,8 @@ productions(p_gram *p;) ; { -STATIC -mkalt(prod,condition,lc,res) p_gram prod; register p_gram res; { +STATIC void mkalt(p_gram prod, int condition,int lc,register p_gram res) +{ /* * Create an alternation and initialise it. */ @@ -356,7 +354,7 @@ simpleproduction(p_gram *p; register int *conflres;) rule_table[n_rules++] = *search(TERMINAL, "LLILLEGAL", BOTH); if (*conflres & DEF) { - error(linecount, "%%illegal not allowed in %%default rule"); + error(linecount, "%%illegal not allowed in %%default rule", NULL); } #endif } @@ -417,7 +415,7 @@ simpleproduction(p_gram *p; register int *conflres;) if ((q->t_flags & RESOLVER) && (kind == PLUS || kind == FIXED)) { error(linecount, - "%%while not allowed in this term"); + "%%while not allowed in this term", NULL); } /* * A persistent fixed term is the same @@ -426,7 +424,7 @@ simpleproduction(p_gram *p; register int *conflres;) if ((q->t_flags & PERSISTENT) && kind == FIXED) { error(linecount, - "Illegal %%persistent"); + "Illegal %%persistent", NULL); } */ } @@ -453,8 +451,8 @@ simpleproduction(p_gram *p; register int *conflres;) ; { -STATIC -mkterm(prod,flags,lc,result) p_gram prod; register p_gram result; { +STATIC void mkterm(p_gram prod, int flags,int lc,register p_gram result) +{ /* * Create a term, initialise it and return * a grammar element containing it @@ -510,7 +508,7 @@ elem (register p_gram pres;) if (erroneous) { if (g_gettype(pres) != TERMINAL){ warning(linecount, - "Erroneous only allowed on terminal"); + "Erroneous only allowed on terminal", NULL); erroneous = 0; } else @@ -520,11 +518,11 @@ elem (register p_gram pres;) } [ C_PARAMS { if (lextoken.t_num > 15) { - error(linecount,"Too many parameters"); + error(linecount,"Too many parameters", NULL); } else g_setnpar(pres,lextoken.t_num); if (g_gettype(pres) == TERMINAL) { error(linecount, - "Terminal with parameters"); + "Terminal with parameters", NULL); } } ]? @@ -620,7 +618,7 @@ number(int *t;) : C_NUMBER { *t = lextoken.t_num; if (*t <= 0 || *t >= 8192) { - error(linecount,"Illegal number"); + error(linecount,"Illegal number", NULL); } } ; @@ -646,8 +644,8 @@ firsts { register string p; } ; { -STATIC p_gram -copyrule(p,length) register p_gram p; { +STATIC p_gram copyrule(register p_gram p,int length) +{ /* * Returns a pointer to a grammar rule that was created in * p. The space pointed to by p can now be reused diff --git a/util/LLgen/src/Lpars.c b/util/LLgen/src/Lpars.c index e241c0e9a..1b002de34 100644 --- a/util/LLgen/src/Lpars.c +++ b/util/LLgen/src/Lpars.c @@ -1,9 +1,6 @@ /* LLgen generated code from source . */ #include "Lpars.h" #define LLNOFIRSTS -#if __STDC__ || __cplusplus -#define LL_ANSI_C 1 -#endif #define LL_LEXI scanner /* $Id$ */ #ifdef LL_DEBUG @@ -37,7 +34,6 @@ extern int LLstartsymb; #define LLsincr(d) LLscnt[d]++ #define LLtincr(d) LLtcnt[d]++ -#if LL_ANSI_C extern int LL_LEXI(void); extern void LLread(void); extern int LLskip(void); @@ -55,35 +51,11 @@ extern int LLfirst(int, int); #if LL_NON_CORR extern void LLnc_recover(void); #endif -#else /* not LL_ANSI_C */ -extern LLread(); -extern int LLskip(); -extern int LLnext(); -extern LLerror(); -extern LLsafeerror(); -extern LLnewlevel(); -extern LLoldlevel(); -#ifndef LL_FASTER -extern LLscan(); -#endif -#ifndef LLNOFIRSTS -extern int LLfirst(); -#endif -#if LL_NON_CORR -extern LLnc_recover(); -#endif -#endif /* not LL_ANSI_C */ #define LL_SSIZE 4 #define LL_NSETS 6 #define LL_NTERMINALS 32 -#if LL_ANSI_C void LL0_spec(void); -#endif -#if LL_ANSI_C void LLparse(void) -#else -LLparse() -#endif { unsigned int s[LL_NTERMINALS+LL_NSETS+2]; LLnewlevel(s); @@ -401,29 +373,15 @@ int LLstartsymb; static int fake_eof = 0; #endif -#if LL_ANSI_C #define LL_VOIDCST (void) void LLmessage(int); -#else -#define LL_VOIDCST -#endif #ifdef LL_USERHOOK -#if LL_ANSI_C static int LLdoskip(int); static int LLuserhook(int, int*); -#else -static int LLdoskip(); -static int LLuserhook(); -#endif #endif #ifndef LL_FASTER -#if LL_ANSI_C void LLscan(int t) -#else -LLscan(t) - int t; -#endif { /* * Check if the next symbol is equal to the parameter @@ -469,12 +427,7 @@ LLscan(t) } #endif -#if LL_ANSI_C void LLread(void) { -#else -LLread() { -#endif - #if LL_NON_CORR /* Again, check if another parser has crashed, * in that case intercept and go to the @@ -506,12 +459,7 @@ LLread() { /* NOTREACHED */ } -#if LL_ANSI_C void LLerror(int t) -#else -LLerror(t) - int t; -#endif { register int i; @@ -573,12 +521,7 @@ LLerror(t) } } -#if LL_ANSI_C void LLsafeerror(int t) -#else -LLsafeerror(t) - int t; -#endif { if (t == EOFILE && LLsymb <= 0) return; #ifdef LL_NEWMESS @@ -621,11 +564,7 @@ LLsafeerror(t) } #ifndef LLNOFIRSTS -#if LL_ANSI_C int LLfirst(int x, int d) { -#else -int LLfirst(x, d) { -#endif register int i; return (i = LLindex[x]) >= 0 && @@ -633,12 +572,7 @@ int LLfirst(x, d) { } #endif -#if LL_ANSI_C int LLnext(int n) -#else -int LLnext(n) - int n; -#endif { /* returns: 0 if the current symbol is'nt skipped, and it is'nt a member of "n", @@ -659,11 +593,7 @@ int LLnext(n) return retval; } -#if LL_ANSI_C int LLskip(void) { -#else -int LLskip() { -#endif /* returns 0 if the current symbol is'nt skipped, and 1 if it is, t.i., we have a new symbol */ @@ -671,14 +601,8 @@ int LLskip() { return LLdoskip(0); } -#if LL_ANSI_C extern void LL_USERHOOK(int, int *); static int LLuserhook(int e, int *list) -#else -static int LLuserhook(e, list) - int e; - int *list; -#endif { int old = LLsymb; LL_USERHOOK(e, list); @@ -686,12 +610,7 @@ static int LLuserhook(e, list) return LLsymb != old; } -#if LL_ANSI_C static void LLmklist(register int *list) -#else -static LLmklist(list) - register int *list; -#endif { char Xset[LL_SSIZE]; register char *p; @@ -715,12 +634,7 @@ static LLmklist(list) *list = 0; } -#if LL_ANSI_C static int LLdoskip(int e) -#else -static int LLdoskip(e) - int e; -#endif { int LLx; int list[LL_NTERMINALS+1]; @@ -779,11 +693,7 @@ static int LLdoskip(e) /* NOTREACHED */ } -#if LL_ANSI_C void LLnewlevel(unsigned int *LLsinfo) { -#else -LLnewlevel(LLsinfo) unsigned int *LLsinfo; { -#endif register int i; if (LLlevel++) { @@ -801,11 +711,7 @@ LLnewlevel(LLsinfo) unsigned int *LLsinfo; { LLtincr(0); } -#if LL_ANSI_C void LLoldlevel(unsigned int *LLsinfo) { -#else -LLoldlevel(LLsinfo) unsigned int *LLsinfo; { -#endif register int i; LLtdecr(0); diff --git a/util/LLgen/src/alloc.c b/util/LLgen/src/alloc.c index 25ca26d6e..eb089d135 100644 --- a/util/LLgen/src/alloc.c +++ b/util/LLgen/src/alloc.c @@ -17,6 +17,7 @@ */ #include +# include "alloc.h" # include "types.h" # include "extern.h" @@ -26,29 +27,29 @@ static string rcsid = "$Id$"; static string e_nomem = "Out of memory"; -p_mem -alloc(size) unsigned size; { +p_mem alloc(unsigned int size) +{ /* Allocate "size" bytes. Panic if it fails */ p_mem p; - if ((p = malloc(size)) == 0) fatal(linecount,e_nomem); + if ((p = malloc(size)) == 0) fatal(linecount,e_nomem, NULL); return p; } -p_mem -ralloc(p,size) p_mem p; unsigned size; { +p_mem ralloc(p_mem p,unsigned int size) +{ /* Re-allocate the chunk of memory indicated by "p", to occupy "size" bytes */ - if ((p = realloc(p,size)) == 0) fatal(linecount,e_nomem); + if ((p = realloc(p,size)) == 0) fatal(linecount,e_nomem, NULL); return p; } -p_mem -new_mem(p) register p_info p; { +p_mem new_mem(register p_info p) +{ /* This routine implements arrays that can grow. It must be called every time a new element is added to it. diff --git a/util/LLgen/src/alloc.h b/util/LLgen/src/alloc.h new file mode 100644 index 000000000..a86b46064 --- /dev/null +++ b/util/LLgen/src/alloc.h @@ -0,0 +1,19 @@ +/* Copyright (c) 2019 ACK Project. + * See the copyright notice in the ACK home directory, + * in the file "Copyright". + * + * Created on: 2019-02-16 + * + */ +#ifndef ALLOC_H_ +#define ALLOC_H_ + +#include "types.h" + +p_mem alloc(unsigned int size); +p_mem ralloc(p_mem p,unsigned int size); +p_mem new_mem(register p_info p); + + + +#endif /* ALLOC_H_ */ diff --git a/util/LLgen/src/check.c b/util/LLgen/src/check.c index 6efe80c02..488021c72 100644 --- a/util/LLgen/src/check.c +++ b/util/LLgen/src/check.c @@ -17,6 +17,7 @@ */ #include +#include #include # include "types.h" # include "extern.h" @@ -28,247 +29,287 @@ static string rcsid1 = "$Id$"; # endif -static string c_first = "> firstset "; -static string c_contains = "> containset "; -static string c_follow = "> followset "; -p_set setalloc(); -static int level; +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 void prrule(p_gram p); -STATIC cfcheck(); -STATIC void resolve(p_gram p); -STATIC propagate(); -STATIC spaces(); +void conflchecks(void); +STATIC void prline(char *); +STATIC void printset(register p_set, string); +STATIC int check(register p_gram); +STATIC void moreverbose(register p_set); +STATIC void prrule(p_gram); +STATIC void cfcheck(p_set, p_set, int); +STATIC void resolve(p_gram); +STATIC void propagate(p_set, register p_gram); +STATIC void spaces(void); -conflchecks() { - /* - * Check for conflicts, that is, - * in a repeating term, the FIRST and FOLLOW must be disjunct, - * unless there is a disambiguating condition. - * in an alternation, the sets that determine the direction to take, - * must be disjunct. - */ - register p_nont p; +/* + * Check for conflicts, that is, + * in a repeating term, the FIRST and FOLLOW must be disjunct, + * unless there is a disambiguating condition. + * in an alternation, the sets that determine the direction to take, + * must be disjunct. + */ +void conflchecks(void) +{ + register p_nont p; register int s; - p_file x = files; + p_file x = files; f_input = x->f_name; - if (verbose >= 3) { - for (p = nonterms; p < maxnt; p++) p->n_flags |= VERBOSE; + if (verbose >= 3) + { + 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 (verbose) + { + if ((fout = fopen(f_out, "w")) == NULL) + fatal(1, e_noopen, f_out); } /* * Check the rules in the order in which they are declared, * and input file by input file, to give proper error messages */ - for (; x < maxfiles; x++) { - f_input = x->f_name; - for (s = x->f_nonterminals; s != -1; s = p->n_next) { - p = &nonterms[s]; - if (check(p->n_rule)) p->n_flags |= VERBOSE; - } - } - for (x = files; x < maxfiles; x++) { - f_input = x->f_name; - for (s = x->f_nonterminals; s != -1; s = p->n_next) { - p = &nonterms[s]; - if (p->n_flags & RECURSIVE) { - error(p->n_lineno, - "Recursion in default for nonterminal %s", - p->n_name); + for (; x < maxfiles; x++) + { + f_input = x->f_name; + for (s = x->f_nonterminals; s != -1; s = p->n_next) + { + p = &nonterms[s]; + if (check(p->n_rule)) + p->n_flags |= VERBOSE; } - /* - * If a printout is needed for this rule in - * LL.output, just do it - */ - if (verbose && (p->n_flags & VERBOSE)) { - fprintf(fout,"\n%s :\n",p->n_name); - printset(p->n_first,c_first); - printset(p->n_contains,c_contains); - printset(p->n_follow,c_follow); - fprintf(fout,"> rule%s\n\t", - p->n_flags&EMPTY ? "\t(EMPTY producing)" : ""); - level = 8; - prrule(p->n_rule); - level = 0; - prline("\n"); + } + for (x = files; x < maxfiles; x++) + { + f_input = x->f_name; + for (s = x->f_nonterminals; s != -1; s = p->n_next) + { + p = &nonterms[s]; + if (p->n_flags & RECURSIVE) + { + error(p->n_lineno, "Recursion in default for nonterminal %s", + p->n_name); + } + /* + * If a printout is needed for this rule in + * LL.output, just do it + */ + if (verbose && (p->n_flags & VERBOSE)) + { + fprintf(fout, "\n%s :\n", p->n_name); + printset(p->n_first, c_first); + printset(p->n_contains, c_contains); + printset(p->n_follow, c_follow); + fprintf(fout, "> rule%s\n\t", + p->n_flags & EMPTY ? "\t(EMPTY producing)" : ""); + level = 8; + prrule(p->n_rule); + level = 0; + prline("\n"); + } + /* + * Now, the conflicts may be resolved + */ + resolve(p->n_rule); } - /* - * Now, the conflicts may be resolved - */ - resolve(p->n_rule); - } } - if (verbose) fclose(fout); + if (verbose) + fclose(fout); } -STATIC -prline(s) char *s; { +STATIC void prline(char *s) +{ fputs(s, fout); spaces(); } -STATIC -printset(p,s) register p_set p; string s; { +STATIC void printset(register p_set p, string s) +{ /* * Print the elements of a set */ - register int i; - register int j; + register int i; + register int j; register p_token pt; - string name; - int k; - int hulp; + string name; + int k; + int hulp; k = strlen(s) + 2 + level; /* * k contains relative level of indentation */ - fprintf(fout,"%s{ ",s); + fprintf(fout, "%s{ ", s); j = k; /* * j will gather the total length of the line */ - for (i = 0, pt = tokens; i < ntokens; i++,pt++) { - if (IN(p,i)) { - hulp = strlen(pt->t_string)+1; - if (pt->t_tokno < 0400) hulp += 2; - if ((j += hulp) >= 78) { + for (i = 0, pt = tokens; i < ntokens; i++, pt++) + { + if (IN(p, i)) + { + hulp = strlen(pt->t_string) + 1; + if (pt->t_tokno < 0400) + hulp += 2; + if ((j += hulp) >= 78) + { /* * Line becoming too long */ - j = k+hulp; + j = k + hulp; prline("\n"); - fprintf(fout,">%*c",k - level - 1,' '); + fprintf(fout, ">%*c", k - level - 1, ' '); } - fprintf(fout, pt->t_tokno<0400 ? "'%s' " : "%s ",pt->t_string); + fprintf(fout, pt->t_tokno < 0400 ? "'%s' " : "%s ", pt->t_string); } } - if (ntprint) for (i = 0; i < nnonterms; i++) { - /* - * Nonterminals in the set must also be printed - */ - if (NTIN(p,i)) { - name = nonterms[i].n_name; - hulp = strlen(name) + 3; - if ((j += hulp) >= 78) { - j = k + hulp; - prline("\n"); - fprintf(fout,">%*c",k - level - 1,' '); + if (ntprint) + for (i = 0; i < nnonterms; i++) + { + /* + * Nonterminals in the set must also be printed + */ + if (NTIN(p, i)) + { + name = nonterms[i].n_name; + hulp = strlen(name) + 3; + if ((j += hulp) >= 78) + { + j = k + hulp; + prline("\n"); + fprintf(fout, ">%*c", k - level - 1, ' '); + } + fprintf(fout, "<%s> ", name); } - fprintf(fout,"<%s> ",name); } - } prline("}\n"); } -STATIC int -check(p) register p_gram p; { +STATIC int check(register p_gram p) +{ /* * Search for conflicts in a grammar rule. */ - register p_set temp; + register p_set temp; register int retval; retval = 0; - for (;;) { - switch (g_gettype(p)) { - case EORULE : + for (;;) + { + switch (g_gettype(p)) + { + case EORULE: return retval; - case NONTERM : { + case NONTERM: + { register p_nont n; n = &nonterms[g_getcont(p)]; - if (g_getnpar(p) != getntparams(n)) { - error(p->g_lineno, - "Call of %s: parameter count mismatch", - n->n_name); + if (g_getnpar(p) != getntparams(n)) + { + error(p->g_lineno, "Call of %s: parameter count mismatch", + n->n_name); } - break; } - case TERM : { + break; + } + case TERM: + { register p_term q; q = g_getterm(p); retval |= check(q->t_rule); - if (r_getkind(q) == FIXED) break; - if (setempty(q->t_first)) { + if (r_getkind(q) == FIXED) + break; + if (setempty(q->t_first)) + { q->t_flags |= EMPTYFIRST; retval = 1; - error(p->g_lineno, "No symbols in term"); + error(p->g_lineno, "No symbols in term", NULL); } - if (empty(q->t_rule)) { + if (empty(q->t_rule)) + { q->t_flags |= EMPTYTERM; retval = 1; - error(p->g_lineno, "Term with variable repetition count produces empty"); + error(p->g_lineno, + "Term with variable repetition count produces empty", + NULL); } temp = setalloc(); - setunion(temp,q->t_first); - if (!setintersect(temp,q->t_follow)) { + setunion(temp, q->t_first); + if (!setintersect(temp, q->t_follow)) + { /* * q->t_first * q->t_follow != EMPTY */ - if (!(q->t_flags & RESOLVER)) { + if (!(q->t_flags & RESOLVER)) + { /* * No conflict resolver */ - error(p->g_lineno, - "Repetition conflict"); + error(p->g_lineno, "Repetition conflict", NULL); retval = 1; moreverbose(temp); } } - else { - if (q->t_flags & RESOLVER) { + else + { + if (q->t_flags & RESOLVER) + { q->t_flags |= NOCONF; - warning(p->g_lineno, - "%%while without conflict"); + warning(p->g_lineno, "%%while without conflict", NULL); } } free((p_mem) temp); - break; } - case ALTERNATION : { + break; + } + case ALTERNATION: + { register p_link l; l = g_getlink(p); temp = setalloc(); - setunion(temp,l->l_symbs); - if(!setintersect(temp,l->l_others)) { + setunion(temp, l->l_symbs); + if (!setintersect(temp, l->l_others)) + { /* * temp now contains the conflicting * symbols */ - if (!(l->l_flag & (COND|PREFERING|AVOIDING))) { - error(p->g_lineno, -"Alternation conflict"); + if (!(l->l_flag & (COND | PREFERING | AVOIDING))) + { + error(p->g_lineno, "Alternation conflict", NULL); retval = 1; moreverbose(temp); } - } else { - if (l->l_flag & (COND|PREFERING|AVOIDING)) { + } + else + { + if (l->l_flag & (COND | PREFERING | AVOIDING)) + { l->l_flag |= NOCONF; - warning(p->g_lineno, -"Conflict resolver without conflict"); + warning(p->g_lineno, "Conflict resolver without conflict", + NULL); } } - free( (p_mem) temp); - if (l->l_flag & PREFERING) propagate(l->l_symbs,p+1); + free((p_mem) temp); + if (l->l_flag & PREFERING) + propagate(l->l_symbs, p + 1); retval |= check(l->l_rule); - break; } + break; + } } p++; } } -STATIC -moreverbose(t) register p_set t; { +STATIC void moreverbose(register p_set t) +{ /* * t points to a set containing conflicting symbols and pssibly * also containing nonterminals. @@ -277,116 +318,141 @@ moreverbose(t) register p_set t; { register int i; register p_nont p; - if (verbose == 2) for (i = 0, p = nonterms; i < nnonterms; i++, p++) { - if (NTIN(t,i)) p->n_flags |= VERBOSE; - } + if (verbose == 2) + for (i = 0, p = nonterms; i < nnonterms; i++, p++) + { + if (NTIN(t, i)) + p->n_flags |= VERBOSE; + } } -STATIC -void prrule(p_gram p) { +STATIC void prrule(p_gram p) +{ /* * Create a verbose printout of grammar rule p */ - register FILE *f; - int present = 0; - int firstalt = 1; + register FILE *f; + int present = 0; + int firstalt = 1; f = fout; - for (;;) { - switch (g_gettype(p)) { - case EORULE : - fputs("\n",f); + for (;;) + { + switch (g_gettype(p)) + { + case EORULE: + fputs("\n", f); return; - case TERM : { - register p_term q; - register int c; + case TERM: + { + register p_term q; + register int c; q = g_getterm(p); - if (present) prline("\n"); - fputs("[ ",f); + if (present) + prline("\n"); + fputs("[ ", f); level += 4; - if (q->t_flags & RESOLVER) { + if (q->t_flags & RESOLVER) + { prline("%while (..)\n"); } - if (q->t_flags & PERSISTENT) { + if (q->t_flags & PERSISTENT) + { prline("%persistent\n"); } - if (r_getkind(q) != FIXED) { - if (!(q->t_flags & PERSISTENT)) { - prline("> continue repetition on the\n"); + if (r_getkind(q) != FIXED) + { + if (!(q->t_flags & PERSISTENT)) + { + prline("> continue repetition on the\n"); } printset(q->t_first, c_first); - if (q->t_flags & PERSISTENT) { - prline("> continue repetition on the\n"); + if (q->t_flags & PERSISTENT) + { + prline("> continue repetition on the\n"); } printset(q->t_contains, c_contains); prline("> terminate repetition on the\n"); - printset(q->t_follow,c_follow); - if (q->t_flags & EMPTYFIRST) { - prline(">>> empty first\n"); + printset(q->t_follow, c_follow); + if (q->t_flags & EMPTYFIRST) + { + prline(">>> empty first\n"); } - if (q->t_flags & EMPTYTERM) { - prline(">>> term produces empty\n"); + if (q->t_flags & EMPTYTERM) + { + prline(">>> term produces empty\n"); } - cfcheck(q->t_first,q->t_follow, - q->t_flags & RESOLVER); + cfcheck(q->t_first, q->t_follow, q->t_flags & RESOLVER); } prrule(q->t_rule); level -= 4; spaces(); c = r_getkind(q); - fputs(c == STAR ? "]*" : c == PLUS ? "]+" : - c == OPT ? "]?" : "]", f); - if (c = r_getnum(q)) { - fprintf(f,"%d",c); + fputs(c == STAR ? "]*" : c == PLUS ? "]+" : c == OPT ? "]?" : "]", + f); + if ((c = r_getnum(q))) + { + fprintf(f, "%d", c); } prline("\n"); - break; } - case ACTION : - fputs("{..} ",f); break; - case ALTERNATION : { + } + case ACTION: + fputs("{..} ", f); + break; + case ALTERNATION: + { register p_link l; l = g_getlink(p); - if (firstalt) { + if (firstalt) + { firstalt = 0; } - else prline("|\n"); - printset(l->l_symbs,"> alternative on "); - cfcheck(l->l_symbs, - l->l_others, - (int)(l->l_flag&(COND|PREFERING|AVOIDING))); - fputs(" ",f); + else + prline("|\n"); + printset(l->l_symbs, "> alternative on "); + cfcheck(l->l_symbs, l->l_others, + (int) (l->l_flag & (COND | PREFERING | AVOIDING))); + fputs(" ", f); level += 4; - if (l->l_flag & DEF) { + if (l->l_flag & DEF) + { prline("%default\n"); } - if (l->l_flag & AVOIDING) { + if (l->l_flag & AVOIDING) + { prline("%avoid\n"); } - if (l->l_flag & PREFERING) { + if (l->l_flag & PREFERING) + { prline("%prefer\n"); } - if (l->l_flag & COND) { + if (l->l_flag & COND) + { prline("%if ( ... )\n"); } prrule(l->l_rule); level -= 4; - if (g_gettype(p+1) == EORULE) { + if (g_gettype(p+1) == EORULE) + { return; } spaces(); - p++; continue; } - case LITERAL : - case TERMINAL : { + p++; + continue; + } + case LITERAL: + case TERMINAL: + { register p_token pt = &tokens[g_getcont(p)]; - fprintf(f,pt->t_tokno<0400 ? - "'%s' " : "%s ", pt->t_string); - break; } - case NONTERM : - fprintf(f,"%s ",nonterms[g_getcont(p)].n_name); + fprintf(f, pt->t_tokno < 0400 ? "'%s' " : "%s ", pt->t_string); + break; + } + case NONTERM: + fprintf(f, "%s ", nonterms[g_getcont(p)].n_name); break; } p++; @@ -394,8 +460,8 @@ void prrule(p_gram p) { } } -STATIC -cfcheck(s1,s2,flag) p_set s1,s2; { +STATIC void cfcheck(p_set s1, p_set s2, int flag) +{ /* * Check if s1 and s2 have elements in common. * If so, flag must be non-zero, indicating that there is a @@ -405,70 +471,82 @@ cfcheck(s1,s2,flag) p_set s1,s2; { register p_set temp; temp = setalloc(); - setunion(temp,s1); - if (!setintersect(temp,s2)) { - if (! flag) { - printset(temp,">>> conflict on "); + setunion(temp, s1); + if (!setintersect(temp, s2)) + { + if (!flag) + { + printset(temp, ">>> conflict on "); prline("\n"); } - } else { - if (flag) { + } + else + { + if (flag) + { prline(">>> %if/%while, no conflict\n"); } } free((p_mem) temp); } -STATIC -void resolve(p_gram p) { +STATIC void resolve(p_gram p) +{ /* * resolve conflicts, as specified by the user */ - for (;;) { - switch (g_gettype(p)) { - case EORULE : + for (;;) + { + switch (g_gettype(p)) + { + case EORULE: return; - case TERM : + case TERM: resolve(g_getterm(p)->t_rule); break; - case ALTERNATION : { - register p_link l; + case ALTERNATION: + { + register p_link l; l = g_getlink(p); - if (l->l_flag & AVOIDING) { + if (l->l_flag & AVOIDING) + { /* * On conflicting symbols, this rule * is never chosen */ - setminus(l->l_symbs,l->l_others); + setminus(l->l_symbs, l->l_others); } - if (setempty(l->l_symbs)) { + if (setempty(l->l_symbs)) + { /* * This may be caused by the statement above */ - error(p->g_lineno,"Alternative never chosen"); + error(p->g_lineno, "Alternative never chosen", NULL); } resolve(l->l_rule); - break; } + break; + } } p++; } } -STATIC -propagate(set,p) p_set set; register p_gram p; { +STATIC void propagate(p_set set, register p_gram p) +{ /* * Propagate the fact that on the elements of set the grammar rule * p will not be chosen. */ - while (g_gettype(p) != EORULE) { - setminus(g_getlink(p)->l_symbs,set); + while (g_gettype(p) != EORULE) + { + setminus(g_getlink(p)->l_symbs, set); p++; } } -STATIC -spaces() { - - if (level > 0) fprintf(fout,"%*c",level,' '); +STATIC void spaces(void) +{ + if (level > 0) + fprintf(fout, "%*c", level, ' '); } diff --git a/util/LLgen/src/compute.c b/util/LLgen/src/compute.c index c16c6725c..fddd7b3e0 100644 --- a/util/LLgen/src/compute.c +++ b/util/LLgen/src/compute.c @@ -18,6 +18,8 @@ */ # include +# include +# include "alloc.h" # include "types.h" # include "extern.h" # include "sets.h" @@ -28,70 +30,81 @@ 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. - */ +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; /* Defined in this file : */ -extern do_compute(); -STATIC createsets(); -STATIC void walk(); -STATIC co_trans(); -STATIC int nempty(); -extern empty(); -STATIC int nfirst(); -STATIC first(); -STATIC int nfollow(); -STATIC follow(); -STATIC void co_dirsymb(); -STATIC co_others(); -STATIC do_lengthcomp(); -STATIC void complength(); -STATIC void add(); -STATIC int compare(); -STATIC void setdefaults(); -STATIC do_contains(); -STATIC void contains(); -STATIC int nsafes(); -STATIC int do_safes(); + +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); #ifdef NON_CORRECTING -STATIC int nc_nfirst(); -STATIC nc_first(); -STATIC int nc_nfollow(); -STATIC nc_follow(); +STATIC int nc_first(p_set setp,register p_gram p,int flag); #endif +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); -do_compute() { - /* - * Does all the work, by calling other routines (divide and conquer) - */ - register p_nont p; +/* + * Does all the work, by calling other routines (divide and conquer) + */ +void do_compute(void) +{ + register p_nont p; register p_start st; createsets(); - co_trans(nempty); /* Which nonterminals produce empty? */ - co_trans(nfirst); /* Computes first sets */ + co_trans(nempty); /* Which nonterminals produce empty? */ + co_trans(nfirst); /* Computes first sets */ /* * Compute FOLLOW sets. * First put EOFILE in the follow set of the start nonterminals. */ - for (st = start; st; st = st->ff_next) { + for (st = start; st; st = st->ff_next) + { p = &nonterms[st->ff_nont]; - PUTIN(p->n_follow,0); + PUTIN(p->n_follow, 0); } co_trans(nfollow); /* * Compute the sets which determine which alternative to choose * in case of a choice */ - for (p = nonterms; p < maxnt; p++) { - co_dirsymb(p->n_follow,p->n_rule); + for (p = nonterms; p < maxnt; p++) + { + co_dirsymb(p->n_follow, p->n_rule); } /* * Compute the minimum length of productions of nonterminals, @@ -101,73 +114,80 @@ do_compute() { /* * Compute the contains sets */ - for (p = nonterms; p < maxnt; p++) do_contains(p); - for (p = nonterms; p < maxnt; p++) contains(p->n_rule, (p_set) 0); + for (p = nonterms; p < maxnt; p++) + do_contains(p); + for (p = nonterms; p < maxnt; p++) + contains(p->n_rule, (p_set) 0); /* * 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. */ - for (p = nonterms; p < maxnt; p++) { + for (p = nonterms; p < maxnt; p++) + { /* * Don't know anything yet */ setntsafe(p, NOSAFETY); setntout(p, NOSAFETY); } - for (st = start; st; st = st->ff_next) { + for (st = start; st; st = st->ff_next) + { /* * But start symbols are called with lookahead done */ p = &nonterms[st->ff_nont]; - setntsafe(p,SCANDONE); + setntsafe(p, SCANDONE); } co_trans(nsafes); #ifdef NON_CORRECTING - 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); - } + 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); + } } - if (non_corr) { - /* compute the non_corr first sets for all nonterminals and terms */ + if (non_corr) + { + /* compute the non_corr first sets for all nonterminals and terms */ - 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); + 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); } #endif # ifndef NDEBUG - if (debug) { + if (debug) + { fputs("Safeties:\n", stderr); - for (p = nonterms; p < maxnt; p++) { - fprintf(stderr, "%s\t%d\t%d\n", - p->n_name, - getntsafe(p), - getntout(p)); + for (p = nonterms; p < maxnt; p++) + { + fprintf(stderr, "%s\t%d\t%d\n", p->n_name, getntsafe(p), + getntout(p)); } } # endif } -STATIC -createsets() { - /* - * Allocate space for the sets. Also determine which files use - * which nonterminals, and determine which nonterminals can be - * made static. - */ +/* + * Allocate space for the sets. Also determine which files use + * which nonterminals, and determine which nonterminals can be + * made static. + */STATIC void createsets(void) +{ register p_nont p; register p_file f; register p_start st; @@ -175,11 +195,14 @@ createsets() { int n = NINTS(NBYTES(nnonterms)); p_mem alloc(); - for (f = files; f < maxfiles; f++) { + for (f = files; f < maxfiles; f++) + { register 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) { + 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) + { p = &nonterms[i]; p->n_flags |= GENSTATIC; p->n_first = get_set(); @@ -191,32 +214,39 @@ createsets() { walk(f->f_used, p->n_rule); } } - for (f = files; f < maxfiles; f++) { - for (i = f->f_nonterminals; i != -1; i = p->n_next) { + for (f = files; f < maxfiles; f++) + { + for (i = f->f_nonterminals; i != -1; i = p->n_next) + { register p_file f2; p = &nonterms[i]; - for (f2 = files; f2 < maxfiles; f2++) { - if (f2 != f && IN(f2->f_used, i)) { + for (f2 = files; f2 < maxfiles; f2++) + { + if (f2 != f && IN(f2->f_used, i)) + { p->n_flags &= ~GENSTATIC; } } } } - for (st = start; st; st = st->ff_next) { + for (st = start; st; st = st->ff_next) + { nonterms[st->ff_nont].n_flags &= ~GENSTATIC; } } -STATIC void -walk(u, p) p_set u; register p_gram p; { - /* - * Walk through the grammar rule p, allocating sets - */ - - for (;;) { - switch (g_gettype(p)) { - case TERM : { +/* + * 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: + { register p_term q; q = g_getterm(p); @@ -227,8 +257,10 @@ walk(u, p) p_set u; register p_gram p; { #endif q->t_follow = get_set(); walk(u, q->t_rule); - break; } - case ALTERNATION : { + break; + } + case ALTERNATION: + { register p_link l; l = g_getlink(p); @@ -238,159 +270,192 @@ walk(u, p) p_set u; register p_gram p; { #endif l->l_others = get_set(); walk(u, l->l_rule); - break; } - case NONTERM : { + break; + } + case NONTERM: + { register int i = g_getcont(p); PUTIN(u, i); - break; } - case EORULE : + break; + } + case EORULE: return; } p++; } } -STATIC -co_trans(fc) int (*fc)(); { +STATIC void co_trans(int (*fc)()) +{ register p_nont p; register int change; - do { + do + { change = 0; - for (p = nonterms; p < maxnt; p++) { - if ((*fc)(p)) change = 1; + for (p = nonterms; p < maxnt; p++) + { + if ((*fc)(p)) + change = 1; } } while (change); } -STATIC int -nempty(p) register p_nont p; { - if (!(p->n_flags & EMPTY) && empty(p->n_rule)) { +STATIC int nempty(register p_nont p) +{ + if (!(p->n_flags & EMPTY) && empty(p->n_rule)) + { p->n_flags |= EMPTY; return 1; } return 0; } -empty(p) register p_gram p; { - /* - * Does the rule pointed to by p produce empty ? - */ +/* + * Does the rule pointed to by p produce empty ? + */ +int empty(register p_gram p) +{ - for (;;) { - switch (g_gettype(p)) { - case EORULE : + for (;;) + { + switch (g_gettype(p)) + { + case EORULE: return 1; - case TERM : { + case TERM: + { register p_term q; q = g_getterm(p); - 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)) { + 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)) + { return 1; } - if (g_gettype(p+1) == EORULE) return 0; + if (g_gettype(p+1) == EORULE) + return 0; break; - case NONTERM : - if (nonterms[g_getcont(p)].n_flags & EMPTY) { + case NONTERM: + if (nonterms[g_getcont(p)].n_flags & EMPTY) + { break; } /* Fall through */ - case LITERAL : - case TERMINAL : + case LITERAL: + case TERMINAL: return 0; } p++; } } -STATIC int -nfirst(p) register p_nont p; { +STATIC int nfirst(register 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(register 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; { - /* - * 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 +/* + * 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. */ - register 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. - */ s = 0; noenter = 0; - for (;;) { - switch (g_gettype(p)) { - case EORULE : + for (;;) + { + switch (g_gettype(p)) + { + case EORULE: return s; - case TERM : { + case TERM: + { register p_term q; q = g_getterm(p); - if (flag == 0) { - if (first(q->t_first,q->t_rule,0))/*nothing*/; + if (flag == 0) + { + if (first(q->t_first, q->t_rule, 0))/*nothing*/ + ; } - if (!noenter) s |= setunion(setp,q->t_first); + if (!noenter) + s |= setunion(setp, q->t_first); p++; - if (r_getkind(q) == STAR || - r_getkind(q) == OPT || - empty(q->t_rule)) continue; - break; } - case ALTERNATION : { + if (r_getkind(q) == STAR || r_getkind(q) == OPT || empty(q->t_rule)) + continue; + break; + } + case ALTERNATION: + { register p_link l; l = g_getlink(p); - if (flag == 0) { - if (first(l->l_symbs,l->l_rule,0))/*nothing*/; + if (flag == 0) + { + if (first(l->l_symbs, l->l_rule, 0))/*nothing*/ + ; } - if (noenter == 0) { - s |= setunion(setp,l->l_symbs); - } - if (g_gettype(p+1) == EORULE) return s; + if (noenter == 0) + { + s |= setunion(setp, l->l_symbs); } + if (g_gettype(p+1) == EORULE) + return s; + } /* Fall Through */ - case ACTION : + case ACTION: p++; continue; - case LITERAL : - case TERMINAL : - if ((noenter == 0) && !IN(setp,g_getcont(p))) { + case LITERAL: + case TERMINAL: + if ((noenter == 0) && !IN(setp,g_getcont(p))) + { s = 1; - PUTIN(setp,g_getcont(p)); + PUTIN(setp, g_getcont(p)); } p++; break; - case NONTERM : { + case NONTERM: + { register p_nont n; n = &nonterms[g_getcont(p)]; - if (noenter == 0) { - s |= setunion(setp,n->n_first); - if (ntneeded) NTPUTIN(setp,g_getcont(p)); + if (noenter == 0) + { + s |= setunion(setp, n->n_first); + if (ntneeded) + NTPUTIN(setp, g_getcont(p)); } p++; - if (n->n_flags & EMPTY) continue; - break; } + if (n->n_flags & EMPTY) + continue; + break; + } } - if (flag == 0) { + if (flag == 0) + { noenter = 1; continue; } @@ -399,99 +464,113 @@ 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; { - /* - * 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 - */ +/* + * 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) +{ - register 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. - */ + 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. + */ s = 0; noenter = 0; - for (;;) { - switch (g_gettype(p)) { - case EORULE : + for (;;) + { + switch (g_gettype(p)) + { + case EORULE : return s; - case TERM : { - register p_term q; - - q = g_getterm(p); - if (flag == 0) { - if (nc_first(q->t_nc_first,q->t_rule,0))/*nothing*/; - } - 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; + case TERM : + { + register p_term q; + + q = g_getterm(p); + if (flag == 0) + { + if (nc_first(q->t_nc_first,q->t_rule,0))/*nothing*/; + } + 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; - l = g_getlink(p); - if (flag == 0) { - if (nc_first(l->l_nc_symbs,l->l_rule,0))/*nothing*/; - } - if (noenter == 0) { - s |= setunion(setp,l->l_nc_symbs); - } - if (g_gettype(p+1) == EORULE) return s; + l = g_getlink(p); + if (flag == 0) + { + if (nc_first(l->l_nc_symbs,l->l_rule,0))/*nothing*/; + } + if (noenter == 0) + { + s |= setunion(setp,l->l_nc_symbs); + } + if (g_gettype(p+1) == EORULE) return s; } p++; continue; - case ACTION : { - register p_start subp; + case ACTION : + { + register 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); - 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); - - } - p++; - continue; + } + p++; + continue; } - case LITERAL : - case TERMINAL : - if (g_getcont(p) == g_getcont(illegal_gram)) { + case LITERAL : + case TERMINAL : + if (g_getcont(p) == g_getcont(illegal_gram)) + { /* Ignore for this set. */ p++; continue; } - if ((noenter == 0) && !IN(setp,g_getcont(p))) { + if ((noenter == 0) && !IN(setp,g_getcont(p))) + { s = 1; PUTIN(setp,g_getcont(p)); } p++; break; - 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; } + 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;} } - if (flag == 0) { + if (flag == 0) + { noenter = 1; continue; } @@ -500,197 +579,221 @@ nc_first(setp,p,flag) p_set setp; register p_gram p; { } #endif -STATIC int -nfollow(p) register p_nont p; { +STATIC int nfollow(register p_nont p) +{ return follow(p->n_follow, p->n_rule); } -STATIC -follow(setp,p) p_set setp; register 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 */ +/* + * 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 */ s = 0; - for (;;) { - switch (g_gettype(p)) { - case EORULE : + for (;;) + { + switch (g_gettype(p)) + { + case EORULE: return s; - case TERM : { + case TERM: + { register p_term q; q = g_getterm(p); - if (empty(p+1)) { + 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_follow,setp); + s |= setunion(q->t_follow, setp); } /* * Everything that can start the rest of the rule * can follow the term */ - s |= first(q->t_follow,p+1,1); - if (r_getkind(q) == STAR || - r_getkind(q) == PLUS || - r_getnum(q) ) { + s |= first(q->t_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 |= follow(q->t_first,q->t_rule); + s |= follow(q->t_first, q->t_rule); } /* * Now propagate the set computed sofar */ s |= follow(q->t_follow, q->t_rule); - break; } - case ALTERNATION : + break; + } + case ALTERNATION: /* * Just propagate setp */ - s |= follow(setp,g_getlink(p)->l_rule); + s |= follow(setp, g_getlink(p)->l_rule); break; - case NONTERM : { + case NONTERM: + { register p_nont n; n = &nonterms[g_getcont(p)]; - s |= first(n->n_follow,p+1,1); - if (empty(p+1)) { + s |= first(n->n_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_follow,setp); + s |= setunion(n->n_follow, setp); } - break; } + break; + } } p++; } + return 0; } #ifdef NON_CORRECTING -STATIC int -nc_nfollow(p) register p_nont p; { +STATIC int nc_nfollow(register 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, register 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 */ + register int s; /* Will gather return value */ s = 0; - for (;;) { - switch (g_gettype(p)) { - case EORULE : + for (;;) + { + switch (g_gettype(p)) + { + case EORULE : return s; - case TERM : { - register p_term q; - - q = g_getterm(p); - if (empty(p+1)) { + 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); + } /* - * If what follows the term can be empty, - * everything that can follow the whole - * rule can also follow the term + * Everything that can start the rest of the rule + * can follow the term */ - s |= setunion(q->t_nc_follow,setp); - } - /* - * Everything that can start the rest of the rule - * can follow the term - */ - s |= nc_first(q->t_nc_follow,p+1,1); - if (r_getkind(q) == STAR || - r_getkind(q) == PLUS || - r_getnum(q) ) { + 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); + } /* - * If the term involves a repetition - * of possibly more than one, - * everything that can start the term - * can also follow it. + * Now propagate the set computed sofar */ - s |= nc_follow(q->t_nc_first,q->t_rule); - } - /* - * Now propagate the set computed sofar - */ - s |= nc_follow(q->t_nc_follow, q->t_rule); - break; } - case ALTERNATION : + s |= nc_follow(q->t_nc_follow, q->t_rule); + break;} + case ALTERNATION : /* * Just propagate setp */ s |= nc_follow(setp,g_getlink(p)->l_rule); break; - 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; } + 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;} } p++; } + return 0; } #endif -STATIC void -co_dirsymb(setp,p) p_set setp; register p_gram p; { +STATIC void co_dirsymb(p_set setp, register p_gram p) +{ /* * Walk the rule p, doing the work for alternations */ register p_gram s = 0; - for (;;) { - switch (g_gettype(p)) { - case EORULE : + for (;;) + { + switch (g_gettype(p)) + { + case EORULE: return; - case TERM : { + case TERM: + { register p_term q; q = g_getterm(p); - co_dirsymb(q->t_follow,q->t_rule); - break; } - case ALTERNATION : { + co_dirsymb(q->t_follow, q->t_rule); + break; + } + case ALTERNATION: + { register p_link l; /* * Save first alternative */ - if (!s) s = p; + if (!s) + s = p; l = g_getlink(p); - co_dirsymb(setp,l->l_rule); - if (empty(l->l_rule)) { + co_dirsymb(setp, l->l_rule); + if (empty(l->l_rule)) + { /* * If the rule can produce empty, everything * that follows it can also start it */ - setunion(l->l_symbs,setp); + setunion(l->l_symbs, setp); } - if (g_gettype(p+1) == EORULE) { + if (g_gettype(p+1) == EORULE) + { /* * Every alternation is implemented as a * choice between two alternatives : @@ -700,25 +803,27 @@ co_dirsymb(setp,p) p_set setp; register p_gram p; { */ co_others(s); return; - } } + } + } } 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; + register p_link l1, l2; l1 = g_getlink(p); p++; l2 = g_getlink(p); - setunion(l1->l_others,l2->l_symbs); - if (g_gettype(p+1) != EORULE) { + setunion(l1->l_others, l2->l_symbs); + if (g_gettype(p+1) != EORULE) + { /* * First compute l2->l_others */ @@ -726,16 +831,14 @@ co_others(p) register p_gram p; { /* * and then l1->l_others */ - setunion(l1->l_others,l2->l_others); + setunion(l1->l_others, l2->l_others); } } 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]; int x = pl->cnt; @@ -745,40 +848,43 @@ ncomplength(p) return pl->cnt < INFINITY && x == INFINITY; } -STATIC -do_lengthcomp() { - /* - * 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. - */ +/* + * 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) +{ register p_length pl; register p_nont p; p_mem alloc(); length = (p_length) alloc((unsigned) (nnonterms * sizeof(*length))); - for (pl = &length[nnonterms-1]; pl >= length; pl--) { + for (pl = &length[nnonterms - 1]; pl >= length; pl--) + { pl->val = pl->cnt = INFINITY; } co_trans(ncomplength); pl = length; - for (p = nonterms; p < maxnt; p++, pl++) { - if (pl->cnt == INFINITY) { + for (p = nonterms; p < maxnt; p++, pl++) + { + if (pl->cnt == INFINITY) + { p->n_flags |= RECURSIVE; } setdefaults(p->n_rule); } - free ((p_mem) length); + free((p_mem) length); } -STATIC void -complength(p,le) register p_gram p; p_length le; { +STATIC void complength(register p_gram p, p_length le) +{ /* * Walk grammar rule p, computing minimum lengths */ @@ -790,82 +896,95 @@ complength(p,le) register p_gram p; p_length le; { X.cnt = 0; X.val = 0; - for (;;) { - switch (g_gettype(p)) { - case LITERAL : - case TERMINAL : + for (;;) + { + switch (g_gettype(p)) + { + case LITERAL: + case TERMINAL: #ifdef NON_CORRECTING - if (g_getcont(p) == g_getcont(illegal_gram)) { + if (g_getcont(p) == g_getcont(illegal_gram)) + { add(&X, INFINITY, 0); break; } #endif add(&X, 1, 0); break; - case ALTERNATION : + case ALTERNATION: X.cnt = INFINITY; X.val = INFINITY; - while (g_gettype(p) != EORULE) { + while (g_gettype(p) != EORULE) + { cnt++; l = g_getlink(p); p++; - complength(l->l_rule,&i); + complength(l->l_rule, &i); i.val += cnt; - if (l->l_flag & DEF) { + if (l->l_flag & DEF) + { X = i; break; } - if (compare(&i, &X) < 0) { + if (compare(&i, &X) < 0) + { X = i; } } /* Fall through */ - case EORULE : + case EORULE: le->cnt = X.cnt; le->val = X.val; return; - case TERM : { + case TERM: + { register int rep; q = g_getterm(p); rep = r_getkind(q); X.val += 1; - if ((q->t_flags&PERSISTENT) || - rep==FIXED || rep==PLUS) { - complength(q->t_rule,&i); + if ((q->t_flags & PERSISTENT) || rep == FIXED || rep == PLUS) + { + complength(q->t_rule, &i); add(&X, i.cnt, i.val); - if (rep == FIXED && r_getnum(q) > 0) { - for (rep = r_getnum(q) - 1; - rep > 0; rep--) { + if (rep == FIXED && r_getnum(q) > 0) + { + for (rep = r_getnum(q) - 1; rep > 0; rep--) + { add(&X, i.cnt, i.val); } } } - break; } - case NONTERM : { + break; + } + case NONTERM: + { int nn = g_getcont(p); register p_length pl = &length[nn]; int x = pl->cnt; - if (x == INFINITY) { + if (x == INFINITY) + { pl->cnt = -1; - complength(nonterms[nn].n_rule,pl); + complength(nonterms[nn].n_rule, pl); x = pl->cnt; } - else if (x == -1) x = INFINITY; + else if (x == -1) + x = INFINITY; add(&X, x, pl->val); X.val += 1; - } + } } p++; } } -STATIC void -add(a, c, v) register p_length a; { +STATIC void add(register p_length a, int c, int v) +{ - if (a->cnt == INFINITY || c == INFINITY) { + if (a->cnt == INFINITY || c == INFINITY) + { a->cnt = INFINITY; return; } @@ -873,22 +992,26 @@ add(a, c, v) register p_length a; { a->cnt += c; } -STATIC int -compare(a, b) register p_length a, b; { - if (a->cnt != b->cnt) return a->cnt - b->cnt; +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 void -setdefaults(p) register p_gram p; { - for (;;) { - switch(g_gettype(p)) { - case EORULE: +STATIC void setdefaults(register p_gram p) +{ + for (;;) + { + switch (g_gettype(p)) + { + case EORULE: return; - case TERM: + case TERM: setdefaults(g_getterm(p)->t_rule); break; - case ALTERNATION: { + case ALTERNATION: + { register p_link l, l1; int temp = 0, temp1, cnt = 0; t_length count, i; @@ -896,41 +1019,46 @@ setdefaults(p) register p_gram p; { count.cnt = INFINITY; count.val = INFINITY; l1 = g_getlink(p); - do { + do + { cnt++; l = g_getlink(p); p++; - complength(l->l_rule,&i); + complength(l->l_rule, &i); i.val += cnt; - if (l->l_flag & DEF) temp = 1; + if (l->l_flag & DEF) + temp = 1; temp1 = compare(&i, &count); - if (temp1 < 0 || - (temp1 == 0 && l1->l_flag & AVOIDING)) { + if (temp1 < 0 || (temp1 == 0 && l1->l_flag & AVOIDING)) + { l1 = l; count = i; } setdefaults(l->l_rule); } while (g_gettype(p) != EORULE); - if (!temp) { + if (!temp) + { /* No user specified default */ l1->l_flag |= DEF; } - return; } + return; + } } p++; } } -STATIC -do_contains(n) register p_nont n; { +STATIC void do_contains(register p_nont n) +{ /* * Compute the total set of symbols that nonterminal n can * produce */ - if (n->n_contains == 0) { + if (n->n_contains == 0) + { n->n_contains = get_set(); - contains(n->n_rule,n->n_contains); + contains(n->n_rule, n->n_contains); /* * If the rule can produce empty, delete all symbols that * can follow the rule as well as be in the rule. @@ -938,158 +1066,193 @@ do_contains(n) register p_nont n; { * symbols that are guaranteed to be eaten by the rule! * Otherwise, the generated parser may loop forever */ - if (n->n_flags & EMPTY) { - setminus(n->n_contains,n->n_follow); + if (n->n_flags & EMPTY) + { + setminus(n->n_contains, n->n_follow); } /* * But the symbols that can start the rule are always * eaten */ - setunion(n->n_contains,n->n_first); + setunion(n->n_contains, n->n_first); } } -STATIC void -contains(p,set) register p_gram p; register p_set set; { +STATIC void contains(register p_gram p, register p_set set) +{ /* * Does the real computation of the contains-sets */ - for (;;) { - switch (g_gettype(p)) { - case EORULE : + for (;;) + { + switch (g_gettype(p)) + { + case EORULE: return; - case TERM : { + case TERM: + { register p_term q; int rep; q = g_getterm(p); rep = r_getkind(q); - if ((q->t_flags & PERSISTENT) || - rep == PLUS || rep == FIXED) { + if ((q->t_flags & PERSISTENT) || rep == PLUS || rep == FIXED) + { /* * In these cases, the term belongs to the * continuation grammar. * Otherwise, q->t_contains is just * q->t_first */ - if (!q->t_contains) { - q->t_contains = get_set(); + if (!q->t_contains) + { + q->t_contains = get_set(); } - contains(q->t_rule,q->t_contains); - if (rep != FIXED || empty(q->t_rule)) { - setminus(q->t_contains,q->t_follow); + contains(q->t_rule, q->t_contains); + if (rep != FIXED || empty(q->t_rule)) + { + setminus(q->t_contains, q->t_follow); } - setunion(q->t_contains,q->t_first); - } else { + setunion(q->t_contains, q->t_first); + } + else + { contains(q->t_rule, (p_set) 0); q->t_contains = q->t_first; } - if (set) setunion(set,q->t_contains); - break; } - case NONTERM : { + if (set) + setunion(set, q->t_contains); + break; + } + case NONTERM: + { register p_nont n; n = &nonterms[g_getcont(p)]; do_contains(n); - if (set) { + if (set) + { setunion(set, n->n_contains); - if (ntneeded) NTPUTIN(set, g_getcont(p)); + if (ntneeded) + NTPUTIN(set, g_getcont(p)); } - break; } - case ALTERNATION : { + break; + } + case ALTERNATION: + { register p_link l; l = g_getlink(p); - contains(l->l_rule, - (l->l_flag & DEF) ? set : (p_set) 0); - break; } - case LITERAL : - case TERMINAL : { - register hulp; - - if (set) { + contains(l->l_rule, (l->l_flag & DEF) ? set : (p_set) 0); + break; + } + case LITERAL: + case TERMINAL: + { + register int hulp; + + if (set) + { hulp = g_getcont(p); assert(hulp < ntokens); - PUTIN(set,hulp); - }} + PUTIN(set, hulp); + } + } } p++; } } -STATIC int nsafes(p) register p_nont p; { - int ch; +STATIC int nsafes(register p_nont p) +{ + int ch; register int i; ch = 0; i = getntsafe(p); - if (i != NOSAFETY) { + if (i != NOSAFETY) + { i = do_safes(p->n_rule, i, &ch); - if (i < SCANDONE) i = SCANDONE; + if (i < SCANDONE) + i = SCANDONE; /* After a nonterminal, we only know whether a scan was done - or not - */ - if (getntout(p) != i) { + or not + */ + if (getntout(p) != i) + { ch = 1; - setntout(p,i); + setntout(p, i); } } return ch; } -STATIC int -do_safes(p,safe,ch) register p_gram p; register int *ch; { +STATIC int do_safes(register p_gram p, int safe, register int *ch) +{ /* * Walk the grammar rule, doing the computation described in the * comment of the procedure above this one. */ - int retval; + int retval; - for (;;) { - switch (g_gettype(p)) { - case ACTION: + for (;;) + { + switch (g_gettype(p)) + { + case ACTION: p++; continue; - case LITERAL: - case TERMINAL: + case LITERAL: + case TERMINAL: safe = NOSCANDONE; break; - case TERM : { + case TERM: + { register p_term q; - int i,rep; + int i, rep; q = g_getterm(p); i = r_getnum(q); rep = r_getkind(q); retval = do_safes(q->t_rule, - t_safety(rep,i,q->t_flags&PERSISTENT,safe),ch); + t_safety(rep, i, q->t_flags & PERSISTENT, safe), ch); settout(q, retval); safe = t_after(rep, i, retval); - break; } - case ALTERNATION : { + break; + } + case ALTERNATION: + { register p_link l; register int i; retval = -1; - while (g_gettype(p) == ALTERNATION) { + while (g_gettype(p) == ALTERNATION) + { l = g_getlink(p); p++; - if (safe > SAFE && (l->l_flag & DEF)) { - i = do_safes(l->l_rule,SAFESCANDONE,ch); + if (safe > SAFE && (l->l_flag & DEF)) + { + i = do_safes(l->l_rule, SAFESCANDONE, ch); } - else i = do_safes(l->l_rule,SAFE,ch); - if (retval == -1) retval = i; - else if (i != retval) { - if (i == NOSCANDONE || - retval == NOSCANDONE) { + else + i = do_safes(l->l_rule, SAFE, ch); + if (retval == -1) + retval = i; + else if (i != retval) + { + if (i == NOSCANDONE || retval == NOSCANDONE) + { retval = SCANDONE; } - else if (i > retval) retval = i; + else if (i > retval) + retval = i; } } - return retval; } - case NONTERM : { + return retval; + } + case NONTERM: + { register p_nont n; register int nsafe, osafe; @@ -1097,63 +1260,82 @@ do_safes(p,safe,ch) register p_gram p; register int *ch; { nsafe = getntsafe(n); osafe = safe; safe = getntout(n); - if (safe == NOSAFETY) safe = SCANDONE; - if (osafe == nsafe) break; - if (nsafe == NOSAFETY) { + if (safe == NOSAFETY) + safe = SCANDONE; + if (osafe == nsafe) + break; + if (nsafe == NOSAFETY) + { *ch = 1; setntsafe(n, osafe); break; } - if (osafe == NOSCANDONE || nsafe == NOSCANDONE) { - if (nsafe != SCANDONE) { + if (osafe == NOSCANDONE || nsafe == NOSCANDONE) + { + if (nsafe != SCANDONE) + { *ch = 1; setntsafe(n, SCANDONE); } break; } - if (osafe > nsafe) { + if (osafe > nsafe) + { setntsafe(n, osafe); *ch = 1; } - break; } - case EORULE : + break; + } + case EORULE: return safe; } p++; } } -t_safety(rep, count, persistent, safety) { +int t_safety(int rep, int count, int persistent, int safety) +{ - if (safety == NOSCANDONE) safety = SCANDONE; - switch(rep) { - default: + if (safety == NOSCANDONE) + safety = SCANDONE; + switch (rep) + { + default: assert(0); - case OPT: - if (!persistent || safety < SAFESCANDONE) return SAFE; + case OPT: + if (!persistent || safety < SAFESCANDONE) + return SAFE; return SAFESCANDONE; - case STAR: - if (persistent) return SAFESCANDONE; + case STAR: + if (persistent) + return SAFESCANDONE; return SAFE; - case PLUS: - if (persistent) { - if (safety > SAFESCANDONE) return safety; + case PLUS: + if (persistent) + { + if (safety > SAFESCANDONE) + return safety; return SAFESCANDONE; } return safety; - case FIXED: - if (!count) return safety; + case FIXED: + if (!count) + return safety; return SCANDONE; } /* NOTREACHED */ } -t_after(rep, count, outsafety) { - if (count == 0 && (rep == STAR || rep == PLUS)) { +int t_after(int rep, int count, int outsafety) +{ + if (count == 0 && (rep == STAR || rep == PLUS)) + { return SAFESCANDONE; } - if (rep != FIXED) { - if (outsafety <= SAFESCANDONE) return SAFESCANDONE; + if (rep != FIXED) + { + if (outsafety <= SAFESCANDONE) + return SAFESCANDONE; return SCANDONE; } return outsafety; diff --git a/util/LLgen/src/extern.h b/util/LLgen/src/extern.h index fca911761..f1405e786 100644 --- a/util/LLgen/src/extern.h +++ b/util/LLgen/src/extern.h @@ -2,6 +2,8 @@ * For full copyright and restrictions on use see the file COPYING in the top * level of the LLgen tree. */ +#ifndef EXTERN_H_ +#define EXTERN_H_ /* * L L G E N @@ -84,7 +86,6 @@ extern string nc_rec_file, nc_incl_file; extern int low_percentage, high_percentage; extern int min_cases_for_jmptable; extern int jmptable_option; -extern int ansi_c; #ifdef NON_CORRECTING extern int non_corr; extern int subpars_sim; @@ -92,3 +93,23 @@ extern p_gram illegal_gram; #endif extern int strip_grammar; extern int in_production; + +void error(int lineno,string s,string t); +void warning(int lineno,string s,string t); +void fatal(int lineno,string s,string t); + + +int empty(register p_gram); +int t_safety(int, int, int, int); +int t_after(int, int, int); +string store(string); +void name_init(void); +p_gram search(int, register string, int); +void co_reach(void); +void install(string, string); +void copyfile(string); + + + + +#endif /* EXTERN_H_ */ diff --git a/util/LLgen/src/gencode.c b/util/LLgen/src/gencode.c index 484319511..6599a386a 100644 --- a/util/LLgen/src/gencode.c +++ b/util/LLgen/src/gencode.c @@ -19,6 +19,8 @@ */ #include +#include +# include "alloc.h" # include "types.h" # include "io.h" # include "extern.h" @@ -34,85 +36,84 @@ static string rcsid3 = "$Id$"; * Some codestrings 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 string c_arrend = "0 };\n"; +static string c_close = "}\n"; +static string c_break = "break;\n"; +static string c_read = "LLread();\n"; /* Some constants used for reading from the action file */ # define ENDDECL 0400 # define IDENT 0401 -static int nlabel; /* count for the generation of labels */ -static int firsts; /* are there any? */ +static int nlabel; /* count for the generation of labels */ +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(); +STATIC void doclose(FILE *); +STATIC int *mk_tokenlist(void); +STATIC void genhdr(void); +STATIC void opentemp(string); +STATIC void geninclude(void); +STATIC void genrecovery(void); #ifdef NON_CORRECTING -STATIC genncrecovery(); +STATIC void genncrecovery(void); #endif -STATIC string genname(); -STATIC generate(); -STATIC void prset(); -STATIC void macro(); -STATIC controlline(); -STATIC getparams(); -STATIC getansiparams(); -STATIC genprototypes(); -STATIC gettok(); -STATIC void rulecode(); -STATIC int * dopush(); -STATIC int * mk_tokenlist(); -STATIC void getaction(); -STATIC void alternation(); -STATIC codeforterm(); -STATIC genswhead(); -STATIC gencases(); -STATIC genpush(); -STATIC genpop(); -STATIC void genincrdecr(); -STATIC add_cases(); -STATIC int analyze_switch(); -STATIC out_list(); -STATIC genextname(); -STATIC correct_prefix(); +STATIC void generate(p_file); +STATIC void prset(p_set); +STATIC void macro(string, p_nont); +STATIC void controlline(void); +STATIC void getparams(void); +STATIC void genprototypes(register p_file); +STATIC void getansiparams(int); +STATIC int gettok(void); +STATIC void rulecode(register p_gram, int, int, int); +STATIC void alternation(p_gram, int, int, int, int); +STATIC int *dopush(register p_gram, int, int, int **); +STATIC void getaction(int); +STATIC int codeforterm(register p_term, int, int); +STATIC void genswhead(register p_term, int, int, int, int); +STATIC void gencases(int *, int, int); +STATIC string genname(string); +STATIC void genpush(int); +STATIC void genincrdecr(string, int); +STATIC void genpop(int); +STATIC int analyze_switch(int *); +STATIC void add_cases(p_set, int *, int); +STATIC void out_list(int *, int, int); +STATIC void genextname(int, char *, FILE *); +STATIC void correct_prefix(void); + + +extern void save_grammar(FILE *f); # define NOPOP -20000 -p_mem alloc(), ralloc(); -doclose(f) - FILE *f; +STATIC void doclose(FILE *f) { - if (ferror(f) != 0) { - fatal(0,"Write error on temporary"); + if (ferror(f) != 0) + { + fatal(0, "Write error on temporary", NULL ); } fclose(f); } -STATIC int * -mk_tokenlist() +STATIC int *mk_tokenlist(void) { register int i = ntokens; - register int *p = (int *)alloc((unsigned)(i * sizeof(int))) + i; + register int *p = (int *) alloc((unsigned) (i * sizeof(int))) + i; - while (i--) *--p = -1; + while (i--) + *--p = -1; return p; } -STATIC -genhdr() +STATIC void genhdr(void) { - if (!firsts) fputs("#define LLNOFIRSTS\n", fpars); - if (ansi_c) fputs("#define LL_ANSI_C 1\n", fpars); - else { - fputs("#if __STDC__ || __cplusplus\n#define LL_ANSI_C 1\n#endif\n", fpars); - } + if (!firsts) + fputs("#define LLNOFIRSTS\n", fpars); #ifdef NON_CORRECTING if (non_corr) fputs("#define LL_NON_CORR 1\n", fpars); #endif @@ -120,26 +121,29 @@ genhdr() copyfile(incl_file); } -gencode(argc) { +void gencode(int argc) +{ register p_file p = files; /* Set up for code generation */ - if ((fact = fopen(f_temp,"r")) == NULL) { - fatal(0,e_noopen,f_temp); + if ((fact = fopen(f_temp, "r")) == NULL ) + { + fatal(0, e_noopen, f_temp); } #ifdef NON_CORRECTING /* The non-correcting error recovery must be generated BEFORE - parser code is generated!!!! In case of conflict resolvers, - the code-generation process will delete conflicting symbols - from first and followsets, making them UNUSABLE for the - non-correcting error recovery code. + parser code is generated!!!! In case of conflict resolvers, + the code-generation process will delete conflicting symbols + from first and followsets, making them UNUSABLE for the + non-correcting error recovery code. */ if (non_corr) - genncrecovery(); + genncrecovery(); #endif /* For every source file .... */ - while (argc--) { + while (argc--) + { /* Open temporary */ f_input = p->f_name; opentemp(f_input); @@ -149,7 +153,7 @@ gencode(argc) { getaction(2); doclose(fpars); /* And install */ - install(genname(p->f_name),p->f_name); + install(genname(p->f_name), p->f_name); p++; } geninclude(); @@ -158,34 +162,37 @@ gencode(argc) { fclose(fact); } -STATIC -opentemp(str) string str; { +STATIC void opentemp(string str) +{ - if ((fpars = fopen(f_pars,"w")) == NULL) { - fatal(0,e_noopen,f_pars); + if ((fpars = fopen(f_pars, "w")) == NULL ) + { + fatal(0, e_noopen, f_pars); } - if (!str) str = "."; - fprintf(fpars,LLgenid,str); + if (!str) + str = "."; + fprintf(fpars, LLgenid, str); } -STATIC -geninclude() { +STATIC void geninclude(void) +{ register p_token p; int maxno = 0; opentemp((string) 0); - for (p = tokens; p < maxt; p++) { - if (p->t_tokno > maxno) maxno = p->t_tokno; - if (p->t_tokno >= 0400) { - fprintf(fpars,"#define %s %d\n", - p->t_string, - p->t_tokno); + for (p = tokens; p < maxt; p++) + { + if (p->t_tokno > maxno) + maxno = p->t_tokno; + if (p->t_tokno >= 0400) + { + fprintf(fpars, "#define %s %d\n", p->t_string, p->t_tokno); } } - fprintf(fpars, "#define %s_MAXTOKNO %d\n", prefix ? prefix : "LL", - maxno); + fprintf(fpars, "#define %s_MAXTOKNO %d\n", prefix ? prefix : "LL", maxno); #ifdef NON_CORRECTING - if (non_corr) { + if (non_corr) + { fprintf(fpars, "#define %sNONCORR\n", prefix ? prefix : "LL"); } #endif @@ -193,108 +200,121 @@ geninclude() { install(f_include, "."); } -STATIC -genrecovery() { - register FILE *f; +STATIC void genrecovery(void) +{ + register FILE *f; register p_token t; - register int *q; - register p_nont p; - register p_set *psetl; - int *index; - int i; + register int *q; + register p_nont p; + register p_set *psetl; + int *index; + int i; register p_start st; opentemp((string) 0); f = fpars; correct_prefix(); genhdr(); - for (st = start; st; st = st->ff_next) { + for (st = start; st; st = st->ff_next) + { /* Make sure that every set the parser needs is in the list * before generating a define of the number of them! */ p = &nonterms[st->ff_nont]; - if (g_gettype(p->n_rule) == ALTERNATION) { + if (g_gettype(p->n_rule) == ALTERNATION) + { findindex(p->n_contains); } } i = maxptr - setptr; fprintf(f, -"#define LL_SSIZE %d\n#define LL_NSETS %d\n#define LL_NTERMINALS %d\n", - nbytes, - i > 0 ? i : 1, - ntokens); - if (onerror) fprintf(f,"#define LL_USERHOOK %s\n", onerror); + "#define LL_SSIZE %d\n#define LL_NSETS %d\n#define LL_NTERMINALS %d\n", + nbytes, i > 0 ? i : 1, ntokens); + if (onerror) + fprintf(f, "#define LL_USERHOOK %s\n", onerror); #ifdef NON_CORRECTING - if (non_corr) { - fputs("static int nc_done = 0;\n", f); - fputs("static int err_seen = 0;\n", f); + if (non_corr) + { + fputs("static int nc_done = 0;\n", f); + fputs("static int err_seen = 0;\n", f); } #endif /* Now generate the routines that call the startsymbols */ - fputs("#if LL_ANSI_C\n", f); - for (st = start; st; st = st->ff_next) { + for (st = start; st; st = st->ff_next) + { p = &nonterms[st->ff_nont]; fputs("void ", f); genextname(st->ff_nont, p->n_name, f); fputs("(void);\n", f); } - fputs("#endif\n", f); - for (st = start; st; st = st->ff_next) { - fprintf(f, "#if LL_ANSI_C\nvoid %s(void)\n#else\n%s()\n#endif\n", st->ff_name, st->ff_name); + for (st = start; st; st = st->ff_next) + { + fprintf(f, "void %s(void)\n", st->ff_name); p = &nonterms[st->ff_nont]; fputs(" {\n\tunsigned int s[LL_NTERMINALS+LL_NSETS+2];", f); #ifdef NON_CORRECTING - if (non_corr) { - fputs(" \n\tint oldstartsymb;", f); - fputs(" \n\tint oldncflag;", f); - fputs(" \n\toldstartsymb = LLstartsymb;", f); - fputs(" \n\toldncflag = nc_done;", f); - fputs(" \n\tnc_done = 0;", f); - fprintf(f, "\n\tLLstartsymb = %d;", st->ff_nont + assval); + if (non_corr) + { + fputs(" \n\tint oldstartsymb;", f); + fputs(" \n\tint oldncflag;", f); + fputs(" \n\toldstartsymb = LLstartsymb;", f); + fputs(" \n\toldncflag = nc_done;", f); + fputs(" \n\tnc_done = 0;", f); + fprintf(f, "\n\tLLstartsymb = %d;", st->ff_nont + assval); } #endif fputs("\n\tLLnewlevel(s);\n\tLLread();\n", f); - if (g_gettype(p->n_rule) == ALTERNATION) { + if (g_gettype(p->n_rule) == ALTERNATION) + { genpush(findindex(p->n_contains)); } genextname(st->ff_nont, p->n_name, f); fputs("();\n", f); - if (getntout(p) == NOSCANDONE) { - fputs("\tLL_NOSCANDONE(EOFILE);\n",f); + if (getntout(p) == NOSCANDONE) + { + fputs("\tLL_NOSCANDONE(EOFILE);\n", f); } - else fputs("\tLL_SCANDONE(EOFILE);\n",f); - fputs("\tLLoldlevel(s);\n",f); + else + fputs("\tLL_SCANDONE(EOFILE);\n", f); + fputs("\tLLoldlevel(s);\n", f); #ifdef NON_CORRECTING - if (non_corr) { - fputs("\tLLstartsymb = oldstartsymb;\n", f); - fputs("\tif (nc_done == 1) { \n", f); - fputs("\t\terr_seen = 1;\n", f); - fputs("\tnc_done = oldncflag;\n", f); - fputs("\t}\n", f); + if (non_corr) + { + fputs("\tLLstartsymb = oldstartsymb;\n", f); + fputs("\tif (nc_done == 1) { \n", f); + fputs("\t\terr_seen = 1;\n", f); + fputs("\tnc_done = oldncflag;\n", f); + fputs("\t}\n", f); } #endif fputs("}\n", f); } /* Now generate the sets */ - fputs("static char LLsets[] = {\n",f); - for (psetl = setptr; psetl < maxptr; psetl++) prset(*psetl); + fputs("static char LLsets[] = {\n", f); + for (psetl = setptr; psetl < maxptr; psetl++) + prset(*psetl); fputs(c_arrend, f); index = (int *) alloc((unsigned) (assval * sizeof(int))); - for (q = index; q < &index[assval];) *q++ = -1; - for (t = tokens; t < maxt; t++) { + for (q = index; q < &index[assval];) + *q++ = -1; + for (t = tokens; t < maxt; t++) + { index[t->t_tokno] = t - tokens; } - fputs("#define LLindex (LL_index+1)\nstatic short LL_index[] = {0,0,\n",f); - for (q = index+1; q < &index[assval]; q++) { + fputs("#define LLindex (LL_index+1)\nstatic short LL_index[] = {0,0,\n", f); + for (q = index + 1; q < &index[assval]; q++) + { fprintf(f, "%d,\n", *q); } fputs(c_arrend, f); free((p_mem) index); - if (onerror) { + if (onerror) + { fputs("static short LLtok[] = {\n", f); - for (t = tokens; t < maxt; t++) { - fprintf(f, t->t_tokno<0400 ? "'%s',\n" : "%s,\n",t->t_string); + for (t = tokens; t < maxt; t++) + { + fprintf(f, t->t_tokno < 0400 ? "'%s',\n" : "%s,\n", t->t_string); } fputs(c_arrend, f); } @@ -305,52 +325,54 @@ genrecovery() { } #ifdef NON_CORRECTING -STATIC -genncrecovery() { - register FILE *f; - register p_token t; - register int *q; - int *index; - - /* Generate the non-correcting error recovery file */ - - opentemp((string) 0); - f = fpars; - - genhdr(); - correct_prefix(); - save_grammar(f); - - fprintf(f, "#define LLFIRST_NT %d\n", assval); - fprintf(f, "#define LLSETSIZE %d\n", nbytes); - - index = (int *) alloc((unsigned) (assval * sizeof(int))); - for (q = index; q < &index[assval];) *q++ = -1; - for (t = tokens; t < maxt; t++) { - index[t->t_tokno] = t - tokens; - } - fputs("#define LLindex (LL_index+1)\nstatic short LL_index[] = {0,0,\n",f); - for (q = index+1; q < &index[assval]; q++) { - fprintf(f, "%d,\n", *q); - } - fputs(c_arrend, f); - free((p_mem) index); - - copyfile(nc_incl_file); - copyfile(nc_rec_file); - - doclose(f); - install(f_nc, "."); +STATIC void genncrecovery(void) +{ + register FILE *f; + register p_token t; + register int *q; + int *index; + + /* Generate the non-correcting error recovery file */ + + opentemp((string) 0); + f = fpars; + + genhdr(); + correct_prefix(); + save_grammar(f); + + fprintf(f, "#define LLFIRST_NT %d\n", assval); + fprintf(f, "#define LLSETSIZE %d\n", nbytes); + + index = (int *) alloc((unsigned) (assval * sizeof(int))); + for (q = index; q < &index[assval];) *q++ = -1; + for (t = tokens; t < maxt; t++) + { + index[t->t_tokno] = t - tokens; + } + fputs("#define LLindex (LL_index+1)\nstatic short LL_index[] = {0,0,\n",f); + for (q = index+1; q < &index[assval]; q++) + { + fprintf(f, "%d,\n", *q); + } + fputs(c_arrend, f); + free((p_mem) index); + + copyfile(nc_incl_file); + copyfile(nc_rec_file); + + doclose(f); + install(f_nc, "."); } #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; + register p_nont p; int i; register p_first ff; int mustpop; @@ -359,75 +381,81 @@ generate(f) p_file f; { fprintf(fpars, "#define LL_LEXI %s\n", lexical); listcount = 0; /* Generate first sets */ - for (ff = f->f_firsts; ff; ff = ff->ff_next) { - macro(ff->ff_name,&nonterms[ff->ff_nont]); + for (ff = f->f_firsts; ff; ff = ff->ff_next) + { + macro(ff->ff_name, &nonterms[ff->ff_nont]); } genhdr(); /* For every nonterminal generate a function */ - for (s = f->f_nonterminals; s != -1; s = p->n_next) { + for (s = f->f_nonterminals; s != -1; s = p->n_next) + { p = &nonterms[s]; /* Generate functions in the order in which the nonterminals * were defined in the grammar. This is important, because * of synchronisation with the action file */ - while (p->n_count--) getaction(1); - if (g_gettype(p->n_rule) == EORULE && - getntparams(p) == 0) { + while (p->n_count--) + getaction(1); + if (g_gettype(p->n_rule) == EORULE && getntparams(p) == 0) + { continue; } - if (is_first) genprototypes(f); + if (is_first) + genprototypes(f); is_first = 0; - if (p->n_flags & GENSTATIC) fputs("static\n", fpars); - fputs("#if LL_ANSI_C\nvoid\n#endif\n", fpars); + if (p->n_flags & GENSTATIC) + fputs("static\n", fpars); + fputs("void\n", fpars); genextname(s, p->n_name, fpars); - if (p->n_flags & PARAMS) { - long off = ftell(fact); - fputs("(\n#if LL_ANSI_C\n", fpars); + if (p->n_flags & PARAMS) + { + fputs("(\n", fpars); +/* long off = ftell(fact);*/ controlline(); getansiparams(1); - fseek(fact, off, 0); - fputs("#else\n", fpars); - controlline(); - getparams(); - fputs("#endif\n{\n", fpars); +/* fseek(fact, off, 0);*/ + fputs("{\n", fpars); } - else fputs("(\n#if LL_ANSI_C\nvoid\n#endif\n) {\n", fpars); - if (p->n_flags & LOCALS) getaction(1); + else + fputs("(\nvoid\n) {\n", fpars); + if (p->n_flags & LOCALS) + getaction(1); i = getntsafe(p); mustpop = NOPOP; - if (g_gettype(p->n_rule) == ALTERNATION && - i > SAFESCANDONE) { + if (g_gettype(p->n_rule) == ALTERNATION && i > SAFESCANDONE) + { mustpop = findindex(p->n_contains); - if (i == NOSCANDONE) { + if (i == NOSCANDONE) + { fputs(c_read, fpars); i = SCANDONE; } } nlabel = 1; - rulecode(p->n_rule, - i, - getntout(p) != NOSCANDONE, - mustpop); + rulecode(p->n_rule, i, getntout(p) != NOSCANDONE, mustpop); fputs(c_close, fpars); } } -STATIC void -prset(p) p_set p; { +STATIC void prset(p_set p) +{ register int k; register unsigned i; int j; j = nbytes; - for (;;) { + for (;;) + { i = (unsigned) *p++; - for (k = 0; k < sizeof(int); k++) { - fprintf(fpars,"'\\%o',",(int)(i & 0377)); + for (k = 0; k < sizeof(int); k++) + { + fprintf(fpars, "'\\%o',", (int) (i & 0377)); i >>= 8; - if (--j == 0) { - fputs("\n",fpars); + if (--j == 0) + { + fputs("\n", fpars); return; } } @@ -435,39 +463,42 @@ prset(p) p_set p; { /* NOTREACHED */ } -STATIC void -macro(s,n) string s; p_nont n; { +STATIC void macro(string s, p_nont n) +{ int i; i = findindex(n->n_first); - if (i < 0) { - fprintf(fpars, "#define %s(x) ((x) == %d)\n", - s, - tokens[-(i+1)].t_tokno); + if (i < 0) + { + fprintf(fpars, "#define %s(x) ((x) == %d)\n", s, + tokens[-(i + 1)].t_tokno); return; } firsts = 1; - fprintf(fpars,"#define %s(x) LLfirst((x), %d)\n", s, i); + fprintf(fpars, "#define %s(x) LLfirst((x), %d)\n", s, i); } -STATIC -controlline() { +STATIC void controlline(void) +{ /* Copy a compiler control line */ register int l; - register FILE *f1,*f2; + register FILE *f1, *f2; - f1 = fact; f2 = fpars; + f1 = fact; + f2 = fpars; l = getc(f1); assert(l == '\0'); - do { + do + { l = getc(f1); - if (l == EOF) fatal(0, "temp file mangled"); - putc(l,f2); - } while ( l != '\n' ) ; + if (l == EOF) + fatal(0, "temp file mangled", NULL ); + putc(l, f2); + } while (l != '\n'); } -STATIC -getparams() { +STATIC void getparams(void) +{ /* getparams is called if a nonterminal has parameters. The names * of the parameters have to be found, and they should be declared */ @@ -482,98 +513,92 @@ getparams() { off = ftell(fact); /* First pass through declaration, find the parameter names */ ltext[0] = '\0'; - while ((l = gettok()) != ENDDECL) { - if ((l == ';' || l == ',') && ltext[0] != '\0') { + while ((l = gettok()) != ENDDECL) + { + if ((l == ';' || l == ',') && ltext[0] != '\0') + { /* * The last identifier found before a ';' or a ',' * must be a parameter */ - fprintf(fpars,"%c%s", first, ltext); + fprintf(fpars, "%c%s", first, ltext); first = ','; ltext[0] = '\0'; } } - if (ltext[0] != '\0') { + if (ltext[0] != '\0') + { fprintf(fpars, "%c%s", first, ltext); add_semi = ';'; } - fputs(") ",fpars); + fputs(") ", fpars); /* * Now copy the declarations */ - l = getc(fact); /* patch: some implementations of fseek - do not work properly after "ungetc" - */ - fseek(fact,off,0); + l = getc(fact); /* patch: some implementations of fseek + do not work properly after "ungetc" + */ + fseek(fact, off, 0); getaction(0); - fprintf(fpars, "%c\n",add_semi); + fprintf(fpars, "%c\n", add_semi); } -STATIC -genprototypes(f) - register p_file f; +STATIC void genprototypes(register p_file f) { /* * Generate prototypes for all nonterminals */ register int i; - register p_nont p; - long off = ftell(fact); + register p_nont p; + long off = ftell(fact); - fputs("#if LL_ANSI_C\n", fpars); - for (i = 0; i < nnonterms; i++) { - if (! IN(f->f_used, i)) continue; + for (i = 0; i < nnonterms; i++) + { + if (!IN(f->f_used, i)) + continue; p = &nonterms[i]; - if (g_gettype(p->n_rule) == EORULE && - getntparams(p) == 0) { + if (g_gettype(p->n_rule) == EORULE && getntparams(p) == 0) + { continue; } - if (p->n_flags & GENSTATIC) fputs("static ", fpars); + if (p->n_flags & GENSTATIC) + fputs("static ", fpars); fputs("void ", fpars); genextname(i, p->n_name, fpars); - if (p->n_flags & PARAMS) { + if (p->n_flags & PARAMS) + { fputs("(\n", fpars); fseek(fact, p->n_off, 0); controlline(); getansiparams(0); } - else fputs("(void);\n", fpars); + else + fputs("(void);\n", fpars); } fseek(fact, off, 0); - fputs("#else\n", fpars); - for (i = 0; i < nnonterms; i++) { - if (! IN(f->f_used, i)) continue; - p = &nonterms[i]; - if (!(p->n_flags & GENSTATIC)) continue; - if (g_gettype(p->n_rule) == EORULE && - getntparams(p) == 0) { - continue; - } - fputs("static ", fpars); - genextname(i, p->n_name, fpars); - fputs("();\n", fpars); - } - fputs("#endif\n", fpars); } -STATIC -getansiparams(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. - */ +/* 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. + */STATIC void getansiparams(int mkdef) +{ register int l; int delayed = 0; ltext[0] = '\0'; - while ((l = gettok()) != ENDDECL) { - if (l > 0177 || c_class[l] != ISSPA) { - if (delayed) { + while ((l = gettok()) != ENDDECL) + { + if (l > 0177 || c_class[l] != ISSPA) + { + if (delayed) + { fputc(',', fpars); delayed = 0; } } - if ((l == ';' || l == ',') && ltext[0] != '\0') { + if ((l == ';' || l == ',') && ltext[0] != '\0') + { /* * The last identifier found before a ';' or a ',' * must be a parameter @@ -581,152 +606,182 @@ getansiparams(mkdef) { delayed = 1; ltext[0] = '\0'; } - else if (l == IDENT) fprintf(fpars, "%s", ltext); - else fputc(l, fpars); + else if (l == IDENT) + fprintf(fpars, "%s", ltext); + else + fputc(l, fpars); } fprintf(fpars, ") %c\n", mkdef ? ' ' : ';'); } -STATIC -gettok() { +STATIC int gettok(void) +{ /* Read from the action file. */ register int ch; - register string c; + register string c; register FILE *f; f = fact; ch = getc(f); - switch(ch) { - case '\n': - ch = getc(f); - if (ch != EOF) { - ungetc(ch,f); - if (ch != '\0') return '\n'; - } - return ENDDECL; - case '\0': - ungetc(ch,f); - /* Fall through */ - case EOF : - return ENDDECL; - default : - if (c_class[ch] == ISLET) { - c = ltext; - do { - *c++ = ch; - if (c-ltext >= LTEXTSZ) --c; - ch = getc(f); - } while (c_class[ch] == ISLET || c_class[ch] == ISDIG); - if (ch != EOF) ungetc(ch,f); - *c = '\0'; - return IDENT; - } - return ch; + switch (ch) + { + case '\n': + ch = getc(f); + if (ch != EOF) + { + ungetc(ch, f); + if (ch != '\0') + return '\n'; + } + return ENDDECL; + case '\0': + ungetc(ch, f); + /* Fall through */ + case EOF: + return ENDDECL; + default: + if (c_class[ch] == ISLET) + { + c = ltext; + do + { + *c++ = ch; + if (c - ltext >= LTEXTSZ) + --c; + ch = getc(f); + } while (c_class[ch] == ISLET || c_class[ch] == ISDIG); + if (ch != EOF) + ungetc(ch, f); + *c = '\0'; + return IDENT; + } + return ch; } } -STATIC void -rulecode(p,safety,mustscan,mustpop) register p_gram p; { +STATIC void rulecode(register 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 *pushlist; - int *ppushlist; + register int toplevel = 1; + register FILE *f; + register int *ppu; + int *pushlist; + int *ppushlist; /* * First generate code to push the contains sets of this rule * on a stack */ - ppushlist = dopush(p,safety,1, &pushlist); - if (mustpop != NOPOP) for (ppu = pushlist; ppu < ppushlist; ppu++) { - if (*ppu == mustpop) { - *ppu = mustpop = NOPOP; - break; + ppushlist = dopush(p, safety, 1, &pushlist); + if (mustpop != NOPOP) + for (ppu = pushlist; ppu < ppushlist; ppu++) + { + if (*ppu == mustpop) + { + *ppu = mustpop = NOPOP; + break; + } } - } - if (g_gettype(p) != ALTERNATION) { + if (g_gettype(p) != ALTERNATION) + { genpop(mustpop); mustpop = NOPOP; } - for (ppu = pushlist; ppu < ppushlist; ppu++) genpush(*ppu); + for (ppu = pushlist; ppu < ppushlist; ppu++) + genpush(*ppu); free((p_mem) pushlist); f = fpars; - for (;;) { - switch (g_gettype(p)) { - case EORULE : - if (mustscan && safety == NOSCANDONE) { - fputs(c_read,f); + for (;;) + { + switch (g_gettype(p)) + { + case EORULE: + if (mustscan && safety == NOSCANDONE) + { + fputs(c_read, f); } return; - case LITERAL : - case TERMINAL : { + case LITERAL: + case TERMINAL: + { register p_token t; string s; t = &tokens[g_getcont(p)]; - if (toplevel == 0) { - fprintf(f,"LLtdecr(%d);\n", g_getcont(p)); + if (toplevel == 0) + { + fprintf(f, "LLtdecr(%d);\n", g_getcont(p)); } - if (safety == SAFE) { - fputs("LL_SAFE(",f); + if (safety == SAFE) + { + fputs("LL_SAFE(", f); } - else if (safety == SAFESCANDONE) { - fputs("LL_SSCANDONE(",f); + else if (safety == SAFESCANDONE) + { + fputs("LL_SSCANDONE(", f); } - else if (safety == SCANDONE) { - fputs("LL_SCANDONE(",f); + else if (safety == SCANDONE) + { + fputs("LL_SCANDONE(", f); } - else /* if (safety == NOSCANDONE) */ { + else /* if (safety == NOSCANDONE) */ + { fputs("LL_NOSCANDONE(", f); } - if (t->t_tokno < 0400) s = "'%s');\n"; - else s = "%s);\n"; - fprintf(f,s,t->t_string); - if (safety <= SAFESCANDONE && toplevel > 0) { + if (t->t_tokno < 0400) + s = "'%s');\n"; + else + s = "%s);\n"; + fprintf(f, s, t->t_string); + if (safety <= SAFESCANDONE && toplevel > 0) + { safety = NOSCANDONE; toplevel = -1; p++; continue; } safety = NOSCANDONE; - break; } - case NONTERM : { + break; + } + case NONTERM: + { register p_nont n = &nonterms[g_getcont(p)]; - if (safety == NOSCANDONE && - getntsafe(n) < NOSCANDONE) { + if (safety == NOSCANDONE && getntsafe(n) < NOSCANDONE) + { safety = getntsafe(n); fputs(c_read, f); } - if (toplevel == 0 && - (g_gettype(n->n_rule) != ALTERNATION || - getntsafe(n) <= SAFESCANDONE)) { + if (toplevel == 0 + && (g_gettype(n->n_rule) != ALTERNATION + || getntsafe(n) <= SAFESCANDONE)) + { genpop(findindex(n->n_contains)); } genextname(g_getcont(p), n->n_name, f); - if (g_getnpar(p)) { + if (g_getnpar(p)) + { fputs("(\n", f); controlline(); getaction(0); - fputs(");\n",f); + fputs(");\n", f); } - else fputs("();\n", f); + else + fputs("();\n", f); safety = getntout(n); - break; } - case TERM : - safety = codeforterm(g_getterm(p), - safety, - toplevel); break; - case ACTION : + } + case TERM: + safety = codeforterm(g_getterm(p), safety, toplevel); + break; + case ACTION: getaction(1); p++; continue; - case ALTERNATION : + case ALTERNATION: alternation(p, safety, mustscan, mustpop, 0); return; } @@ -735,54 +790,66 @@ rulecode(p,safety,mustscan,mustpop) register p_gram p; { } } -STATIC void -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; - int hulp, hulp1,hulp2; - int haddefault = 0; - int nsafe; - p_set set; - p_set setalloc(); - int *tokenlist = mk_tokenlist(); - int casecnt = 0; - int compacted; - int unsafe; + register p_gram p = pp; + register FILE *f = fpars; + register p_link l; + int hulp, hulp1, hulp2; + int haddefault = 0; + int nsafe; + p_set set; + p_set setalloc(); + int *tokenlist = mk_tokenlist(); + int casecnt = 0; + int compacted; + int unsafe; assert(safety < NOSCANDONE); hulp = nlabel++; hulp1 = nlabel++; hulp2 = nlabel++; - if (!lb) lb = hulp1; + if (!lb) + lb = hulp1; unsafe = onerror || safety > SAFESCANDONE; - if (!unsafe) { + if (!unsafe) + { genpop(mustpop); mustpop = NOPOP; } - if (unsafe && hulp1 == lb) { - fprintf(f,"goto L_%d; /* so that the label is used for certain */\nL_%d: ;\n", hulp1, hulp1); + if (unsafe && hulp1 == lb) + { + fprintf(f, + "goto L_%d; /* so that the label is used for certain */\nL_%d: ;\n", + hulp1, hulp1); } - if (safety == SAFE) { + if (safety == SAFE) + { /* check if we can avoid to generate the switch */ - for (;;) { - if (g_gettype(p) == EORULE) return; + for (;;) + { + if (g_gettype(p) == EORULE) + return; l = g_getlink(p); - if (l->l_flag & COND) break; - if ((g_gettype(l->l_rule) != TERMINAL && - g_gettype(l->l_rule) != LITERAL) || - g_gettype(l->l_rule+1) != EORULE) break; + if (l->l_flag & COND) + break; + if ((g_gettype(l->l_rule) != TERMINAL + && g_gettype(l->l_rule) != LITERAL) + || g_gettype(l->l_rule+1) != EORULE) + break; p++; } p = pp; } - while (g_gettype(p) != EORULE) { + while (g_gettype(p) != EORULE) + { set = 0; l = g_getlink(p); - if (l->l_flag & COND) { - if (!(l->l_flag & NOCONF)) { + if (l->l_flag & COND) + { + if (!(l->l_flag & NOCONF)) + { set = setalloc(); setunion(set, l->l_others); setintersect(set, l->l_symbs); @@ -791,24 +858,31 @@ alternation(pp, safety, mustscan, mustpop, lb) add_cases(set, tokenlist, casecnt++); } } - if (!unsafe && (l->l_flag & DEF)) { + if (!unsafe && (l->l_flag & DEF)) + { haddefault = 1; } - else add_cases(l->l_symbs, tokenlist, casecnt++); - if (l->l_flag & DEF) { + else + add_cases(l->l_symbs, tokenlist, casecnt++); + if (l->l_flag & DEF) + { haddefault = 1; } - if ((l->l_flag & COND) && !(l->l_flag & NOCONF)) { + if ((l->l_flag & COND) && !(l->l_flag & NOCONF)) + { p++; - if (g_gettype(p+1) == EORULE) { + if (g_gettype(p+1) == EORULE) + { setminus(g_getlink(p)->l_symbs, set); free((p_mem) set); continue; } free((p_mem) set); - if (!haddefault) { + if (!haddefault) + { } - else { + else + { add_cases(l->l_others, tokenlist, casecnt++); unsafe = 0; } @@ -821,78 +895,96 @@ alternation(pp, safety, mustscan, mustpop, lb) p = pp; haddefault = 0; compacted = analyze_switch(tokenlist); - if (compacted) { + if (compacted) + { fputs("{", f); out_list(tokenlist, listcount++, casecnt); } - else fputs("switch(LLcsymb) {\n", f); + else + fputs("switch(LLcsymb) {\n", f); casecnt = 0; - while (g_gettype(p) != EORULE) { + while (g_gettype(p) != EORULE) + { l = g_getlink(p); - if (l->l_flag & COND) { - if (l->l_flag & NOCONF) { + if (l->l_flag & COND) + { + if (l->l_flag & NOCONF) + { fputs("#ifdef ___NOCONFLICT___\n", f); } - else gencases(tokenlist, casecnt++, compacted); + else + gencases(tokenlist, casecnt++, compacted); controlline(); - fputs("if (!",f); + fputs("if (!", f); getaction(0); - fprintf(f,") goto L_%d;\n", hulp); - if (l->l_flag & NOCONF) { + fprintf(f, ") goto L_%d;\n", hulp); + if (l->l_flag & NOCONF) + { fputs("#endif\n", f); } } - if (!unsafe && (l->l_flag & DEF)) { + if (!unsafe && (l->l_flag & DEF)) + { haddefault = 1; fputs("default:\n", f); } - else gencases(tokenlist, casecnt++, compacted); + else + gencases(tokenlist, casecnt++, compacted); nsafe = SAFE; - if (l->l_flag & DEF) { - if (unsafe) { - fprintf(f,"goto L_%d;\nL_%d: ;\n", hulp2, hulp2); + if (l->l_flag & DEF) + { + if (unsafe) + { + fprintf(f, "goto L_%d;\nL_%d: ;\n", hulp2, hulp2); } - if (safety != SAFE) nsafe = SAFESCANDONE; + if (safety != SAFE) + nsafe = SAFESCANDONE; } rulecode(l->l_rule, nsafe, mustscan, mustpop); - fputs(c_break,f); - if (unsafe && (l->l_flag & DEF)) { + fputs(c_break, f); + if (unsafe && (l->l_flag & DEF)) + { haddefault = 1; - fprintf(f, -"default: if (LLskip()) goto L_%d;\ngoto L_%d;\n", - lb, hulp2); + fprintf(f, "default: if (LLskip()) goto L_%d;\ngoto L_%d;\n", lb, + hulp2); } - if ((l->l_flag & COND) && !(l->l_flag & NOCONF)) { + if ((l->l_flag & COND) && !(l->l_flag & NOCONF)) + { p++; - fprintf(f,"goto L_%d;\nL_%d : ;\n", hulp, hulp); - if (g_gettype(p+1) == EORULE) { + fprintf(f, "goto L_%d;\nL_%d : ;\n", hulp, hulp); + if (g_gettype(p+1) == EORULE) + { continue; } - if (!haddefault) { + if (!haddefault) + { fputs("default:\n", f); } - else { + else + { gencases(tokenlist, casecnt++, compacted); safety = SAFE; unsafe = 0; } - if (! unsafe) { + if (!unsafe) + { genpop(mustpop); mustpop = NOPOP; } - alternation(p,safety,mustscan,mustpop,lb); + alternation(p, safety, mustscan, mustpop, lb); break; } p++; } - if (compacted) fputs(c_close, f); + if (compacted) + fputs(c_close, f); fputs(c_close, f); free((p_mem) tokenlist); } -STATIC int * -dopush(p,safety,toplevel,pp) register p_gram p; int **pp; { +STATIC int *dopush(register p_gram p, int safety, int toplevel, int **pp) +{ /* * The safety only matters if toplevel != 0 */ @@ -901,50 +993,60 @@ dopush(p,safety,toplevel,pp) register p_gram p; int **pp; { *pp = ip; - for (;;) { - if (ip - *pp >= i) { - *pp = (int *) - ralloc((p_mem) (*pp), (i + 100) * sizeof(int)); + for (;;) + { + if (ip - *pp >= i) + { + *pp = (int *) ralloc((p_mem) (*pp), (i + 100) * sizeof(int)); ip = *pp + i; i += 100; } - switch(g_gettype(p)) { - case EORULE : - case ALTERNATION : + switch (g_gettype(p)) + { + case EORULE: + case ALTERNATION: return ip; - case TERM : { + case TERM: + { register p_term q; int rep_kind, rep_count; q = g_getterm(p); rep_kind = r_getkind(q); rep_count = r_getnum(q); - if (!(toplevel > 0 && safety <= SAFESCANDONE && - (rep_kind == OPT || (rep_kind == FIXED && rep_count == 0)))) { + if (!(toplevel > 0 && safety <= SAFESCANDONE + && (rep_kind == OPT || (rep_kind == FIXED && rep_count == 0)))) + { *ip++ = findindex(q->t_contains); } - break; } - case LITERAL : - case TERMINAL : - if (toplevel > 0 && safety <= SAFESCANDONE) { + break; + } + case LITERAL: + case TERMINAL: + if (toplevel > 0 && safety <= SAFESCANDONE) + { toplevel = -1; p++; safety = NOSCANDONE; continue; } - if (toplevel == 0) *ip++ = -(g_getcont(p)+1); + if (toplevel == 0) + *ip++ = -(g_getcont(p) + 1); break; - case NONTERM : { + case NONTERM: + { register p_nont n; n = &nonterms[g_getcont(p)]; - if (toplevel == 0 || - (g_gettype(n->n_rule) == ALTERNATION && - getntsafe(n) > SAFESCANDONE)) { + if (toplevel == 0 + || (g_gettype(n->n_rule) == ALTERNATION + && getntsafe(n) > SAFESCANDONE)) + { *ip++ = findindex(n->n_contains); } - break; } - case ACTION : + break; + } + case ACTION: p++; continue; } @@ -956,158 +1058,185 @@ dopush(p,safety,toplevel,pp) register p_gram p; int **pp; { # define max(a,b) ((a) < (b) ? (b) : (a)) -STATIC void -getaction(flag) { - /* Read an action from the action file. - * flag = 1 if it is an action, - * 0 when reading parameters - */ +/* Read an action from the action file. + * flag = 1 if it is an action, + * 0 when reading parameters + */STATIC void getaction(int flag) +{ register int ch; register FILE *f; int mark = 0; - if (flag == 1) { + if (flag == 1) + { controlline(); } f = fpars; - for (;;) { + for (;;) + { ch = gettok(); - switch(ch) { - case ENDDECL: - if (flag != 2) break; + switch (ch) + { + case ENDDECL: + if (flag != 2) + break; ch = getc(fact); assert(ch == '\0'); - fputs("\n",f); - if (mark) return; + fputs("\n", f); + if (mark) + return; mark = 1; continue; - case IDENT : - fputs(ltext,f); + case IDENT: + fputs(ltext, f); continue; } mark = 0; - if (ch == ENDDECL) break; - putc(ch,f); + if (ch == ENDDECL) + break; + putc(ch, f); } - if (flag) fputs("\n",f); + if (flag) + fputs("\n", f); } -STATIC -codeforterm(q,safety,toplevel) register p_term q; { - /* - * Generate code for a term - */ - register FILE *f = fpars; - register int rep_count = r_getnum(q); - register int rep_kind = r_getkind(q); - int term_is_persistent = (q->t_flags & PERSISTENT); - int ispushed = NOPOP; - - if (!(toplevel > 0 && - (safety == 0 || (!onerror && safety <= SAFESCANDONE)) && - (rep_kind == OPT || (rep_kind == FIXED && rep_count == 0)))) { +/* + * Generate code for a term + */STATIC int codeforterm(register p_term q, int safety, int toplevel) +{ + register FILE *f = fpars; + register int rep_count = r_getnum(q); + register int rep_kind = r_getkind(q); + int term_is_persistent = (q->t_flags & PERSISTENT); + int ispushed = NOPOP; + + if (!(toplevel > 0 && (safety == 0 || (!onerror && safety <= SAFESCANDONE)) + && (rep_kind == OPT || (rep_kind == FIXED && rep_count == 0)))) + { ispushed = findindex(q->t_contains); } - if (safety == NOSCANDONE && (rep_kind != FIXED || rep_count == 0 || - gettout(q) != NOSCANDONE)) { + if (safety == NOSCANDONE + && (rep_kind != FIXED || rep_count == 0 || gettout(q) != NOSCANDONE)) + { fputs(c_read, f); safety = SCANDONE; } - if (rep_kind == PLUS && !term_is_persistent) { + if (rep_kind == PLUS && !term_is_persistent) + { int temp; temp = findindex(q->t_first); - if (temp != ispushed) { + if (temp != ispushed) + { genpop(ispushed); ispushed = temp; genpush(temp); } } - if (rep_count) { + if (rep_count) + { /* N > 0, so generate fixed forloop */ - fputs("{\nregister LL_i;\n", f); + fputs("{\nregister int LL_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) { + if (rep_kind == FIXED) + { fputs("if (!LL_i) ", f); genpop(ispushed); genpush(ispushed); - if (safety == NOSCANDONE) { + if (safety == NOSCANDONE) + { assert(gettout(q) == NOSCANDONE); fputs(c_read, f); } } } - else if (rep_kind != OPT && rep_kind != FIXED) { + else if (rep_kind != OPT && rep_kind != FIXED) + { /* '+' or '*', so generate infinite loop */ - fputs("for (;;) {\n",f); + fputs("for (;;) {\n", f); } - else if (rep_kind == OPT && - (safety == 0 || (!onerror && safety <= SAFESCANDONE))) { + else if (rep_kind == OPT + && (safety == 0 || (!onerror && safety <= SAFESCANDONE))) + { genpop(ispushed); ispushed = NOPOP; } - if (rep_kind == STAR || rep_kind == OPT) { + if (rep_kind == STAR || rep_kind == OPT) + { genswhead(q, rep_kind, rep_count, safety, ispushed); } rulecode(q->t_rule, - t_safety(rep_kind,rep_count,term_is_persistent,safety), - gettout(q) != NOSCANDONE, - rep_kind == FIXED ? ispushed : NOPOP); - if (gettout(q) == NOSCANDONE && rep_kind != FIXED) { + t_safety(rep_kind, rep_count, term_is_persistent, safety), + gettout(q) != NOSCANDONE, rep_kind == FIXED ? ispushed : NOPOP); + if (gettout(q) == NOSCANDONE && rep_kind != FIXED) + { fputs(c_read, f); } /* in the case of '+', the if is after the code for the rule */ - if (rep_kind == PLUS) { - if (rep_count) { + if (rep_kind == PLUS) + { + if (rep_count) + { fputs("if (!LL_i) break;\n", f); } genswhead(q, rep_kind, rep_count, safety, ispushed); } - if (rep_kind != OPT && rep_kind != FIXED) fputs("continue;\n", f); - if (rep_kind != FIXED) { + if (rep_kind != OPT && rep_kind != FIXED) + fputs("continue;\n", f); + if (rep_kind != FIXED) + { fputs(c_close, f); /* Close switch */ fputs(c_close, f); - if (rep_kind != OPT) { + if (rep_kind != OPT) + { genpop(ispushed); fputs(c_break, f); } } - if (rep_kind != OPT && (rep_kind != FIXED || rep_count > 0)) { - fputs(c_close, f); /* Close for */ - if (rep_count > 0) { + 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 ... */ } } 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(register p_term q, int rep_kind, int rep_count, + int safety, int ispushed) +{ /* * Generate switch statement for term q */ - register FILE *f = fpars; - p_set p1; - p_set setalloc(); - int hulp1 = 0, hulp2; - int safeterm; - int termissafe = 0; - int casecnt = 0; - int *tokenlist = mk_tokenlist(); - int compacted; - - if (rep_kind == PLUS) safeterm = gettout(q); - else if (rep_kind == OPT) safeterm = safety; - else /* if (rep_kind == STAR) */ safeterm = max(safety, gettout(q)); + register FILE *f = fpars; + p_set p1; + p_set setalloc(); + int hulp1 = 0, hulp2; + int safeterm; + int termissafe = 0; + int casecnt = 0; + int *tokenlist = mk_tokenlist(); + int compacted; + + if (rep_kind == PLUS) + safeterm = gettout(q); + else if (rep_kind == OPT) + safeterm = safety; + else + /* if (rep_kind == STAR) */safeterm = max(safety, gettout(q)); hulp2 = nlabel++; fprintf(f, "goto L_%d;\nL_%d : ", hulp2, hulp2); - if (q->t_flags & RESOLVER) { + if (q->t_flags & RESOLVER) + { hulp1 = nlabel++; - if (! (q->t_flags & NOCONF)) { + if (!(q->t_flags & NOCONF)) + { p1 = setalloc(); - setunion(p1,q->t_first); - setintersect(p1,q->t_follow); + setunion(p1, q->t_first); + setintersect(p1, q->t_follow); /* * p1 now points to a set containing the conflicting * symbols @@ -1119,61 +1248,81 @@ genswhead(q, rep_kind, rep_count, safety, ispushed) register p_term q; { free((p_mem) p1); } } - if (safeterm == 0 || (!onerror && safeterm <= SAFESCANDONE)) { + if (safeterm == 0 || (!onerror && safeterm <= SAFESCANDONE)) + { termissafe = 1; } - else add_cases(q->t_follow, tokenlist, casecnt++); - if (!onerror && (q->t_flags & PERSISTENT) && safeterm != SAFE) { + else + add_cases(q->t_follow, tokenlist, casecnt++); + if (!onerror && (q->t_flags & PERSISTENT) && safeterm != SAFE) + { add_cases(q->t_contains, tokenlist, casecnt); } - else add_cases(q->t_first, tokenlist, casecnt); + else + add_cases(q->t_first, tokenlist, casecnt); compacted = analyze_switch(tokenlist); fputs("{", f); - if (compacted) { + if (compacted) + { out_list(tokenlist, listcount++, casecnt); } - else fputs("switch(LLcsymb) {\n", f); + else + fputs("switch(LLcsymb) {\n", f); casecnt = 0; - if (q->t_flags & RESOLVER) { - if (q->t_flags & NOCONF) { + if (q->t_flags & RESOLVER) + { + if (q->t_flags & NOCONF) + { fputs("#ifdef ___NOCONFLICT___\n", f); } - else gencases(tokenlist, casecnt++, compacted); + else + gencases(tokenlist, casecnt++, compacted); controlline(); fputs("if (", f); getaction(0); fprintf(f, ") goto L_%d;\n", hulp1); - if (q->t_flags & NOCONF) { + if (q->t_flags & NOCONF) + { fputs("#endif /* ___NOCONFLICT___ */\n", f); } } - if (safeterm == 0 || (!onerror && safeterm <= SAFESCANDONE)) { + if (safeterm == 0 || (!onerror && safeterm <= SAFESCANDONE)) + { fputs("default:\n", f); } - else gencases(tokenlist, casecnt++, compacted); - if (rep_kind == OPT) genpop(ispushed); + else + gencases(tokenlist, casecnt++, compacted); + if (rep_kind == OPT) + genpop(ispushed); fputs(c_break, f); - if (! termissafe) { + if (!termissafe) + { int i; static int nvar; assert(ispushed != NOPOP); - if (ispushed >= 0) i = -ispushed; - else i = tokens[-(ispushed+1)].t_tokno; + if (ispushed >= 0) + i = -ispushed; + else + i = tokens[-(ispushed + 1)].t_tokno; ++nvar; - fprintf(f,"default:{int LL_%d=LLnext(%d);\n;if (!LL_%d) {\n", - nvar, i, nvar); - if (rep_kind == OPT) genpop(ispushed); + fprintf(f, "default:{int LL_%d=LLnext(%d);\n;if (!LL_%d) {\n", nvar, i, + nvar); + if (rep_kind == OPT) + genpop(ispushed); fputs(c_break, f); fputs(c_close, f); - fprintf(f,"else if (LL_%d & 1) goto L_%d;}\n",nvar, hulp2); + fprintf(f, "else if (LL_%d & 1) goto L_%d;}\n", nvar, hulp2); } gencases(tokenlist, casecnt, compacted); - if (q->t_flags & RESOLVER) { + if (q->t_flags & RESOLVER) + { fprintf(f, "goto L_%d;\nL_%d : ;\n", hulp1, hulp1); } - if (rep_kind == OPT) genpop(ispushed); - if (rep_count > 0) { + if (rep_kind == OPT) + genpop(ispushed); + if (rep_count > 0) + { assert(ispushed != NOPOP); fputs(rep_kind == STAR ? "if (!LL_i) " : "if (LL_i == 1) ", f); genpop(ispushed); @@ -1181,9 +1330,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 @@ -1203,43 +1350,54 @@ gencases(tokenlist, caseno, compacted) register p_token p; register int i; - if (compacted) fprintf(fpars, "case %d :\n", caseno); - for (i = 0, p = tokens; i < ntokens; i++, p++) { - if (tokenlist[i] == caseno) { + 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); + 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); } } } static char namebuf[20]; -STATIC string -genname(s) string s; { - /* - * Generate a target file name from the - * source file name s. - */ - register string c,d; +/* + * Generate a target file name from the + * source file name s. + */STATIC string genname(string s) +{ + register string c, d; c = namebuf; - while (*s) { - if (*s == '/') { - while (*s == '/') s++; - if (*s) c = namebuf; - else break; + while (*s) + { + if (*s == '/') + { + while (*s == '/') + s++; + if (*s) + c = namebuf; + else + break; } *c++ = *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); + for (d = c; --d > namebuf;) + if (*d == '.') + break; + if (d == namebuf) + d = c; + if (d >= &namebuf[12]) + { + fatal(0, "%s : filename too long", namebuf); } *d++ = '.'; *d++ = 'c'; @@ -1247,97 +1405,96 @@ genname(s) string s; { return namebuf; } -STATIC -genpush(d) { +STATIC void genpush(int d) +{ genincrdecr("incr", d); } -STATIC void -genincrdecr(s, d) string s; { - if (d == NOPOP) return; - if (d >= 0) { - fprintf(fpars, "LLs%s(%d);\n", s, d / nbytes); +STATIC void genincrdecr(string s, int d) +{ + if (d == NOPOP) + return; + if (d >= 0) + { + fprintf(fpars, "LLs%s(%d);\n", s, d / nbytes); return; } 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 ncases = 0; int percentage; int maxcase = 0, mincase = 0; - if (! jmptable_option) return 0; - for (i = 0; i < ntokens; i++) { - if (tokenlist[i] >= 0) { + if (!jmptable_option) + return 0; + for (i = 0; i < ntokens; i++) + { + if (tokenlist[i] >= 0) + { ncases++; - if (! mincase) mincase = i + 1; + if (!mincase) + mincase = i + 1; maxcase = i + 1; } } - if (ncases < min_cases_for_jmptable) return 0; + if (ncases < min_cases_for_jmptable) + return 0; percentage = ncases * 100 / (maxcase - mincase); fprintf(fpars, "/* percentage is %d */\n", percentage); 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; - for (i = 0; i < ntokens; i++) { - if (IN(s, i)) { + for (i = 0; i < ntokens; i++) + { + if (IN(s, i)) + { tokenlist[i] = 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; - fprintf(f, "static %s LL%d_tklist[] = {", - casecnt <= 127 ? "char" : "short", - listno); + fprintf(f, "static %s LL%d_tklist[] = {", casecnt <= 127 ? "char" : "short", + listno); - for (i = 0; i < ntokens; i++) { - fprintf(f, "%c%d,", i % 10 == 0 ? '\n': ' ', tokenlist[i]); + for (i = 0; i < ntokens; i++) + { + fprintf(f, "%c%d,", i % 10 == 0 ? '\n' : ' ', tokenlist[i]); } fputs(c_arrend, f); 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(void) { register FILE *f = fpars; register char *s = prefix; - if (s) { + if (s) + { fprintf(f, "#define LLsymb %ssymb\n", s); fprintf(f, "#define LLerror %serror\n", s); fprintf(f, "#define LLsafeerror %ssafeerror\n", s); diff --git a/util/LLgen/src/global.c b/util/LLgen/src/global.c index 8c54e7bd4..e2fcc8599 100644 --- a/util/LLgen/src/global.c +++ b/util/LLgen/src/global.c @@ -16,6 +16,7 @@ * Contains declarations visible in several other source files */ +#include # include "types.h" # include "extern.h" # include "io.h" @@ -44,8 +45,8 @@ FILE *fout; FILE *fpars; FILE *finput; FILE *fact; -char f_pars[] = PARSERFILE; -char f_temp[] = ACTFILE; +char f_pars[L_tmpnam+sizeof(char)]; /* Add one more character for NULL, just in case of buggy implementations. */ +char f_temp[L_tmpnam+sizeof(char)]; #ifdef NON_CORRECTING char f_nc[20]; #endif @@ -77,7 +78,6 @@ string nc_rec_file, nc_incl_file; int low_percentage = 10, high_percentage = 30; int min_cases_for_jmptable = 8; int jmptable_option; -int ansi_c = 0; #ifdef NON_CORRECTING int non_corr = 0; int subpars_sim = 0; diff --git a/util/LLgen/src/io.h b/util/LLgen/src/io.h index d81f96446..fdfe0d630 100644 --- a/util/LLgen/src/io.h +++ b/util/LLgen/src/io.h @@ -21,8 +21,6 @@ /* FILES */ # 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 */ #ifdef NON_CORRECTING diff --git a/util/LLgen/src/machdep.c b/util/LLgen/src/machdep.c index 7fddf8eb2..babe3b34d 100644 --- a/util/LLgen/src/machdep.c +++ b/util/LLgen/src/machdep.c @@ -15,48 +15,51 @@ * machdep.c * Machine dependant things */ - +#include +#ifdef USE_SYS +#include +#endif #include #include +# include "alloc.h" +# include "extern.h" # include "types.h" # ifndef NORCSID static string rcsid5 = "$Id$"; # endif -/* In this file the following routines are defined: */ -extern UNLINK(); -extern RENAME(); -extern string libpath(); +#ifndef LIBDIR +#define LIBDIR "lib" +#endif -UNLINK(x) string x; { +void UNLINK(string x) +{ /* Must remove the file "x" */ - #ifdef USE_SYS sys_remove(x); /* systemcall to remove file */ #else - unlink(x); + remove(x); #endif } -RENAME(x,y) string x,y; { +void RENAME(string x,string 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); + 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); #endif } -string -libpath(s) string s; { +string libpath(string s) +{ /* Must deliver a full pathname to the library file "s" */ - register string p; - register length; - p_mem alloc(); + register int length; char* libdir = getenv("LLGEN_LIB_DIR"); if (!libdir) @@ -68,3 +71,11 @@ libpath(s) string s; { strcat(p,s); return p; } + +void TMPNAM(string result) +{ + if (tmpnam(result)==NULL) + { + fatal(1, "Cannot create temporary file.", NULL); + } +} diff --git a/util/LLgen/src/main.c b/util/LLgen/src/main.c index 11a5c26a8..f26446e51 100644 --- a/util/LLgen/src/main.c +++ b/util/LLgen/src/main.c @@ -29,96 +29,103 @@ static string rcsid6 = "$Id$"; # 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 void install(); +STATIC void readgrammar(int, char *[]); +STATIC void doparse(register p_file); +STATIC void comfatal(void); -main(argc,argv) register string argv[]; { +extern void UNLINK(string); +extern void RENAME(string, string); +extern void TMPNAM(string); +extern string libpath(string); +extern void conflchecks(void); +extern void do_compute(void); +extern void gencode(int); + +int main(int argc, register string argv[]) +{ register string arg; - string libpath(); + + TMPNAM(f_temp); + TMPNAM(f_pars); /* Initialize */ assval = 0400; /* read options */ - while (argc >= 2 && (arg = argv[1], *arg == '-')) { - while (*++arg) { - switch(*arg) { - case 'j': - case 'J': + while (argc >= 2 && (arg = argv[1], *arg == '-')) + { + while (*++arg) + { + switch (*arg) + { + case 'j': + case 'J': jmptable_option = 1; if (*++arg) min_cases_for_jmptable = atoi(arg); break; - case 'w': - case 'W': + case 'w': + case 'W': wflag = 1; continue; - case 'v': - case 'V': + case 'v': + case 'V': verbose++; continue; - case 'l': - case 'L': + case 'l': + case 'L': low_percentage = atoi(++arg); break; - case 'h': - case 'H': + case 'h': + case 'H': high_percentage = atoi(++arg); break; # ifndef NDEBUG - case 'd': - case 'D': + case 'd': + case 'D': debug++; continue; - case 'r': - case 'R': - if (rec_file) { - fprintf(stderr,"duplicate -r flag\n"); + case 'r': + case 'R': + if (rec_file) + { + fprintf(stderr, "duplicate -r flag\n"); exit(1); } rec_file = ++arg; break; - case 'i': - case 'I': - if (incl_file) { - fprintf(stderr,"duplicate -i flag\n"); + case 'i': + case 'I': + if (incl_file) + { + fprintf(stderr, "duplicate -i flag\n"); exit(1); } incl_file = ++arg; break; #endif /* not NDEBUG */ - case 'x': - case 'X': + case 'x': + case 'X': ntneeded = 1; ntprint = 1; continue; - case 'a': - case 'A': - ansi_c = 1; - continue; #ifdef NON_CORRECTING - case 'n': - case 'N': + case 'n': + case 'N': non_corr = 1; continue; - case 's': - case 'S': + case 's': + case 'S': subpars_sim = 1; continue; #endif - case 'g': - case 'G': + case 'g': + case 'G': strip_grammar = 1; continue; - default: - fprintf(stderr,"illegal option : %c\n",*arg); + default: + fprintf(stderr, "illegal option : %c\n", *arg); exit(1); } break; @@ -128,55 +135,59 @@ main(argc,argv) register string argv[]; { } #ifdef NON_CORRECTING - if ((subpars_sim) && (!non_corr)) { - fprintf(stderr,"option -s illegal without -n, turned off\n"); - subpars_sim = 0; + if ((subpars_sim) && (!non_corr)) + { + fprintf(stderr,"option -s illegal without -n, turned off\n"); + subpars_sim = 0; } #endif /* * Now check wether the sets should include nonterminals */ - if (verbose == 2) ntneeded = 1; + if (verbose == 2) + ntneeded = 1; /* * Initialise */ # ifndef NDEBUG - if (!rec_file) { + if (!rec_file) + { # endif rec_file = libpath("rec"); # ifndef NDEBUG } - if (!incl_file) { + if (!incl_file) + { # endif incl_file = libpath("incl"); # ifndef NDEBUG } # endif #ifdef NON_CORRECTING - if (non_corr) { - nc_incl_file = libpath("nc_incl"); - nc_rec_file = libpath ("nc_rec"); + if (non_corr) + { + nc_incl_file = libpath("nc_incl"); + nc_rec_file = libpath ("nc_rec"); } #endif - close(mkstemp(f_temp)); - close(mkstemp(f_pars)); - if ((fact = fopen(f_temp,"w")) == NULL) { - fputs("Cannot create temporary\n",stderr); + if ((fact = fopen(f_temp, "w")) == NULL) + { + fputs("Cannot create temporary\n", stderr); exit(1); } name_init(); - readgrammar(argc,argv); + readgrammar(argc, argv); sprintf(f_out, OUTFILE, prefix ? prefix : "LL"); /* for the following two filenames only one L is used; historical - reasons ... - */ + reasons ... + */ sprintf(f_include, HFILE, prefix ? prefix : "L"); sprintf(f_rec, RFILE, prefix ? prefix : "L"); #ifdef NON_CORRECTING if (non_corr) - sprintf(f_nc, NCFILE, prefix ? prefix : "L"); + sprintf(f_nc, NCFILE, prefix ? prefix : "L"); #endif setinit(ntneeded); maxnt = &nonterms[nnonterms]; @@ -184,50 +195,60 @@ main(argc,argv) register string argv[]; { /* * Now, the grammar is read. Do some computations */ - co_reach(); /* Check for undefined and unreachable */ - if (nerrors) comfatal(); + co_reach(); /* Check for undefined and unreachable */ + if (nerrors) + comfatal(); do_compute(); conflchecks(); - if (nerrors) comfatal(); + if (nerrors) + comfatal(); fclose(fact); - if (argc-- == 1) { + if (argc-- == 1) + { fputs("No code generation for input from standard input\n", - stderr); + stderr); } - else gencode(argc); + else + gencode(argc); UNLINK(f_temp); UNLINK(f_pars); - if (verbose) { + if (verbose) + { fprintf(stderr, "number of nonterminals: %d\n", nnonterms); 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); } - exit(0); + exit(EXIT_SUCCESS); } -STATIC -readgrammar(argc,argv) char *argv[]; { +STATIC void readgrammar(int argc, char *argv[]) +{ /* * Do just what the name suggests : read the grammar */ - register p_file p; - p_mem alloc(); + register p_file p; + p_mem alloc(); linecount = 0; f_input = "no filename"; /* * Build the file structure */ - files = p = (p_file) alloc((unsigned) (argc+1) * sizeof(t_file)); - if (argc-- == 1) { + files = p = (p_file) alloc((unsigned) (argc + 1) * sizeof(t_file)); + if (argc-- == 1) + { finput = stdin; f_input = "standard input"; doparse(p++); - } else { - while (argc--) { - if ((finput = fopen(f_input=argv[1],"r")) == NULL) { - fatal(0,e_noopen,f_input); + } + else + { + while (argc--) + { + if ((finput = fopen(f_input = argv[1], "r")) == NULL) + { + fatal(0, e_noopen, f_input); } doparse(p++); argv++; @@ -235,19 +256,21 @@ readgrammar(argc,argv) char *argv[]; { } } maxfiles = p; - if (! lexical) lexical = "yylex"; + if (!lexical) + lexical = "yylex"; /* * There must be a start symbol! */ - if (! nerrors && start == 0) { - fatal(linecount,"Missing %%start"); + if (!nerrors && start == 0) + { + fatal(linecount, "Missing %%start", NULL); } - if (nerrors) comfatal(); + if (nerrors) + comfatal(); } -STATIC -doparse(p) register p_file p; { - +STATIC void doparse(register p_file p) +{ linecount = 0; p->f_name = f_input; p->f_firsts = 0; @@ -259,90 +282,100 @@ doparse(p) register p_file p; { p->f_terminals = torder; } -/* VARARGS1 */ -error(lineno,s,t,u) string s,t,u; { +void error(int lineno, string s, string t) +{ /* * Just an error message */ ++nerrors; - if (!lineno) lineno = 1; - fprintf(stderr,"\"%s\", line %d: ",f_input, lineno); - fprintf(stderr,s,t,u); - fputs("\n",stderr); + if (!lineno) + lineno = 1; + fprintf(stderr, "\"%s\", line %d: ", f_input, lineno); + fprintf(stderr, s, t); + fputs("\n", stderr); } -/* VARARGS1 */ -void -warning(lineno,s,t,u) string s,t,u; { +void warning(int lineno, string s, string t) +{ /* * Just a warning */ - if (wflag) return; - if (!lineno) lineno = 1; - fprintf(stderr,"\"%s\", line %d: (Warning) ",f_input, lineno); - fprintf(stderr,s,t,u); - fputs("\n",stderr); + if (wflag) + return; + if (!lineno) + lineno = 1; + fprintf(stderr, "\"%s\", line %d: (Warning) ", f_input, lineno); + fprintf(stderr, s, t); + fputs("\n", stderr); } -/* VARARGS1 */ -fatal(lineno,s,t,u) string s,t,u; { +void fatal(int lineno, string s, string t) +{ /* * Fatal error */ - error(lineno,s,t,u); + error(lineno, s, t); comfatal(); } -comfatal() { +STATIC void comfatal(void) +{ /* * Some common code for exit on errors */ - if (fact != NULL) { + if (fact != NULL) + { fclose(fact); UNLINK(f_temp); } - if (fpars != NULL) fclose(fpars); + if (fpars != NULL) + fclose(fpars); UNLINK(f_pars); - exit(1); + exit(EXIT_FAILURE); } -copyfile(file) string file; { +void copyfile(string file) +{ /* * Copies a file indicated by the parameter to filedescriptor fpars. */ - register int c; - register FILE *f; + register int c; + register FILE *f; - if ((f = fopen(file,"r")) == NULL) { - fatal(0,"Cannot open library file %s, call an expert",file); + if ((f = fopen(file, "r")) == NULL) + { + fatal(0, "Cannot open library file %s, call an expert", file); } - while ((c = getc(f)) != EOF) putc(c,fpars); + while ((c = getc(f)) != EOF) + putc(c, fpars); fclose(f); } -void -install(target, source) string target, source; { +void install(string target, string source) +{ /* * Copy the temporary file generated from source to target * if allowed (which means that the target must be generated * by LLgen from the source, or that the target is not present */ - register int c1, c2; - register FILE *f1, *f2; - int cnt; + register int c1, c2; + register FILE *f1, *f2; + int cnt; /* * First open temporary, generated for source */ - if ((f1 = fopen(f_pars,"r")) == NULL) { - fatal(0,e_noopen,f_pars); + if ((f1 = fopen(f_pars, "r")) == NULL) + { + fatal(0, e_noopen, f_pars); } /* * Now open target for reading */ - if ((f2 = fopen(target,"r")) == NULL) { + if ((f2 = fopen(target, "r")) == NULL) + { fclose(f1); RENAME(f_pars, target); return; @@ -355,20 +388,24 @@ install(target, source) string target, source; { /* * Now compare the target with the temporary */ - do { + do + { c1 = getc(f1); c2 = getc(f2); - if (cnt >= 0) cnt--; + if (cnt >= 0) + cnt--; } while (c1 == c2 && c1 != EOF); fclose(f1); fclose(f2); /* * Here, if c1 != c2 the target must be recreated */ - if (c1 != c2) { - if (cnt >= 0) { - fatal(0,"%s : not a file generated by LLgen",target); + if (c1 != c2) + { + if (cnt >= 0) + { + fatal(0, "%s : not a file generated by LLgen", target); } - RENAME(f_pars,target); + RENAME(f_pars, target); } } diff --git a/util/LLgen/src/name.c b/util/LLgen/src/name.c index 2b9ba11fb..25fd22d74 100644 --- a/util/LLgen/src/name.c +++ b/util/LLgen/src/name.c @@ -18,6 +18,8 @@ */ #include +#include +# include "alloc.h" # include "types.h" # include "extern.h" # include "assert.h" @@ -30,38 +32,36 @@ static string rcsid7 = "$Id$"; # define HASHSIZE 128 # define NMSIZ 2048 /* Room for names allocated NMSIZ bytes at a time */ -static char *name, *maxname; -static p_entry h_root[HASHSIZE]; /* hash table */ -static string e_literal = "Illegal literal"; -static p_entry entries, maxentries; -static t_info token_info, nont_info; +static char *name, *maxname; +static p_entry h_root[HASHSIZE]; /* hash table */ +static string e_literal = "Illegal literal"; +static p_entry entries, maxentries; +static t_info token_info, nont_info; /* Defined in this file are: */ -extern string store(); -extern name_init(); -STATIC int hash(); -STATIC p_entry newentry(); -extern p_gram search(); +STATIC int hash(string str); +STATIC p_entry newentry(string str, p_entry next); -p_mem alloc(); -p_mem new_mem(); -name_init() { - token_info.i_esize = sizeof (t_token); + +void name_init(void) +{ + token_info.i_esize = sizeof(t_token); token_info.i_incr = 50; - nont_info.i_esize = sizeof (t_nont); + nont_info.i_esize = sizeof(t_nont); nont_info.i_incr = 50; - search(TERMINAL,"EOFILE",ENTERING); + search(TERMINAL, "EOFILE", ENTERING); #ifdef NON_CORRECTING illegal_gram = search(TERMINAL,"LLILLEGAL",ENTERING); #endif } -STATIC p_entry -newentry(str, next) string str; p_entry next; { +STATIC p_entry newentry(string str, p_entry next) +{ register p_entry p; - if ((p = entries) == maxentries) { + if ((p = entries) == maxentries) + { p = (p_entry) alloc(50 * sizeof(t_entry)); maxentries = p + 50; } @@ -75,18 +75,20 @@ newentry(str, next) string str; p_entry next; { return p; } -string -store(s) string s; { - /* - * Store a string s in the name table - */ - register string s1, t ,u; +/* + * Store a string s in the name table + */ +string store(string s) +{ + register string s1, t, u; u = name; t = s; s1 = u; - do { - if (u >= maxname) { + do + { + if (u >= maxname) + { u = alloc(NMSIZ); maxname = u + NMSIZ; t = s; @@ -98,60 +100,63 @@ store(s) string s; { return s1; } -STATIC int -hash(str) string str; { - /* - * Compute the hash for string str - */ - register int i; +/* + * Compute the hash for string str + */ +STATIC int hash(string str) +{ + register int i; register string l; l = str; i = 0; - while (*l != '\0') i += *l++ & 0377; + while (*l != '\0') + i += *l++ & 0377; i += l - str; return i % HASHSIZE; } -p_gram -search(type,str,option) register string str; { - /* - * Search for object str. - * It has type UNKNOWN, LITERAL, TERMINAL or NONTERM. - * option can be ENTERING or BOTH (also looking). - */ - register int val = 0; - register p_entry p; - register int i; - int type1; +/* + * Search for object str. + * It has type UNKNOWN, LITERAL, TERMINAL or NONTERM. + * option can be ENTERING or BOTH (also looking). + */ +p_gram search(int type, register string str, int option) +{ + register int val = 0; + register p_entry p; + register int i; + int type1; i = hash(str); /* * Walk hash chain */ - for (p = h_root[i]; p != (p_entry) 0; p = p->h_next) { - if(!strcmp(p->h_name,str)) { + for (p = h_root[i]; p != (p_entry) 0; p = p->h_next) + { + if (!strcmp(p->h_name, str)) + { type1 = g_gettype(&(p->h_type)); - if (type1 != type) { - if (type1 == LITERAL || type == LITERAL) { + if (type1 != type) + { + if (type1 == LITERAL || type == LITERAL) + { continue; } - if (type == TERMINAL) { - error(linecount, - "%s: is already a nonterminal", - str); + if (type == TERMINAL) + { + error(linecount, "%s: is already a nonterminal", str); continue; } - else if (type == NONTERM) { - error(linecount, - "%s : is already a token", - str); + else if (type == NONTERM) + { + error(linecount, "%s : is already a token", str); continue; } } - if (option==ENTERING) { - error(linecount, - "%s : is already defined",str); + if (option == ENTERING) + { + error(linecount, "%s : is already defined", str); } p->h_type.g_lineno = linecount; return &(p->h_type); @@ -159,64 +164,76 @@ search(type,str,option) register string str; { } p = newentry(store(str), h_root[i]); h_root[i] = p; - if (type == TERMINAL || type == LITERAL) { + if (type == TERMINAL || type == LITERAL) + { register p_token pt; pt = (p_token) new_mem(&token_info); tokens = (p_token) token_info.i_ptr; pt->t_string = p->h_name; - if (type == LITERAL) { - if (str[0] == '\\') { + if (type == LITERAL) + { + if (str[0] == '\\') + { /* * Handle escapes in literals */ - if (str[2] == '\0') { - switch(str[1]) { - case 'n' : + if (str[2] == '\0') + { + switch (str[1]) + { + case 'n': val = '\n'; break; - case 'r' : + case 'r': val = '\r'; break; - case 'b' : + case 'b': val = '\b'; break; - case 'f' : + case 'f': val = '\f'; break; - case 't' : + case 't': val = '\t'; break; - case '\'': + case '\'': val = '\''; break; - case '\\': + case '\\': val = '\\'; break; - default : - error(linecount,e_literal); + default: + error(linecount, e_literal, NULL); } - } else { + } + else + { /* * Here, str[2] != '\0' */ - if (str[1] > '3' || str[1] < '0' || - str[2] > '7' || str[2] < '0' || - str[3] > '7' || str[3] < '0' || - str[4] != '\0') error(linecount,e_literal); - val = 64*str[1] - 73*'0' + - 8*str[2] + str[3]; + if (str[1] > '3' || str[1] < '0' || str[2] > '7' + || str[2] < '0' || str[3] > '7' || str[3] < '0' + || str[4] != '\0') + error(linecount, e_literal, NULL); + val = 64 * str[1] - 73 * '0' + 8 * str[2] + str[3]; } - } else { + } + else + { /* * No escape in literal */ - if (str[1] == '\0') val = str[0]; - else error(linecount,e_literal); + if (str[1] == '\0') + val = str[0]; + else + error(linecount, e_literal, NULL); } pt->t_tokno = val; g_settype(&(p->h_type), LITERAL); - } else { + } + else + { /* * Here, type = TERMINAL */ diff --git a/util/LLgen/src/reach.c b/util/LLgen/src/reach.c index a8ca81a47..eebdecdcb 100644 --- a/util/LLgen/src/reach.c +++ b/util/LLgen/src/reach.c @@ -17,6 +17,7 @@ * are all defined. */ +#include # include "types.h" # include "extern.h" # include "io.h" @@ -26,12 +27,18 @@ static string rcsid8 = "$Id$"; # endif + + + /* In this file the following routines are defined: */ -extern co_reach(); -STATIC reachable(); -STATIC void reachwalk(); +void co_reach(void); +STATIC void reachable(register p_nont p); +STATIC void reachwalk(p_gram p); + + -co_reach() { +void co_reach(void) +{ /* * Check for undefined or unreachable nonterminals. */ @@ -80,8 +87,8 @@ co_reach() { } } -STATIC -reachable(p) register p_nont p; { +STATIC void reachable(register p_nont p) +{ /* * Enter the fact that p is reachable, and look for implications */ @@ -94,8 +101,8 @@ reachable(p) register p_nont p; { } } -STATIC void -reachwalk(p) register p_gram p; { +STATIC void reachwalk(p_gram p) +{ /* * Walk through rule p, looking for nonterminals. * The nonterminals found are entered as reachable diff --git a/util/LLgen/src/savegram.c b/util/LLgen/src/savegram.c index a32935ecc..3638182b5 100644 --- a/util/LLgen/src/savegram.c +++ b/util/LLgen/src/savegram.c @@ -26,6 +26,7 @@ #include +#include # include "types.h" # include "extern.h" # include "io.h" @@ -69,7 +70,8 @@ static FILE *fgram; used when LLgen called with -n -s options */ int act_nt; -save_grammar(f) FILE *f; { +void save_grammar(FILE *f) +{ /* * Save the grammar */ @@ -267,8 +269,8 @@ save_grammar(f) FILE *f; { fprintf(fgram, "#define LLNNONTERMINALS %d\n", nt_highest - assval + 1); } -STATIC void -save_rule(p, tail) register p_gram p; int tail; { +STATIC void save_rule(register p_gram p, int tail) +{ /* Walk through rule p, saving it. The non-terminal tail is appended to the rule. It needs to be appended in this function @@ -363,8 +365,8 @@ save_rule(p, tail) register p_gram p; int tail; { } } -STATIC void -save_set(p) p_set p; { +STATIC void save_set(p_set p) +{ register int k; register unsigned i; int j; diff --git a/util/LLgen/src/sets.c b/util/LLgen/src/sets.c index dd1c1364f..4422039b0 100644 --- a/util/LLgen/src/sets.c +++ b/util/LLgen/src/sets.c @@ -16,25 +16,17 @@ * Set manipulation and allocation routines. */ +#include +# include # include "types.h" # include "extern.h" # include "sets.h" -# include "assert.h" # ifndef NORCSID static string rcsid9 = "$Id$"; # endif -/* In this file the following routines are defined: */ -extern setinit(); -extern p_set setalloc(); -extern p_set get_set(); -extern int setunion(); -extern int setintersect(); -extern setminus(); -extern int setempty(); -extern int findindex(); -extern int setcount(); + int nbytes; static int setsize; @@ -43,10 +35,11 @@ p_set *setptr, *maxptr; static t_info set_info; p_mem alloc(); -setinit(nt_needed) { - /* - * Initialises some variables needed for setcomputations - */ +/* + * Initialises some variables needed for setcomputations + */ +void setinit(int nt_needed) +{ register int bitset; nbytes = NBYTES(ntokens); @@ -61,8 +54,8 @@ setinit(nt_needed) { set_info.i_incr = 20; } -p_set -get_set() { +p_set get_set(void) +{ /* * Allocate a set that cannot be freed */ @@ -80,8 +73,8 @@ get_set() { return p; } -p_set -setalloc() { +p_set setalloc(void) +{ /* * Allocate a set which can later be freed. */ @@ -95,8 +88,8 @@ setalloc() { return p; } -int -setunion(a,b) register p_set a,b; { +int setunion(register p_set a,register p_set b) +{ /* * a = a union b. * Return 1 if the set a changed @@ -115,8 +108,8 @@ setunion(a,b) register p_set a,b; { return nsub; } -int -setintersect(a,b) register p_set a,b; { +int setintersect(register p_set a,register p_set b) +{ /* * a = a intersect b. * return 1 if the result is empty @@ -132,7 +125,8 @@ setintersect(a,b) register p_set a,b; { return nempty; } -setminus(a,b) register p_set a,b; { +void setminus(register p_set a,register p_set b) +{ /* * a = a setminus b */ @@ -144,8 +138,8 @@ setminus(a,b) register p_set a,b; { } while (--i); } -int -setempty(p) register p_set p; { +int setempty(register p_set p) +{ /* * Return 1 if the set p is empty */ @@ -158,8 +152,8 @@ setempty(p) register p_set p; { return 1; } -int -findindex(set) p_set set; { +int findindex(p_set set) +{ /* * The set "set" will serve as a recovery set. * Search for it in the table. If not present, enter it. @@ -204,8 +198,8 @@ findindex(set) p_set set; { return nbytes * (maxptr++ - setptr); } -int -setcount(set, saved) register p_set set; int *saved; { +int setcount(register p_set set, int *saved) +{ register int i, j; for (j = 0, i = 0; i < ntokens; i++) { diff --git a/util/LLgen/src/sets.h b/util/LLgen/src/sets.h index 501c3c154..87de245d3 100644 --- a/util/LLgen/src/sets.h +++ b/util/LLgen/src/sets.h @@ -2,6 +2,8 @@ * For full copyright and restrictions on use see the file COPYING in the top * level of the LLgen tree. */ +#ifndef SETS_H_ +#define SETS_H_ /* * L L G E N @@ -31,3 +33,44 @@ extern int tsetsize; extern p_set *setptr, *maxptr; extern int nbytes; + +/* + * Initialises some variables needed for setcomputations + */ +void setinit(int nt_needed); +/* + * Allocate a set that cannot be freed + */ +p_set get_set(void); +/* + * Allocate a set which can later be freed. + */ +p_set setalloc(void); +/* + * a = a union b. + * Return 1 if the set a changed + */ +int setunion(register p_set a,register p_set b); +/* + * a = a intersect b. + * return 1 if the result is empty + */ +int setintersect(register p_set a,register p_set b); +/* + * a = a setminus b + */ +void setminus(register p_set a,register p_set b); +/* + * Return 1 if the set p is empty + */ +int setempty(register p_set p); +/* + * The set "set" will serve as a recovery set. + * Search for it in the table. If not present, enter it. + * Here is room for improvement. At the moment, the list of + * sets is examined with linear search. + */ +int findindex(p_set set); +int setcount(register p_set set, int *saved); + +#endif /* SETS_H_ */ diff --git a/util/LLgen/src/tokens.c b/util/LLgen/src/tokens.c index 9d94d8716..0ac98a93b 100644 --- a/util/LLgen/src/tokens.c +++ b/util/LLgen/src/tokens.c @@ -2,9 +2,6 @@ #include "Lpars.h" #define LL_LEXI scanner #define LLNOFIRSTS -#if __STDC__ || __cplusplus -#define LL_ANSI_C 1 -#endif #define LL_LEXI scanner /* $Id$ */ #ifdef LL_DEBUG @@ -38,7 +35,6 @@ extern int LLstartsymb; #define LLsincr(d) LLscnt[d]++ #define LLtincr(d) LLtcnt[d]++ -#if LL_ANSI_C extern int LL_LEXI(void); extern void LLread(void); extern int LLskip(void); @@ -56,29 +52,11 @@ extern int LLfirst(int, int); #if LL_NON_CORR extern void LLnc_recover(void); #endif -#else /* not LL_ANSI_C */ -extern LLread(); -extern int LLskip(); -extern int LLnext(); -extern LLerror(); -extern LLsafeerror(); -extern LLnewlevel(); -extern LLoldlevel(); -#ifndef LL_FASTER -extern LLscan(); -#endif -#ifndef LLNOFIRSTS -extern int LLfirst(); -#endif -#if LL_NON_CORR -extern LLnc_recover(); -#endif -#endif /* not LL_ANSI_C */ # line 20 "tokens.g" #include -# include "types.h" +#include "types.h" # include "io.h" # include "extern.h" # include "assert.h" @@ -89,17 +67,17 @@ static string rcsidc = "$Id$"; # endif /* Here are defined : */ -extern int scanner(); -extern LLmessage(); -extern int input(); -extern unput(); -extern void skipcomment(); +int scanner(void); +void LLmessage(int d); +int input(void); +void unput(int c); +void skipcomment(int flag); # ifdef LINE_DIRECTIVE -STATIC linedirective(); +STATIC void linedirective(void); # endif -STATIC string cpy(); -STATIC string vallookup(); -STATIC void copyact(); +STATIC string cpy(int s,register string p,int inserted); +STATIC string vallookup(int s); +STATIC void copyact(char ch1,char ch2,int flag,int level); static int nparams; # line 76 "tokens.g" @@ -145,8 +123,8 @@ static t_token savedtok; /* to save lextoken in case of an insertion */ static int nostartline; /* = 0 if at the start of a line */ # endif -STATIC void -copyact(ch1,ch2,flag,level) char ch1,ch2; { +STATIC void copyact(char ch1,char ch2,int flag,int level) +{ /* * Copy an action to file f. Opening bracket is ch1, closing bracket * is ch2. @@ -155,8 +133,8 @@ copyact(ch1,ch2,flag,level) char ch1,ch2; { */ static int text_seen = 0; register FILE *f; - register ch; /* Current char */ - register match; /* used to read strings */ + register int ch; /* Current char */ + register int match; /* used to read strings */ int saved = linecount; /* save linecount */ int sav_strip = strip_grammar; @@ -187,7 +165,7 @@ copyact(ch1,ch2,flag,level) char ch1,ch2; { case ')': case '}': case ']': - error(linecount,"Parentheses mismatch"); + error(linecount,"Parentheses mismatch", NULL); break; case '(': text_seen = 1; @@ -218,7 +196,7 @@ copyact(ch1,ch2,flag,level) char ch1,ch2; { text_seen = 0; nparams++; if (ch == ',' && (flag & 2)) { - warning(linecount, "Parameters may not be separated with a ','"); + warning(linecount, "Parameters may not be separated with a ','",NULL); ch = ';'; } } @@ -239,7 +217,7 @@ copyact(ch1,ch2,flag,level) char ch1,ch2; { ch = input(); } if (ch == '\n') { - error(linecount,"Newline in string"); + error(linecount,"Newline in string", NULL); unput(match); } putc(ch,f); @@ -247,7 +225,7 @@ copyact(ch1,ch2,flag,level) char ch1,ch2; { if (ch == match) break; /* Fall through */ case EOF : - if (!level) error(saved,"Action does not terminate"); + if (!level) error(saved,"Action does not terminate", NULL); strip_grammar = sav_strip; return; default: @@ -257,7 +235,8 @@ copyact(ch1,ch2,flag,level) char ch1,ch2; { } } -scanner() { +int scanner(void) +{ /* * Lexical analyser, what else */ @@ -307,7 +286,7 @@ scanner() { for (;;) { ch = input(); if (ch == '\n' || ch == EOF) { - error(linecount,"Missing '"); + error(linecount,"Missing '", NULL); break; } if (ch == '\'') break; @@ -327,7 +306,7 @@ scanner() { case ISSPA : continue; case ISDIG : { - register i = 0; + register int i = 0; do { i = 10 * i + (ch - '0'); ch= input(); @@ -371,7 +350,7 @@ scanner() { } w++; } - error(linecount,"Illegal reserved word"); + error(linecount,"Illegal reserved word",NULL); } lextoken.t_string = ltext; return C_IDENT; @@ -382,11 +361,12 @@ scanner() { static int backupc; /* for unput() */ static int nonline; /* = 1 if last char read was a newline */ -input() { +int input(void) +{ /* * Low level input routine, used by all other input routines */ - register c; + register int c; if (c = backupc) { /* Last char was "unput()". Deliver it again @@ -413,15 +393,16 @@ input() { return c; } -unput(c) { +void unput(int c) +{ /* * "unread" c */ backupc = c; } -void -skipcomment(flag) { +void skipcomment(int flag) +{ /* * Skip comment. If flag != 0, the comment is inside a fragment * of C-code, so keep it. @@ -430,7 +411,7 @@ skipcomment(flag) { int saved; /* line count on which comment starts */ saved = linecount; - if (input() != '*') error(linecount,"Illegal comment"); + if (input() != '*') error(linecount,"Illegal comment",NULL); if (flag) putc('*', fact); do { ch = input(); @@ -441,12 +422,12 @@ skipcomment(flag) { if (ch == '/') return; } } while (ch != EOF); - error(saved,"Comment does not terminate"); + error(saved,"Comment does not terminate", NULL); } # ifdef LINE_DIRECTIVE -STATIC -linedirective() { +STATIC void linedirective(void) +{ /* * Read a line directive */ @@ -463,7 +444,7 @@ linedirective() { ch = input(); } while (ch != '\n' && c_class[ch] != ISDIG); if (ch == '\n') { - error(linecount,s_error); + error(linecount,s_error, NULL); return; } i = 0; @@ -478,7 +459,7 @@ linedirective() { *c++ = ch = input(); } while (ch != '"' && ch != '\n'); if (ch == '\n') { - error(linecount,s_error); + error(linecount,s_error, NULL); return; } *--c = '\0'; @@ -494,8 +475,8 @@ linedirective() { } # endif -STATIC string -vallookup(s) { +STATIC string vallookup(int s) +{ /* * Look up the keyword that has token number s */ @@ -508,8 +489,8 @@ vallookup(s) { return 0; } -STATIC string -cpy(s,p,inserted) register string p; { +STATIC string cpy(int s,register string p,int inserted) +{ /* * Create a piece of error message for token s and put it at p. * inserted = 0 if the token s was deleted (in which case we have @@ -579,7 +560,9 @@ cpy(s,p,inserted) register string p; { return p; } -LLmessage(d) { + +void LLmessage(int d) +{ /* * d is either 0, in which case the current token has been deleted, * or non-zero, in which case it represents a token that is inserted diff --git a/util/LLgen/src/tokens.g b/util/LLgen/src/tokens.g index 79ee579da..3f8eb29b5 100644 --- a/util/LLgen/src/tokens.g +++ b/util/LLgen/src/tokens.g @@ -19,7 +19,7 @@ { #include -# include "types.h" +#include "types.h" # include "io.h" # include "extern.h" # include "assert.h" @@ -30,17 +30,17 @@ static string rcsidc = "$Id$"; # endif /* Here are defined : */ -extern int scanner(); -extern LLmessage(); -extern int input(); -extern unput(); -extern void skipcomment(); +int scanner(void); +void LLmessage(int d); +int input(void); +void unput(int c); +void skipcomment(int flag); # ifdef LINE_DIRECTIVE -STATIC linedirective(); +STATIC void linedirective(void); # endif -STATIC string cpy(); -STATIC string vallookup(); -STATIC void copyact(); +STATIC string cpy(int s,register string p,int inserted); +STATIC string vallookup(int s); +STATIC void copyact(char ch1,char ch2,int flag,int level); static int nparams; } @@ -115,8 +115,8 @@ static t_token savedtok; /* to save lextoken in case of an insertion */ static int nostartline; /* = 0 if at the start of a line */ # endif -STATIC void -copyact(ch1,ch2,flag,level) char ch1,ch2; { +STATIC void copyact(char ch1,char ch2,int flag,int level) +{ /* * Copy an action to file f. Opening bracket is ch1, closing bracket * is ch2. @@ -125,8 +125,8 @@ copyact(ch1,ch2,flag,level) char ch1,ch2; { */ static int text_seen = 0; register FILE *f; - register ch; /* Current char */ - register match; /* used to read strings */ + register int ch; /* Current char */ + register int match; /* used to read strings */ int saved = linecount; /* save linecount */ int sav_strip = strip_grammar; @@ -157,7 +157,7 @@ copyact(ch1,ch2,flag,level) char ch1,ch2; { case ')': case '}': case ']': - error(linecount,"Parentheses mismatch"); + error(linecount,"Parentheses mismatch", NULL); break; case '(': text_seen = 1; @@ -188,7 +188,7 @@ copyact(ch1,ch2,flag,level) char ch1,ch2; { text_seen = 0; nparams++; if (ch == ',' && (flag & 2)) { - warning(linecount, "Parameters may not be separated with a ','"); + warning(linecount, "Parameters may not be separated with a ','",NULL); ch = ';'; } } @@ -209,7 +209,7 @@ copyact(ch1,ch2,flag,level) char ch1,ch2; { ch = input(); } if (ch == '\n') { - error(linecount,"Newline in string"); + error(linecount,"Newline in string", NULL); unput(match); } putc(ch,f); @@ -217,7 +217,7 @@ copyact(ch1,ch2,flag,level) char ch1,ch2; { if (ch == match) break; /* Fall through */ case EOF : - if (!level) error(saved,"Action does not terminate"); + if (!level) error(saved,"Action does not terminate", NULL); strip_grammar = sav_strip; return; default: @@ -227,7 +227,8 @@ copyact(ch1,ch2,flag,level) char ch1,ch2; { } } -scanner() { +int scanner(void) +{ /* * Lexical analyser, what else */ @@ -277,7 +278,7 @@ scanner() { for (;;) { ch = input(); if (ch == '\n' || ch == EOF) { - error(linecount,"Missing '"); + error(linecount,"Missing '", NULL); break; } if (ch == '\'') break; @@ -297,7 +298,7 @@ scanner() { case ISSPA : continue; case ISDIG : { - register i = 0; + register int i = 0; do { i = 10 * i + (ch - '0'); ch= input(); @@ -341,7 +342,7 @@ scanner() { } w++; } - error(linecount,"Illegal reserved word"); + error(linecount,"Illegal reserved word",NULL); } lextoken.t_string = ltext; return C_IDENT; @@ -352,11 +353,12 @@ scanner() { static int backupc; /* for unput() */ static int nonline; /* = 1 if last char read was a newline */ -input() { +int input(void) +{ /* * Low level input routine, used by all other input routines */ - register c; + register int c; if (c = backupc) { /* Last char was "unput()". Deliver it again @@ -383,15 +385,16 @@ input() { return c; } -unput(c) { +void unput(int c) +{ /* * "unread" c */ backupc = c; } -void -skipcomment(flag) { +void skipcomment(int flag) +{ /* * Skip comment. If flag != 0, the comment is inside a fragment * of C-code, so keep it. @@ -400,7 +403,7 @@ skipcomment(flag) { int saved; /* line count on which comment starts */ saved = linecount; - if (input() != '*') error(linecount,"Illegal comment"); + if (input() != '*') error(linecount,"Illegal comment",NULL); if (flag) putc('*', fact); do { ch = input(); @@ -411,12 +414,12 @@ skipcomment(flag) { if (ch == '/') return; } } while (ch != EOF); - error(saved,"Comment does not terminate"); + error(saved,"Comment does not terminate", NULL); } # ifdef LINE_DIRECTIVE -STATIC -linedirective() { +STATIC void linedirective(void) +{ /* * Read a line directive */ @@ -433,7 +436,7 @@ linedirective() { ch = input(); } while (ch != '\n' && c_class[ch] != ISDIG); if (ch == '\n') { - error(linecount,s_error); + error(linecount,s_error, NULL); return; } i = 0; @@ -448,7 +451,7 @@ linedirective() { *c++ = ch = input(); } while (ch != '"' && ch != '\n'); if (ch == '\n') { - error(linecount,s_error); + error(linecount,s_error, NULL); return; } *--c = '\0'; @@ -464,8 +467,8 @@ linedirective() { } # endif -STATIC string -vallookup(s) { +STATIC string vallookup(int s) +{ /* * Look up the keyword that has token number s */ @@ -478,8 +481,8 @@ vallookup(s) { return 0; } -STATIC string -cpy(s,p,inserted) register string p; { +STATIC string cpy(int s,register string p,int inserted) +{ /* * Create a piece of error message for token s and put it at p. * inserted = 0 if the token s was deleted (in which case we have @@ -549,7 +552,9 @@ cpy(s,p,inserted) register string p; { return p; } -LLmessage(d) { + +void LLmessage(int d) +{ /* * d is either 0, in which case the current token has been deleted, * or non-zero, in which case it represents a token that is inserted diff --git a/util/LLgen/src/types.h b/util/LLgen/src/types.h index da18696f6..0de5397b8 100644 --- a/util/LLgen/src/types.h +++ b/util/LLgen/src/types.h @@ -2,6 +2,8 @@ * For full copyright and restrictions on use see the file COPYING in the top * level of the LLgen tree. */ +#ifndef TYPES_H_ +#define TYPES_H_ /* * L L G E N @@ -270,3 +272,5 @@ typedef struct info_alloc { # else /* not NDEBUG */ # define STATIC extern # endif /* not NDEBUG */ + +#endif /* TYPES_H_ */ -- 2.34.1