-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleParser.cpp
More file actions
193 lines (191 loc) · 3.32 KB
/
SimpleParser.cpp
File metadata and controls
193 lines (191 loc) · 3.32 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include<iostream>
#include<cstdlib>
#include<cctype>
#include<cstring>
using namespace std;
enum types { DELIMITER = 1, VARIABLE, NUMBER };
class SimpleParser
{
char *exp_ptr; //Points to the expression
char token[80]; //holds current token
char tok_type; // holds token type
void eval_exp2(double &result);
void eval_exp3(double &result);
void eval_exp4(double &result);
void eval_exp5(double &result);
void eval_exp6(double &result);
void get_token();
int isdelim(char c);
public:
SimpleParser();
double eval_exp(char *exp);
};
//Parser constructor
SimpleParser::SimpleParser()
{
exp_ptr = NULL;
}
//Parser entry point
double SimpleParser::eval_exp(char *exp)
{
double result;
exp_ptr = exp;
get_token();
if(!*token)
{
serror(2); //No expression currently
return 0.0;
}
eval_exp2(result);
if(*token)
serror(0); //last token must be NULL
return result;
}
//Add or subtract
void SimpleParser::eval_exp2(double &result)
{
register char op;
double 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
void SimpleParser::eval_exp3(double &result)
{
register char op;
double 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
void SimpleParser::eval_exp4(double &result)
{
double temp, ex;
register int t;
eval_exp5(result);
if(*token== '^')
{
get_token();
eval_exp4(temp);
ex = result;
if(temp==0.0)
{
result = 1.0;
return;
}
for(t=(int)temp-1; t>0; --t)
result = result * (double)ex;
}
}
//Evaluate unary + or -
void SimpleParser::eval_exp5(double &result)
{
register char op;
op = 0;
if((tok_type == DELIMITER) && *token=='+' || *token=='-')
{
op = *token;
get_token();
}
eval_exp6(result);
if(op=='-')
result = -result;
}
//Process parenthesis expression
void SimpleParser::eval_exp6(double &result)
{
if((*token == '('))
{
get_token();
eval_exp2(result);
if(*token != ')')
serror(1);
get_token();
};
else atom(result);
}
//Get the value of a number
void SimpleParser::atom(double &result)
{
switch(tok_type)
{
case NUMBER:
result = atof(token);
get_token();
return;
default:
serror(0);
}
}
//Display syntax error
void SimpleParser::serror(int error)
{
static char *e[]= {"Syntax error", "unbalanced parenthesis", "no expression present"};
cout << e[error] << endl;
}
//Get the next token
void SimpleParser::get_token()
{
register char *temp;
tok_type = 0;
temp = token;
*temp = '\0';
if(!*exp_ptr)
return;
while(isspace(*exp_ptr))
++exp_ptr;
if(strchr("+-*/%^=()", *exp_ptr))
{
tok_type = DELIMITER;
*temp++ = *exp_ptr++;
}
else if(isalpha(*exp_ptr))
{
while(!isdelim(*exp_ptr))
*temp++ = *exp_ptr++;
tok_type = VARIABLE;
}
else if(isdigit(*exp_ptr))
{
while(!isdelim(*exp_ptr))
*temp++ = *exp_ptr++;
tok_type = NUMBER;
}
*temp = '\0';
}
//Return true if c is delimiter
int SimpleParser::isdelim(char c)
{
if(strchr("+-*/%^=()", c || c==9 || c=='\r' || c==0)
return 1;
return 0;
}