Thread: Parser Help

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    38

    Parser Help

    Hey,

    I've bought the book Expert C++ and have tried to assemble one of the programs in Dev-C++, it comes up with loads of errors, I was wondering if anyone wouldnt mind looking over the code and helping me to fix it.

    Thanks

    Code:
    //  GENERIC PARSER
    
    #include <iostream.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    enum types { DELIMITER = 1, VARIABLE, NUMBER};
    
    const int NUMVARS = 26;
    
    template <class PType> class parser {
             char *exp_ptr; //points to the expression
             char token[80]; //holds current token
             char tok_type; //holds token's type
             PType vars[NUMVARS]; //holds variable's values
             
             void eval_exp1(PType &result);
             void eval_exp2(PType &result);
             void eval_exp3(PType &result);
             void eval_exp4(PType &result);
             void eval_exp5(PType &result);
             void eval_exp6(PType &result);
             void atom (PType &result);
             void get_token(), pushback();
             void serror(int error);
             PType find_var(char *s);
             int isdelim(char c);
             public:
                    parser();
                    PType eval_exp(char *exp);
    };
    
    //parser constructor
    template <class Ptype> parser<PType>::parser() {
             int i;
             exp_ptr = NULL;
             for(i=0; i<NUMVARS; i++) vars[i] = (PType) 0;
    }
    
    //parser entry point
    template <class PType> Ptype parser<Ptype>::eval_exp(char *exp) {
             PType result;
             exp_ptr = exp;
             
             get_token();
             if(!*token) {
                         serror(2); //No Expression present
                         return (PType) 0;
             }
             eval_exp1(result);
             if(*token) serror(0); //Last Token Must Be NULL
             return result;
    }
    
    //Process an assignment
    template <class PType> void parser<PType>::eval_exp1(Ptype &result) {
             int slot;
             char ttok_type;
             char temp_token[80];
             if(tok_type==VARIABLE) {
                                    strcpy(temp_token, token);
                                    ttok_type = tok_type;
                                    slot = toupper(*token) - 'A';
                                    get_token();
                                    if(*token != '=') {
                                              putback();
                                              strcpy(token, temp_token);
                                              tok_type = ttok_type;
                                    }
                                    else {
                                         get_token();
                                         eval_exp2(result);
                                         vars[slot] = result;
                                         return;
                                    }
             }
             eval_exp2(result);
    }
    
    //Add Or Subtract Two Terms
    template <class PType> void parser<PType>::eval_exp2(PType &result) {
             register char op;
             PType temp;
             eval_exp3(result);
             while((op = *token) == '+' || op == '-') {
                       get_token();
                       eval_exp3(temp);
                       switch(op) {
                                  case '-':
                                       result = result - temp;
                                       break;
                                  case '+':
                                       result = result + temp;
                                       break;
                       }
             }
    }
    
    //Multiply Or Divide Two Factors
    template <class PType> void parser<PType>::eval_exp3(PType &result) {
             register char op;
             PType temp;
             eval_exp4(result);
             while((op = *token) == '*' || op == '/' || op == '%') {
                       get_token();
                       eval_exp4(temp);
                       switch(op) {
                                  case '*':
                                       result = result * temp;
                                       break;
                                  case '/':
                                       result = result / temp;
                                       break;
                                  case '%':
                                       result = (int) result % (int) temp;
                                       break;
                       }
             }
    }
    
    //Process an exponent
    template <class PType> void parser<PType>::eval_exp4(PType &result) {
             PType temp, ex;
             register int t;
             eval_exp5(result);
             if(*token == '^') {
                       get_token();
                       eval_exp4(temp);
                       ex = result;
                       if (temp == 0.0) {
                                result = (PType) 1;
                                return;
                       }
                       for (t=(int)temp-1; t>0; --t) result = result * ex;
             }
    }
    
    //Evaluate a unary + or -
    template <class PType> void parser<PType>::eval_exp5(PType &result) {
             register char op;
             op = 0;
             if((tok_type == DELIMITER) && *token == '+' || *token == '-') {
                          op = *token;
                          get_token();
             }
             eval_exp6(result);
             if(op=='-') result = -result;
    }
    
    //Processed a parenthesized expression.
    template <class PType> void parser<Ptype>::eval_exp6(PType &result) {
             if((*token == '(')) {
                        get_token();
                        eval_exp2(result);
                        if(*token != ')') serror(1);
                        get_token();
             }
             else atom(result);
    }
    
    //Get The Value Of A Number Or A Variable
    template <class PType> void parser<PType>::atom(Ptype &result) {
             switch(tok_type) {
                              case VARIABLE:
                                   result = find_var(token);
                                   get_token();
                                   return;
                              case NUMBER:
                                   result = (PType) atof(token);
                                   get_token();
                                   return;
                              default:
                                      serror(0);
             }
    }
    
    //Return A Token To The Input Stream
    template <class PType> void parser<PType>::putback() {
             char *t;
             t = token;
             for(; *; t++) exp_ptr--;
    }
    
    //Display Syntax Error
    template <class PType> void parser<Ptype>::serror(int error) {
             static char *e[] = {
                    "Syntax Error",
                    "Unbalanced Parentheses",
                    "No Expression Present"
             };
             cout << e[error] << endl;
    }
    
    //Obtain The Next Token
    template <class PType> void parser<PType>::get_token() {
             register char *temp;
             tok_type = 0;
             temp = token;
             *temp = '\0';
             if(!*exp_pre) return; //end of expression
             while(isspace(*exp_ptr)) ++exp_ptr //skip over the blanks
             if (strchr("+-*/%^=()", *exp_ptr)) {
                                     tok_type = DELIMITER;
                                     //advance to next char
                                     *temp++ = *exp_ptr++;
             }
             else if (isalpha(*exp_ptr)) {
                  while(!isdelim(*exp_ptr)) *temp++ = *exp_ptr++;
                  tok_type = NUMBER;
             }
             else if (isdigit(*exp_ptr)) {
                  while(!isdelim(*exp_ptr)) *temp++ = *exp_ptr++;
                  tok+type = NUMBER;
             }
             *temp = '\0';
    }
    
    //Return True If c Is A Delimiter
    template <class PType> PType parser<PType>::find_var(char *s) {
             if(!isalpha(*s)) {
                              serror(1);
                              return (PType) 0;
             }
             return var[touper(*token) - 'A'];
    }
    
    int main() {
        char expstr[80];
        parser<double> ob;
        cout << "Enter A period to stop\n";
        for(;;) {
                cout << "Enter expression: ";
                cin.getline(expstr, 79);
                if (*expstr=='.') break;
                cout << "Answer is: " << ob.eval_exp(expstr) << "\n\n";
        }
        cout << endl;
        return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    First off,
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    all those headers are deprecated. The ones you should be using are:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cctype>
    #include <cstring>
    However that will bring up the issue of namespaces. The easiest way to deal with that for right now would be to include this line after the above #include statements:
    Code:
    using namespace std;
    As to your errors, it would help if you posted the errors reported and perhaps highlight the lines involved in the code sample you provide.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    Thanks for the reply,

    Yeah the books a tad old, that'll be why those bits were like that. Ive sorted out loads of the errors so not many to go now!!:

    Code:
    //  GENERIC PARSER
    
    #include <iostream>
    #include <cstdlib>
    #include <cctype>
    #include <cstring>
    
    using namespace std;
    
    enum types { DELIMITER = 1, VARIABLE, NUMBER};
    
    const int NUMVARS = 26;
    
    template <class PType> class parser {
             char *exp_ptr; //points to the expression
             char token[80]; //holds current token
             char tok_type; //holds token's type
             PType vars[NUMVARS]; //holds variable's values
             
             void eval_exp1(PType &result);
             void eval_exp2(PType &result);
             void eval_exp3(PType &result);
             void eval_exp4(PType &result);
             void eval_exp5(PType &result);
             void eval_exp6(PType &result);
             void atom (PType &result);
             void get_token(), pushback();
             void serror(int error);
             PType find_var(char *s);
             int isdelim(char c);
             public:
                    parser();
                    PType eval_exp(char *exp);
    };
    
    //parser constructor
    template <class PType> parser<PType>::parser() {
             int i;
             exp_ptr = NULL;
             for(i=0; i<NUMVARS; i++) vars[i] = (PType) 0;
    }
    
    //parser entry point
    template <class PType> PType parser<PType>::eval_exp(char *exp) {
             PType result;
             exp_ptr = exp;
             
             get_token();
             if(!*token) {
                         serror(2); //No Expression present
                         return (PType) 0;
             }
             eval_exp1(result);
             if(*token) serror(0); //Last Token Must Be NULL
             return result;
    }
    
    //Process an assignment
    template <class PType> void parser<PType>::eval_exp1(PType &result) {
             int slot;
             char ttok_type;
             char temp_token[80];
             if(tok_type==VARIABLE) {
                                    strcpy(temp_token, token);
                                    ttok_type = tok_type;
                                    slot = toupper(*token) - 'A';
                                    get_token();
                                    if(*token != '=') {
                                              putback();
                                              strcpy(token, temp_token);
                                              tok_type = ttok_type;
                                    }
                                    else {
                                         get_token();
                                         eval_exp2(result);
                                         vars[slot] = result;
                                         return;
                                    }
             }
             eval_exp2(result);
    }
    
    //Add Or Subtract Two Terms
    template <class PType> void parser<PType>::eval_exp2(PType &result) {
             register char op;
             PType temp;
             eval_exp3(result);
             while((op = *token) == '+' || op == '-') {
                       get_token();
                       eval_exp3(temp);
                       switch(op) {
                                  case '-':
                                       result = result - temp;
                                       break;
                                  case '+':
                                       result = result + temp;
                                       break;
                       }
             }
    }
    
    //Multiply Or Divide Two Factors
    template <class PType> void parser<PType>::eval_exp3(PType &result) {
             register char op;
             PType temp;
             eval_exp4(result);
             while((op = *token) == '*' || op == '/' || op == '%') {
                       get_token();
                       eval_exp4(temp);
                       switch(op) {
                                  case '*':
                                       result = result * temp;
                                       break;
                                  case '/':
                                       result = result / temp;
                                       break;
                                  case '%':
                                       result = (int) result % (int) temp;
                                       break;
                       }
             }
    }
    
    //Process an exponent
    template <class PType> void parser<PType>::eval_exp4(PType &result) {
             PType temp, ex;
             register int t;
             eval_exp5(result);
             if(*token == '^') {
                       get_token();
                       eval_exp4(temp);
                       ex = result;
                       if (temp == 0.0) {
                                result = (PType) 1;
                                return;
                       }
                       for (t=(int)temp-1; t>0; --t) result = result * ex;
             }
    }
    
    //Evaluate a unary + or -
    template <class PType> void parser<PType>::eval_exp5(PType &result) {
             register char op;
             op = 0;
             if((tok_type == DELIMITER) && *token == '+' || *token == '-') {
                          op = *token;
                          get_token();
             }
             eval_exp6(result);
             if(op=='-') result = -result;
    }
    
    //Processed a parenthesized expression.
    template <class PType> void parser<PType>::eval_exp6(PType &result) {
             if((*token == '(')) {
                        get_token();
                        eval_exp2(result);
                        if(*token != ')') serror(1);
                        get_token();
             }
             else atom(result);
    }
    
    //Get The Value Of A Number Or A Variable
    template <class PType> void parser<PType>::atom(PType &result) {
             switch(tok_type) {
                              case VARIABLE:
                                   result = find_var(token);
                                   get_token();
                                   return;
                              case NUMBER:
                                   result = (PType) atof(token);
                                   get_token();
                                   return;
                              default:
                                      serror(0);
             }
    }
    
    //Return A Token To The Input Stream
    template <class PType> void parser<PType>::putback() {
             char *t;
             t = token;
             for(; *t; t++) exp_ptr--;
    }
    
    //Display Syntax Error
    template <class PType> void parser<PType>::serror(int error) {
             static char *e[] = {
                    "Syntax Error",
                    "Unbalanced Parentheses",
                    "No Expression Present"
             };
             cout << e[error] << endl;
    }
    
    //Obtain The Next Token
    template <class PType> void parser<PType>::get_token() {
             register char *temp;
             tok_type = 0;
             temp = token;
             *temp = '\0';
             if(!*exp_ptr) return; //end of expression
             while(isspace(*exp_ptr)) ++exp_ptr; //skip over the blanks
             if (strchr("+-*/%^=()", *exp_ptr)) {
                                     tok_type = DELIMITER;
                                     //advance to next char
                                     *temp++ = *exp_ptr++;
             }
             else if (isalpha(*exp_ptr)) {
                  while(!isdelim(*exp_ptr)) *temp++ = *exp_ptr++;
                  tok_type = NUMBER;
             }
             else if (isdigit(*exp_ptr)) {
                  while(!isdelim(*exp_ptr)) *temp++ = *exp_ptr++;
                  tok_type = NUMBER;
             }
             *temp = '\0';
    }
    
    //Return True If c Is A Delimiter
    template <class PType> PType parser<PType>::find_var(char *s) {
             if(!isalpha(*s)) {
                              serror(1);
                              return (PType) 0;
             }
             return vars[toupper(*token) - 'A'];
    }
    
    int main() {
        char expstr[80];
        parser<double> ob;
        cout << "Enter A period to stop\n";
        for(;;) {
                cout << "Enter expression: ";
                cin.getline(expstr, 79);
                if (*expstr=='.') break;
                cout << "Answer is: " << ob.eval_exp(expstr) << "\n\n";
        }
        cout << endl;
        return 0;
    }
    The main problems seem to be with the putback() function:

    the errors:

    D:\Programs\Parser.cpp In member function `void parser<PType>::eval_exp1(PType&)':
    69 D:\Programs\Parser.cpp there are no arguments to `putback' that depend on a template parameter, so a declaration of `putback' must be available
    69 D:\Programs\Parser.cpp (if you use `-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
    69 D:\Programs\Parser.cpp At global scope:
    181 D:\Programs\Parser.cpp no `void parser<PType>:utback()' member function declared in class `parser<PType>'
    181 D:\Programs\Parser.cpp template definition of non-template `void parser<PType>:utback()'
    D:\Programs\Parser.cpp In member function `void parser<PType>::eval_exp1(PType&) [with PType = double]':
    53 D:\Programs\Parser.cpp instantiated from `PType parser<PType>::eval_exp(char*) [with PType = double]'
    238 D:\Programs\Parser.cpp instantiated from here
    69 D:\Programs\Parser.cpp `putback' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each function it appears in.)
    Lines:

    69:
    Code:
    putback();
    181:
    Code:
    template <class PType> void parser<PType>::putback() {
    53:
    Code:
    eval_exp1(result);
    238:
    Code:
    cout << "Answer is: " << ob.eval_exp(expstr) << "\n\n";
    Thanks for any help you can give me.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    Bump :P

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    Sorry for the spam posting :P I fixed the problems, nowit just quits after i enter the string!!

    Code:
    //  GENERIC PARSER
    
    #include <iostream>
    #include <cstdlib>
    #include <cctype>
    #include <cstring>
    
    using namespace std;
    
    enum types { DELIMITER = 1, VARIABLE, NUMBER};
    
    const int NUMVARS = 26;
    
    template <class PType> class parser {
             char *exp_ptr; //points to the expression
             char token[80]; //holds current token
             char tok_type; //holds token's type
             PType vars[NUMVARS]; //holds variable's values
             
             void eval_exp1(PType &result);
             void eval_exp2(PType &result);
             void eval_exp3(PType &result);
             void eval_exp4(PType &result);
             void eval_exp5(PType &result);
             void eval_exp6(PType &result);
             void atom (PType &result);
             void get_token(), putback();
             void serror(int error);
             PType find_var(char *s);
             int isdelim(char c);
             public:
                    parser();
                    PType eval_exp(char *exp);
    };
    
    //parser constructor
    template <class PType> parser<PType>::parser() {
             int i;
             exp_ptr = NULL;
             for(i=0; i<NUMVARS; i++) vars[i] = (PType) 0;
    }
    
    //parser entry point
    template <class PType> PType parser<PType>::eval_exp(char *exp) {
             PType result;
             exp_ptr = exp;
             
             get_token();
             if(!*token) {
                         serror(2); //No Expression present
                         return (PType) 0;
             }
             eval_exp1(result);
             if(*token) serror(0); //Last Token Must Be NULL
             return result;
    }
    
    //Process an assignment
    template <class PType> void parser<PType>::eval_exp1(PType &result) {
             int slot;
             char ttok_type;
             char temp_token[80];
             if(tok_type==VARIABLE) {
                                    strcpy(temp_token, token);
                                    ttok_type = tok_type;
                                    slot = toupper(*token) - 'A';
                                    get_token();
                                    if(*token != '=') {
                                              putback();
                                              strcpy(token, temp_token);
                                              tok_type = ttok_type;
                                    }
                                    else {
                                         get_token();
                                         eval_exp2(result);
                                         vars[slot] = result;
                                         return;
                                    }
             }
             eval_exp2(result);
    }
    
    //Add Or Subtract Two Terms
    template <class PType> void parser<PType>::eval_exp2(PType &result) {
             register char op;
             PType temp;
             eval_exp3(result);
             while((op = *token) == '+' || op == '-') {
                       get_token();
                       eval_exp3(temp);
                       switch(op) {
                                  case '-':
                                       result = result - temp;
                                       break;
                                  case '+':
                                       result = result + temp;
                                       break;
                       }
             }
    }
    
    //Multiply Or Divide Two Factors
    template <class PType> void parser<PType>::eval_exp3(PType &result) {
             register char op;
             PType temp;
             eval_exp4(result);
             while((op = *token) == '*' || op == '/' || op == '%') {
                       get_token();
                       eval_exp4(temp);
                       switch(op) {
                                  case '*':
                                       result = result * temp;
                                       break;
                                  case '/':
                                       result = result / temp;
                                       break;
                                  case '%':
                                       result = (int) result % (int) temp;
                                       break;
                       }
             }
    }
    
    //Process an exponent
    template <class PType> void parser<PType>::eval_exp4(PType &result) {
             PType temp, ex;
             register int t;
             eval_exp5(result);
             if(*token == '^') {
                       get_token();
                       eval_exp4(temp);
                       ex = result;
                       if (temp == 0.0) {
                                result = (PType) 1;
                                return;
                       }
                       for (t=(int)temp-1; t>0; --t) result = result * ex;
             }
    }
    
    //Evaluate a unary + or -
    template <class PType> void parser<PType>::eval_exp5(PType &result) {
             register char op;
             op = 0;
             if((tok_type == DELIMITER) && *token == '+' || *token == '-') {
                          op = *token;
                          get_token();
             }
             eval_exp6(result);
             if(op=='-') result = -result;
    }
    
    //Processed a parenthesized expression.
    template <class PType> void parser<PType>::eval_exp6(PType &result) {
             if((*token == '(')) {
                        get_token();
                        eval_exp2(result);
                        if(*token != ')') serror(1);
                        get_token();
             }
             else atom(result);
    }
    
    //Get The Value Of A Number Or A Variable
    template <class PType> void parser<PType>::atom(PType &result) {
             switch(tok_type) {
                              case VARIABLE:
                                   result = find_var(token);
                                   get_token();
                                   return;
                              case NUMBER:
                                   result = (PType) atof(token);
                                   get_token();
                                   return;
                              default:
                                      serror(0);
             }
    }
    
    //Return A Token To The Input Stream
    template <class PType> void parser<PType>::putback() {
             char *t;
             t = token;
             for(; *t; t++) exp_ptr--;
    }
    
    //Display Syntax Error
    template <class PType> void parser<PType>::serror(int error) {
             static char *e[] = {
                    "Syntax Error",
                    "Unbalanced Parentheses",
                    "No Expression Present"
             };
             cout << e[error] << endl;
    }
    
    //Obtain The Next Token
    template <class PType> void parser<PType>::get_token() {
             register char *temp;
             tok_type = 0;
             temp = token;
             *temp = '\0';
             if(!*exp_ptr) return; //end of expression
             while(isspace(*exp_ptr)) ++exp_ptr; //skip over the blanks
             if (strchr("+-*/%^=()", *exp_ptr)) {
                                     tok_type = DELIMITER;
                                     //advance to next char
                                     *temp++ = *exp_ptr++;
             }
             else if (isalpha(*exp_ptr)) {
                  while(!isdelim(*exp_ptr)) *temp++ = *exp_ptr++;
                  tok_type = NUMBER;
             }
             else if (isdigit(*exp_ptr)) {
                  while(!isdelim(*exp_ptr)) *temp++ = *exp_ptr++;
                  tok_type = NUMBER;
             }
             *temp = '\0';
    }
    
    template <class PType> int parser<PType>::isdelim(char c) {
             if(strchr(" +-/*%^=()", c) || c==9 || c=='\r' || c==0) {
                         return 0;
             }
    }
    
    //Return True If c Is A Delimiter
    template <class PType> PType parser<PType>::find_var(char *s) {
             if(!isalpha(*s)) {
                              serror(1);
                              return (PType) 0;
             }
             return vars[toupper(*token) - 'A'];
    }
    
    int main() {
        char expstr[80];
        parser<double> ob;
        cout << "Enter A period to stop\n";
        for(;;) {
                cout << "Enter expression: ";
                cin.getline(expstr, 79);
                if (*expstr=='.') break;
                cout << "Answer is: " << ob.eval_exp(expstr) << "\n\n";
                cin.getline(expstr, 79);
        }
        cout << endl;
        return 0;
    }

  6. #6
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    try system("pause");
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Have you tried the most basic of debugging tools, putting output statements and pauses in your functions to track values and note where the problem seems to be occuring? What string are you entering?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    No need for the system pause, i slapped a cin.getline at the end.

    Im putting in somthing simple like 5*2.

    Ill try the printing thing.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    Tried what you said:

    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\Documents and Settings\Sam>D:\Programs\parser.exe
    Enter A period to stop
    Enter expression: 5+4
    Got Input
    Function Parser
    Function get_token
    Function delim
    Function serror
    No Expression Present
    Answer is: 0

    5*6
    Enter expression: 5*6
    Got Input
    Function Parser
    Function get_token
    Function delim
    Function serror
    No Expression Present
    Answer is: 0


    Enter expression: .
    Got Input

    Seems that it cant recognise the expression

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I get a warning when I compile your code:

    'parser<double>::isdelim' : not all control paths return a value
    Here is the isdelim function:
    Code:
    template <class PType> int parser<PType>::isdelim(char c) {
             if(strchr(" +-/*%^=()", c) || c==9 || c=='\r' || c==0) {
                         return 0;
             }
    }
    Notice that you only return a value if your input argument is one of those you are testing for. Otherwise... nothing. You are also returning the wrong value here (for how you are using it), in the if block you should return a 1, and you should also add a return 0; outside the if block.

    There may be another problem with the following function:

    Code:
    //Obtain The Next Token
    template <class PType> void parser<PType>::get_token() {
             register char *temp;
             tok_type = 0;
             temp = token;
             *temp = '\0';
             if(!*exp_ptr) return; //end of expression
             while(isspace(*exp_ptr)) ++exp_ptr; //skip over the blanks
             if (strchr("+-*/%^=()", *exp_ptr)) {
                                     tok_type = DELIMITER;
                                     //advance to next char
                                     *temp++ = *exp_ptr++;
             }
             else if (isalpha(*exp_ptr)) {
                  while(!isdelim(*exp_ptr)) *temp++ = *exp_ptr++;
                  tok_type = NUMBER;
             }
             else if (isdigit(*exp_ptr)) {
                  while(!isdelim(*exp_ptr)) *temp++ = *exp_ptr++;
                  tok_type = NUMBER;
             }
             *temp = '\0';
    }
    I'm not sure, but I think you meant to say VARIABLE there instead of NUMBER.

    I made the above changes and I got a correct result of 30 for the expression 5*6.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    Hey thanks alot

    I already changed the NUMBER bit, it was the return's that were wrong.

    thanks again for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with Parser Generators - Functions
    By jason_m in forum C Programming
    Replies: 1
    Last Post: 09-09-2008, 09:38 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Help! Bad parser :/.
    By Blackroot in forum C++ Programming
    Replies: 13
    Last Post: 03-07-2006, 11:08 AM
  4. Problem with a file parser.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2005, 09:54 AM
  5. Parser - need help!
    By thelma in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:06 PM