#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <cmath>
#include <fstream>
#define LEN 6
#define END ";"
#define COL 28
#define ROW 49
#define ACCEPT "ac"
using namespace std;
class Token
{
public:
typedef double Value;
typedef string Property;
string name;
Value sync;
Property property;
Token():sync(0.0){}
Token(const string &s):name(s),property(s),sync(0.0) {}
Token(const char* s):name(s),property(s),sync(0.0) {}
friend bool operator ==(const Token& t1,const Token& t2);
};
bool operator ==(const Token& t1,const Token& t2)
{
return t1.name==t2.name;
}
class LRpda//定义LL语法分析下推自动机
{
typedef vector<Token> Engender;//定义产生式
typedef int State;
typedef string Action;
typedef struct eexpr//定义表达式
{
Token uToken;
Engender engender;
}eExpr;
public:
LRpda();
LRpda(const char* filename);
bool in_propertys(Token::Property pro,vector<Token::Property> &pVec);
bool is_unTerminateToken(const Token& token);//判断是否为非终结符
bool is_TerminateToken(const Token& token);//判断是否为终结符
bool StringToEngender(const char *exprtmp,Engender &engender);
bool is_delim(char ch);
bool is_operator(char ch);
bool is_letter(char ch);
bool is_num(const string& str);
bool is_num(const Token& token) {}
bool is_id(const string& str);
double get_digit(char *&exprtmp);
string get_operator(char *&exprtmp);
void get_propertys(const char* str,vector<Token::Property> &pVec);
Token::Property get_property(const string& str);
void combine_token(int index);
void addExpr(const Token& token,const Engender& engender);//添加产生式
void addExpr(const Token& token,const string& engender);
void addType(const string type);
string LRpda::der_operate(const Token& t1,const string& d1,const string& op,const Token& t2,const string& d2);
Action ACTION(const State state,const Token& ttoken);//返回 LL语法分析表中行为uToken,列为tToken中的元素
Action GOTO(const State state,const Token& utoken);
bool Execute(const char * exprtmp);//执行PDA
bool Caculate(const string& exprtmp);//执行PDA
bool Derivative(const string& exprtmp);
private:
State start; //开始符号
Token end; //结束符号
vector<Token::Property> uTokens;//非终结符号集
vector<Token::Property> tTokens;//终结符号集
vector<Token::Property> TYPE;
vector<Token::Property> Prefix;
vector<Token::Property> KeyWords;
vector<string> Error;
vector<eExpr> eExprs;//表达式集合
stack<Token> tStack;
stack<State> sStack;
stack<Token::Value> vStack;
stack<string> dStack;
unsigned int uTokenCount;//非终结符号数量
unsigned int tTokenCount;//终结符号数量
string matrix[ROW][COL];//LR语法分析表
};
LRpda::LRpda()
{
//ext stat const TYPE id num * & = ( ) [ ] , ; M E S C T D F A L I I'
string copymatrix[ROW][COL]={ {"s3","r3","r3","r3","e0","r3","r3","r3","r3","r3","r3","r3","r3","r3","r3","1","2","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //0
{"e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","ac","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //1
{"r5","s5","r5","r5","r5","r5","r5","r5","r5","r5","r5","r5","r5","r5","r5","-1","-1","4","-1","-1","-1","-1","-1","-1","-1","-1"}, //2
{"r2","r2","r2","r2","r2","r2","r2","r2","r2","r2","r2","r2","r2","r2","r2","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //3
{"r7","r7","s7","r7","r7","r7","r7","r7","r7","r7","r7","r7","r7","r7","r7","-1","-1","-1","6","-1","-1","-1","-1","-1","-1","-1"}, //4
{"r4","r4","r4","r4","r4","r4","r4","r4","r4","r4","r4","r4","r4","r4","r4","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //5
{"e0","e0","e0","s8","e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //6
{"r6","r6","r6","r6","r6","r6","r6","r6","r6","r6","r6","r6","r6","r6","r6","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //7
{"e0","e0","r7","e0","s12","e0","r7","r7","e0","s13","e0","e0","e0","e0","e0","-1","-1","-1","10","-1","9","-1","-1","-1","11","-1"}, //8
{"r24","r24","r24","r24","e0","r24","r24","r24","r24","s16","r1","r24","r24","s15","r1","-1","-1","-1","-1","-1","-1","14","-1","-1","-1","-1"}, //9
{"e0","e0","e0","e0","e0","e0","s41","s42","e0","e0","e0","e0","e0","e0","e0","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //10
{"r13","r13","r13","r13","r13","r13","r13","r13","r13","r13","r13","r13","r13","r13","r13","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //11
{"r22","r22","r22","r22","r22","e0","e0","e0","s26","s32","r22","r22","r22","r22","r22","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","25"}, //12
{"e0","e0","s7","e0","s12","e0","r7","r7","e0","s13","e0","e0","e0","e0","e0","-1","-1","-1","10","-1","23","-1","-1","-1","11","-1"}, //13
{"r29","r29","r29","r29","r29","r29","r29","r29","r29","r29","r29","s18","r29","r29","r29","-1","-1","-1","-1","-1","-1","-1","17","-1","-1","-1"}, //14
{"r7","r7","r7","r7","s12","r7","r7","r7","r7","s13","r7","r7","r7","r7","r7","-1","-1","-1","10","-1","22","-1","-1","-1","11","-1"}, //15
{"r3","r3","r3","r3","r3","r3","r3","r3","r3","r3","r27","r3","r3","r3","r3","37","2","-1","-1","-1","-1","-1","-1","36","-1","-1"}, //16
{"r11","r11","r11","r11","r11","r11","r11","r11","r11","r11","r11","r11","r11","r11","r11","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //17
{"e0","e0","e0","e0","s46","s19","e0","e0","e0","e0","e0","e0","e0","e0","e0","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //18
{"e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","s20","e0","e0","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //19
{"r29","r29","r29","r29","r29","r29","r29","r29","r29","r29","r29","s18","r29","r29","r29","-1","-1","-1","-1","-1","-1","-1","21","-1","-1","-1"}, //20
{"r28","r28","r28","r28","r28","r28","r28","r28","r28","r28","r28","r28","r28","r28","r28","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //21
{"r24","r24","r24","r24","r24","r24","r24","r24","r24","s16","r24","r24","r24","s15","r12","-1","-1","-1","-1","-1","-1","14","-1","-1","-1","-1"}, //22
{"r24","r24","r24","r24","e0","r24","r24","r24","r24","s16","s24","r24","r24","e0","e0","-1","-1","-1","-1","-1","-1","14","-1","-1","-1","-1"}, //23
{"r8","r8","r8","r8","r8","r8","r8","r8","r8","r8","r8","r8","r8","r8","e0","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //24
{"r15","r15","r15","r15","r15","r15","r15","r15","r15","r15","r15","r15","r15","r15","r15","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //25
{"e0","e0","e0","e0","s45","s31","s27","s29","e0","s32","e0","e0","e0","e0","e0","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","33"}, //26
{"e0","e0","e0","e0","s28","e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //27
{"r17","r17","r17","r17","r17","r17","r17","r17","r17","r17","r17","r17","r17","r17","r17","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //28
{"e0","e0","e0","e0","s30","e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //29
{"r18","r18","r18","r18","r18","r18","r18","r18","r18","r18","r18","r18","r18","r18","r18","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //30
{"r20","r20","r20","r20","r20","r20","r20","r20","r20","r20","r20","r20","r20","r20","r20","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //31
{"e0","e0","e0","e0","s45","s31","s27","s29","e0","s32","e0","e0","e0","e0","e0","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","34"}, //32
{"r16","r16","r16","r16","r16","r16","r16","r16","r16","r16","r16","r16","r16","r16","r16","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //33
{"e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","s35","e0","e0","e0","e0","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //34
{"r19","r19","r19","r19","r19","r19","r19","r19","r19","r19","r19","r19","r19","r19","r19","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //35
{"e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","s38","e0","e0","e0","e0","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //36
{"r25","r25","r25","r25","r25","r25","r25","r25","r25","r25","r25","r25","r25","s39","r25","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //37
{"r23","r23","r23","r23","r23","r23","r23","r23","r23","r23","r23","r23","r23","r23","r23","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //38
{"r3","r3","r3","r3","r3","r3","r3","r3","r3","r3","r3","r3","r3","r3","r3","37","2","-1","10","-1","-1","-1","-1","40","11","-1"}, //39
{"r26","r26","r26","r26","r26","r26","r26","r26","r26","r26","r26","r26","r26","r26","r26","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //40
{"e0","e0","s7","r7","s12","r7","r7","r7","r7","s13","r14","r7","r7","r14","r7","-1","-1","-1","10","-1","43","-1","-1","-1","11","-1"}, //41
{"e0","e0","s7","r7","s12","r7","r7","r7","r7","s13","r14","r7","r7","r14","r7","-1","-1","-1","10","-1","44","-1","-1","-1","11","-1"}, //42
{"r9","r9","r9","r9","r9","r9","r9","r9","r9","r9","r9","r9","r9","r9","r9","-1","-1","-1","-1","-1","14","-1","-1","-1","-1","-1"}, //43
{"r10","r10","r10","r10","r10","r10","r10","r10","r10","r10","r10","r10","r10","r10","r10","-1","-1","-1","-1","-1","14","-1","-1","-1","-1","-1"}, //44
{"r21","r21","r21","r21","r21","r21","r21","r21","r21","r21","r21","r21","r21","r21","r21","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //45
{"e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","e0","s47","e0","e0","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //46
{"r29","r29","r29","r29","r29","r29","r29","r29","r29","r29","r29","s18","r29","r29","r29","-1","-1","-1","-1","-1","-1","-1","48","-1","-1","-1"}, //47
{"r30","r30","r30","r30","r30","r30","r30","r30","r30","r30","r30","r30","r30","r30","r30","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1","-1"}, //48
};
Token tTmp;
string sTmp;
int i,j;
for(i=0;i<ROW;++i)
for(j=0;j<COL;++j)
matrix[i][j]=copymatrix[i][j];
get_propertys("extern static const type id num * & = ( ) [ ] , ;",tTokens);
get_propertys("M E S C T D F A L I I'",uTokens);
get_propertys("int float char void",TYPE);
get_propertys("extern static const",Prefix);
get_propertys("return if while class struct default: typedef",KeyWords);
uTokenCount=uTokens.size();
tTokenCount=tTokens.size();
start=0;
end=END;
addExpr("M'","M ;"); //0
addExpr("M","E S C T D"); //1
addExpr("E","extern");
addExpr("E","");
addExpr("S","static");
addExpr("S","");
addExpr("C","const");
addExpr("C","");
addExpr("D","( D )"); //2
addExpr("D","C * D"); //3
addExpr("D","C & D");
addExpr("D","D F A"); //4
addExpr("D","D , D");
addExpr("D","I");
addExpr("D","");
addExpr("I","id I'");
addExpr("I'","= I'");
addExpr("I'","* id");
addExpr("I'","& id");
addExpr("I'","( I' )");
addExpr("I'","num");
addExpr("I'","id");
addExpr("I'","");
addExpr("F","( L )"); //5
addExpr("F",""); //6
addExpr("L","M"); //7
addExpr("L","M , L"); //8
addExpr("L",""); //9
addExpr("A","[ num ] A"); //9
addExpr("A",""); //9
addExpr("A","[ id ] A");
Error.push_back("语法错误!");
}
void LRpda::get_propertys(const char* str,vector<Token::Property> &pVec)
{
stringstream ss(str);
string sTmp;
while( ss>>sTmp)
pVec.push_back(sTmp);
}
bool LRpda::in_propertys(Token::Property pro,vector<Token::Property> &pVec)
{
int i,size=pVec.size();
for(i=0;i<size;++i)
{
if(pVec[i]==pro)
return true;
}
return false;
}
Token::Property LRpda::get_property(const string& str)
{
if(in_propertys(str,TYPE))
return "type";
else if(str==",")
return ",";
else if(in_propertys(str,Prefix))
return str;
else if(is_num(str))
return "num";
else if(is_id(str))
return "id";
else if(str==")")
return ")";
else if(str=="(")
return "(";
else if(str=="[")
return "[";
else if(str=="]")
return "]";
else if(str=="*")
return "*";
else if(str=="&")
return "&";
else if(str=="=")
return "=";
else if(str==END)
return END;
else
return "";
}
void LRpda::combine_token(int index)
{
Token tokens[LEN],token;
int i,len;
if(index>=eExprs.size())
return;
len=eExprs[index].engender.size();
if(len>tStack.size()||len>=LEN)
return;
if(len<1)
{
token.name="";
}
else
{
for(i=0;i<len;++i)
{
tokens[i]=tStack.top();
tStack.pop();
}
for(i=len-1;i>=0;--i)
{
token.name=token.name+tokens[i].name;
}
}
tStack.push(token);
}
bool LRpda::is_num(const string& str)
{
int state=0;
char ch;
bool flag=1;
const char *exprtmp=str.c_str();
while((ch=*exprtmp)!=0 && flag)
{
switch(state)
{
case 0:
if(ch=='+' || ch=='-')
state=2;
else if(isdigit(ch))
state=5;
else
flag=0;
break;
case 1:
if(ch=='+' || ch=='-')
state=4;
else
flag=0;
break;
case 2:
if(isdigit(ch))
state=5;
else
flag=0;
break;
case 3:
if(isdigit(ch))
state=6;
else
flag=0;
break;
case 4:
if(isdigit(ch))
state=7;
else
flag=0;
break;
case 5:
if(isdigit(ch))
state=5;
else if(ch=='.')
state=3;
else if(ch=='e' || ch=='E')
state=1;
else
flag=0;
break;
case 6:
if(isdigit(ch))
state=6;
else if(ch=='e' || ch=='E')
state=1;
else
flag=0;
break;
case 7:
if(isdigit(ch))
state=7;
else
flag=0;
break;
default: break;
}
++exprtmp;
}
if(flag==1 && (state==5 || state==6 || state==7))
return true;
return false;
}
bool LRpda::is_letter(char ch)
{
if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z')||ch=='_'||ch==':'||ch=='<'||ch=='>'||ch==','||ch==' ')
return true;
return false;
}
bool LRpda::is_id(const string &str)
{
int state=0;
char ch;
bool flag=1;
const char *exprtmp=str.c_str();
while((ch=*exprtmp)!=0 && flag)
{
switch(state)
{
case 0:
if(is_letter(ch))
state=1;
else
flag=0;
break;
case 1:
if(is_letter(ch)||isdigit(ch))
state=1;
else
flag=0;
break;
default: break;
}
++exprtmp;
}
if(flag==1 && state==1)
return true;
return false;
}
double LRpda::get_digit(char *&exprtmp)
{
int state=0;
char ch;
string digit;
double resault=0.0;
bool flag=1;
while((ch=*exprtmp)!=0 && flag)
{
digit.push_back(ch);
switch(state)
{
case 0:
if(is_letter(ch))
state=1;
else
flag=0;
break;
case 1:
if(is_letter(ch)||isdigit(ch))
state=1;
else
flag=0;
break;
default: break;
}
++exprtmp;
}
if(flag==0)
--exprtmp;
if(state==1)
sscanf(digit.c_str(),"%lf",&resault);
return resault;
}
bool LRpda::is_delim(char ch)
{
return (ch==' '||ch=='\n' ||ch=='\t');
}
bool LRpda::StringToEngender(const char *expr,Engender &engender)
{
string sTmp;
char ch;
Token token;
int flag=1;
bool type_first=0;
int bracecounts=0;
bool brace_first=0;
while(is_delim(*expr))
++expr;
if(*expr=='\0')
return 0;
while((ch=*expr)!='\0')
{
if(ch=='<' )
{
brace_first=1;
}
if(brace_first==0)
{
if(ch==';')
{
token.name=sTmp;
token.property=get_property(sTmp);
if(type_first==0)
{
if(token.property=="type")
type_first=1;
else if(token.property=="id"&&!in_propertys(token.name,KeyWords))
{
token.property="type";
type_first=1;
}
}
if(token.property=="")
{
return 0;
}
sTmp.clear();
engender.push_back(token);
sTmp.push_back(ch);
break;
}
else if(ch=='(' || ch==')' ||ch=='[' ||ch==']' ||ch==',' ||ch=='*' ||ch=='&' ||ch=='=' ||ch==';' )
{
if(sTmp.size()!=0)
{
token.name=sTmp;
token.property=get_property(sTmp);
if(type_first==0)
{
if(token.property=="type")
type_first=1;
else if(token.property=="id"&&!in_propertys(token.name,KeyWords))
{
token.property="type";
type_first=1;
}
}
if(token.property=="")
{
return 0;
}
sTmp.clear();
engender.push_back(token);
}
++expr;
sTmp.push_back(ch);
flag=1;
}
else if(is_delim(ch))
{
if(sTmp.size()!=0)
{
token.name=sTmp;
token.property=get_property(sTmp);
if(type_first==0)
{
if(token.property=="type")
type_first=1;
else if(token.property=="id"&&!in_propertys(token.name,KeyWords))
{
token.property="type";
type_first=1;
}
}
if(token.property=="")
{
return 0;
}
sTmp.clear();
engender.push_back(token);
}
++expr;
}
else if(flag)
{
if(sTmp.size()!=0)
{
token.name=sTmp;
token.property=get_property(sTmp);
if(type_first==0)
{
if(token.property=="type")
type_first=1;
else if(token.property=="id"&&!in_propertys(token.name,KeyWords))
{
token.property="type";
type_first=1;
}
}
if(token.property=="")
{
return 0;
}
sTmp.clear();
engender.push_back(token);
}
++expr;
sTmp.push_back(ch);
flag=0;
}
else
{
sTmp.push_back(ch);
++expr;
}
}
else
{
if(ch=='<' )
{
++bracecounts;
}
else if(ch=='>')
{
--bracecounts;
if(bracecounts==0)
{
brace_first=0;
}
}
sTmp.push_back(ch);
++expr;
}
}
if(sTmp.size()!=0)
{
token.name=sTmp;
token.property=get_property(sTmp);
if(token.property=="")
{
return 0;
}
engender.push_back(token);
}
return 1;
}
bool LRpda::Execute(const char * exprtmp)
{
State state;
Action action,actiontmp;
Token token,token1,token2,token3,token4,token5,token0;
Engender expr;
int index=0,enlen,cur;
string sTmp;
int expr_len=0;
string suffix;
int para_num=0;
char sbuf[128]={};
string var;
bool cur8=0;
if(StringToEngender(exprtmp,expr)==0)
{
return 0;
}
expr_len=expr.size();
state=start;
token=expr[index++];
sStack.push(state);
while(1)
{
action=ACTION(state,token);
if(state==12)
{
if(index<expr_len)
{
token1=expr[index];
if(in_propertys(token1.name,TYPE))
action="r22";
}
}
else if(state==9)
{
if(index<expr_len)
{
if(token.name==",")
{
token1=expr[index];
if(in_propertys(token1.name,TYPE)||token1.name=="const")
action="r1";
}
}
}
else if(state==8)
{
if(index<expr_len)
{
if(token.name=="(")
{
token1=expr[index];
if(in_propertys(token1.name,TYPE)||token1.name=="const")
action="r14";
}
if(token.name==",")
{
token1=expr[index];
if(in_propertys(token1.name,TYPE)||token1.name=="const")
action="r14";
}
}
}
else if(state==41||state==42)
{
if(index<expr_len)
{
if(token.name=="(")
{
token1=expr[index];
if(in_propertys(token1.name,TYPE)||token1.name=="const"||token1.name==")")
action="r14";
}
}
}
if(action[0]=='e')
{
actiontmp=action.substr(1,action.size());
state=atoi(actiontmp.c_str());
return 0;
}
else if(action[0]=='s')
{
actiontmp=action.substr(1,action.size());
state=atoi(actiontmp.c_str());
sStack.push(state);
tStack.push(token);
if(index >=expr_len)
{
return 0;
}
token=expr[index++];
}
else if(action[0]=='r')
{
actiontmp=action.substr(1,action.size());
cur=atoi(actiontmp.c_str());
switch(cur)
{
case 8:
cur8=1;
break;
case 15:
token2=tStack.top();
tStack.pop();
token1=tStack.top();
tStack.pop();
break;
case 23:
if(cur8==0)
return 0;
break;
}
combine_token(cur);
enlen=eExprs[cur].engender.size();
while(enlen)
{
sStack.pop();
--enlen;
}
state=sStack.top();
action=GOTO(state,eExprs[cur].uToken);
state=atoi(action.c_str());
if(state==-1)
{
return 0;
}
sStack.push(state);
}
else if(action==ACCEPT)
{
return 1;
}
else
{
return 0;
}
}
}
void LRpda::addExpr(const Token& token,const string& engender)
{
stringstream enStream(engender);
Token tTmp;
eExpr eTmp;
string sTmp;
eTmp.uToken=token;
while(enStream>>sTmp)
{
tTmp.name=sTmp;
tTmp.property=sTmp;
eTmp.engender.push_back(tTmp);
}
eExprs.push_back(eTmp);
}
void LRpda::addType(const string type)
{
TYPE.push_back(type);
}
LRpda::Action LRpda::ACTION(const State state,const Token& ttoken)
{
int j;
for(j=0;j<tTokenCount;++j)
if(tTokens[j]==ttoken.property)
break;
if(j>=tTokenCount)
{
return "e0";
}
return matrix[state][j];
}
LRpda::Action LRpda::GOTO(const State state,const Token& utoken)
{
int j;
for(j=0;j<uTokenCount;++j)
if(uTokens[j]==utoken.property)
break;
if(j>=uTokenCount)
{
return "e0";
}
return matrix[state][tTokenCount+j];
}
void treate_code(string& code)
{
string::iterator it,del_begin,del_end;
int bracecounts=0;
for(it=code.begin();it!=code.end();)
{
if(*it=='{' )
{
if(bracecounts==0)
{
del_begin=it;
}
++bracecounts;
++it;
}
else if(*it=='}')
{
--bracecounts;
++it;
if(bracecounts==0)
{
del_end=it;
it=code.erase(del_begin,del_end);
}
}
else
++it;
}
}
void analyze(const char * filename1,char * filename2)
{
ifstream fin(filename1);
ofstream fout(filename2);
string stmp,code;
string::iterator it;
LRpda lrpda;
if(fin.fail())
{
cout<<"无法打开文件"<<filename1<<endl;
return;
}
if(fout.fail())
{
cout<<"无法打开文件"<<filename2<<endl;
return;
}
while(!fin.eof())
{
getline(fin,stmp);
code+=stmp+"\n";
}
for(it=code.begin();it!=code.end();++it)
{
if(*it!='\n')
stmp.push_back(*it);
else
{
if(lrpda.Execute(stmp.c_str()))
{
cout<<stmp<<endl;
fout<<stmp<<endl;
}
stmp.clear();
}
}
fin.close();
fout.close();
}
void menu(void)
{
LRpda lrpda;
string expr;
char *filename="resault.txt";
while(1)
{
cout<<"请输入代码文件(直接按回车退出): "<<endl;
getline(cin,expr);
if(expr.size()==0)
{
cout<<"谢谢使用,再见!"<<endl;
return;
}
cout<<"变量如下:"<<endl;
analyze(expr.c_str(),filename);
cout<<"结果保存在"<<filename<<endl;
expr.clear();
}
}
int main(void)
{
menu();
getchar();
return 0;
}