?? do.txt
字號:
#define MAX 100
#include<iostream.h>
#include<stdio.h>
#include<string.h>
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<<"輸入程序,以“#”作為結束標志。"<<endl;
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右部出錯"<<endl;return 0;}
}else {cout<<"error 'step'"<<endl;return 0;}
}
int B(int * b,int & o)
{
if(b[o++]==4)
{
if(b[o]==7)
{ o++;
return 1;
}
else {cout<<"until右部出錯"<<endl;return 0;}
}else {cout<<"error 'until'"<<endl;return 0;}
}
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<<"賦值語句右部出錯 "<<endl;return 0;}
}else {cout<<"賦值語句缺少賦值運算符 "<<endl;return 0;}
}else {cout<<"賦值語句左部出錯 "<<endl;return 0;}
}
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)
{
if(S2(a,z))
{
cout<<"succeed!"<<endl;return 1;
}else return 0;
}else {cout<<"error 'do'"<<endl; return 0;}
}else return 0;
}else {cout<<"error 'for'"<<endl; return 0;}
}
void main()
{
cout<<"*************產生式***************"<<endl;// for step until do i j =
cout<<" S ->for S1 do S2"<<endl; // 編號 2 3 4 5 6 7 8
cout<<" S1 ->S2AB"<<endl;
cout<<" S2 ->i=j"<<endl;
cout<<" A ->stepj"<<endl;
cout<<" B ->untilj"<<endl;
int num;
turn=0;
num=buffer()-1;
int x=0;//計識別的單詞的個數
for(;turn<=num;turn++)//總循環,ch存放剛讀入的字符,strtoken[]存放已識別的標志付或保留字,turn是數組str[]的下標
{
ch=GetChar(turn);
ch=GetBC(ch);
if(IsLetter(ch))
{
while(IsLetter(ch)&&turn<=num||IsDigit(ch)&&turn<=num)
{
Concat();
ch=GetChar(++turn);
}
strToken[n]='\0';
ch=NULL;//此ch不是標志符中的符號
turn=turn-1;
kind=Reserve();
record[x]=new Word; record[x]->sort=kind;//12345或6
//cout<<kind; //測試
cout<<"(";
for(int i=0;i<n;i++)
{
record[x]->word[i]=strToken[i];
cout<<record[x]->word[i];//輸出識別的標志符或保留字
}
cout<<","<<kind<<")"<<endl;
record[x]->word[i]='\0';
clear();
x++;
}
else if(IsDigit(ch))
{
while(IsDigit(ch)&&turn<=num)
{
Concat();
ch=GetChar(++turn);
}
ch=NULL;
turn=turn-1;
kind=7;
//////////////
record[x]=new Word;
record[x]->sort=kind;
////////////////
cout<<"(";
for(int i=0;i<n;i++)
{
record[x]->word[i]=strToken[i];
cout<<record[x]->word[i];
}
cout<<","<<kind<<")"<<endl;
record[x]->word[i]='\0';
clear();x++;
}
else if(ch=='=')
{
kind=8;
///////
record[x]=new Word;
record[x]->word[0]='=';
record[x++]->sort=kind;
cout<<"(=,"<<kind<<")"<<endl;
}
else
cout<<"error input!"<<endl;
}
//////////////////////*語法分析*////////////////
//int y;
/*for(y=0;y<x;y++)
{cout<<record[y]->sort<<" ";//打印單詞的編號 。
}cout<<endl;*/
int ana[MAX];//存放詞法分析得到的單詞序列的編號的序列
int m;
for(m=0;m<x;m++)
{
ana[m]=record[m]->sort;//將sort作為數組保存起來
}
/////////語法分析///////
int j=0;
///////////////////制導翻譯//////////////////
if(!S(ana,j)) cout<<"語法出錯!"<<endl;
else
{ cout<<"三地址碼如下:"<<endl;
cout<<"100 ";
int i=0;
while(record[1]->word[i]!='\0')
cout<<record[1]->word[i++];cout<<record[2]->word[0];
i=0;
while(record[3]->word[i]!='\0')
cout<<record[3]->word[i++];cout<<endl;
cout<<"101 goto 103"<<endl;
cout<<"102 ";
i=0;
while(record[1]->word[i]!='\0')
cout<<record[1]->word[i++];cout<<":=";
i=0;
while(record[1]->word[i]!='\0')
cout<<record[1]->word[i++];cout<<"+";
i=0;
while(record[5]->word[i]!='\0')
cout<<record[5]->word[i++];cout<<endl;
cout<<"103 if ";
i=0;
while(record[1]->word[i]!='\0')
cout<<record[1]->word[i++];cout<<"<";
i=0;
while(record[7]->word[i]!='\0')
cout<<record[7]->word[i++];
cout<<" goto 105"<<endl;
cout<<"104 goto 107"<<endl;
cout<<"105 ";
i=0;
while(record[9]->word[i]!='\0')
cout<<record[9]->word[i++];cout<<":=";
i=0;
while(record[11]->word[i]!='\0')
cout<<record[11]->word[i++];cout<<endl;
cout<<"106 goto 102"<<endl;
cout<<"107 end"<<endl;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -