-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
146 lines (138 loc) · 2.51 KB
/
parser.cpp
File metadata and controls
146 lines (138 loc) · 2.51 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <cctype>
#include "parser.h"
#include "add_node.h"
#include "op_node.h"
#include "num_node.h"
#include "sub_node.h"
#include "mul_node.h"
#include "unary_sub_node.h"
//influenced by Mr. Vagner's class and http://math.hws.edu/javanotes/c9/s5.html
base_node* parser::parse(const std::string&str)
{
parser p(str);
return p.create_tree();
}
parser::parser(const std::string& str) : str(str), tok(str)
{
}
/**
\brief creates the syntactic tree
*/
base_node* parser::create_tree()
{
base_node* node = NULL;
current_token = tok.next_token();
node = get_expression();
if(current_token != "")
{
if(node != NULL)
delete node;
throw parse_exception("invalid characters at the end of the input");
}
return node;
}
base_node* parser::get_expression()
{
//current_token = tok.next_token();
base_node* res = NULL;
bool negative_expr = false;
if(current_token == "-")
{
negative_expr = true;
current_token = tok.next_token();
}
try {
res = get_term();
if(negative_expr)
{
res = new unary_sub_node(res);
negative_expr = false;
}
while(current_token == "+" || current_token == "-")
{
if(current_token == "+")
{
current_token = tok.next_token();
res = new add_node(res, get_term());
}
else
{
current_token = tok.next_token();
res = new sub_node(res, get_term());
}
}
}
catch(parse_exception& e)
{
if(res != NULL)
delete res;
throw;
}
return res;
}
base_node* parser::get_term()
{
//current_token = tok.next_token();
base_node* res = NULL;
try {
res = get_factor();
while(current_token == "*")
{
if(current_token == "*")
{
current_token = tok.next_token();
res = new mul_node(res, get_factor());
}
}
}
catch(parse_exception& e)
{
if(res != NULL)
delete res;
throw;
}
return res;
}
base_node* parser::get_factor()
{
base_node* res = NULL;
if(is_number(current_token))
{
res = new num_node(current_token);
current_token = tok.next_token();
return res;
}
else if(current_token == "(")
{
current_token = tok.next_token();
res = get_expression();
if(current_token != ")")
{
delete res;
throw parse_exception("missing right parenthesis");
}
current_token = tok.next_token();
return res;
}
else
throw parse_exception("invalid input");
}
bool parser::is_number(const std::string& str) const
{
int dec_count = 0;
for(size_t i = 0; i < str.length(); i++)
{
if(!std::isdigit(str[i]))
{
if(str[i] == '.' && dec_count == 0)
{
dec_count++;
}
else
{
return false;
}
}
}
return true;
}