#include \#include \#include \#include %using namespace std;
char prog[100],token[8],ch;
char *rwtab[6]={\int syn,p,m,n,sum,q; int kk;
struct
{
char result1[8]; char ag11[8]; char op1[8]; char ag21[8]; } quad[20];
char *factor(); char *expression();
int yucu(); char *term(); int statement();
int lrparser(); //在原来语法分析的基础上插入相应的语义动作:将输入串翻译成四元式序列 char *newtemp();//该函数回送一个新的临时变量名,临时变量名产生的顺序是T1,T2。。。 void scaner(); //扫描函数获得一个单词
char getch() //读入一个字符 {
ch = prog[p++]; p=p+1; return (ch); }
void emit(char *result,char *ag1,char *op,char *ag2)
//该函数的功能是生成一个三地址语句送到四元式表中 {
strcpy(quad[q].result1,result); strcpy(quad[q].ag11,ag1); strcpy(quad[q].op1,op); strcpy(quad[q].ag21,ag2); q++; }
int main()
{
int j;
q=p=kk=0;
printf(\do
{
scanf(\ prog[p++]=ch; }
while(ch!='#'); p=0; scaner(); lrparser(); if(q>19)
printf(\ else
for (j=0;j printf(\ %s = %s %s %s \\n\\n\ getch(); system(\} int lrparser() { int schain=0; kk=0; if (syn==1) { scaner();//读下一个单词符号 schain=yucu(); //调用语句串分析函数进行分析 if(syn==6) { scaner();//读下一个单词符号 if((syn==0)&&(kk==0)) printf(\ } else { if(kk!=1)printf(\ kk=1; getch(); exit(0); } } else { } return (schain); } int yucu() { int schain=0; schain=statement();//调用语句分析函数进行分析 while(syn==26) { scaner(); schain=statement(); } return (schain); } int statement() { char tt[8],eplace[8]; int schain=0; if (syn==10) { strcpy(tt,token); scaner(); if(syn==18) { scaner(); strcpy(eplace,expression());//调用exprecision进行分析 emit(tt,eplace,\生成四元式送入四元式表 schain=0; } else { printf(\ kk=1; getch(); exit(0); } return (schain); printf(\kk=1; getch(); exit(0); } } char *expression() { char *tp,*ep2,*eplace,*tt; tp=(char *) malloc(12); ep2=(char *) malloc(12); eplace=(char *) malloc(12); tt=(char *) malloc(12); //分配空间 strcpy(eplace,term());//调用term分析产生表达式计算的第一项eplace while((syn==13)||(syn==14)) { if (syn==13) strcpy(tt,\ else strcpy(tt,\ scaner(); strcpy(ep2,term());//调用term分析产生表达式计算的第二项eplace strcpy(tp,newtemp());//调用newtemp产生临时变量tp存储计算结果 emit(tp,eplace,tt,ep2);//生成四元式送入四元式表 strcpy(eplace,tp);//将计算结果作为下一次表达式计算的第一项eplace } return (eplace); } char *term() { char *tp,*ep2,*eplace,*tt; tp=(char *) malloc(12); ep2=(char *) malloc(12); eplace=(char *) malloc(12); tt=(char *) malloc(12); strcpy (eplace,factor()); while((syn==15)||(syn==16)) { if (syn==15) strcpy(tt,\ else strcpy(tt,\ scaner(); strcpy(ep2,factor()); strcpy(tp,newtemp()); emit(tp,eplace,tt,ep2); strcpy(eplace,tp); } return (eplace); } char *factor() { char *fplace; fplace=(char *)malloc(12); strcpy(fplace,\将标识符token的值赋给fplace if(syn==10) { strcpy(fplace,token); scaner();//读下一个单词符号 } else if(syn==11) { itoa(sum,fplace,10); scaner();//读下一个单词符号 } else if(syn==27) { scaner(); fplace=expression();//调用expression分析返回表达式的值 if(syn==28) scaner();//读下一个单词符号 else { printf(\ kk=1; getch(); exit(0); } } else { printf(\ kk=1; getch(); exit(0); } return (fplace); } 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库编译原理实验(语义分析)在线全文阅读。
相关推荐: