### Calculating using lexical analyzer/tokenizer

How can I calculate something in postfix notation like 5 + 4 by typing in calc "5 4 *" for some program. I have a Lexer that parses the string. For the Lexer, the tokens are identifiers, integers, and strings. I was able to get the program to recognize bye, so that it exits the program. What goes in the eval_calc function?
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````#include #include "Lexer.h" #include "term_control.h" #include "error_handling.h" #include "Postfix_Evaluator.h" #include #include using namespace std; typedef void (*cmd_t)(Lexer); void bye(Lexer); void eval_calc(Lexer); /** * ---------------------------------------------------------------------------------- * just print a prompt. * ---------------------------------------------------------------------------------- */ void prompt() { cout << term_cc(BLUE) << "> " << term_cc() << flush; } /** exits program */ void bye(Lexer lexer) { if (lexer.has_more_token()) { error_return("Syntax error: use bye/exit/quit\n"); } else { exit(0); } } /** calculate expression. */ void eval_calc(Lexer lexer) { } int main() { Lexer lexer; string line; Postfix_Evaluator pe; Token tok; map cmd_map; cmd_map["bye"] = &bye; cmd_map["calc"] = &eval_calc; while (cin) { prompt(); getline(cin, line); lexer.set_input(line); if (!lexer.has_more_token()) continue; tok = lexer.next_token(); if (tok.type != IDENT) { error_return("Syntax error\n"); continue; } if (cmd_map.find(tok.value) != cmd_map.end()) cmd_map[tok.value](lexer); else error_return("Unknown command"); } return 0; }``````
Last edited on
Topic archived. No new replies allowed.