语法制导翻译

  • 格式:doc
  • 大小:29.00 KB
  • 文档页数:8

下载文档原格式

  / 22
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

for循环语句翻译递归下降法输出三地址码/////////////

#define MAX 100

#include

#include

#include

char str[MAX];

char ch;

int turn;

char strToken[MAX];

int kind;

int n=0;//存放strtoken[]元素的个数

struct Word//结构体存放单词

{

int sort;

char word[MAX];//存放strtoken[]的内容

};

//record[x]=new Word;

Word *record[12];//放所有识别出来的单词,分别存放他们的编号以及字符串,x是其下标

////////////////////词法分析///////////////////////

int buffer()//载入

{

int i=0;

cout<<"输入程序,以“#”作为结束标志。"<

for(int n=0;n<=MAX;n++)

{

for(;i<=MAX;i++)

{

scanf("%c",&str[i]);

/////////////cin>>str[i]不可用,用C语言读入字符。

if(str[i]=='#')

break;///////如果尾数为识别码#,则表示程序读完,跳出循环.

}

break;

}

return(i);

bool IsLetter(char ch)///////////判断是否是字母

{

if(ch>=65&&ch<=90||ch>=97&&ch<=122)

return(true);

else

return(false);

}

bool IsDigit(char ch)//////////判断是否是数字

{

if(ch>=48&&ch<=57)

return(true);

else

return(false);

}

char GetChar(int i)///////读取字符

{

char ch;

ch=str[i];

return(ch);

}

char GetBC(char ch)////判断是不是空格或者换行,如果是,直接读取下一个字符直道不再空白为止{

if(ch==32||ch==10)

{

turn++;

ch=GetChar(turn);

ch=GetBC(ch);/////////递归实现

return(ch);

}

else

return(ch);

}

void Concat()/////////////连接,即为strtoken[]赋值

{

strToken[n]=ch;

n++;

int Reserve()/////以单词为单位查找保留字,是则返回编码,不是则返回0,用来区分标志符和保留字{

if(strcmp(strToken," DIM\0")==0)///////调用strcmp函数实现,

return(1);

else if(strcmp(strToken,"for\0")==0)

return(2);

else if(strcmp(strToken,"step\0")==0)

return(3);

else if(strcmp(strToken,"until\0")==0)

return(4);

else if(strcmp(strToken,"do\0")==0)

return(5);

else

return(6);

}

void clear()

{

n=0;

}

/////////////*语法递归分析*/////////////////

int A(int * c,int & q)

{

if(c[q++]==3)

{

if(c[q]==7)

{ q++;

return 1;

}

else {cout<<"step右部出错"<

}else {cout<<"error 'step'"<

}

int B(int * b,int & o)

if(b[o++]==4)

{

if(b[o]==7)

{ o++;

return 1;

}

else {cout<<"until右部出错"<

}else {cout<<"error 'until'"<

}

int S2(int * d,int & h)

{

if(d[h++]==6)

{

if(d[h++]==8)

{

if((d[h]==6||d[h]==7)) {h++; return 1;}

else {cout<<"赋值语句右部出错"<

}else {cout<<"赋值语句缺少赋值运算符"<

}

int S1(int * m,int & n)

{

if(S2(m,n))

{

if(A(m,n))

{

if(B(m,n)) return 1;

else return 0;

}else return 0;

}else return 0;

}

int S(int *a,int & z)

{

if (a[z++]==2)

{

if (S1(a,z))

{

if(a[z++]==5)

{