编译原理实验三 | 您所在的位置:网站首页 › 语义分析算法第一 › 编译原理实验三 |
一、实验目的
采用递归下降语法制导翻译法,对算术表达式、赋值语句进行语义分析并生成四元式序列。 二、实验要求采用递归下降语法制导翻译法,对算术表达式,赋值语句进行语义分析并生成四元式序列。1.实验的输入和输出 输入是语法分析提供的正确的单词串,输出为三地址指令形式的四元式序列。 例如:对于语句串 begin a:=2+3*4;x:=(a+b)/c end# 输出的三地址指令如下: (1) t1=3*4 (2) t2=2+t1 (3) a=t2 (4) t3=a+b (5) t4=t3/c (6) x=t4 三、源程序 #include #include #include #include char prog[80],token[6]; int count=0; char ch; /*当前读入单词*/ int syn,p,m,n,sum,kk=0,k=0; //p/*输入缓冲区指针*/ int length=0; //输入字符长度 int mark=0; /*标记注释//*/ int mark2;// 用于/* */ 注释 char * rwtab[6]={"begin","if","then","while","do","end"}; char *expression(void); struct { char result[8]; char ag1[8]; char op[8]; char ag2[8]; }quad[20]; void emit(char * result,char * ag1,char * op,char * ag2) { strcpy(quad[count].result,result); strcpy(quad[count].ag1,ag1); strcpy(quad[count].op,op); strcpy(quad[count].ag2,ag2); count++; return; } /*词法扫描程序:*/ scaner() { for(n=0;n |
CopyRight 2018-2019 实验室设备网 版权所有 |