| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315 |
- #include "defs.h"
- static int nvectors;
- static int nentries;
- static Yshort **froms;
- static Yshort **tos;
- static Yshort *conflicts, nconflicts;
- static Yshort *tally;
- static Yshort *width;
- static Yshort *state_count;
- static Yshort *order;
- static Yshort *base;
- static Yshort *pos;
- static int maxtable;
- static Yshort *table;
- static Yshort *check;
- static int lowzero;
- static int high;
- void output()
- {
- free_itemsets();
- free_shifts();
- free_reductions();
- output_stored_text();
- output_defines();
- output_rule_data();
- output_yydefred();
- output_actions();
- free_parser();
- output_debug();
- output_stype();
- if (rflag) write_section("tables");
- write_section("header");
- output_trailing_text();
- write_section("body");
- output_semantic_actions();
- write_section("trailer");
- }
- void output_rule_data()
- {
- register int i;
- register int j;
- if (!rflag)
- fprintf(output_file, "static ");
- fprintf(output_file, "int yylhs[] = {%42d,",
- symbol_value[start_symbol]);
- j = 10;
- for (i = 3; i < nrules; i++)
- {
- if (j >= 10)
- {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = 1;
- }
- else
- ++j;
- fprintf(output_file, "%5d,", symbol_value[rlhs[i]]);
- }
- if (!rflag) outline += 2;
- fprintf(output_file, "\n};\n");
- if (!rflag)
- fprintf(output_file, "static ");
- fprintf(output_file, "int yylen[] = {%42d,", 2);
- j = 10;
- for (i = 3; i < nrules; i++)
- {
- if (j >= 10)
- {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = 1;
- }
- else
- j++;
- fprintf(output_file, "%5d,", rrhs[i + 1] - rrhs[i] - 1);
- }
- if (!rflag) outline += 2;
- fprintf(output_file, "\n};\n");
- }
- void output_yydefred()
- {
- register int i, j;
- if (!rflag)
- fprintf(output_file, "static ");
- fprintf(output_file, "int yydefred[] = {%39d,",
- (defred[0] ? defred[0] - 2 : 0));
- j = 10;
- for (i = 1; i < nstates; i++)
- {
- if (j < 10)
- ++j;
- else
- {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = 1;
- }
- fprintf(output_file, "%5d,", (defred[i] ? defred[i] - 2 : 0));
- }
- if (!rflag) outline += 2;
- fprintf(output_file, "\n};\n");
- }
- void output_actions()
- {
- nvectors = 3*nstates + nvars;
- froms = NEW2(nvectors, Yshort *);
- tos = NEW2(nvectors, Yshort *);
- tally = NEW2(nvectors, Yshort);
- width = NEW2(nvectors, Yshort);
- if (SRtotal+RRtotal)
- conflicts = NEW2(4*(SRtotal+RRtotal), Yshort);
- else
- conflicts = 0;
- nconflicts = 0;
- token_actions();
- FREE(lookaheads);
- FREE(LA);
- FREE(LAruleno);
- FREE(accessing_symbol);
- goto_actions();
- FREE(goto_map + ntokens);
- FREE(from_state);
- FREE(to_state);
- sort_actions();
- pack_table();
- output_base();
- output_table();
- output_check();
- output_ctable();
- }
- int find_conflict_base(int cbase)
- {
- int i,j;
- for (i=0; i<cbase; i++) {
- for (j=0; j+cbase < nconflicts; j++) {
- if (conflicts[i+j] != conflicts[cbase+j])
- break; }
- if (j+cbase >= nconflicts)
- return i; }
- return cbase;
- }
- void token_actions()
- {
- register int i, j;
- register int shiftcount, reducecount, conflictcount, csym, cbase;
- register int max, min;
- register Yshort *actionrow, *r, *s;
- register action *p;
- actionrow = NEW2(3*ntokens, Yshort);
- for (i = 0; i < nstates; ++i) {
- if (parser[i]) {
- for (j = 0; j < 3*ntokens; ++j)
- actionrow[j] = 0;
- shiftcount = 0;
- reducecount = 0;
- conflictcount = 0;
- csym = -1;
- cbase = nconflicts;
- for (p = parser[i]; p; p = p->next) {
- if (csym != -1 && csym != p->symbol) {
- conflictcount++;
- conflicts[nconflicts++] = -1;
- j = find_conflict_base(cbase);
- actionrow[csym + 2*ntokens] = j + 1;
- if (j == cbase) {
- cbase = nconflicts; }
- else {
- if (conflicts[cbase] == -1) cbase++;
- nconflicts = cbase; }
- csym = -1; }
- if (p->suppressed == 0) {
- if (p->action_code == SHIFT) {
- ++shiftcount;
- actionrow[p->symbol] = p->number; }
- else if (p->action_code == REDUCE &&
- p->number != defred[i]) {
- ++reducecount;
- actionrow[p->symbol + ntokens] = p->number; } }
- else if (p->suppressed == 1) {
- csym = p->symbol;
- if (p->action_code == SHIFT) {
- conflicts[nconflicts++] = p->number; }
- else if (p->action_code == REDUCE &&
- p->number != defred[i]) {
- if (cbase == nconflicts) {
- if (cbase) cbase--;
- else conflicts[nconflicts++] = -1; }
- conflicts[nconflicts++] = p->number - 2; } } }
- if (csym != -1) {
- conflictcount++;
- conflicts[nconflicts++] = -1;
- j = find_conflict_base(cbase);
- actionrow[csym + 2*ntokens] = j + 1;
- if (j == cbase) {
- cbase = nconflicts; }
- else {
- if (conflicts[cbase] == -1) cbase++;
- nconflicts = cbase; } }
- tally[i] = shiftcount;
- tally[nstates+i] = reducecount;
- tally[2*nstates+i] = conflictcount;
- width[i] = 0;
- width[nstates+i] = 0;
- width[2*nstates+i] = 0;
- if (shiftcount > 0) {
- froms[i] = r = NEW2(shiftcount, Yshort);
- tos[i] = s = NEW2(shiftcount, Yshort);
- min = MAXSHORT;
- max = 0;
- for (j = 0; j < ntokens; ++j) {
- if (actionrow[j]) {
- if (min > symbol_value[j])
- min = symbol_value[j];
- if (max < symbol_value[j])
- max = symbol_value[j];
- *r++ = symbol_value[j];
- *s++ = actionrow[j]; } }
- width[i] = max - min + 1; }
- if (reducecount > 0) {
- froms[nstates+i] = r = NEW2(reducecount, Yshort);
- tos[nstates+i] = s = NEW2(reducecount, Yshort);
- min = MAXSHORT;
- max = 0;
- for (j = 0; j < ntokens; ++j) {
- if (actionrow[ntokens+j]) {
- if (min > symbol_value[j])
- min = symbol_value[j];
- if (max < symbol_value[j])
- max = symbol_value[j];
- *r++ = symbol_value[j];
- *s++ = actionrow[ntokens+j] - 2; } }
- width[nstates+i] = max - min + 1; }
- if (conflictcount > 0) {
- froms[2*nstates+i] = r = NEW2(conflictcount, Yshort);
- tos[2*nstates+i] = s = NEW2(conflictcount, Yshort);
- min = MAXSHORT;
- max = 0;
- for (j = 0; j < ntokens; ++j) {
- if (actionrow[2*ntokens+j]) {
- if (min > symbol_value[j])
- min = symbol_value[j];
- if (max < symbol_value[j])
- max = symbol_value[j];
- *r++ = symbol_value[j];
- *s++ = actionrow[2*ntokens+j] - 1; } }
- width[2*nstates+i] = max - min + 1; } } }
- FREE(actionrow);
- }
- void goto_actions()
- {
- register int i, j, k;
- state_count = NEW2(nstates, Yshort);
- k = default_goto(start_symbol + 1);
- if (!rflag)
- fprintf(output_file, "static ");
- fprintf(output_file, "int yydgoto[] = {%40d,", k);
- save_column(start_symbol + 1, k);
- j = 10;
- for (i = start_symbol + 2; i < nsyms; i++)
- {
- if (j >= 10)
- {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = 1;
- }
- else
- ++j;
- k = default_goto(i);
- fprintf(output_file, "%5d,", k);
- save_column(i, k);
- }
- if (!rflag) outline += 2;
- fprintf(output_file, "\n};\n");
- FREE(state_count);
- }
- int default_goto(int symbol)
- {
- register int i;
- register int m;
- register int n;
- register int default_state;
- register int max;
- m = goto_map[symbol];
- n = goto_map[symbol + 1];
- if (m == n) return (0);
- for (i = 0; i < nstates; i++)
- state_count[i] = 0;
- for (i = m; i < n; i++)
- state_count[to_state[i]]++;
- max = 0;
- default_state = 0;
- for (i = 0; i < nstates; i++)
- {
- if (state_count[i] > max)
- {
- max = state_count[i];
- default_state = i;
- }
- }
- return (default_state);
- }
- void save_column(int symbol, int default_state)
- {
- register int i;
- register int m;
- register int n;
- register Yshort *sp;
- register Yshort *sp1;
- register Yshort *sp2;
- register int count;
- register int symno;
- m = goto_map[symbol];
- n = goto_map[symbol + 1];
- count = 0;
- for (i = m; i < n; i++)
- {
- if (to_state[i] != default_state)
- ++count;
- }
- if (count == 0) return;
- symno = symbol_value[symbol] + 3*nstates;
- froms[symno] = sp1 = sp = NEW2(count, Yshort);
- tos[symno] = sp2 = NEW2(count, Yshort);
- for (i = m; i < n; i++)
- {
- if (to_state[i] != default_state)
- {
- *sp1++ = from_state[i];
- *sp2++ = to_state[i];
- }
- }
- tally[symno] = count;
- width[symno] = sp1[-1] - sp[0] + 1;
- }
- void sort_actions()
- {
- register int i;
- register int j;
- register int k;
- register int t;
- register int w;
- order = NEW2(nvectors, Yshort);
- nentries = 0;
- for (i = 0; i < nvectors; i++)
- {
- if (tally[i] > 0)
- {
- t = tally[i];
- w = width[i];
- j = nentries - 1;
- while (j >= 0 && (width[order[j]] < w))
- j--;
- while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
- j--;
- for (k = nentries - 1; k > j; k--)
- order[k + 1] = order[k];
- order[j + 1] = i;
- nentries++;
- }
- }
- }
- void pack_table()
- {
- register int i;
- register int place;
- register int state;
- base = NEW2(nvectors, Yshort);
- pos = NEW2(nentries, Yshort);
- maxtable = 1000;
- table = NEW2(maxtable, Yshort);
- check = NEW2(maxtable, Yshort);
- lowzero = 0;
- high = 0;
- for (i = 0; i < maxtable; i++)
- check[i] = -1;
- for (i = 0; i < nentries; i++)
- {
- state = matching_vector(i);
- if (state < 0)
- place = pack_vector(i);
- else
- place = base[state];
- pos[i] = place;
- base[order[i]] = place;
- }
- for (i = 0; i < nvectors; i++)
- {
- if (froms[i])
- FREE(froms[i]);
- if (tos[i])
- FREE(tos[i]);
- }
- FREE(froms);
- FREE(tos);
- FREE(tally);
- FREE(width);
- FREE(pos);
- }
- /* The function matching_vector determines if the vector specified by */
- /* the input parameter matches a previously considered vector. The */
- /* test at the start of the function checks if the vector represents */
- /* a row of shifts over terminal symbols or a row of reductions, or a */
- /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
- /* check if a column of shifts over a nonterminal symbols matches a */
- /* previously considered vector. Because of the nature of LR parsing */
- /* tables, no two columns can match. Therefore, the only possible */
- /* match would be between a row and a column. Such matches are */
- /* unlikely. Therefore, to save time, no attempt is made to see if a */
- /* column matches a previously considered vector. */
- /* */
- /* Matching_vector is poorly designed. The test could easily be made */
- /* faster. Also, it depends on the vectors being in a specific */
- /* order. */
- /* Not really any point in checking for matching conflicts -- it is */
- /* extremely unlikely to occur, and conflicts are (hopefully) rare. */
- int matching_vector(int vector)
- {
- register int i;
- register int j;
- register int k;
- register int t;
- register int w;
- register int match;
- register int prev;
- i = order[vector];
- if (i >= 2*nstates)
- return (-1);
- t = tally[i];
- w = width[i];
- for (prev = vector - 1; prev >= 0; prev--)
- {
- j = order[prev];
- if (width[j] != w || tally[j] != t)
- return (-1);
- match = 1;
- for (k = 0; match && k < t; k++)
- {
- if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
- match = 0;
- }
- if (match)
- return (j);
- }
- return (-1);
- }
- int pack_vector(int vector)
- {
- register int i, j, k, l;
- register int t;
- register int loc;
- register int ok;
- register Yshort *from;
- register Yshort *to;
- int newmax;
- i = order[vector];
- t = tally[i];
- assert(t);
- from = froms[i];
- to = tos[i];
- j = lowzero - from[0];
- for (k = 1; k < t; ++k)
- if (lowzero - from[k] > j)
- j = lowzero - from[k];
- for (;; ++j)
- {
- if (j == 0)
- continue;
- ok = 1;
- for (k = 0; ok && k < t; k++)
- {
- loc = j + from[k];
- if (loc >= maxtable)
- {
- if (loc >= MAXTABLE)
- fatal("maximum table size exceeded");
- newmax = maxtable;
- do { newmax += 200; } while (newmax <= loc);
- table = (Yshort *) REALLOC(table, newmax*sizeof(Yshort));
- if (table == 0) no_space();
- check = (Yshort *) REALLOC(check, newmax*sizeof(Yshort));
- if (check == 0) no_space();
- for (l = maxtable; l < newmax; ++l)
- {
- table[l] = 0;
- check[l] = -1;
- }
- maxtable = newmax;
- }
- if (check[loc] != -1)
- ok = 0;
- }
- for (k = 0; ok && k < vector; k++)
- {
- if (pos[k] == j)
- ok = 0;
- }
- if (ok)
- {
- for (k = 0; k < t; k++)
- {
- loc = j + from[k];
- table[loc] = to[k];
- check[loc] = from[k];
- if (loc > high) high = loc;
- }
- while (check[lowzero] != -1)
- ++lowzero;
- return (j);
- }
- }
- }
- void output_base()
- {
- register int i, j;
- if (!rflag)
- fprintf(output_file, "static ");
- fprintf(output_file, "int yysindex[] = {%39d,", base[0]);
- j = 10;
- for (i = 1; i < nstates; i++) {
- if (j >= 10) {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = 1; }
- else
- ++j;
- fprintf(output_file, "%5d,", base[i]); }
- if (!rflag) outline += 2;
- fprintf(output_file, "\n};\n");
- if (!rflag)
- fprintf(output_file, "static ");
- fprintf(output_file, "int yyrindex[] = {%39d,", base[nstates]);
- j = 10;
- for (i = nstates + 1; i < 2*nstates; i++) {
- if (j >= 10) {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = 1; }
- else
- ++j;
- fprintf(output_file, "%5d,", base[i]); }
- if (!rflag) outline += 2;
- fprintf(output_file, "\n};\n");
- if (!rflag)
- fprintf(output_file, "static ");
- fprintf(output_file, "int yycindex[] = {%39d,", base[2*nstates]);
- j = 10;
- for (i = 2*nstates + 1; i < 3*nstates; i++) {
- if (j >= 10) {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = 1; }
- else
- ++j;
- fprintf(output_file, "%5d,", base[i]); }
- if (!rflag) outline += 2;
- fprintf(output_file, "\n};\n");
- if (!rflag)
- fprintf(output_file, "static ");
- fprintf(output_file, "int yygindex[] = {%39d,",
- base[3*nstates]);
- j = 10;
- for (i = 3*nstates + 1; i < nvectors - 1; i++) {
- if (j >= 10) {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = 1; }
- else
- ++j;
- fprintf(output_file, "%5d,", base[i]); }
- if (!rflag) outline += 2;
- fprintf(output_file, "\n};\n");
- FREE(base);
- }
- void output_table()
- {
- register int i;
- register int j;
- ++outline;
- fprintf(stderr, "YYTABLESIZE: %d\n", high);
- if(high >= MAXSHORT) {
- fprintf(stderr, "Table is longer than %d elements. It's not gonna fly.\n", MAXSHORT);
- exit(1);
- }
- fprintf(code_file, "#define YYTABLESIZE %d\n", high);
- if (!rflag)
- fprintf(output_file, "static ");
- fprintf(output_file, "int yytable[] = {%40d,", table[0]);
- j = 10;
- for (i = 1; i <= high; i++)
- {
- if (j >= 10)
- {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = 1;
- }
- else
- ++j;
- fprintf(output_file, "%5d,", table[i]);
- }
- if (!rflag) outline += 2;
- fprintf(output_file, "\n};\n");
- FREE(table);
- }
- void output_check()
- {
- register int i;
- register int j;
- if (!rflag)
- fprintf(output_file, "static ");
- fprintf(output_file, "int yycheck[] = {%40d,", check[0]);
- j = 10;
- for (i = 1; i <= high; i++)
- {
- if (j >= 10)
- {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = 1;
- }
- else
- ++j;
- fprintf(output_file, "%5d,", check[i]);
- }
- if (!rflag) outline += 2;
- fprintf(output_file, "\n};\n");
- FREE(check);
- }
- void output_ctable()
- {
- register int i;
- register int j;
- if (!rflag)
- fprintf(output_file, "static ");
- fprintf(output_file, "int yyctable[] = {%39d,", conflicts ?
- conflicts[0] : 0);
- j = 10;
- for (i = 1; i < nconflicts; i++)
- {
- if (j >= 10)
- {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = 1;
- }
- else
- ++j;
- fprintf(output_file, "%5d,", conflicts[i]);
- }
- if (!rflag) outline += 2;
- fprintf(output_file, "\n};\n");
- if (conflicts)
- FREE(conflicts);
- }
- int is_C_identifier(char *name)
- {
- register char *s;
- register int c;
- s = name;
- c = *s;
- if (c == '"')
- {
- c = *++s;
- if (!isalpha(c) && c != '_' && c != '$')
- return (0);
- while ((c = *++s) != '"')
- {
- if (!isalnum(c) && c != '_' && c != '$')
- return (0);
- }
- return (1);
- }
- if (!isalpha(c) && c != '_' && c != '$')
- return (0);
- while ((c = *++s))
- {
- if (!isalnum(c) && c != '_' && c != '$')
- return (0);
- }
- return (1);
- }
- void output_defines()
- {
- register int c, i;
- register char *s;
- FILE *dc_file;
- /* ASF: changed to separate (_yacc_defines_keywords and _yacc_defines_yystype instead of
- _yacc_defines_h_) keyword definitions of YYSTYPE because keywords may conflict with other
- things */
- if(dflag) {
- /***
- fprintf(defines_file, "#ifndef _yacc_defines_h_\n");
- fprintf(defines_file, "#define _yacc_defines_h_\n\n");
- ***/
- fprintf(defines_file, "#ifndef _yacc_defines_keywords\n");
- fprintf(defines_file, "#define _yacc_defines_keywords\n\n");
- }
- /* VM: Print to either code file or defines file but not to both */
- dc_file = dflag ? defines_file : code_file;
- for (i = 2; i < ntokens; ++i)
- {
- s = symbol_name[i];
- if (is_C_identifier(s))
- {
- fprintf(dc_file, "#define ");
- c = *s;
- if (c == '"')
- {
- while ((c = *++s) != '"')
- {
- putc(c, dc_file);
- }
- }
- else
- {
- do
- {
- putc(c, dc_file);
- }
- while ((c = *++s));
- }
- ++outline;
- fprintf(dc_file, " %d\n", symbol_value[i]);
- }
- }
- ++outline;
- fprintf(dc_file, "#define YYERRCODE %d\n", symbol_value[1]);
- if(dflag) {
- fprintf(defines_file, "\n#endif /* _yacc_defines_keywords */\n\n");
- }
- if (dflag && unionized)
- {
- fprintf(defines_file, "#ifndef _yacc_defines_yystype\n");
- fprintf(defines_file, "#define _yacc_defines_yystype\n");
- fclose(union_file);
- union_file = fopen(union_file_name, "r");
- if (union_file == NULL) open_error(union_file_name);
- while ((c = getc(union_file)) != EOF) {
- putc(c, defines_file);
- }
- /* ASF: we define it on the Parser class
- fprintf(defines_file, "extern YYSTYPE yylval;\n"); */
- fprintf(defines_file, "\n#endif /* _yacc_defines_yystype */\n\n");
- }
- /***
- if(dflag) {
- fprintf(defines_file, "\n#endif\n");
- }
- ***/
- }
- void output_stored_text()
- {
- register int c;
- register FILE *in, *out;
- register int state; /* 0=middle of line, 1=start of line, 2=seen '#' */
- state = 1;
- fclose(text_file);
- text_file = fopen(text_file_name, "r");
- if (text_file == NULL)
- open_error(text_file_name);
- in = text_file;
- if ((c = getc(in)) == EOF)
- return;
- out = code_file;
- do {
- if (c == '\n') {
- ++outline;
- if (state == 2) {
- fprintf(out, line_format+1, outline + 1, code_file_name);
- state = 1;
- continue; }
- state = 1; }
- else if (state == 1 && c == '#')
- state = 2;
- else
- state = 0;
- putc(c, out);
- } while ((c = getc(in)) != EOF);
- if (!lflag)
- fprintf(out, line_format, ++outline + 1, code_file_name);
- }
- void output_debug()
- {
- register int i, j, k, max;
- char **symnam, *s;
- ++outline;
- fprintf(code_file, "#define YYFINAL %d\n", final_state);
- outline += 3;
- fprintf(code_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
- tflag);
- if (rflag)
- fprintf(output_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
- tflag);
- max = 0;
- for (i = 2; i < ntokens; ++i)
- if (symbol_value[i] > max)
- max = symbol_value[i];
- ++outline;
- fprintf(code_file, "#define YYMAXTOKEN %d\n", max);
- symnam = (char **) MALLOC((max+1)*sizeof(char *));
- if (symnam == 0) no_space();
- /* Note that it is not necessary to initialize the element */
- /* symnam[max]. */
- for (i = 0; i < max; ++i)
- symnam[i] = 0;
- for (i = ntokens - 1; i >= 2; --i)
- symnam[symbol_value[i]] = symbol_name[i];
- symnam[0] = "end-of-file";
- if (!rflag) ++outline;
- fprintf(output_file, "#if YYDEBUG\n");
- if (!rflag)
- fprintf(output_file, "static ");
- fprintf(output_file, "const char *yyname[] = {");
- j = 80;
- for (i = 0; i <= max; ++i)
- {
- if ((s = symnam[i]))
- {
- if (s[0] == '"')
- {
- k = 7;
- while (*++s != '"')
- {
- ++k;
- if (*s == '\\')
- {
- k += 2;
- if (*++s == '\\')
- ++k;
- }
- }
- j += k;
- if (j > 80)
- {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = k;
- }
- fprintf(output_file, "\"\\\"");
- s = symnam[i];
- while (*++s != '"')
- {
- if (*s == '\\')
- {
- fprintf(output_file, "\\\\");
- if (*++s == '\\')
- fprintf(output_file, "\\\\");
- else
- putc(*s, output_file);
- }
- else
- putc(*s, output_file);
- }
- fprintf(output_file, "\\\"\",");
- }
- else if (s[0] == '\'')
- {
- if (s[1] == '"')
- {
- j += 7;
- if (j > 80)
- {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = 7;
- }
- fprintf(output_file, "\"'\\\"'\",");
- }
- else
- {
- k = 5;
- while (*++s != '\'')
- {
- ++k;
- if (*s == '\\')
- {
- k += 2;
- if (*++s == '\\')
- ++k;
- }
- }
- j += k;
- if (j > 80)
- {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = k;
- }
- fprintf(output_file, "\"'");
- s = symnam[i];
- while (*++s != '\'')
- {
- if (*s == '\\')
- {
- fprintf(output_file, "\\\\");
- if (*++s == '\\')
- fprintf(output_file, "\\\\");
- else
- putc(*s, output_file);
- }
- else
- putc(*s, output_file);
- }
- fprintf(output_file, "'\",");
- }
- }
- else
- {
- k = strlen(s) + 3;
- j += k;
- if (j > 80)
- {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = k;
- }
- putc('"', output_file);
- do { putc(*s, output_file); } while (*++s);
- fprintf(output_file, "\",");
- }
- }
- else
- {
- j += 2;
- if (j > 80)
- {
- if (!rflag) ++outline;
- putc('\n', output_file);
- j = 2;
- }
- fprintf(output_file, "0,");
- }
- }
- if (!rflag) outline += 2;
- fprintf(output_file, "\n};\n");
- FREE(symnam);
- if (!rflag) ++outline;
- if (!rflag)
- fprintf(output_file, "static ");
- fprintf(output_file, "const char *yyrule[] = {\n");
- for (i = 2; i < nrules; ++i)
- {
- fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
- for (j = rrhs[i]; ritem[j] > 0; ++j)
- {
- s = symbol_name[ritem[j]];
- if (s[0] == '"')
- {
- fprintf(output_file, " \\\"");
- while (*++s != '"')
- {
- if (*s == '\\')
- {
- if (s[1] == '\\')
- fprintf(output_file, "\\\\\\\\");
- else
- fprintf(output_file, "\\\\%c", s[1]);
- ++s;
- }
- else
- putc(*s, output_file);
- }
- fprintf(output_file, "\\\"");
- }
- else if (s[0] == '\'')
- {
- if (s[1] == '"')
- fprintf(output_file, " '\\\"'");
- else if (s[1] == '\\')
- {
- if (s[2] == '\\')
- fprintf(output_file, " '\\\\\\\\");
- else
- fprintf(output_file, " '\\\\%c", s[2]);
- s += 2;
- while (*++s != '\'')
- putc(*s, output_file);
- putc('\'', output_file);
- }
- else
- fprintf(output_file, " '%c'", s[1]);
- }
- else
- fprintf(output_file, " %s", s);
- }
- if (!rflag) ++outline;
- fprintf(output_file, "\",\n");
- }
- if (!rflag) outline += 2;
- fprintf(output_file, "};\n#endif\n");
- }
- void output_stype()
- {
- if (!unionized && ntags == 0)
- {
- outline += 3;
- fprintf(code_file, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
- }
- }
- void output_trailing_text()
- {
- register int c, last;
- register FILE *in, *out;
- if (line == 0)
- return;
- in = input_file;
- out = code_file;
- c = *cptr;
- if (c == '\n')
- {
- ++lineno;
- if ((c = getc(in)) == EOF)
- return;
- if (!lflag)
- {
- ++outline;
- fprintf(out, line_format, lineno, (inc_file?inc_file_name:input_file_name));
- }
- if (c == '\n')
- ++outline;
- putc(c, out);
- last = c;
- }
- else
- {
- if (!lflag)
- {
- ++outline;
- fprintf(out, line_format, lineno, (inc_file?inc_file_name:input_file_name));
- }
- do { putc(c, out); } while ((c = *++cptr) != '\n');
- ++outline;
- putc('\n', out);
- last = '\n';
- }
- while ((c = getc(in)) != EOF)
- {
- if (c == '\n')
- ++outline;
- putc(c, out);
- last = c;
- }
- if (last != '\n')
- {
- ++outline;
- putc('\n', out);
- }
- if (!lflag)
- fprintf(out, line_format, ++outline + 1, code_file_name);
- }
- void output_semantic_actions()
- {
- register int c, last;
- register FILE *out;
- register int state; /* 0=middle of line, 1=start of line, 2=seen '#' */
- state = 1;
- fclose(action_file);
- action_file = fopen(action_file_name, "r");
- if (action_file == NULL)
- open_error(action_file_name);
- if ((c = getc(action_file)) == EOF)
- return;
- out = code_file;
- do {
- last = c;
- if (c == '\n') {
- ++outline;
- if (state == 2) {
- fprintf(out, line_format+1, outline + 1, code_file_name);
- state = 1;
- continue; }
- state = 1; }
- else if (state == 1 && c == '#')
- state = 2;
- else
- state = 0;
- putc(c, out);
- } while ((c = getc(action_file)) != EOF);
- if (last != '\n')
- {
- ++outline;
- putc('\n', out);
- }
- if (!lflag)
- fprintf(out, line_format, ++outline + 1, code_file_name);
- }
- void free_itemsets()
- {
- register core *cp, *next;
- FREE(state_table);
- for (cp = first_state; cp; cp = next)
- {
- next = cp->next;
- FREE(cp);
- }
- }
- void free_shifts()
- {
- register shifts *sp, *next;
- FREE(shift_table);
- for (sp = first_shift; sp; sp = next)
- {
- next = sp->next;
- FREE(sp);
- }
- }
- void free_reductions()
- {
- register reductions *rp, *next;
- FREE(reduction_table);
- for (rp = first_reduction; rp; rp = next)
- {
- next = rp->next;
- FREE(rp);
- }
- }
- void write_section(char *section_name)
- {
- char **section;
- FILE *fp;
- int i;
- struct section *sl;
- for(sl=§ion_list[0]; sl->name; sl++) {
- if(strcmp(sl->name,section_name)==0) {
- break;
- }
- }
- if(sl->name==0) {
- fprintf(stderr, "Cannot find section '%s' in your skeleton file\n", section_name);
- exit(1);
- }
- section = sl->ptr;
- fp = code_file;
- for (i = lflag ? 1 : 0; section[i]; ++i)
- {
- ++outline;
- fprintf(fp, "%s\n", section[i]);
- }
- }
|