Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleParser.cpp
More file actions
Latest commit
193 lines (191 loc) · 3.32 KB
/
Copy pathSimpleParser.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>
usingnamespacestd;
enum types { DELIMITER = 1, VARIABLE, NUMBER };
classSimpleParser
{
char *exp_ptr; //Points to the expression
char token[80]; //holds current token
char tok_type; // holds token type
voideval_exp2(double &result);
voideval_exp3(double &result);
voideval_exp4(double &result);
voideval_exp5(double &result);
voideval_exp6(double &result);
voidget_token();
intisdelim(char c);
public:
SimpleParser();
doubleeval_exp(char *exp);
};
//Parser constructor
SimpleParser::SimpleParser()
{
exp_ptr = NULL;
}
//Parser entry point
doubleSimpleParser::eval_exp(char *exp)
{
double result;
exp_ptr = exp;
get_token();
if(!*token)
{
serror(2); //No expression currently
return0.0;
}
eval_exp2(result);
if(*token)
serror(0); //last token must be NULL
return result;
}
//Add or subtract
voidSimpleParser::eval_exp2(double &result)
{
registerchar 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
voidSimpleParser::eval_exp3(double &result)
{
registerchar 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
voidSimpleParser::eval_exp4(double &result)
{
double temp, ex;
registerint 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 -
voidSimpleParser::eval_exp5(double &result)
{
registerchar op;
op = 0;
if((tok_type == DELIMITER) && *token=='+' || *token=='-')
{
op = *token;
get_token();
}
eval_exp6(result);
if(op=='-')
result = -result;
}
//Process parenthesis expression
voidSimpleParser::eval_exp6(double &result)
{
if((*token == '('))
{
get_token();
eval_exp2(result);
if(*token != ')')
serror(1);
get_token();
};
elseatom(result);
}
//Get the value of a number
voidSimpleParser::atom(double &result)
{
switch(tok_type)
{
caseNUMBER:
result = atof(token);
get_token();
return;
default:
serror(0);
}
}
//Display syntax error
voidSimpleParser::serror(int error)
{
staticchar *e[]= {"Syntax error", "unbalanced parenthesis", "no expression present"};
cout << e[error] << endl;
}
//Get the next token
voidSimpleParser::get_token()
{
registerchar *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++;
}
elseif(isalpha(*exp_ptr))
{
while(!isdelim(*exp_ptr))
*temp++ = *exp_ptr++;
tok_type = VARIABLE;
}
elseif(isdigit(*exp_ptr))
{
while(!isdelim(*exp_ptr))
*temp++ = *exp_ptr++;
tok_type = NUMBER;
}
*temp = '\0';
}
//Return true if c is delimiter
intSimpleParser::isdelim(char c)
{
if(strchr("+-*/%^=()", c || c==9 || c=='\r' || c==0)
return1;
return0;
}