Recursive Descent Interpreter

Pages: 12
Thanks
now my only problems are the
1
2
'const class IdNode' has no member named 'end' /'begin' // for interpreter.h 
'class IdNode' has no member named 'push_front'/'end'/'begin' //for interpreter.cpp  

and the weird stl_algo.h header file

=)
=)
YOU'RE a GREAT HELP
=)
=) I made new progress
created member functions in the class IdNode to solve the error messages it kinda looks like this
1
2
3
4
5
6
class IdNode
{ 
IdNode *begin;
IdNode *end;
IdNode *push_front;
}

The error message were replaced by other error messages such as
1
2
3
4
5
6
7
8
9
10
11
12
13
  
In function `std::ostream& operator<<(std::ostream&, const Statement&)':
s->Statement::idList.IdNode::begin' cannot be used as a function
s->Statement::idList.IdNode::end' cannot be used as a function 
//for interpreter.h
In member function `double Statement::findValue(char*)': 
((Statement*)this)->Statement::idList.IdNode::begin' cannot be used as a function 
((Statement*)this)->Statement::idList.IdNode::end' cannot be used as a function 
 In member function `void Statement::processNode(char*, double)': 
((Statement*)this)->Statement::idList.IdNode::begin' cannot be used as a function 
((Statement*)this)->Statement::idList.IdNode::end' cannot be used as a function 
((Statement*)this)->Statement::idList.IdNode::push_front' cannot be used as a function 
//for intepreter.cpp 


Please help, I have no idea on what to do
=)
Last edited on
Can I have a look at the code within std::ostream& operator<<(std::ostream&, const Statement&) ?

1
2
3
4
5
6
class IdNode
{ 
IdNode *begin;
IdNode *end;
IdNode *push_front;
}


begin, end push_front are not functions so you cannot use them like a function I presume that is why the C++ compiler is complaining.
Here are my interpreter.cpp and interpreter.h files
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
//**************************  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;
    }
     IdNode *end;
     IdNode *begin;
     IdNode *push_front;
     
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 

My original problem was
1
2
'const class IdNode' has no member named 'end' /'begin' // for interpreter.h 
'class IdNode' has no member named 'push_front'/'end'/'begin' //for interpreter.cpp   

When created member functions end,begin, and push_front for the class IdNode that's when the new error codes appear.

Is there a way to link my class IdNode and my class list since my class list already has member functions end,begin, and push_front .

I tried inheritance but it does not seem to work since my class list uses my class Iterator which in turn uses class IdNode. Which is why i resulted into making member functions of the same name in class IdNode. The only problem is that I do not know which data type to use. That is why i declared it as IdNode *begin;/*end;/*push_front;

I tired using other data types such as int, void, struct, class, friend, etc but it still odes not work

In need of HUGE HELP PLEASE =)

Thanks =)




Last edited on
Topic archived. No new replies allowed.
Pages: 12