Ho scritto il codice che fa la divisione in token, ma non l' ho testato molto e non sono sicuro che si comporti correttamente per tutti gli input:
codice:
#include <iostream>
#include <stack>
using namespace std;
void split (stack<string>& s, string& str)
{
unsigned long first,last;
string temp,left,right;
first=str.find("(");
last=str.rfind(")");
if(first==string::npos || last==string::npos)
{
s.push(str);
return;
}
temp=str.substr(first+1,last-first-1);
if(last!= str.size()-1)
{
right= str.substr(last+1,str.size()-last);
s.push(right);
}
if(first!=0)
{
left=str.substr(0,first);
s.push(left);
}
if(temp.size()>0)
split(s,temp);
}
int main(int argc, char** argv)
{
string str;
stack<string> s;
cin >> str;
split(s,str);
while(!s.empty())
{
string temp=s.top();
cout << temp << endl;
s.pop();
}
return 0;
}