stack.Push(sheet[17]);
cout <<“push(var)“;
break;
case 18:
stack.Push(sheet[18]);
cout <<“push(con)“;
break;
case 19:
stack.Push(sheet[18]);
cout <<“push(con)“;
break;
case 20:
stack.Push(sheet[18]);
cout <<“push(con)“;
break;
default:
cout <<“error : NO PATTERN FOR H!\n“;
flag = 0;
break;
}
}
void I_P(node *sheet, node *a, my_stack<node>&stack){
cout <<“pop(I),“;
switch (a[ip].id){
case 8:
break;
case 9:
break;
case 11:
break;
case 12:
break;
case 13:
break;
case 14:
break;
case 16:
stack.Push(H);
stack.Push(sheet[16]);
cout <<“push(^H)“;
break;
default:
cout <<“error : NO PATTERN FOR I!\n“;
flag = 0;
break;
}
}
void J_P(node *sheet, node *a, my_stack<node>&stack){
cout <<“pop(J),“;
switch (a[ip].id){
case 8:
stack.Push(sheet[8]);
cout <<“push())“;
break;
case 22:
stack.Push(sheet[8]);
stack.Push(H);
stack.Push(sheet[22]);
cout <<“push(,H))“;
default:
cout <<“error : NO PATTERN FOR J!\n“;
flag = 0;
break;
}
}
string show(node *a){
string s;
for (int i = ip; i < N; i++)
{
s += a[i].text;
}
return s;
}
void ana(my_stack<node>&stack, node *a){
int i = 0;
while (flag){
p = stack.Top().index;//当前指针
i++;
cout << right << setw(3)<< i <<“)|“;
cout << left << setw(20)<< stack.Show();
cout << left << setw(60)<< show(a);
switch (stack.Top().id){
case 23:
stack.Pop();
S_P(sheet, a, stack);
break;
case 24:
stack.Pop();
A_P(sheet, a, stack);
break;
case 25:
stack.Pop();
B_P(sheet, a, stack);
break;
case 26:
stack.Pop();
C_P(sheet, a, stack);
break;
case 27:
stack.Pop();
D_P(sheet, a, stack);
break;
case 28:
stack.Pop();
E_P(sheet, a, stack);
break;
case 29:
stack.Pop();
F_P(sheet, a, stack);
break;
case 30:
stack.Pop();
G_P(sheet, a, stack);
break;
case 31:
stack.Pop();
H_P(sheet, a, stack);
break;
case 32:
stack.Pop();
I_P(sheet, a, stack);
break;
case 33:
stack.Pop();
J_P(sheet, a, stack);
break;
default:
if (stack.Top().id == a[ip].id){
if (stack.Top().id == 17)
stack.Top().index->text = a[ip].text;
else if (stack.Top().id == 18)
{
if (a[ip].text ==“PI“)
{
stack.Top().index->text = a[ip].text;
stack.Top().index->val = 3.141592;
}
else if (a[ip].text ==“E“)
{
stack.Top().index->text = a[ip].text;
stack.Top().index->val = 2.718281;
}
else {
stack.Top().index->text = a[ip].text;
stack.Top().index->val = stod(a[ip].text);
}
}
ip++;
if(stack.Top().id<23&& stack.Top().id>=0)
cout <<“pop(“<< sheet[stack.Top().id].text <<“),“;
else
cout <<“pop(“<< stack.Top().text <<“),“;
stack.Pop();
cout <<“next(ip)“;
if (stack.Empty()) cout <<“\nSuccess\n“;
}
else {
cout <<“\nerror: Fail!\n“;
flag = 0;
break;
}
}
cout << endl;
if (stack.Empty()) break;
if (n>0)
{p->child = stack.Top(0).index;
node *temp = p->child->index;
for (int i = 1; i < n; i++)
{
temp->bro = stack.Top(i).index;
temp = temp->bro;
}
}
n = 0;
}
}
/********************************************************************/
/*语义分析*/
void semanti_ana(my_stack<node>&stack, node *s)//主体部分,对后序遍历得到的后缀树进行栈操作,得出最后结果,放在后序遍历循环体中
{
string out=““;
bool hasundefine=false;
if (s->id >= 0 && s->id < 19)
{
if (s->id == 10)
{
cout << left << setw(40)<<“?“;
cout <<“Return Top[0].val“;
cout << endl;
cout <<“结果:“;
if (hasundefine)
cout << s;
else
cout<<setw(40)<<stack.show();
}
else if (s->id == 0)
{
if (s->index->hasval)
stack.Top().index->val = sin(stack.Top().index->val);
else
{
out+=“sin(“+ stack.Top().index->text +“)“;
stack.Top().index->text = out;
stack.Top().index->hasval = false;
}
cout << setw(40)<< stack.show();
cout <<“Top[0].text =“<< stack.Top().index->text;
cout <<“,Top[0].val=sin(Top[0].val)“;
}
else if (s->id == 1)
{
if (s->index->hasval)
stack.Top().index->val = cos(stack.Top().index->val);
else
{
out =“cos(“+ stack.Top().index->text +“)“;
stack.Top().index->text = out;
stack.Top().index->hasval = false;
}
cout<<setw(40)<<stack.show();
cout <<“Top[0].text =“<< stack.Top().index->text;
cout <<“,Top[0].val=cos(Top[0].val)“;
}
else if (s->id == 2)
{
if (s->index->hasval)
stack.Top().index->val = tan(stack.Top().index->val);
else
{
out =“tan(“+ stack.Top().index->text +“)“;
stack.Top().index->text = out;
stack.Top().index->hasval = false;
}
cout<<setw(40)<<stack.show();
cout <<“Top[0].text =“<< stack.Top().index->text;
cout <<“,Top[0].val=tg(Top[0].val)“;
}
else if (s->id == 3)
{
if (s->index->hasval)
stack.Top().index->val = tan(1.57-stack.Top().index->val);
else
{
out =“cot(“+ stack.Top().index->text +“)“;
stack.Top().index->text = out;
stack.Top().index->hasval = false;
}
cout<<setw(40)<<stack.show();
cout <<“Top[0].text =“<< stack.Top().index->text;
cout <<“,Top[0].val=ctg(Top[0].val)“;
}
else if (s->id == 4)
{
if (s->index->hasval)
stack.Top().index->val = log(stack.Top().index->val);
else
{
out =“log(“+ stack.Top().index->text +“)“;
stack.Top().index->text = out;
stack.Top().index->hasval = false;
}
cout<<setw(40)<<stack.show();
cout <<“Top[0].text =“<< stack.Top().index->text;
cout <<“,Top[0].val=log(Top[0].val)“;
}
else if (s->id == 5)
{
if (s->index->hasval)
stack.Top().index->val = log10(stack.Top().index->val);
else
{
out =“lg(“+ stack.Top().index->text +“)“;
stack.Top().index->text = out;
stack.Top().index->hasval = false;
}
cout<<setw(40)<<stack.show();
cout <<“Top[0].text =“<< stack.Top().index->text;
cout <<“,Top[0].val=lg(Top[0].val)“;
}
else if (s->id == 6)
{
if (s->index->hasval)
stack.Top().index->val = log(stack.Top().index->val);
else
{
out =“ln(“+ stack.Top().index->text +“)“;
stack.Top().index->text = out;
stack.Top().index->hasval = false;
}
cout<<setw(40)<<stack.show();
cout <<“Top[0].text =“<< stack.Top().index->text;
cout <<“,Top[0].val=ln(Top[0].val)“;
}
else if (s->id == 11)
{
if (stack.Top(0).index->hasval&&stack.Top(1).index->hasval)
{
double temp = stack.Top().index->val;
stack.Pop();
stack.Top().index->val += temp;
stack.Top().index->text = to_string(stack.Top().index->val);
}
else {
out =“(“+ stack.Top(1).index->text +“+“+stack.Top(0).index->text +“)“;
stack.Top(1).index->text = out;
stack.Top(1).index->hasval = false;
stack.Pop();
}
cout<<setw(40)<<stack.show();
cout <<“Top[1].val+=Top[0].val , Pop()“;
}
else if (s->id == 12)
{
if (stack.Top(0).index->hasval&&stack.Top(1).index->hasval)
{
double temp = stack.Top().index->val;
stack.Pop();
stack.Top().index->val -= temp;
stack.Top().index->text = to_string(stack.Top().index->val);
}
else {
out =“(“+ stack.Top(1).index->text +“-“+ stack.Top(0).index->text +“)“;
stack.Top(1).index->text = out;
stack.Top(1).index->hasval = false;
stack.Pop();
}
cout<<setw(40)<<stack.show();
cout <<“Top[1].val-=Top[0].val , Pop()“;
}
else if (s->id == 13)
{
if (stack.Top(0).index->hasval&&stack.Top(1).index->hasval)
{
double temp = stack.Top().index->val;
stack.Pop();
stack.Top().index->val *= temp;
stack.Top().index->text = to_string(stack.Top().index->val);
}
else {
out = stack.Top(1).index->text +“*“+ stack.Top(0).index->text ;
stack.Top(1).index->text = out;
stack.Top(1).index->hasval = false;
stack.Pop();
}
cout<<setw(40)<<stack.show();
cout <<“Top[1].val*=Top[0].val , Pop()“;
}
else if (s->id == 14)
{
if (stack.Top(0).index->hasval&&stack.Top(1).index->hasval)
{
double temp = stack.Top().index->val;
stack.Pop();
stack.Top().index->val /= temp;
stack.Top().index->text = to_string(stack.Top().index->val);
}
else {
out = stack.Top(1).index->text +“/“+ stack.Top(0).index->text;
stack.Top(1).index->text = out;
stack.Top(1).index->hasval = false;
stack.Pop();
}
cout<<setw(40)<<stack.show();
cout <<“Top[1].val/=Top[0].val , Pop()“;
}
else if (s->id == 15)
{
var[top].val = stack.Top().index->val;
stack.Push(*s);
cout<<setw(40)<<stack.show();
stack.Pop();
cout <<“Variable[top].val =“<< stack.Top().index->val;
stack.Pop();
top++;
}
else if (s->id == 16)
{
if (stack.Top(0).index->hasval&&stack.Top(1).index->hasval){
double temp = stack.Top().index->val;
stack.Pop();
stack.Top().index->val = pow(stack.Top().index->val, temp);
stack.Top().index->text = to_string(stack.Top().index->val);
}
else {
out = stack.Top(1).index->text +“^“+ stack.Top(0).index->text;
stack.Top(1).index->text = out;
stack.Top(1).index->hasval = false;
stack.Pop();
}
cout<<setw(40)<<stack.show();
cout <<“Top[1].val=Top[1].val^Top[0].val , Pop()“;
}
else if (s->id == 18)
{
stack.Push(*s);
cout<<setw(40)<<stack.show();
cout<<“Push(“<<stack.Top().index->val<<“)“;
}
else if (s->id == 17)
{
if (var[top - 1].name ==““)
{
var[top - 1].name = s->index->text;
string temp= to_string(var[top - 1].val)+“=“+ s->index->text ;
cout << setw(40)<< temp;
cout <<“Variable[Top].text =“<< s->index->text<<“,Pop(3)“;
}
else
{
if (find_var(s->index->text)>= 0)
{
s->index->val = var[find_var(s->index->text)].val;
stack.Push(*s);
cout << setw(40)<< stack.show();
cout <<“Find(“<< s->index->text <<“)=“<< s->index->val;
}
else
{
s->index->hasval = false;
hasundefine = true;
stack.Push(*s);
cout << setw(40)<< stack.show();
cout <<“No Define “<<s->index->text;
}
}
}
}
}
int count_p = 0;
void post(node *s)//对树的后序遍历
{
if (s != NULL){
post( s->child);
post( s->bro);
if(s->id < 23 && s->id != 7 && s->id != 8 && s->id != 9)cout << s->text;
}
}
void post(my_stack<node>&stack,node *s)//对树的后序遍历
{
if (s != NULL ){
post(stack,s->child);
post(stack,s->bro);
if (s->id < 23&&s->id!=7&&s->id!=8&&s->id!=9)
{
cout <<setw(10)<< left<< count_p <<““;
count_p++;
semanti_ana(stack, s);
cout << endl;
}
}
}
int main()
{
node *start =&S;
sheet = new node[23];
sheet[0].text =“sin“;
sheet[1].text =“cos“;
sheet[2].text =“tg“;
sheet[3].text =“ctg“;
sheet[4].text =“log“;
sheet[5].text =“lg“;
sheet[6].text =“ln“;
sheet[7].text =“(“;
sheet[8].text =“)“;
sheet[9].text =“;“;
sheet[10].text =“?“;
sheet[11].text =“+“;
sheet[12].text =“-“;
sheet[13].text =“*“;
sheet[14].text =“/“;
sheet[15].text =“=“;
sheet[16].text =“^“;
sheet[17].text =“var“;
sheet[18].text =“con“;
sheet[19].text =“PI“;
sheet[20].text =“E“;
sheet[21].text =“#“;
sheet[22].text =“,“;
S.id = 23; S.text =“S“;
A.id = 24; A.text =“A“;
B.id = 25; B.text =“B“;
C.id = 26; C.text =“C“;
D.id = 27; D.text =“D“;
E.id = 28; E.text =“E“;
F.id = 29; F.text =“F“;
G.id = 30; G.text =“G“;
H.id = 31; H.text =“H“;
I.id = 32; I.text =“I“;
J.id = 33; J.text =“J“;
for (int i = 0; i < 23; i++)
sheet[i].id = i;
ifstream in;
in.open(“D:\\词法.txt“);
node *a = new node[MAXSIZE];//当前输入
char buffer[256];
while (in.getline(buffer, 100))//读取输入内容,既词法分析结果
{
a[N].text = buffer;
int c1 = a[N].text.find(“<“);
int c2 = a[N].text.find(“,“);
int c3 = a[N].text.find(“>“);
a[N].id = stoi(a[N].text.substr(c2 + 1, c3 - 1).data());
if (a[N].id == 19 || a[N].id == 20) a[N].id = 18;
a[N].text = a[N].text.substr(c1 + 1, c2 - 1);
N++;
}
a[N].text =“#“;
a[N].id =-1;
a[N].child = NULL;
a[N].bro = NULL;
in.close();
N++;
my_stack<node> stack;
stack.Push(a[N - 1]);
stack.Push(S);
start = stack.Top().index;
n -= 2;
cout <<“**************************************************语法分析***************************************************\n“;
cout <<“步骤栈内容当前输入操作\n“;
string c =“--------------------------------------------------------------------------------------------------------------\n“;
cout << c << c;
ana(stack, a);
cout <<“**************************************************语法分析***************************************************\n\n\n“;
cout <<“**************************************************语义分析***************************************************\n“;
my_stack<node> lexme;//语义分析栈
cout << endl;
cout<<“后缀式:“;
post(start);
cout << endl;
cout << endl;
cout <<“步骤栈内容操作\n“;
cout << c << c;
post(lexme,start);
cout <<“**************************************************语义分析***************************************************\n\n\n“;
return 0;
}
本站域名已经更换为m.adouyinxs.com 。请牢记。