-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
68 lines (60 loc) · 1.34 KB
/
main.cpp
File metadata and controls
68 lines (60 loc) · 1.34 KB
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
64
65
66
67
68
#include "parser.hpp"
#include "lexer.hpp"
#include "error.hpp"
#include "table.hpp"
#include "driver.hpp"
#include <iostream>
#include <sstream>
using namespace std;
using namespace Error;
using namespace Lexer;
using namespace Parser;
using namespace Driver;
int Driver::no_of_errors = 0;
std::istream* Driver::input = 0;
void Driver::skip() {
no_of_errors++;
while (*input) {
char ch;
input->get(ch);
switch (ch) {
case '\n':
case ';':
input->get(ch);
return;
}
}
}
int main(int argc, char* argv[]) {
table["pi"] = 3.14159265;
table["e"] = 2.71828182;
switch (argc) {
case 1:
input = &cin;
break;
case 2:
input = new istringstream(argv[1]);
break;
default:
// error("too many arguments");
return 1;
}
while(*input) {
try {
get_token();
if (curr_tok == END) break;
if (curr_tok == PRINT) continue;
cout << expr(false) << '\n';
}
catch (Zero_divide) {
cerr << "attempt to divide by zero\n";
skip();
}
catch (Syntax_error e) {
cerr << "syntax error:" << e.p << "\n";
skip();
}
if (input != &cin) delete input;
return no_of_errors;
}
}