假条,今天要写编译原理,来不及更新啦(2/2)
stackpush(sheet[17]);
cout <<“push(var)“;
break;
case 18:
stackpush(sheet[18]);
cout <<“push(con)“;
break;
case 19:
stackpush(sheet[18]);
cout <<“push(con)“;
break;
case 20:
stackpush(sheet[18]);
cout <<“push(con)“;
break;
default:
cout <<“error : no patte for h!\n“;
fg = 0;
break;
}
}
void i_p(node *sheet, node *a, y_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:
stackpush(h);
stackpush(sheet[16]);
cout <<“push(^h)“;
break;
default:
cout <<“error : no patte for i!\n“;
fg = 0;
break;
}
}
void j_p(node *sheet, node *a, y_stack<node>≈stack){
cout <<“pop(j),“;
switch (a[ip]id){
case 8:
stackpush(sheet[8]);
cout <<“push())“;
break;
case 22:
stackpush(sheet[8]);
stackpush(h);
stackpush(sheet[22]);
cout <<“push(,h))“;
default:
cout <<“error : no patte for j!\n“;
fg = 0;
break;
}
}
string show(node *a){
string s;
for (int i = ip; i < n; i)
{
s = a[i]text;
}
retu s;
}
void ana(y_stack<node>≈stack, node *a){
int i = 0;
while (fg){
p = stacktop()index;//当前指针
i;
cout << right << setw(3)<< i <<“)|“;
cout << left << setw(20)<< stackshow();
cout << left << setw(60)<< show(a);
switch (stacktop()id){
case 23:
stackpop();
s_p(sheet, a, stack);
break;
case 24:
stackpop();
a_p(sheet, a, stack);
break;
case 25:
stackpop();
b_p(sheet, a, stack);
break;
case 26:
stackpop();
c_p(sheet, a, stack);
break;
case 27:
stackpop();
d_p(sheet, a, stack);
break;
case 28:
stackpop();
e_p(sheet, a, stack);
break;
case 29:
stackpop();
f_p(sheet, a, stack);
break;
case 30:
stackpop();
g_p(sheet, a, stack);
break;
case 31:
stackpop();
h_p(sheet, a, stack);
break;
case 32:
stackpop();
i_p(sheet, a, stack);
break;
case 33:
stackpop();
j_p(sheet, a, stack);
break;
default:
if (stacktop()id == a[ip]id){
if (stacktop()id == 17)
stacktop()index->text = a[ip]text;
else if (stacktop()id == 18)
{
if (a[ip]text ==“pi“)
{
stacktop()index->text = a[ip]text;
stacktop()index->val = 3141592;
}
else if (a[ip]text ==“e“)
{
stacktop()index->text = a[ip]text;
stacktop()index->val = 2718281;
}
else {
stacktop()index->text = a[ip]text;
stacktop()index->val = stod(a[ip]text);
}
}
ip;
if(stacktop()id<23≈≈ stacktop()id>=0)
cout <<“pop(“<< sheet[stacktop()id]text <<“),“;
else
cout <<“pop(“<< stacktop()text <<“),“;
stackpop();
cout <<“next(ip)“;
if (stackepty()) cout <<“\nsuess\n“;
}
else {
cout <<“\nerror: fail!\n“;
fg = 0;
break;
}
}
cout << endl;
if (stackepty()) break;
if (n>0)
{p->child = stacktop(0)index;
node *tep = p->child->index;
for (int i = 1; i < n; i)
{
tep->bro = stacktop(i)index;
tep = tep->bro;
}
}
n = 0;
}
}
/********************************************************************/
/*语义分析*/
void seanti_ana(y_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 <<“retu top[0]val“;
cout << endl;
cout <<“结果:“;
if (hasundefine)
cout << s;
else
cout<<setw(40)<<stackshow();
}
else if (s->id == 0)
{
if (s->index->hasval)
stacktop()index->val = sin(stacktop()index->val);
else
{
out=“sin(“ stacktop()index->text “)“;
stacktop()index->text = out;
stacktop()index->hasval = false;
}
cout << setw(40)<< stackshow();
cout <<“top[0]text =“<< stacktop()index->text;
cout <<“,top[0]val=sin(top[0]val)“;
}
else if (s->id == 1)
{
if (s->index->hasval)
stacktop()index->val = cos(stacktop()index->val);
else
{
out =“cos(“ stacktop()index->text “)“;
stacktop()index->text = out;
stacktop()index->hasval = false;
}
cout<<setw(40)<<stackshow();
cout <<“top[0]text =“<< stacktop()index->text;
cout <<“,top[0]val=cos(top[0]val)“;
}
else if (s->id == 2)
{
if (s->index->hasval)
stacktop()index->val = tan(stacktop()index->val);
else
{
out =“tan(“ stacktop()index->text “)“;
stacktop()index->text = out;
stacktop()index->hasval = false;
}
cout<<setw(40)<<stackshow();
cout <<“top[0]text =“<< stacktop()index->text;
cout <<“,top[0]val=tg(top[0]val)“;
}
else if (s->id == 3)
{
if (s->index->hasval)
stacktop()index->val = tan(157-stacktop()index->val);
else
{
out =“cot(“ stacktop()index->text “)“;
stacktop()index->text = out;
stacktop()index->hasval = false;
}
cout<<setw(40)<<stackshow();
cout <<“top[0]text =“<< stacktop()index->text;
cout <<“,top[0]val=ctg(top[0]val)“;
}
else if (s->id == 4)
{
if (s->index->hasval)
stacktop()index->val = log(stacktop()index->val);
else
{
out =“log(“ stacktop()index->text “)“;
stacktop()index->text = out;
stacktop()index->hasval = false;
}
cout<<setw(40)<<stackshow();
cout <<“top[0]text =“<< stacktop()index->text;
cout <<“,top[0]val=log(top[0]val)“;
}
else if (s->id == 5)
{
if (s->index->hasval)
stacktop()index->val = log10(stacktop()index->val);
else
{
out =“lg(“ stacktop()index->text “)“;
stacktop()index->text = out;
stacktop()index->hasval = false;
}
cout<<setw(40)<<stackshow();
cout <<“top[0]text =“<< stacktop()index->text;
cout <<“,top[0]val=lg(top[0]val)“;
}
else if (s->id == 6)
{
if (s->index->hasval)
stacktop()index->val = log(stacktop()index->val);
else
{
out =“ln(“ stacktop()index->text “)“;
stacktop()index->text = out;
stacktop()index->hasval = false;
}
cout<<setw(40)<<stackshow();
cout <<“top[0]text =“<< stacktop()index->text;
cout <<“,top[0]val=ln(top[0]val)“;
}
else if (s->id == 11)
{
if (stacktop(0)index->hasval≈≈stacktop(1)index->hasval)
{
double tep = stacktop()index->val;
stackpop();
stacktop()index->val = tep;
stacktop()index->text = to_string(stacktop()index->val);
}
else {
out =“(“ stacktop(1)index->text ““stacktop(0)index->text “)“;
stacktop(1)index->text = out;
stacktop(1)index->hasval = false;
stackpop();
}
cout<<setw(40)<<stackshow();
cout <<“top[1]val=top[0]val , pop()“;
}
else if (s->id == 12)
{
if (stacktop(0)index->hasval≈≈stacktop(1)index->hasval)
{
double tep = stacktop()index->val;
stackpop();
stacktop()index->val -= tep;
stacktop()index->text = to_string(stacktop()index->val);
}
else {
out =“(“ stacktop(1)index->text “-“ stacktop(0)index->text “)“;
stacktop(1)index->text = out;
stacktop(1)index->hasval = false;
stackpop();
}
cout<<setw(40)<<stackshow();
cout <<“top[1]val-=top[0]val , pop()“;
}
else if (s->id == 13)
{
if (stacktop(0)index->hasval≈≈stacktop(1)index->hasval)
{
double tep = stacktop()index->val;
stackpop();
stacktop()index->val *= tep;
stacktop()index->text = to_string(stacktop()index->val);
}
else {
out = stacktop(1)index->text “*“ stacktop(0)index->text ;
stacktop(1)index->text = out;
stacktop(1)index->hasval = false;
stackpop();
}
cout<<setw(40)<<stackshow();
cout <<“top[1]val*=top[0]val , pop()“;
}
else if (s->id == 14)
{
if (stacktop(0)index->hasval≈≈stacktop(1)index->hasval)
{
double tep = stacktop()index->val;
stackpop();
stacktop()index->val /= tep;
stacktop()index->text = to_string(stacktop()index->val);
}
else {
out = stacktop(1)index->text “/“ stacktop(0)index->text;
stacktop(1)index->text = out;
stacktop(1)index->hasval = false;
stackpop();
}
cout<<setw(40)<<stackshow();
cout <<“top[1]val/=top[0]val , pop()“;
}
else if (s->id == 15)
{
var[top]val = stacktop()index->val;
stackpush(*s);
cout<<setw(40)<<stackshow();
stackpop();
cout <<“variable[top]val =“<< stacktop()index->val;
stackpop();
top;
}
else if (s->id == 16)
{
if (stacktop(0)index->hasval≈≈stacktop(1)index->hasval){
double tep = stacktop()index->val;
stackpop();
stacktop()index->val = pow(stacktop()index->val, tep);
stacktop()index->text = to_string(stacktop()index->val);
}
else {
out = stacktop(1)index->text “^“ stacktop(0)index->text;
stacktop(1)index->text = out;
stacktop(1)index->hasval = false;
stackpop();
}
cout<<setw(40)<<stackshow();
cout <<“top[1]val=top[1]val^top[0]val , pop()“;
}
else if (s->id == 18)
{
stackpush(*s);
cout<<setw(40)<<stackshow();
cout<<“push(“<<stacktop()index->val<<“)“;
}
else if (s->id == 17)
{
if (var[top - 1]nae ==““)
{
var[top - 1]nae = s->index->text;
string tep= to_string(var[top - 1]val)“=“ s->index->text ;
cout << setw(40)<< tep;
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;
stackpush(*s);
cout << setw(40)<< stackshow();
cout <<“find(“<< s->index->text <<“)=“<< s->index->val;
}
else
{
s->index->hasval = false;
hasundefine = true;
stackpush(*s);
cout << setw(40)<< stackshow();
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(y_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;
seanti_ana(stack, s);
cout << endl;
}
}
}
int ain()
{
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 =“,“;
sid = 23; stext =“s“;
aid = 24; atext =“a“;
bid = 25; btext =“b“;
cid = 26; ctext =“c“;
did = 27; dtext =“d“;
eid = 28; etext =“e“;
fid = 29; ftext =“f“;
gid = 30; gtext =“g“;
hid = 31; htext =“h“;
iid = 32; itext =“i“;
jid = 33; jtext =“j“;
for (int i = 0; i < 23; i)
sheet[i]id = i;
ifstrea in;
inopen(“d:\\词法“);
node *a = new node[axsize];//当前输入
char buffer[256];
while (ingetline(buffer, 100))//读取输入内容,既词法分析结果
{
a[n]text = buffer;
int c1 = a[n]textfind(“<“);
int c2 = a[n]textfind(“,“);
int c3 = a[n]textfind(“>“);
a[n]id = stoi(a[n]textsubstr(c21, c3 - 1)data());
if (a[n]id == 19 || a[n]id == 20) a[n]id = 18;
a[n]text = a[n]textsubstr(c11, c2 - 1);
n;
}
a[n]text =“#“;
a[n]id =-1;
a[n]child = null;
a[n]bro = null;
inclose();
n;
y_stack<node> stack;
stackpush(a[n - 1]);
stackpush(s);
start = stacktop()index;
n -= 2;
cout <<“**************************************************语法分析***************************************************\n“;
cout <<“步骤栈内容当前输入操作\n“;
string c =“--------------------------------------------------------------------------------------------------------------\n“;
cout << c << c;
ana(stack, a);
cout <<“**************************************************语法分析***************************************************\n\n\n“;
cout <<“**************************************************语义分析***************************************************\n“;
y_stack<node> lexe;//语义分析栈
cout << endl;
cout<<“后缀式:“;
post(start);
cout << endl;
cout << endl;
cout <<“步骤栈内容操作\n“;
cout << c << c;
post(lexe,start);
cout <<“**************************************************语义分析***************************************************\n\n\n“;
retu 0;
}
(
请访问最新地址www.83kk.net