資源簡介
算符優先分析法 C++ 編譯原理
運行環境:Visual Studio 2005
#include "SStack.h"
#include
#include
using namespace std;
class Functor
{
private :
char ** table;
string ** production;
string prog;//待分析字符串
int p;//字符指針
int num;//終結符個數
int num1;//產生式個數
SStack stack;
public:
Functor(int m,int n,char ** T,string **prod,string pr)
{
num=m;
num1=n;
table=T;
production=prod;
prog=pr;
p=0;
stack.push('$');
}
void traversal()
{
while(p<(prog.length()))
{
stack.display();
cout<<prog.substr(p)<<" ";
char ch;
if(Getnum(stack.gettop()))
{
ch=stack.gettop();
}
else
{
ch=stack.getsecond();
}
switch(compare(ch,prog[p]))
{
case 1:
case 2:stack.push(prog[p]);p++;cout<<"移入"<<endl;break;
case 3:reduct();cout<<"歸約"<<endl;break;
}
}
cout<<"分析成功!"<<endl;
}
int Getnum(char ch)
{
for(int i=1;i': return 3;break;
case '<': return 2;break;
case '=': return 1;break;
default:cout<<endl<<"輸入串有誤,程序將終止!"<<endl;system("pause");exit(0);break;
}
}
void reduct()
{
//待定
string token="";
int temp;
string str="";
if(!Getnum(stack.gettop()))
{
token+=stack.gettop();
stack.pop();
}
char ch=stack.gettop();
str+=ch;
temp=Haven(str);
if(temp!=-1)
{
token+=production[temp][0];
}
else
{
token+=ch;
}
stack.pop();
bool Nover=true;
while(Nover)
{
if(Getnum(stack.gettop()))
{
if(compare(stack.gettop(),ch)==2)
{
Nover=false;
}
else
{
ch=stack.gettop();
str="";
str+=ch;
temp=Haven(str);
if(temp!=-1)
{
token+=production[temp][0];
}
else
{
token+=ch;
}
stack.pop();
}
}
else
{
token+=stack.gettop();
stack.pop();
}
}
string token2="";

代碼片段和文件信息
#include?
#include?“Functor.h“
#include?
using?namespace?std;
void?main()
{
int?num=7;
int?num1=3;
char?**table;
table=new?char?*[num];
for(int?i=0;i {
table[i]=new?char?[num];
for(int?j=0;j {
table[i][j]=NULL;
}
}
table[0][1]=‘+‘;
table[0][2]=‘*‘;
table[0][3]=‘i‘;
table[0][4]=‘(‘;
table[0][5]=‘)‘;
table[0][6]=‘$‘;
table[1][0]=‘+‘;
table[1][1]=‘>‘;
table[1][2]=‘<‘;
table[1][3]=‘<‘;
table[1][4]=‘<‘;
table[1][5]=‘>‘;
table[1][6]=‘>‘;
table[2][0]=‘*‘;
table[2][1]=‘>‘;
table[2][2]=‘>‘;
table[2][3]=‘<‘;
table[2][4]=‘<‘;
table[2][5]=‘>‘;
table[2][6]=‘>‘;
table[3][0]=‘i‘;
table[3][1]=‘>‘;
table[3][2]=‘>‘;
table[3][5]=‘>‘;
table[3][6]=‘>‘;
table[4][0]=‘(‘;
table[4][1]=‘<‘;
table[4][2]=‘<‘;
table[4][3]=‘<‘;
table[4][4]=‘<‘;
table[4][5]=‘=‘;
table[5][0]=‘)‘;
table[5][1]=‘>‘;
table[5][2]=‘>‘;
table[5][5]=‘>‘;
table[5][6]=‘>‘;
table[6][0]=‘$‘;
table[6][1]=‘<‘;
table[6][2]=‘<‘;
table[6][3]=‘<‘;
table[6][4]=‘<‘;
table[6][6]=‘=‘;
string?**?css;
css=new?string?*?[num1];
for(int?i=0;i {
css[i]=new?string?[6];
for(int?j=0;j<5;j++)
{
css[i][j]=““;
}
}
css[0][0]=“E“;
css[0][1]=“E+T“;
css[0][2]=“T+T“;
css[0][3]=“T“;
css[0][4]=“F+F“;
css[1][0]=“T“;
css[1][1]=“T*F“;
css[1][2]=“F*F“;
css[1][3]=“F“;
css[2][0]=“F“;
css[2][1]=“(E)“;
css[2][2]=“i“;
string?prog;
cout<<“請輸入待分析字符串:“< cin>>prog;
Functor?myFun(numnum1tablecssprog);
myFun.traversal();
system(“pause“);
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1614??2009-05-18?18:05??main.cpp
?????文件???????1342??2009-05-18?18:38??SStack.h
?????文件???????2707??2009-05-18?18:44??Functor.h
-----------?---------??----------?-----??----
?????????????????5663????????????????????3
- 上一篇:內部排序算法的比較分析與實現
- 下一篇:小型編譯器的設計C語言實現
評論
共有 條評論