假条,今天要写编译原理,来不及更新啦

//ConsoleApplication14.cpp:

//

#include“stdafx.h“

#include

#include

#include

#include

#include

#include

#include

usingnamespac

#defineMAXSIZE0xffff

intip=0;

intN=0;

intn=0;//stacknumber

boolflag=1;

inttop=0;//变量存储

structnode{

node*i

node*bro=

node*child=

i

string

boolhasval=

doubl

}S,A,B,C,D,E,F,G,H,I,J,*sheet,*p,*s

structvariable{

string

doubl

}var[20];

intfind_var(stringa){

for(inti=0;i

if(var[i].name==a)

ret

return-1;

}

template

classmy_stack

{

in

type*

intmax

public:

my_stack():top(-1),maxsize(MAXSIZE)

{

my_s=newtype[maxsize];

if(my_s==NULL)

{

cerr<“动态存储分配失败!“<

exit(1);

}

}

my_stack(intsize):top(-1),maxsize(size)

{

my_s=newtype[maxsize];

if(my_s==NULL)

{

cerr<“动态存储分配失败!“<

exit(1);

}

}

~my_stack()

{

delete[]

}

//是否为空

boolEmpty();

//压栈

node*Push(typetp);

//返回栈顶元素

typeTop();

//返回第n个栈顶元素

typeTop(intn);

//出栈

voidPop();

//栈大小

intSize();

stringShow();

stringshow();

intNum();

};

template

boolmy_stack::Empty()

{

if(top==-1){

return

}

else

returnf

}

template

typemy_stack::Top()

{

if(top!=-1)

{

returnmy_s[top];

}

else

{

cout<“栈空“;

exit(1);

}

}

template

typemy_stack::Top(intn)

{

if((top-n)>-1)

{

returnmy_s[top-n];

}

else

{

cout<“栈空“;

exit(1);

}

}

node*my_stack::Push(nodetp)

{

node*temp=new

*temp=tp;

temp->index=

n++;

if(top+1

{

my_s[++top]=*

}

else

{

cout<“栈满“;

exit(1);

}

return

}

template

intmy_stack::Num(){

retur

}

template

voidmy_stack::Pop()

{

if(top>=0)

{

top--;

}

else

{

cout<“栈空“;

exit(1);

}

}

template

intmy_stack::Size()

{

returntop+1;

}

template

stringmy_stack::Show(){

str

for(inti=0;i<=top;i++)

s+=my_s[i].

ret

}

stringmy_stack::show(){

str

for(inti=0;i<=top;i++)

s+=(my_s[i].index->text)+““;

ret

}

voidS_P(node*sheet,node*a,my_stack&stack){

cout<“pop(S),“;

nodet10=sheet[10];

nodet9=sheet[9];

switch(a[ip].id){

case10:

stack.Push(t9);

stack.Push(B);

stack.Push(t10);

stack.Push(A);

cout<“push(AB;)“;

b

case17:

stack.Push(t9);

stack.Push(B);

stack.Push(t10);

stack.Push(A);

cout<“push(AB;)“;

b

case21:

b

default:

cout<“error:NOPATTERNFORS!“;

flag=0;

b

}

}

voidA_P(node*sheet,node*a,my_stack&stack){

cout<“pop(A),“;

nodet9=sheet[9];

switch(a[ip].id){

case10:

b

case17:

stack.Push(A);

stack.Push(t9);

stack.Push(C);

cout<“push(C;A)“;

b

default:

cout<“error:NOPATTERNFORA!“;

flag=0;

b

}

}

voidB_P(node*sheet,node*a,my_stack&stack){

cout<“pop(B),“;

switch(a[ip].id){

case0:

stack.Push(E);

stack.Push(D);

cout<“push(DE)“;

b

case1:

stack.Push(E);

stack.Push(D);

cout<“push(DE)“;

b

case2:

stack.Push(E);

stack.Push(D);

cout<“push(DE)“;

b

case3:

stack.Push(E);

stack.Push(D);

cout<“push(DE)“;

b

case4:

stack.Push(E);

stack.Push(D);

cout<“push(DE)“;

b

case5:

stack.Push(E);

stack.Push(D);

cout<“push(DE)“;

b

case6:

stack.Push(E);

stack.Push(D);

cout<“push(DE)“;

b

case7:

stack.Push(E);

stack.Push(D);

cout<“push(DE)“;

b

case12:

stack.Push(E);

stack.Push(D);

cout<“push(DE)“;

b

case17:

stack.Push(E);

stack.Push(D);

cout<“push(DE)“;

b

case18:

stack.Push(E);

stack.Push(D);

cout<“push(DE)“;

b

case19:

stack.Push(E);

stack.Push(D);

cout<“push(DE)“;

b

case20:

stack.Push(E);

stack.Push(D);

cout<“push(DE)“;

b

default:

cout<“error:NOPATTERNFORB!“;

flag=0;

b

}

}

voidC_P(node*sheet,node*a,my_stack&stack)

{

cout<“pop(C),“;

nodet17=sheet[17];

nodet15=sheet[15];

switch(a[ip].id){

case17:

stack.Push(B);

stack.Push(t15);

stack.Push(t17);

cout<“push(var=B)“;

b

default:

cout<“error:NOPATTERNFORC!“;

flag=0;

b

}

}

voidD_P(node*sheet,node*a,my_stack&stack){

cout<“pop(D),“;

switch(a[ip].id){

case0:

stack.Push(G);

stack.Push(F);

cout<“push(FG)“;

b

case1:

stack.Push(G);

stack.Push(F);

cout<“push(FG)“;

b

case2:

stack.Push(G);

stack.Push(F);

cout<“push(FG)“;

b

case3:

stack.Push(G);

stack.Push(F);

cout<“push(FG)“;

b

case4:

stack.Push(G);

stack.Push(F);

cout<“push(FG)“;

case5:

stack.Push(G);

stack.Push(F);

cout<“push(FG)“;

b

case6:

stack.Push(G);

stack.Push(F);

cout<“push(FG)“;

b

case7:

stack.Push(G);

stack.Push(F);

cout<“push(FG)“;

b

case12:

stack.Push(G);

stack.Push(F);

cout<“push(FG)“;

b

case17:

stack.Push(G);

stack.Push(F);

cout<“push(FG)“;

b

case18:

stack.Push(G);

stack.Push(F);

cout<“push(FG)“;

b

case19:

stack.Push(G);

stack.Push(F);

cout<“push(FG)“;

case20:

stack.Push(G);

stack.Push(F);

b

cout<“push(FG)“;

b

b

default:

flag=0;

cout<“error:NOPATTERNFORD!“;

b

}

}

voidE_P(node*sheet,node*a,my_stack&stack){

cout<“pop(E),“;

nodet11=sheet[11],t12=sheet[12];

switch(a[ip].id){

case8:

b

case9:

b

case11:

stack.Push(E);

stack.Push(D);

stack.Push(sheet[11]);

cout<“push(+DE)“;

b

case12:

stack.Push(E);

stack.Push(D);

stack.Push(sheet[12]);

cout<“push(-DE)“;

b

default:

flag=0;

cout<“error:NOPATTERNFORE!“;

b

}

}

voidF_P(node*sheet,node*a,my_stack&stack){

cout<“pop(F),“;

switch(a[ip].id){

case0:

stack.Push(sheet[8]);

stack.Push(H);

stack.Push(sheet[7]);

stack.Push(sheet[0]);

cout<“push(sin(H))“;

b

case1:

stack.Push(sheet[8]);

stack.Push(H);

stack.Push(sheet[7]);

stack.Push(sheet[1]);

cout<“push(cos(H))“;

b

case2:

stack.Push(sheet[8]);

stack.Push(H);

stack.Push(sheet[7]);

stack.Push(sheet[2]);

cout<“push(tg(H))“;

b

case3:

stack.Push(sheet[8]);

stack.Push(H);

stack.Push(sheet[7]);

stack.Push(sheet[3]);

cout<“push(ctg(H))“;

b

case4:

stack.Push(sheet[8]);

stack.Push(H);

stack.Push(sheet[7]);

stack.Push(sheet[4]);

cout<“push(log(H))“;

b

case5:

stack.Push(sheet[8]);

stack.Push(H);

stack.Push(sheet[7]);

stack.Push(sheet[5]);

cout<“push(lg(H))“;

b

case6:

stack.Push(sheet[8]);

stack.Push(H);

stack.Push(sheet[7]);

stack.Push(sheet[6]);

cout<“push(ln(H))“;

b

case7:

stack.Push(I);

stack.Push(H);

cout<“push(HI)“;

b

case12:

stack.Push(I);

stack.Push(H);

cout<“push(HI)“;

b

case17:

stack.Push(I);

stack.Push(H);

cout<“push(HI)“;

b

case18:

stack.Push(I);

stack.Push(H);

cout<“push(HI)“;

b

case19:

stack.Push(I);

stack.Push(H);

cout<“push(HI)“;

b

case20:

stack.Push(I);

stack.Push(H);

cout<“push(HI)“;

b

default:

cout<“error:NOPATTERNFORF!“;

flag=0;

b

}

}

voidG_P(node*sheet,node*a,my_stack&stack){

cout<“pop(G),“;

switch(a[ip].id){

case8:

b

case9:

b

case11:

b

case12:

b

case13:

stack.Push(G);

stack.Push(F);

stack.Push(sheet[13]);

cout<“push(*FG)“;

b

case14:

stack.Push(G);

stack.Push(F);

stack.Push(sheet[14]);

cout<“push(/FG)“;

b

default:

cout<“error:NOPATTERNFORG!“;

flag=0;

b

}

}

voidH_P(node*sheet,node*a,my_stack&stack){

cout<“pop(H),“;

switch(a[ip].id){

case7:

stack.Push(sheet[8]);

stack.Push(B);

stack.Push(sheet[7]);

cout<“push((B))“;

b

case12:

stack.Push(H);

stack.Push(sheet[12]);

cout<“push(-H)“;

b

case17:

stack.Push(sheet[17]);

cout<“push(var)“;

b

case18:

stack.Push(sheet[18]);

cout<“push(con)“;

b

case19:

stack.Push(sheet[18]);

cout<“push(con)“;

b

case20:

stack.Push(sheet[18]);

cout<“push(con)“;

b

default:

cout<“error:NOPATTERNFORH!“;

flag=0;

b

}

}

voidI_P(node*sheet,node*a,my_stack&stack){

cout<“pop(I),“;

switch(a[ip].id){

case8:

b

case9:

b

case11:

b

case12:

b

case13:

b

case14:

b

case16:

stack.Push(H);

stack.Push(sheet[16]);

cout<“push(^H)“;

b

default:

cout<“error:NOPATTERNFORI!“;

flag=0;

b

}

}

voidJ_P(node*sheet,node*a,my_stack&stack){

cout<“pop(J),“;

switch(a[ip].id){

case8:

stack.Push(sheet[8]);

cout<“push())“;

b

case22:

stack.Push(sheet[8]);

stack.Push(H);

stack.Push(sheet[22]);

cout<“push(,H))“;

default:

cout<“error:NOPATTERNFORJ!“;

flag=0;

b

}

}

stringshow(node*a){

str

for(inti=ip;i

{

s+=a[i].

}

ret

}

voidana(my_stack&stack,node*a){

inti=0;

while(flag){

p=stack.Top().i//当前指针

i++;

cout

cout

cout

switch(stack.Top().id){

case23:

stack.Pop();

S_P(sheet,a,stack);

b

case24:

stack.Pop();

A_P(sheet,a,stack);

b

case25:

stack.Pop();

B_P(sheet,a,stack);

b

case26:

stack.Pop();

C_P(sheet,a,stack);

b

case27:

stack.Pop();

D_P(sheet,a,stack);

b

case28:

stack.Pop();

E_P(sheet,a,stack);

b

case29:

stack.Pop();

F_P(sheet,a,stack);

b

case30:

stack.Pop();

G_P(sheet,a,stack);

b

case31:

stack.Pop();

H_P(sheet,a,stack);

b

case32:

stack.Pop();

I_P(sheet,a,stack);

b

case33:

stack.Pop();

J_P(sheet,a,stack);

b

default:

if(stack.Top().id==a[ip].id){

if(stack.Top().id==17)

stack.Top().index->text=a[ip].

elseif(stack.Top().id==1

{

if(a[ip].text==“PI“)

{

stack.Top().index->text=a[ip].

stack.Top().index->val=3.14

}

elseif(a[ip].text==“E“)

{

stack.Top().index->text=a[ip].

stack.Top().index->val=2.71

}

else{

stack.Top().index->text=a[ip].

stack.Top().index->val=stod(a[ip].text);

}

}

ip++;

if(stack.Top().id<23&&stack.Top().id>=0)

cout<“pop(“

else

cout<“pop(“

stack.Pop();

cout<“next(ip)“;

if(stack.Empty())cout<“Success“;

}

else{

cout<“error:Fail!“;

flag=0;

b

}

}

cout<

if(stack.Empty())b

if(n>0)

{p->child=stack.Top(0).i

node*temp=p->child->i

for(inti=1;i

{

temp->bro=stack.Top(i).i

temp=temp->bro;

}

}

n=0;

}

}

/********************************************************************/

/*语义分析*/

voidsemanti_ana(my_stack&stack,node*s)//主体部分,对后序遍历得到的后缀树进行栈操作,得出最后结果,放在后序遍历循环体中

{

stringout=““;

boolhasundefine=f

if(s->id>=0&&s->id<19)

{

if(s->id==10)

{

cout

cout<“ReturnTop[0].val“;

cout<

cout<“结果:“;

if(hasundefine)

cout

else

cout

}

elseif(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=f

}

cout

cout<“Top[0].text=“

cout<“,Top[0].val=sin(Top[0].val)“;

}

elseif(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=f

}

cout

cout<“Top[0].text=“

cout<“,Top[0].val=cos(Top[0].val)“;

}

elseif(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=f

}

cout

cout<“Top[0].text=“

cout<“,Top[0].val=tg(Top[0].val)“;

}

elseif(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=f

}

cout

cout<“Top[0].text=“

cout<“,Top[0].val=ctg(Top[0].val)“;

}

elseif(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=f

}

cout

cout<“Top[0].text=“

cout<“,Top[0].val=log(Top[0].val)“;

}

elseif(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=f

}

cout

cout<“Top[0].text=“

cout<“,Top[0].val=lg(Top[0].val)“;

}

elseif(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=f

}

cout

cout<“Top[0].text=“

cout<“,Top[0].val=ln(Top[0].val)“;

}

elseif(s->id==11)

{

if(stack.Top(0).index->hasval&&stack.Top(1).index->hasval)

{

doubletemp=stack.Top().index->val;

stack.Pop();

stack.Top().index->val+=

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=f

stack.Pop();

}

cout

cout<“Top[1].val+=Top[0].val,Pop()“;

}

elseif(s->id==12)

{

if(stack.Top(0).index->hasval&&stack.Top(1).index->hasval)

{

doubletemp=stack.Top().index->val;

stack.Pop();

stack.Top().index->val-=

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=f

stack.Pop();

}

cout

cout<“Top[1].val-=Top[0].val,Pop()“;

}

elseif(s->id==13)

{

if(stack.Top(0).index->hasval&&stack.Top(1).index->hasval)

{

doubletemp=stack.Top().index->val;

stack.Pop();

stack.Top().index->val*=

stack.Top().index->text=to_string(stack.Top().index->val);

}

else{

out=stack.Top(1).index->text+“*“+stack.Top(0).index->

stack.Top(1).index->text=out;

stack.Top(1).index->hasval=f

stack.Pop();

}

cout

cout<“Top[1].val*=Top[0].val,Pop()“;

}

elseif(s->id==14)

{

if(stack.Top(0).index->hasval&&stack.Top(1).index->hasval)

{

doubletemp=stack.Top().index->val;

stack.Pop();

stack.Top().index->val/=

stack.Top().index->text=to_string(stack.Top().index->val);

}

else{

out=stack.Top(1).index->text+“/“+stack.Top(0).index->

stack.Top(1).index->text=out;

stack.Top(1).index->hasval=f

stack.Pop();

}

cout

cout<“Top[1].val/=Top[0].val,Pop()“;

}

elseif(s->id==15)

{

var[top].val=stack.Top().index->val;

stack.Push(*s);

cout

stack.Pop();

cout<“Variable[top].val=“val;

stack.Pop();

top++;

}

elseif(s->id==16)

{

if(stack.Top(0).index->hasval&&stack.Top(1).index->hasval){

doubletemp=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->

stack.Top(1).index->text=out;

stack.Top(1).index->hasval=f

stack.Pop();

}

cout

cout<“Top[1].val=Top[1].val^Top[0].val,Pop()“;

}

elseif(s->id==1

{

stack.Push(*s);

cout

cout<“Push(“val<“)“;

}

elseif(s->id==17)

{

if(var[top-1].name==““)

{

var[top-1].name=s->index->

stringtemp=to_string(var[top-1].val)+“=“+s->index->

cout

cout<“Variable[Top].text=“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

cout<“Find(“index->text<“)=“index->val;

}

else

{

s->index->hasval=f

hasundefine=

stack.Push(*s);

cout

cout<“NoDefine“index->

}

}

}

}

}

intcount_p=0;

voidpost(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

}

}

voidpost(my_stack&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

count_p++;

semanti_ana(stack,s);

cout<

}

}

}

intmain()

{

node*start=&S;

sheet=newnode[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(inti=0;i<23;i++)

sheet[i].id=i;

ifstre

in.open(“D:\\词法.txt“);

node*a=newnode[MAXSIZE];//当前输入

charbuffer[256];

while(in.getline(buffer,100))//读取输入内容,既词法分析结果

{

a[N].text=bu

intc1=a[N].text.find(“<“);

intc2=a[N].text.find(“,“);

intc3=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=

a[N].bro=

in.close();

N++;

my_stacks

stack.Push(a[N-1]);

stack.Push(S);

start=stack.Top().i

n-=2;

cout<“**************************************************语法分析***************************************************“;

cout<“步骤栈内容当前输入操作“;

stringc=“--------------------------------------------------------------------------------------------------------------“;

cout

ana(stack,a);

cout<“**************************************************语法分析***************************************************“;

cout<“**************************************************语义分析***************************************************“;

my_stackl//语义分析栈

cout<

cout<“后缀式:“;

post(start);

cout<

cout<

cout<“步骤栈内容操作“;

cout

post(lexme,start);

cout<“**************************************************语义分析***************************************************“;

ret

}