📄 sub2.c
字号:
nchar[nptr] = ach[j]; nexts[++nptr] = ast[j]; j++; } if(nchar[p-1] == 0)break; if(ach[j] > nchar[p-1]){ warning("bad transition %d %d",st,cmin); goto nopack; } /* ach[j] == nchar[p-1] */ if(ast[j] != nexts[p] || ast[j] == -1 || (cpackflg[st] && ach[j] != match[ach[j]])){ k++; nchar[nptr] = ach[j]; nexts[++nptr] = ast[j]; } p++; j++; } while(ach[j]){ nchar[nptr] = ach[j]; nexts[++nptr] = ast[j++]; k++; } nexts[gotof[st]] = cnt = k; nchar[nptr++] = 0; } else {# endifnopack: /* stick it in */ gotof[st] = nptr; nexts[nptr] = cnt; for(i=0;i<cnt;i++){ nchar[nptr] = ach[i]; nexts[++nptr] = ast[i]; } nchar[nptr++] = 0;# ifdef PS }# endif if(cnt < 1){ gotof[st] = -1; nptr--; } else if(nptr > ntrans) error("Too many transitions %s",(ntrans==NTRANS?"\nTry using %a num":"")); return; }# ifdef DEBUGpstate(s) int s; { register int *p,i,j; printf("State %d:\n",s); p = state[s]; i = *p++; if(i == 0) return; printf("%4d",*p++); for(j = 1; j<i; j++){ printf(", %4d",*p++); if(j%30 == 0)putchar('\n'); } putchar('\n'); return; }# endifmember(d,t) int d; char *t; { register int c; register char *s; c = d; s = t; c = cindex[c]; while(*s) if(*s++ == c) return(1); return(0); }# ifdef DEBUGstprt(i) int i; { register int p, t; printf("State %d:",i); /* print actions, if any */ t = atable[i]; if(t != -1)printf(" final"); putchar('\n'); if(cpackflg[i] == TRUE)printf("backup char in use\n"); if(sfall[i] != -1)printf("fall back state %d\n",sfall[i]); p = gotof[i]; if(p == -1) return; printf("(%d transitions)\n",nexts[p]); while(nchar[p]){ charc = 0; if(nexts[p+1] >= 0) printf("%d\t",nexts[p+1]); else printf("err\t"); allprint(nchar[p++]); while(nexts[p] == nexts[p+1] && nchar[p]){ if(charc > LINESIZE){ charc = 0; printf("\n\t"); } allprint(nchar[p++]); } putchar('\n'); } putchar('\n'); return; }# endifacompute(s) /* compute action list = set of poss. actions */ int s; { register int *p, i, j; int cnt, m; int temp[300], k, neg[300], n; k = 0; n = 0; p = state[s]; cnt = *p++; if(cnt > 300) error("Too many positions for one state - acompute"); for(i=0;i<cnt;i++){ if(name[*p] == FINAL)temp[k++] = left[*p]; else if(name[*p] == S1FINAL){temp[k++] = left[*p]; if (left[*p] >NACTIONS) error("Too many right contexts"); extra[left[*p]] = 1; } else if(name[*p] == S2FINAL)neg[n++] = left[*p]; p++; } atable[s] = -1; if(k < 1 && n < 1) return;# ifdef DEBUG if(debug) printf("final %d actions:",s);# endif /* sort action list */ for(i=0; i<k; i++) for(j=i+1;j<k;j++) if(temp[j] < temp[i]){ m = temp[j]; temp[j] = temp[i]; temp[i] = m; } /* remove dups */ for(i=0;i<k-1;i++) if(temp[i] == temp[i+1]) temp[i] = 0; /* copy to permanent quarters */ atable[s] = aptr;# ifdef DEBUG if(!ratfor)fprintf(fout,"/* actions for state %d */",s);# endif putc('\n',fout); for(i=0;i<k;i++) if(temp[i] != 0){ ratfor ? fprintf(fout,"data vstop(%d)/%d/\n",aptr,temp[i]) : fprintf(fout,"%d,\n",temp[i]);# ifdef DEBUG if(debug) printf("%d ",temp[i]);# endif aptr++; } for(i=0;i<n;i++){ /* copy fall back actions - all neg */ ratfor ? fprintf(fout,"data vstop(%d)/%d/\n",aptr,neg[i]) : fprintf(fout,"%d,\n",neg[i]); aptr++;# ifdef DEBUG if(debug)printf("%d ",neg[i]);# endif }# ifdef DEBUG if(debug)putchar('\n');# endif ratfor ? fprintf(fout,"data vstop (%d)/0/\n",aptr) : fprintf(fout,"0,\n"); aptr++; return; }# ifdef DEBUGpccl() { /* print character class sets */ register int i, j; printf("char class intersection\n"); for(i=0; i< ccount; i++){ charc = 0; printf("class %d:\n\t",i); for(j=1;j<NCH;j++) if(cindex[j] == i){ allprint(j); if(charc > LINESIZE){ printf("\n\t"); charc = 0; } } putchar('\n'); } charc = 0; printf("match:\n"); for(i=0;i<NCH;i++){ allprint(match[i]); if(charc > LINESIZE){ putchar('\n'); charc = 0; } } putchar('\n'); return; }# endifmkmatch(){ register int i; char tab[NCH]; for(i=0; i<ccount; i++) tab[i] = 0; for(i=1;i<NCH;i++) if(tab[cindex[i]] == 0) tab[cindex[i]] = i; /* tab[i] = principal char for new ccl i */ for(i = 1; i<NCH; i++) match[i] = tab[cindex[i]]; return; }layout(){ /* format and output final program's tables */ register int i, j, k; int top, bot, startup, omin; startup = 0; for(i=0; i<outsize;i++) verify[i] = advance[i] = 0; omin = 0; yytop = 0; for(i=0; i<= stnum; i++){ /* for each state */ j = gotof[i]; if(j == -1){ stoff[i] = 0; continue; } bot = j; while(nchar[j])j++; top = j - 1;# if DEBUG if (debug) { printf("State %d: (layout)\n", i); for(j=bot; j<=top;j++) { printf(" %o", nchar[j]); if (j%10==0) putchar('\n'); } putchar('\n'); }# endif while(verify[omin+ZCH]) omin++; startup = omin;# if DEBUG if (debug) printf("bot,top %d, %d startup begins %d\n",bot,top,startup);# endif if(chset){ do { startup =+ 1; if(startup > outsize - ZCH) error("output table overflow"); for(j = bot; j<= top; j++){ k=startup+ctable[nchar[j]]; if(verify[k])break; } } while (j <= top);# if DEBUG if (debug) printf(" startup will be %d\n",startup);# endif /* have found place */ for(j = bot; j<= top; j++){ k = startup + ctable[nchar[j]]; if (ctable[nchar[j]]<=0) printf("j %d nchar %d ctable.nch %d\n",j,nchar[j],ctable[nchar[k]]); verify[k] = i+1; /* state number + 1*/ advance[k] = nexts[j+1]+1; /* state number + 1*/ if(yytop < k) yytop = k; } } else { do { startup =+ 1; if(startup > outsize - ZCH) error("output table overflow"); for(j = bot; j<= top; j++){ k = startup + nchar[j]; if(verify[k])break; } } while (j <= top); /* have found place */# if DEBUG if (debug) printf(" startup going to be %d\n", startup);# endif for(j = bot; j<= top; j++){ k = startup + nchar[j]; verify[k] = i+1; /* state number + 1*/ advance[k] = nexts[j+1]+1; /* state number + 1*/ if(yytop < k) yytop = k; } } stoff[i] = startup; } /* stoff[i] = offset into verify, advance for trans for state i */ /* put out yywork */ if(ratfor){ fprintf(fout, "define YYTOPVAL %d\n", yytop); rprint(verify,"verif",yytop+1); rprint(advance,"advan",yytop+1); shiftr(stoff, stnum); rprint(stoff,"stoff",stnum+1); shiftr(sfall, stnum); upone(sfall, stnum+1); rprint(sfall,"sfall",stnum+1); bprint(extra,"extra",casecount+1); bprint(match,"match",NCH); shiftr(atable, stnum); rprint(atable,"atable",stnum+1); return; } fprintf(fout,"# define YYTYPE %s\n",stnum+1 > NCH ? "int" : "char"); fprintf(fout,"struct yywork { YYTYPE verify, advance; } yycrank[] ={\n"); for(i=0;i<=yytop;i=+4){ for(j=0;j<4;j++){ k = i+j; if(verify[k]) fprintf(fout,"%d,%d,\t",verify[k],advance[k]); else fprintf(fout,"0,0,\t"); } putc('\n',fout); } fprintf(fout,"0,0};\n"); /* put out yysvec */ fprintf(fout,"struct yysvf yysvec[] ={\n"); fprintf(fout,"0,\t0,\t0,\n"); for(i=0;i<=stnum;i++){ /* for each state */ if(cpackflg[i])stoff[i] = -stoff[i]; fprintf(fout,"yycrank+%d,\t",stoff[i]); if(sfall[i] != -1) fprintf(fout,"yysvec+%d,\t", sfall[i]+1); /* state + 1 */ else fprintf(fout,"0,\t\t"); if(atable[i] != -1) fprintf(fout,"yyvstop+%d,",atable[i]); else fprintf(fout,"0,\t");# ifdef DEBUG fprintf(fout,"\t\t/* state %d */",i);# endif putc('\n',fout); } fprintf(fout,"0,\t0,\t0};\n"); /* put out yymatch */ fprintf(fout,"struct yywork *yytop = yycrank+%d;\n",yytop); fprintf(fout,"struct yysvf *yybgin = yysvec+1;\n"); if(optim){ fprintf(fout,"char yymatch[] ={\n"); if (chset==0) /* no chset, put out in normal order */ { for(i=0; i<NCH; i=+8){ for(j=0; j<8; j++){ int fbch; fbch = match[i+j]; if(printable(fbch) && fbch != '\'' && fbch != '\\') fprintf(fout,"'%c' ,",fbch); else fprintf(fout,"0%-3o,",fbch); } putc('\n',fout); } } else { int *fbarr; fbarr = myalloc(2*NCH, sizeof(*fbarr)); if (fbarr==0) error("No space for char table reverse",0); for(i=0; i<ZCH; i++) fbarr[i]=0; for(i=0; i<NCH; i++) fbarr[ctable[i]] = ctable[match[i]]; for(i=0; i<ZCH; i+=8) { for(j=0; j<8; j++) fprintf(fout, "0%-3o,",fbarr[i+j]); putc('\n',fout); } cfree(fbarr, 2*NCH, 1); } fprintf(fout,"0};\n"); } /* put out yyextra */ fprintf(fout,"char yyextra[] ={\n"); for(i=0;i<casecount;i=+8){ for(j=0;j<8;j++) fprintf(fout, "%d,", i+j<NACTIONS ? extra[i+j] : 0); putc('\n',fout); } fprintf(fout,"0};\n"); return; }rprint(a,s,n) char *s; int *a, n; { register int i; fprintf(fout,"block data\n"); fprintf(fout,"common /L%s/ %s\n",s,s); fprintf(fout,"define S%s %d\n",s,n); fprintf(fout,"integer %s (S%s)\n",s,s); for(i=1; i<=n; i++) { if (i%8==1) fprintf(fout, "data "); fprintf(fout, "%s (%d)/%d/",s,i,a[i]); fprintf(fout, (i%8 && i<n) ? ", " : "\n"); } fprintf(fout,"end\n"); }shiftr(a, n) int *a;{int i;for(i=n; i>=0; i--) a[i+1]=a[i];}upone(a,n) int *a;{int i;for(i=0; i<=n ; i++) a[i]++;}bprint(a,s,n) char *s, *a; int n; { register int i, j, k; fprintf(fout,"block data\n"); fprintf(fout,"common /L%s/ %s\n",s,s); fprintf(fout,"define S%s %d\n",s,n); fprintf(fout,"integer %s (S%s)\n",s,s); for(i=1;i<n;i=+8){ fprintf(fout,"data %s (%d)/%d/",s,i,a[i]); for(j=1;j<8;j++){ k = i+j; if(k < n)fprintf(fout,", %s (%d)/%d/",s,k,a[k]); } putc('\n',fout); } fprintf(fout,"end\n"); }# ifdef PPpadd(array,n) int **array; int n; { register int i, *j, k; array[n] = nxtpos; if(count == 0){ *nxtpos++ = 0; return; } for(i=tptr-1;i>=0;i--){ j = array[i]; if(j && *j++ == count){ for(k=0;k<count;k++) if(!tmpstat[*j++])break; if(k >= count){ array[n] = array[i]; return; } } } add(array,n); return; }# endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -