I'm currently working on a recursive descent interpreter using C++. Though there is already a code on the project, I'm asked to rewrite the Recursive Descent Interpreter implementing it using a list class without using templates and facilites from the standard template library. I already worked through part of the code and I'm having a bit of trouble with finding a solution to an error. Here's the code.
//************************** interpreter.cpp ***********************
#include <cctype>
#include "interpreter.h"
Iterator list :: begin()
{
}
Iterator list :: end()
{
}
Iterator list :: push_front()
{
}
double Statement::findValue(char *id) {
IdNode tmp(id);
list::iterator i = find(idList.begin(),idList.end(),tmp);
if (i != idList.end())
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(),idList.end(),tmp);
if (i != idList.end())
i->value = e;
else idList.push_front(tmp);
}
// 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");
}
}
//************************** 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;
}
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 ();
Iterator end ();
Iterator push_front ();
};
class Statement {
public:
Statement() {
}
void getStatement();
private:
IdNode 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, const Statement& s) {
list::const_iterator i = s.idList.begin();
for ( ; i != s.idList.end(); i++)
out << *i;
out << endl;
return out;
}
};
#endif
//************************** 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.
}
The errors are that IdNode has no member named 'end', 'begin' and 'push_front'. Help guys. Thanks.