problem with recursive descent parser

I am writing a code in c++ to do a recursive descent parser/evaluation for int's for a grammar. I will say right now yes this is homework. I have made a lot of progress on this, but am running into a problem when i come to a closing parenthesis. I believe the problem to be in my parseF function around line 173 but am uncertain. A point in the right direction would be greatly appreciated(I hope it is easy enough to read).

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
/*****************************/
/*  E-->C|CAC                 |
/*  C-->F|FMF                 |
/*  F-->N|(E)                 |
/*  N-->SI|I                  |
/*  I-->DI|D                  |
/*  D-->0|1|2|3|4|5|6|7|8|9   |
/*  A-->+|-                   |
/*  S-->-                     |
/*  M-->*|/|%                 |
******************************/

#include <iostream>
#include <cstring>
#define DEBUG 1

using namespace std;

int parseE(int *val, char *inp, int *idx);
int parseC(int *val, char *inp, int *idx);
int parseF(int *val, char *inp, int *idx);
int parseN(int *val, char *inp, int *idx);
int parseS(int *val, char *inp, int *idx);
int parseI(int *val, char *inp, int *idx);
int parseD(int *val, char *inp, int *idx);
int parseM(int *val, char *inp, int *idx);
int parseA(int *val, char *inp, int *idx);

int main()
{
        int val, idx;
        val = idx = 0;
        char *inp = new char[256];
        strcpy(inp, "(13+6)+4");
        printf("Input string: %s\n", inp);
        int answer = parseE(&val, inp, &idx);
        cout << answer << endl;
        cout << val << endl;

}

int parseE(int *val, char *inp, int *idx)
{
        int temp;
        #if DEBUG
        printf("parsing E on %s \n", &(inp[*idx]));
        #endif

        if(!parseC(val, inp, idx))
                return 0;

        if(inp[*idx] != '+' && inp[*idx] != '-')
                return 1;

        if(!parseA(val, inp, idx))
                return 0;

        temp = *val;

        if(inp[*idx - 1] == '+')
        {
                if(!parseC(val, inp, idx))
                        return 0;
                *val = temp + *val;
        }
        else
        {
                if(!parseC(val, inp, idx))
                        return 0;
                *val = temp - *val;
        }
        return 1;
}

int parseI(int *val, char *inp, int *idx)
{
        int temp;
        #if DEBUG
        printf("parsing I on %s \n", &(inp[*idx]));
        #endif
        if(!parseD(val, inp, idx))
        {
        fprintf(stderr, "ERROR: Expecting digit but received %c \n", inp[*idx]);

                        return 0;
        }

        temp = *val;

        while(*idx < strlen(inp) && isdigit(inp[*idx]))
        {
                if(!parseD(val, inp, idx))
                        return 0;
                temp *= 10;
                temp += *val;
        }

        *val = temp;
        return 1;
}

int parseD(int *val, char *inp, int *idx)
{
        #if DEBUG
        printf("parsing D on %s \n", &(inp[*idx]));
        #endif
        if(isdigit(inp[*idx]))
        {
                *val = inp[*idx] - '0';
                *idx += 1;
                return 1;
        }
        return 0;
}

int parseC(int *val, char *inp, int *idx)
{
        int temp;
        #if DEBUG
        printf("parsing C on %s \n", &(inp[*idx]));
        #endif

        if(!parseF(val, inp, idx))
                return 0;

        if(inp[*idx] != '*' && inp[*idx] != '/' && inp[*idx] != '%')
                return 1;

        if(!parseM(val, inp, idx))
                return 0;  //possibly return 1;

        temp = *val;

        if(inp[*idx - 1] == '*')
        {
                if(!parseF(val, inp, idx))
                        return 0;
                *val = temp * *val;
        }
        else if(inp[*idx - 1] == '/')
        {
                if(!parseF(val, inp, idx))
                        return 0;
                *val = temp / *val;
        }
        else if(inp[*idx - 1] == '%')
        {
                if(!parseF(val, inp, idx))
                        return 0;
                *val = temp % *val;
        }
        return 1;
}

int parseF(int *val, char *inp, int *idx)
{
        #if DEBUG
        printf("parsing F on %s \n", &(inp[*idx]));
        #endif
        if(inp[*idx] == '(')
        {
                *idx++;
                if(!parseE(val, inp, idx))
                        return 0;

                if(inp[*idx] != ')')
                {
                fprintf(stderr,"ERROR: Expecting ')' but received '%c'\n", inp[*idx]);

                        *idx++;
                        return 0;
                }
                else                     //look  here
                        *idx++;

                return 1;

        }
        else
        {
                if (!parseN(val, inp, idx))
                        return 0;
        }
        return 0;
}

int parseN(int *val, char *inp, int *idx)
{
        *val = 0;
        int flag = parseS(val, inp, idx); // found -

        #if DEBUG
        printf("parsing N on %s \n", &(inp[*idx]));
        #endif
        if(!parseI(val, inp, idx))
        {
                return 0;
        }
        if(flag)
                *val *= -1;
}

int parseS(int *val, char *inp, int *idx)
{
        int temp = *val;
        #if DEBUG
        printf("parsing S on %s \n", &(inp[*idx]));
        #endif
        if(inp[*idx] == '-')
        {
                *idx += 1;
                return 1;
        }
        return 0;
}

int parseM(int *val, char *inp, int *idx)
{
        #if DEBUG
        printf("parsing M on %s \n", &(inp[*idx]));
        #endif
        if(inp[*idx] == '*' || inp[*idx] == '/' || inp[*idx] == '%')
        {
                *idx += 1;
                return 1;
        }
        return 0;
}

int parseA(int *val, char *inp, int *idx)
{
        #if DEBUG
        printf("parsing A on %s \n", &(inp[*idx]));
        #endif
        if(inp[*idx] == '-' || inp[*idx] == '+')
        {
                *idx += 1;
                return 1;
        }
        return 0;
}
Topic archived. No new replies allowed.