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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include "Lexer.h"
#include "term_control.h"
#include "error_handling.h"
#include "Postfix_Evaluator.h"
#include <map>
#include <cstdlib>

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<string,cmd_t> 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.