Public feed #598670

piromanas

public cpp v1 · immutable
Raw Edit
·published 2007-07-01 15:08 UTC
rendered paste body
#include <iostream>#include <string>#include <sstream>#include <algorithm>#include <cmath>#include <stdlib.h>#include <list> using namespace std;struct token {    token *right, *left ;    float value ;    char operation ;    string ID ;    token() : right(NULL), left(NULL), value(0), operation(0) {} ;    } ;    // the tabe of states     const int E = 0 ;  const int ID = 1 ; const int V = 2 ; const int O = 3 ; const int floating_point = 4 ; const int EXP = 5 ; const int empty = 6 ; const int ERROR = 7 ;//-------------------------   void lexer (string::iterator begin, string::iterator end, list<token *> & output) {        std::string::iterator i = begin, b, e ;    int state = E , count = 0 ;    while ( i != end ) {        switch (state) {                            case    E :                             if ( i != end && isalpha(*i) ) {                                b = i ;                                state = ID ;                            }                            else if ( i != end && isdigit(*i) ) {                                     b = i ;                                     state = V ;                            }                            else if ( i != end && ((*i == '+') || (*i == '-') || (*i == '*') || (*i == '/') || (*i == '^') || (*i == '(') || (*i == ')')) ) {                                    state = O ;                            }                            else if ( i != end && (*i == ' ') ) {                                    state = empty ;                                        }                            else state = ERROR ;                                                        break ;                                case    ID :                                            if ( i != end && (isalpha(*i) || isdigit(*i)) ) {                                state = ID ;                                ++i ;                            }                                                else {                                    token* t = new token ;                                    e = i ;                                    string s(b,e) ;                                    t->ID = s ;                                    output.push_back(t) ;                                    state = E ;                            }                            break ;                case    V :                                             if ( i != end && isdigit(*i) ) {                                            state = V ;                                ++i ;                                continue ;                            }                            else if ( i != end && (((*i) == ',') || ((*i) == '.')) ) {                                                                state = floating_point ;                                *i = '.' ;                                ++count ;                                ++i ;                                continue ;                                                                                }                            else if ( i < end && ((*i == 'E') || (*i == 'e')) ) {                                                                state = EXP ;                                ++i ;                                continue ;                            }                            else if ( i!= end && *i == ' ' ) {                                token* t = new token ;                                    e = i ;                                    string s1 (b,e) ;                                    istringstream stream (s1) ;                                    float d ;                                    stream >> d ;                                    t->value = d ;                                    output.push_back(t) ;                                    count = 0 ;                                    state = E ;                            }                                                           else {                                    token* t = new token ;                                    e = i ;                                    string s1 (b,e) ;                                    istringstream stream (s1) ;                                    float d ;                                    stream >> d ;                                    t->value = d ;                                    output.push_back(t) ;                                    count = 0 ;                                    state = E ;                            }                            break ;                case    O :                             if ( i != end && ((*i == '+') || (*i == '-') || (*i == '*') || (*i == '/') || (*i == '^') || (*i == '(') || (*i == ')')) ) {                                token* t = new token ;                                t->operation = *i ;                                output.push_back(t) ;                                ++i ;                                state = E ;                                continue ;                            }                            break ;                                            case    floating_point :                                              if ( i != end && isdigit(*i) && count < 2 ) {                                                state = V ;                                                ++i ;                                                continue ;                                          }                                          else state = ERROR ;                                                                                    break ;                                                          case    EXP :                                 if ( i != end && isdigit(*i) || (*i == '+') || (*i == '-') ) {                                    state = V ;                                    ++i ;                                    continue ;                                }                                else state = ERROR ;                                                                break ;                case    empty :                                if ( i != end && *i == ' ' ) {                                    state = empty ;                                    ++i ;                                    continue ;                                }                                else state = E ;                                                                break ;                                                                  case    ERROR :                                 default :                                             exit(0) ;                                }        }                  if ( state == ID ) {             token* t = new token ;            e = i ;            string s(b,e) ;            t->ID = s ;            output.push_back(t) ;        }        else if ( i == end && state != ERROR && state != E ) {                            token* t = new token ;                            e = i ;                            string s1 (b,e) ;                            istringstream stream (s1) ;                            float d ;                            stream >> d ;                            t->value = d ;                            if ( state != empty )                            output.push_back(t) ;                                                                        }    }        std::ostream & operator << (std::ostream & out,token const & t) {            if (t.operation)        out << t.operation << endl ;        else if (!(t.ID.empty()))        out << t.ID << endl ;        else         out << t.value << endl ;            }            int main() {        list<token *>::iterator i ;    string str ;    list<token *> output ;    getline(cin,str) ;    lexer(str.begin(),str.end(),output) ;        i = output.begin() ;    while ( i != output.end() ) {    cout << **i ;    ++i ;    }    cin.get() ;}