《词法分析程序(c语言编写,针对pl_0语言)》由会员分享,可在线阅读,更多相关《词法分析程序(c语言编写,针对pl_0语言)(16页珍藏版)》请在金锄头文库上搜索。
1、#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 */#d
2、efine 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 s
3、ymbols */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; /*
4、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 listsw
5、itch;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 a
6、ddr */; /* lit 0,a: load constant a opr 0,a: execute opr alod l,a: load variable 1, asto l,a: store variable 1, acal l,a: call procedure a at level 1int 0,a: increment t-register by ajmp 0,a: jump to ajpc 0,a: jump conditional to a */struct instruction codeCXMAX+1;struct table1char nameAL+1;enum obj
7、ect 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,
8、*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)
9、int i;printf (*);fputs (*, fa1);for (i=0;i=a&ch=aak=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=0if(kNMAX) error(30);else if (ch=:)get_ch( );if (ch=)strcpy(sym
10、,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 )
11、: 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=
12、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(tabletx.name,id);tabletx.kind=k;switch(k)case constant:if (numNMAX)error(31);num=0;tabletx.val=num;break;cas
13、e 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(table0.name,id);i=tx;while (strcmp(tablei.name,id)!=0)i-;return i;void constdeclaration( )if (strcmp(sym,ident)=0)getsym( );if (strcmp(sym
14、,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