コンパイル原理——中間コード生成(逆ポーランド表示)


1、中間コード生成プログラムを作成し、算術式などを逆ポーランド形式に翻訳することができる.
2、プログラムは汎用性があり、すなわち各種の異なる算術式などの文法成分を受け入れることができる.
3、文法の正しい算術式に対して、逆ポーランド表現を生成し、結果を出力することができる.
C++は以下のように実現される.
#include
using namespace std;

char st[1000];	//          
int top=-1;		//     
string ss="";	//         

int main(){
	printf("%s","Input:");
	string s;
	cin>>s;
	int L=s.length();
	for(int i=0;i-1){//   +、-            "("      
					ss.push_back(st[top]);
					top--;
				}
				st[++top]=s[i];	//   +、-    
			}
		}
		else if(s[i]=='*'||s[i]=='/'){
			if(top==-1){
				st[++top]=s[i];
			}
			else{
				while(st[top]!='('&&(st[top]=='*'||st[top]=='/')){	//   *、/     *、/      "("      
					ss.push_back(st[top]);
					top--;
				}
				st[++top]=s[i];	//   *、/    
			}
		}
		else if(s[i]=='('){	//   "("      
			st[++top]=s[i];
		}
		else if(s[i]==')'){	//               ,       
			while(st[top]!='('){
				ss.push_back(st[top]);
				top--;
			}
			top--;	//              
		}
		else{
			ss.push_back(s[i]);	//            
		}
	}
	//  ,               
	while(top>-1){
		ss.push_back(st[top]);
		top--;
	}
	printf("%s","     :");
	cout<