// Code derived from Stroustrup's PPP2 book// § 6.4 Grammars// -and beginning on p 188#include<iostream>#include<stdexcept>#include<vector>usingstd::cerr;usingstd::cin;usingstd::cout;usingstd::exception;usingstd::runtime_error;usingstd::vector;voiderror(constchar*s){throwruntime_error(s);}// a simple user-defined typeclassToken{public:Token(charch):kind{ch}{}Token(charch,doubleval):kind{ch},value{val}{}charkind='0';doublevalue=0.0;};Tokenget_token(){charch;cin>>ch;// note that >> skips whitespace (space, newline, tab, etc.)// clang-format offswitch(ch){case';':case'+':case'-':case'*':case'/':returnToken{ch};// let each character represent itselfcase'.':// a floating-point literal can start with a dotcase'0':case'1':case'2':case'3':case'4':case'5':case'6':case'7':case'8':case'9':{// numeric literalcin.putback(ch);// put digit back into the input streamdoubleval;cin>>val;// read a floating-point numberreturnToken{'8',val};}default:error("Bad token");}// clang-format on// shouldn't reach herereturnToken{'K'};}vector<Token>toks;// we'll put the tokens hereintmain()try{cout<<"Please enter expression (we can handle +, –, *, and /),\n";cout<<" then add a ; to print (e.g., 1+2*3;): \n";while(cin){Tokent=get_token();if(t.kind==';')break;toks.push_back(t);}// print Token contents out to console:cout<<"\nthe Tokens are listed as:\n"<<" t.kind : t.value \n";for(Tokent:toks)cout<<" "<<t.kind<<" : "<<t.value<<'\n';}catch(exception&e){cerr<<"error: "<<e.what()<<'\n';return1;}catch(...){cerr<<"Oops: unknown exception!\n";return2;}