cout<<\ exit(0); } else {
s.DispStack(); if(a=='!')
cout<<\ else
cout<<\ s.PushStack(a); strp++;
statop=s.Getsize()-1; } }
s.DispStack(); s.PopStack(3); if(s.Getsize()==0)
cout<<\ else
cout<<\
}
20
(3)运行结果 1)正确情况下
21
2)不正确的情况下:
22
3.3.基于算符优先分析方法的语法制导翻译程序的设计 (1)流程图 开始 输出优先关系表 输入字符串 ‘#’压栈 当前字符为Y 栈中为N ‘#’ ? “#N#” 不是句子 N Y 栈顶终结符与当前字出栈 符比较 N 是句子 大于? Y 结束 规约 入栈 判断产生式类型 为前三个产生 式? Y 生成产生式 下一个字符 23
(2)程序 #include
char cost[8][8]={' ','+','*','!','i','(',')','#', '+','>','<','<','<','<','>','>', '*','>','>','<','<','<','>','>', '!','>','>','<','<','<','>','>', 'i','>','>','>',' ',' ','>','>', '(','<','<','<','<','<','=',' ', ')','>','>','>',' ',' ','>','>',
'#','<','<','<','<','<',' ','='};//the priority realiton graph int Digital[MAX]; //store the letter swap to digital int Dp=0; //the pointer of the digital char Sign[MAX]; //strore the sign of the string int Sp=0; //the pointer of the sign int Temp[MAX]; // int Tp; //
int Fp=0; //point to the four equation space void DispCost(); bool IsChar(char c);
int SearchCost(int i,int j); int Entry(char ch); int NewTemp(int t[3]);
int CharToDigital(char ch); void DispItem(int i); void DispIndirect(int i); int Merge(int *ch,int ll);
void ToDigital(int digital[],char str[],int &d_len,int len); typedef class four_item {
char op;
int s1,s2,s3; public:
four_item(){}
four_item(int op,int s1,int s2) {
this->op=cost[0][op]; this->s1=s1; this->s2=s2;
this->s3=Fp+300;//metion +100 ,+200,+300
24