资源描述:
《编译原理之语义分析(semantic analysis of compiler principles)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、编译原理之语义分析(Semanticanalysisofcompilerprinciples)//语义分析源代码//functiona=2+3*4;x=(a+b)/c;endfunc##include#include#include#include#includestructquad{charresult[12];charag1[12];charop[12];charag2[12];};structquadquad[30];intcount=0;char
2、*expression(void);charprog[200],token[8];charch;intsyn,p,m,n,sum=0,y=0,mm=0;char*rwtab[6]={"function","if","then","while","do","endfunc"};fromscaner(){m=0;for(n=0;n<8;n++)token[n]=' ';ch=prog[p++];while((ch=='')ch=prog[p++];if((ch>='a'&&ch<='z')
3、
4、(ch<='z'&&ch>="a")){while((ch
5、>='a'&&ch<='z')
6、
7、(ch<='z'&&ch>="a"),
8、
9、(ch>='0'&&ch<="9")){token[m++]=ch;ch=prog[p++];}token[m++]=' ';p--------;syn=10;for(n=0;n<6;n++){if(strcmp(token,rwtab[n])==0){d=n+1;break;}}}elseif(ch>='0'&&ch<="9"){sum=0;while(ch>='0'&&ch<="9"){sum=sum*10+ch-'0');ch=prog[p++];}p-------
10、-;syn=11;}elseswitch(ch){case'<':m=0;token[m++]=ch;ch=prog[++p];if(ch=='='){syn=22;token[m+1]=ch;}else{syn=20;ch=prog[-p];}break;case'>'means:m=0;token[m++]=ch;ch=prog[++p];if(ch=='='){syn=24;token[m++]=ch;}else{syn=23;ch=prog[-p];}break;case'=':m=0;token[m++]=ch;ch=prog[++p];
11、if(ch=='='){syn=25;token[m++]=ch;}else{syn=18;ch=prog[-p];}break;case'!":m=0;token[m++]=ch;ch=prog[++p];if(ch=='='){syn=22;token[m++]=ch;}elsesyn=-1;break;case'+':syn=13;token[0]=ch;break;case'-':syn=14;token[0]=ch;break;case'*':syn=15;token[0]=ch;break;case"/":syn=16;token[0]
12、=ch;break;case';':syn=26;token[0]=ch;break;case'('):syn=27;token[0]=ch;break;case')':syn=28;token[0]=ch;break;case'#':syn=0;token[0]=ch;break;default:syn=-1;}//ch=prog[p++];}fromemit(char*result,char*ag1,char*op,char*ag2){strcpy(quad[count].result,result);strcpy(quad[count].ag
13、1,ag1);strcpy(quad[count].op,op);strcpy(quad[count].ag2,ag2);count++;return;}char*newtemp(){char*p;charm[8];p=(char*)malloc(8);c++;itoa(k,m,10);strcpy(p+1,m);p[0]='t';return(p);}char*factor(void){char*fplace;fplace=(char*)malloc(12);strcpy(fplace,"");if(syn==10){strcpy(fplace,
14、token);scaner();}elseif(syn==11){itoa(sum,fplace,10);scaner()