词法分析程序(C语言编写,针对PL_0语言).doc_第1页
词法分析程序(C语言编写,针对PL_0语言).doc_第2页
词法分析程序(C语言编写,针对PL_0语言).doc_第3页
词法分析程序(C语言编写,针对PL_0语言).doc_第4页
词法分析程序(C语言编写,针对PL_0语言).doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#include #include #include #define NORW 13 /* of reserved words */#define TXMAX 100 /* length of identifier table */#define NMAX 14 /* max number of digits in numbers */#define AL 10 /* length of identifiers */#define AMAX 2047 /* maxinum address */#define LEVMAX 3 /* max depth of block nesting */#define CXMAX 200 /* size of code array */#define STACKSIZE 500char *symbol32= 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, constsym,varsym,procsym; /* type of symbols */char *wordNORW=begin,call,const,do,end,if,odd,procedure, read,then,var,while,write; /* table of reserved words */char *wsymNORW= beginsym,callsym,constsym,dosym,endsym,ifsym, oddsym,procsym,readsym,thensym,varsym,whilesym,writesym; char *mnemonic8= lit,opr,lod,sto,cal,ini,jmp,jpc;char ch; /* last char read */char idAL+1; /*last identifier read */char sym10; /* last symbol read */char line81;char aAL+1,fnameAL+1;enum objectconstant,variable,procedur;enum object kind;enum fctlit,opr,lod,sto,cal,ini,jmp,jpc;enum listswitcherfalse,true; /*true set list object code */enum listswitcher listswitch;FILE *fa;FILE *fa1, *fa2;FILE *fin, *fout;int num; /* last number read */int cc; /* character count */int ll; /* line length */int cx; /* code allocation index */int err;int lev=0,tx=0,dx=3;int linecnt=0;struct instructionenum fct f; /* function code */ int l; /* level */ int a; /* displacement addr */; /* lit 0,a: load constant a opr 0,a: execute opr a lod l,a: load variable 1, a sto l,a: store variable 1, a cal l,a: call procedure a at level 1 int 0,a: increment t-register by a jmp 0,a: jump to a jpc 0,a: jump conditional to a */struct instruction codeCXMAX+1;struct table1char nameAL+1; enum object kind; int val,level,adr,size;struct table1 tableTXMAX+1;struct nodechar *pa32;*declbegsys,*statbegsys,*facbegsys,*tempsetsys;int in(str, set)char *str;struct node *set;int i=0;while(set-pai!=NULL)if(strcmp(str,set-pai)=0)return( 1 );elsei+;return( 0 );struct node *add(set1,set2)struct node *set1,*set2;int i=0,j=0,k=0,cnt;struct node *pt;pt=(struct node *)malloc(sizeof(struct node);for(cnt=0; cnt pacnt=(char*)malloc(10*sizeof(char);while(set1-pai!=NULL)strcpy(pt-paj+,set1-pai+);while(set2-pak!=NULL)if (in(set2-pak,set1)=0)strcpy(pt-paj+,set2-pak+);elsek+;pt-paj=NULL;return( pt );error(int n)int i;printf (*);fputs (*, fa1);for (i=0;icc;i+)printf ( );for (i=0;i=a&ch=z)/* id or reserved word */k=0;do if(k=a&ch=0&ch=9); ak=0;strcpy(id,a);i=0;j=NORW-1;do /* look up reserved words by binary search */k=(i+j)/2;if (strcmp(id,wordk)=0) i=k+1;while (ij) strcpy(sym,wsymk);else strcpy(sym,ident);else if (ch=0&ch=0&chNMAX) error(30);else if (ch=:)get_ch( );if (ch=)strcpy(sym,becomes);get_ch( );else strcpy(sym,nul);else if (ch=)get_ch( );if (ch=)strcpy(sym,geq);get_ch( );else strcpy(sym,gtr);else switch(ch)case +: strcpy(sym,plus);break;case -: strcpy(sym,minus);break;case *: strcpy(sym,times);break;case /: strcpy(sym,slash);break;case (: strcpy(sym,lparen);break;case ): strcpy(sym,rparen);break;case =: strcpy(sym,eql);break;case ,: strcpy(sym,comma);break;case .: strcpy(sym,period);break;case #: strcpy(sym,neq);break;case ;: strcpy(sym,semicolon);break;get_ch( );void gen(x,y,z)enum fct x;int y,z;if (cxCXMAX)printf(program too long);codecx.f=x;codecx.l=y;codecx.a=z;cx+;void test(s1,s2,n)struct node *s1,*s2;int n;if (in(sym,s1)=0)error(n);s1=add(s1,s2);while(in(sym,s1)=0) getsym( );void enter(k) /* enter object into table */enum object k;tx=tx+1;strcpy(,id);tabletx.kind=k;switch(k)case constant:if (numNMAX) error(31);num=0; tabletx.val=num; break;case variable:tabletx.level=lev; tabletx.adr=dx; dx+; break;case procedur:tabletx.level=lev;break;int position(id) /* find identifier in table */char id10;int i;strcpy(,id);i=tx;while (strcmp(,id)!=0)i-;return i;void constdeclaration( )if (strcmp(sym,ident)=0)getsym( );if (strcmp(sym,eql)=0|strcmp(sym,becomes)=0)if (strcmp(sym,becomes)=0) error(1);getsym( );if (strcmp(sym,number)=0)enter(constant);getsym( );else error(2);else error(3);else error(4);void vardeclaration( )if (strcmp(sym,ident)=0)enter(variable);getsym( );else error(4);void listcode(int cx0) /* list code generated for this block */int i;if (listswitch=true)for(i=cx0;ipan+=tempsetm+;temp-pan=NULL;test(facbegsys,fsys,24);while(in(sym,facbegsys)=1)if (strcmp(sym,ident)=0)i=position(id);if (i=0) error(11);else switch(tablei.kind)case constant: gen(lit,0,tablei.val); break; /*some thing error here(lev)*/case variable: gen(lod,lev-tablei.level,tablei.adr); /*must use para pass in*/ break;case procedur: error(21); break;getsym( );else if (strcmp(sym,number)=0)if (numAMAX)error(31);num=0;gen(lit,0,num);getsym( );else if (strcmp(sym,lparen)=0)getsym( );expression(add(temp,fsys);if (strcmp(sym,rparen)=0) getsym( );else error(22);test(fsys,facbegsys,23);void term(fsys)struct node *fsys;int i=0,j=0;char mulop10;char *tempset =times,slash,NULL;struct node *temp;temp=(struct node *)malloc(sizeof(struct node);while(tempseti!=NULL)temp-pai+=tempsetj+;temp-pai=NULL;factor(add(temp,fsys);while (in(sym,temp)=1)strcpy(mulop,sym);getsym( );factor(add(temp,fsys);if (strcmp(mulop,times)=0) gen(opr,0,4);else gen(opr,0,5);void expression(fsys)struct node *fsys;int m=0,n=0;char addop10;char *tempset =plus,minus,NULL;struct node *temp;temp=(struct node *)malloc(sizeof(struct node);while(tempsetm!=NULL)temp-pan+=tempsetm+;temp-pan=NULL;if(in(sym,temp)=1)strcpy(addop,sym);getsym( );term(add(fsys,temp);if (strcmp(addop,minus)=0) gen(opr,0,1);else term(add(fsys,temp);while (in(sym,temp)=1)strcpy(addop,sym);getsym( );term(add(fsys,temp);if (strcmp(addop,plus)=0) gen(opr,0,2);else gen(opr,0,3);void condition(fsys)struct node *fsys;int i=0,j=0;char relop10;char *tempset =eql,neq,lss,leq,gtr,geq,NULL;struct node *temp;temp=(struct node *)malloc(sizeof(struct node);while(tempseti!=NULL)temp-paj+=tempseti+;temp-paj=NULL;if (strcmp(sym,oddsym)=0)getsym( );expression(fsys);gen(opr,0,6);else expression(add(temp,fsys);if (in(sym,temp)=0) error(20);else strcpy(relop,sym);getsym( );expression(fsys);if(strcmp(relop,eql)=0) gen(opr,0,8);if(strcmp(relop,neq)=0) gen(opr,0,9);if(strcmp(relop,lss)=0) gen(opr,0,10);if(strcmp(relop,geq)=0) gen(opr,0,11);if(strcmp(relop,gtr)=0) gen(opr,0,12);if(strcmp(relop,leq)=0) gen(opr,0,13);void statement(fsys,plev)struct node *fsys;int plev;int i,cx1,cx2,m=0,n=0;char *tempset1 =rparen,comma,NULL;char *tempset2 =thensym,dosym,NULL;char *tempset3 =semicolon,endsym,NULL;char *tempset4 =semicolon,NULL;char *tempset5 =dosym,NULL;char *tempset6 =NULL;struct node *temp1,*temp2,*temp3,*temp4,*temp5,*temp6;temp1=(struct node *)malloc(sizeof(struct node);temp2=(struct node *)malloc(sizeof(struct node);temp3=(struct node *)malloc(sizeof(struct node);temp4=(struct node *)malloc(sizeof(struct node);temp5=(struct node *)malloc(sizeof(struct node);temp6=(struct node *)malloc(sizeof(struct node);while(tempset1m!=NULL)temp1-pan+=tempset1m+;temp1-pan=NULL;m=0;n=0;while(tempset2m!=NULL)temp2-pan+=tempset2m+;temp2-pan=NULL;m=0;n=0;while(tempset3m!=NULL)temp3-pan+=tempset3m+;temp3-pan=NULL;m=0;n=0;while(tempset4m!=NULL)temp4-pan+=tempset4m+;temp4-pan=NULL;m=0;n=0;while(tempset5m!=NULL)temp5-pan+=tempset5m+;temp5-pan=NULL;m=0;n=0;while(tempset6m!=NULL)temp6-pan+=tempset6m+;temp6-pan=NULL;m=0;n=0;if (strcmp(sym,ident)=0)i=position(id);if (i=0)error(11);else if (tablei.kind!=variable)error(12);i=0;getsym( );if (strcmp(sym,becomes)=0) getsym( );else error(13);expression(fsys);if (i!=0)gen(sto,plev-tablei.level,tablei.adr);else if (strcmp(sym,readsym)=0)getsym( );if (strcmp(sym,lparen)!=0) error(24);else dogetsym( );if (strcmp(sym,ident)=0) i=position(id);else i=0;if (i=0) error(35);else gen(opr,0,16);gen(sto,plev-tablei.level,tablei.adr);getsym( );while(strcmp(sym,comma)=0);if (strcmp(sym,rparen)!=0) error(22);while(in(sym,fsys)=0) getsym( );else getsym( );else if (strcmp(sym,writesym)=0)getsym( );if (strcmp(sym,lparen)=0)dogetsym( );expression(add(temp1,fsys);gen(opr,0,14);while(strcmp(sym,comma)=0);if (strcmp(sym,rparen)!=0) error(33);else getsym( );gen(opr,0,15);else if (strcmp(sym,callsym)=0)getsym( );if (strcmp(sym,ident)!=0) error(14);else i=position(id);if (i=0) error(11);else if (tablei.kind=procedur)gen(cal,plev-tablei.level,tablei.adr);else error(15);getsym( );else if (strcmp(sym,ifsym)=0)getsym( );condition(add(temp2,fsys);if (strcmp(sym,thensym)=0) getsym( );else error(16);cx1=cx;gen(jpc,0,0);statement(fsys,plev);codecx1.a=cx;else if (strcmp(sym,beginsym)=0)getsym( );statement(add(temp3,fsys),plev);while(in(sym,add(temp4,statbegsys)=1)if (strcmp(sym,semicolon)=0) getsym( );else error(10);statement(add(temp3,fsys),plev);if (strcmp(sym,endsym)=0) getsym( );else error(17);else if (strcmp(sym,whilesym)=0)cx1=cx;getsym( );condition(add(temp5,fsys);cx2=cx;gen(jpc,0,0);if (strcmp(sym,dosym)=0) getsym( );else error(18);statement(fsys,plev);gen(jmp,0,cx1);codecx2.a=cx;test(fsys,temp6,19);void block(plev,fsys)int plev;struct node *fsys;int m=0,n=0; int dx0=3; /* data allocation index */int tx0; /* initial table index */int cx0; /* initial code index */char *tempset1 =semicolon,endsym,NULL;char *tempset2 =ident,procsym,NULL;char *tempset3 =semicolon,NULL;char *tempset4 =ident,NULL;char *tempset5 =NULL;struct node *temp1,*temp2,*temp3,*temp4,*temp5;temp1=(struct node *)malloc(sizeof(struct node);temp2=(struct node *)malloc(sizeof(struct node);temp3=(struct node *)malloc(sizeof(struct node);temp4=(struct node *)malloc(sizeof(struct node);temp5=(struct node *)malloc(sizeof(struct node);while(tempset1m!=NULL)temp1-pan+=tempset1m+;temp1-pan=NULL;m=0;n=0;while(tempset2m!=NULL)temp2-pan+=tempset2m+;temp2-pan=NULL;m=0;n=0;while(tempset3m!=NULL)temp3-pan+=tempset3m+;temp3-pan=NULL;m=0;n=0;while(tempset4m!=NULL)temp4-pan+=tempset4m+;temp4-pan=NULL;m=0;n=0;while(tempset5m!=NULL)temp5-pan+=tempset5m+;temp5-pan=NULL;m=0;n=0;lev=plev;tx0=tx;tabletx.adr=cx;gen(jmp,0,1);if (plevLEVMAX) error(32);doif (strcmp(sym,constsym)=0)getsym( );doconstdeclaration( );while(strcmp(sym,comma)=0)getsym( );constdeclaration( );if (strcmp(sym,semicolon)=0) getsym( );else error(5);while(strcmp(sym,ident)=0);if (strcmp(sym,varsym)=0)getsym( );do dx0+;vardeclaration( );while (strcmp(sym,comma)=0)getsym( ); dx0+;vardeclaration( );if (strcmp(sym,semicolon)=0) getsym( );else error(5);while(strcmp(sym,ident)=0);while (strcmp(sym,procsym)=0)getsym( );if (strcmp(sym,ident)=0)enter(procedur);getsym( );else error(4);if (strcmp(sym,semicolon)=0) getsym( );else error(5);block(plev+1,add(temp3,fsys);lev=lev-1;if (strcmp(sym,semicolon)=0)getsym( );test(add(statbegsys,temp2),fsys,6);else error(5);test(add(statbegsys,temp4),declbegsys,7);while(in(sym,declbegsys)=1);codetabletx0.adr.a=cx;tabletx0.adr=cx;tabletx0.size=dx0;cx0=cx; gen(ini,0,dx0);statement(add(temp1,fsys),plev);gen(opr,0,0);test(fsys,temp5,8);listcode(cx0); int base(l,b,s) int l; int *b; int sSTACKSIZE; int b1; b1=*b; /*find base l level down */ w

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论