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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
//************************** interpreter.h ***********************
#ifndef INTERPRETER
#define INTERPRETER
#include <iostream>
//#include <list>
//#include <algorithm> // find()
using namespace std;
class IdNode {
public:
IdNode(char *s = "", double e = 0) {
id = strdup(s);
value = e;
}
bool operator== (const IdNode& node) const {
return strcmp(id,node.id) == 0;
}
IdNode *prev;
IdNode *next;
string element;
private:
char *id;
double value;
friend class Statement;
friend ostream& operator<< (ostream& out, const IdNode& r) {
out << r.id << " = " << r.value << endl;
return out;
}
};
class Iterator
{
public:
IdNode *node;
Iterator &operator ++ (int);
IdNode &operator * ();
IdNode *operator -> ();
bool operator != (bool);
};
class list
{
IdNode *head, *tail;
public:
typedef Iterator iterator;
typedef Iterator const_iterator;
Iterator begin (const string&);
Iterator end (const string&);
Iterator push_front ();
bool empty() const { return head==NULL; }
};
class Statement {
public:
Statement() {
}
void getStatement();
private:
list idList;
char ch;
double factor();
double term();
double expression();
void readId(char*);
void issueError(char *s) {
cerr << s << endl; exit(1);
}
double findValue(char*);
void processNode(char*, double);
friend ostream& operator<< (ostream& out, Statement& s) {
list::const_iterator i = s.idList.begin(const string&);
for ( ; i != s.idList.end(const string&); i++)
out << *i;
out << endl;
return out;
}
};
#endif
//************************** interpreter.cpp ***********************
#include <cctype>
#include "interpreter.h"
Iterator list :: begin(const string& h)
{
if(empty())
{
IdNode* temp = new IdNode;
head = temp;
tail = temp;
temp->prev = NULL;
temp->next = NULL;
temp->element = h;
}
else
{
IdNode* curr;
curr = tail;
while( h>curr->element && curr->prev != head->prev) curr = curr->prev;
if(curr == tail)
{
IdNode* temp = new IdNode;
temp->element = h;
temp->next = curr;
temp->prev = NULL;
head->prev = temp;
head = temp;
}
else
{
if(curr == head && h>head->element)
{
head->prev = new IdNode;
(head->prev)->next = head;
head = head->prev;
head->prev = NULL;
head->element = h;
}
else
{
IdNode* temp = new IdNode;
temp->element = h;
temp->prev = curr;
(curr->next)->prev = temp;
temp->next = curr->next;
curr->next = temp;
}
}
}
}
Iterator list :: end(const string& t)
{
if(empty())
{
IdNode* temp = new IdNode;
head = temp;
tail = temp;
temp->prev = NULL;
temp->next = NULL;
temp->element = t;
}
else
{
IdNode* curr;
curr = head;
while( t>curr->element && curr->next != tail->next) curr = curr->next;
if(curr == head)
{
IdNode* temp = new IdNode;
temp->element = t;
temp->prev = curr;
temp->next = NULL;
head->next = temp;
tail = temp;
}
else
{
if(curr == tail && t>tail->element)
{
tail->next = new IdNode;
(tail->next)->prev = tail;
tail = tail->next;
tail->next = NULL;
tail->element = t;
}
else
{
IdNode* temp = new IdNode;
temp->element = t;
temp->next = curr;
(curr->prev)->next = temp;
temp->prev = curr->prev;
curr->prev = temp;
}
}
}
}
Iterator list :: push_front()
{
}
double Statement::findValue(char *id) {
IdNode tmp(id);
list::iterator i = find(idList.begin(const string&),idList.end(const string&),tmp);
if (i != idList.end(const string&))
return i->value;
else issueError("Unknown variable");
return 0; // this statement will never be reached;
}
void Statement::processNode(char* id ,double e) {
IdNode tmp(id,e);
list::iterator i = find(idList.begin(const string&),idList.end(const string&),tmp);
if (i != idList.end(const string&))
i->value = e;
else idList.push_front();
}
// readId() reads strings of letters and digits that start with
// a letter, and stores them in array passed to it as an actual
// parameter.
// Examples of identifiers are: var1, x, pqr123xyz, aName, etc.
void Statement::readId(char *id) {
int i = 0;
if (isspace(ch))
cin >> ch; // skip blanks;
if (isalpha(ch)) {
while (isalnum(ch)) {
id[i++] = ch;
cin.get(ch); // don't skip blanks;
}
id[i] = '\0';
}
else issueError("Identifier expected");
}
double Statement::factor() {
double var, minus = 1.0;
static char id[200];
cin >> ch;
while (ch == '+' || ch == '-') { // take all '+'s and '-'s.
if (ch == '-')
minus *= -1.0;
cin >> ch;
}
if (isdigit(ch) || ch == '.') { // Factor can be a number
cin.putback(ch);
cin >> var >> ch;
}
else if (ch == '(') { // or a parenthesized expression,
var = expression();
if (ch == ')')
cin >> ch;
else issueError("Right paren left out");
}
else {
readId(id); // or an identifier.
if (isspace(ch))
cin >> ch;
var = findValue(id);
}
return minus * var;
}
double Statement::term() {
double f = factor();
while (true) {
switch (ch) {
case '*' : f *= factor(); break;
case '/' : f /= factor(); break;
default : return f;
}
}
}
double Statement::expression() {
double t = term();
while (true) {
switch (ch) {
case '+' : t += term(); break;
case '-' : t -= term(); break;
default : return t;
}
}
}
void Statement::getStatement() {
char id[20], command[20];
double e;
cout << "Enter a statement: ";
cin >> ch;
readId(id);
strcpy(command,id);
for (int i = 0; i < strlen(command); i++)
command[i] = toupper(command[i]);
if (strcmp(command,"STATUS") == 0)
cout << *this;
else if (strcmp(command,"PRINT") == 0) {
readId(id);
cout << id << " = " << findValue(id) << endl;
}
else if (strcmp(command,"END") == 0)
exit(0);
else {
if (isspace(ch))
cin >> ch;
if (ch == '=') {
e = expression();
if (ch != ';')
issueError("There are some extras in the statement");
else processNode(id,e);
}
else issueError("'=' is missing");
}
}
//************************** useInterpreter.cpp ***********************
#include "interpreter.h"
int main() {
Statement statement;
cout << "The program processes statements of the following format:\n"
<< "\t<id> = <expr>;\n\tprint <id>\n\tstatus\n\tend\n\n";
while (true) // This infinite loop is broken by exit(1)
statement.getStatement(); // in getStatement() or upon finding
return 0; // an error.
}
|