logo资料库

张素琴编译原理课后附录(C语言版).doc

第1页 / 共32页
第2页 / 共32页
第3页 / 共32页
第4页 / 共32页
第5页 / 共32页
第6页 / 共32页
第7页 / 共32页
第8页 / 共32页
资料共32页,剩余部分请下载后查看
/* 测试用的程序: const a=10; var b,c; procedure p; begin c:=b+a end; begin read(b); while b#0 do begin call p;write(2*c);read(b); end end. */ #include /*#include"pl0.h"*/ #include"string.h" /*PL/0 编译系统 C 版本头文件 pl0.h*/ /*typedef enum{false,true}bool;*/ #define norw 13 #define txmax 100 #define nmax 14 #define al 10 #define amax 2047 #define levmax 3 #define cxmax 200 enum symbol{ nul,ident,number,plus,minus,times,slash,oddsym,eql,neq,lss,leq,gtr,geq,lparen,rparen,comma, semicolon,period, becomes,beginsym,endsym,ifsym,thensym,whilesym,writesym,readsym,dosym,callsym,const sym,varsym,procsym, }; #define symnum 32 enum object{ constant, variable,
procedur, }; enum fct{ lit,opr,lod,sto,cal,inte,jmp,jpc, }; #define fctnum 8 struct instruction { enum fct f; int l; int a; }; FILE* fas; FILE* fa; FILE* fa1; FILE* fa2; bool listswitch; bool tableswitch; char ch; enum symbol sym; char id[al+1]; int num; int cc,ll; int cx; char line[81]; char a[al+1]; struct instruction code[cxmax]; char word[norw][al]; enum symbol wsym[norw]; enum symbol ssym[256]; char mnemonic[fctnum][5]; bool declbegsys[symnum]; bool statbegsys[symnum]; bool facbegsys[symnum]; struct tablestruct { char name[al]; enum object kind; int val; int level; int adr;
int size; }; struct tablestruct table[txmax]; FILE* fin; FILE* fout; char fname[al]; int err; #define getsymdo if(-1==getsym())return -1 #define getchdo if(-1==getch())return -1 #define testdo(a,b,c) if(-1==test(a,b,c))return -1 #define gendo(a,b,c) if(-1==gen(a,b,c))return -1 #define expressiondo(a,b,c) if(-1==expression(a,b,c))return -1 #define factordo(a,b,c) if(-1==factor(a,b,c))return -1 #define termdo(a,b,c) if(-1==term(a,b,c))return -1 #define conditiondo(a,b,c) if(-1==condition(a,b,c))return -1 #define statementdo(a,b,c) if(-1==statement(a,b,c))return -1 #define constdeclarationdo(a,b,c) if(-1==constdeclaration(a,b,c))return -1 #define vardeclarationdo(a,b,c) if(-1==vardeclaration(a,b,c))return -1 void error(int n); int getsym(); int getch(); void init(); int gen(enum fct x,int y,int z); int test(bool* s1,bool* s2,int n); int inset(int e,bool* s); int addset(bool* sr,bool* s1,bool* s2,int n); int subset(bool* sr,bool* s1,bool* s2,int n); int mulset(bool* sr,bool* s1,bool* s2,int n); int block(int lev,int tx,bool* fsys); void interpret(); int factor(bool* fsys,int* ptx,int lev); int term(bool* fsys,int* ptx,int lev); int condition(bool* fsys,int* ptx,int lev); int expression(bool* fsys,int* ptx,int lev); int statement(bool* fsys,int* ptx,int lev); void listcode(int cx0); int vardeclaration(int* ptx,int lev,int* pdx); int constdeclaration(int* ptx,int lev,int*pdx); int position(char* idt,int tx); void enter(enum object k,int* ptx,int lev,int* pdx); int base(int l,int* s,int b);
#define stacksize 500 int main() { bool nxtlev[symnum]; printf("Input pl/0 file?"); scanf("%s",fname); fin=fopen(fname,"r"); if(fin) { printf("List object code? (Y/N)"); scanf("%s",fname); listswitch=(fname[0]=='y'||fname[0]=='Y'); printf("List symbol table?(Y/N)"); scanf("%s",fname); tableswitch=(fname[0]=='y'||fname[0]=='Y'); fa1=fopen("fa1.tmp","w"); fprintf(fa1,"Input pl/0 file?"); fprintf(fa1,"%s\n",fname); init(); err=0; cc=cx=ll=0; ch=' '; if(-1!=getsym()) { fa=fopen("fa.tmp","w"); fas=fopen("fas.tmp","w"); addset(nxtlev,declbegsys,statbegsys,symnum); nxtlev[period]=true; if(-1==block(0,0,nxtlev)) { fclose(fa); fclose(fa1); fclose(fas); fclose(fin); printf("\n"); return 0; } fclose(fa);
fclose(fa1); fclose(fas); if(sym!=period) { error(9); } if(err==0) { fa2=fopen("fa2.tmp","w"); interpret(); fclose(fa2); printf("Errors in pl/0 program"); } else { } } fclose(fin); } else { printf("Can't open file!\n"); } printf("\n"); return 0; } void init() { int i; for(i=0;i<=255;i++) { ssym[i]=nul; } ssym['+']=plus; ssym['-']=minus; ssym['*']=times; ssym['/']=slash; ssym['(']=lparen; ssym[')']=rparen; ssym['=']=eql; ssym[',']=comma; ssym['.']=period; ssym['#']=neq; ssym[';']=semicolon;
strcpy(&(word[0][0]),"begin"); strcpy(&(word[1][0]),"call"); strcpy(&(word[2][0]),"const"); strcpy(&(word[3][0]),"do"); strcpy(&(word[4][0]),"end"); strcpy(&(word[5][0]),"if"); strcpy(&(word[6][0]),"odd"); strcpy(&(word[7][0]),"procedure"); strcpy(&(word[8][0]),"read"); strcpy(&(word[9][0]),"then"); strcpy(&(word[10][0]),"var"); strcpy(&(word[11][0]),"while"); strcpy(&(word[12][0]),"write"); wsym[0]=beginsym; wsym[1]=callsym; wsym[2]=constsym; wsym[3]=dosym; wsym[4]=endsym; wsym[5]=ifsym; wsym[6]=oddsym; wsym[7]=procsym; wsym[8]=readsym; wsym[9]=thensym; wsym[10]=varsym; wsym[11]=whilesym; wsym[12]=writesym; strcpy(&(mnemonic[lit][0]),"lit"); strcpy(&(mnemonic[opr][0]),"opr"); strcpy(&(mnemonic[lod][0]),"lod"); strcpy(&(mnemonic[sto][0]),"sto"); strcpy(&(mnemonic[cal][0]),"cal"); strcpy(&(mnemonic[inte][0]),"int"); strcpy(&(mnemonic[jmp][0]),"jmp"); strcpy(&(mnemonic[jpc][0]),"jpc"); for(i=0;i
declbegsys[constsym]=true; declbegsys[varsym]=true; declbegsys[procsym]=true; statbegsys[beginsym]=true; statbegsys[callsym]=true; statbegsys[ifsym]=true; statbegsys[whilesym]=true; facbegsys[ident]=true; facbegsys[number]=true; facbegsys[lparen]=true; } int inset(int e,bool *s) { return s[e]; } int addset(bool *sr,bool *s1,bool *s2,int n) { int i; for(i=0;i
} return 0; } void error(int n) { char space[81]; memset(space,32,81); space[cc-1]=0; printf("****%s!%d\n",space,n); fprintf(fa1,"****%s!%d\n",space,n); err++; } int getch() { if(cc==ll) { if(feof(fin)) { printf("program incomplete"); return -1; } ll=0; cc=0; printf("%d",cx); fprintf(fa1,"%d",cx); ch=' '; while(ch!=10) { if(EOF==fscanf(fin,"%c",&ch)) { line[ll]=0; break; } printf("%c",ch); fprintf(fa1,"%c",ch); line[ll]=ch; ll++; }
分享到:
收藏