Segmentation fault exchange and add()

Hi, I am writing a compiler and currently I am trying to build a tree that has all the tokens that are necessary (relational operators, variables, etc) from the input file and has all the different parser functions. My scanner works fine and my parser parsed correctly before I added in the nodes to create the tree. Now I added in the nodes and it seg faults. I've had a few people look at my code and most couldn't figure out what the problem is. I also put the core dump and gdb and it returns gnu_cxx::__exchange and add() from /usr/... I tried looking this up and found nothing useful. Well here's all the code and test file.

I believe the seg fault takes place at the newNode function
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
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "parser.h"

using namespace std;

token tk;
FILE *file;

node_t* parser(FILE *infile){
	file = infile;
	node_t* p = s();
	return p;
}

node_t* s(){
	cout << "Starting parsing... \n";
	tk = scanner(file);
	node_t* p = program();
	cout << "Parsing was complete! no errors \n";
	return p;
}

node_t* newNode(node_t* p, string label){
	p = (node_t *) calloc(1, sizeof(node_t));
	cout << "damn" << "\n" << flush;
	p -> nodeID = label;
	cout << "damn2" << "\n" << flush;
	p -> tok;
	p -> child1 = NULL;
	p -> child2 = NULL;
	p -> child3 = NULL;
	p -> child4 = NULL;
    return p;
}

node_t* program(){
		node_t * p = newNode(p, "Program");
		p->child1 = var();
		if (tk.type.compare("DOtk") == 0){
			tk = scanner(file);
			p->child2 = block();
			if (tk.type.compare("RETURNtk") == 0){
				return p;
			}
			else { cout << "Expected a RETURNtk Failure on line: " << tk.linenum << " \n"; exit(0);}
		}	
		else { cout << "Failure on line: " << tk.linenum << " \n"; exit(0);}
}

node_t* var(){
	node_t* p = newNode(p, "Var");
	if (tk.type.compare("VARtk")  == 0){
		tk = scanner(file);
		if (tk.type.compare("IDtk") == 0){
			p->tok = tk;
			tk = scanner(file);
			p->child1 = mvars();
			if (tk.type.compare("DECtk") == 0){
				tk = scanner(file);
				return p;
			}
			else { cout << "Expected a DECtk Failure on line: " << tk.linenum << "\n"; exit(0);}
		}	
		else { cout << "Expected a IDtk Failure on line: " << tk.linenum << "\n"; exit(0);}
	}
	else {return p;}
}	

node_t* mvars(){
	node_t* p = newNode(p, "mVars");
	if (tk.type.compare("COLONtk") == 0){
		tk = scanner(file);
		if (tk.type.compare("IDtk") == 0){
			p->tok = tk;
			tk = scanner(file);
			p->child1 = mvars();
		}
		else { cout << "Expected an IDtk Failure on line: " << tk.linenum << " \n"; exit(0);}
	}
	else {return p;}
}

node_t* block(){
	node_t* p = newNode(p, "Block");
	if (tk.type.compare("STARTtk") == 0){
		tk = scanner(file);
		p->child1 = var();
		p->child2 = stats();
		if (tk.type.compare("FINISHtk") == 0){
			tk = scanner(file);
			return p;
		}
	}
	//else if (tk.type.compare("FINISHtk") == 0){
	//		tk = scanner(file);
	//		return;
	//}
	else { cout << "Expected a FINISH Failure on line: " << tk.linenum << " \n"; exit(0);}
}

node_t* stats(){
	cout << "STATS" << "\n";
	node_t* p = newNode(p, "stats");
	p->child1 = stat();
	p->child2 = mStat();
	return p;
}

node_t* stat(){
	node_t* p = newNode(p, "stat");
	if (tk.type.compare("READtk") == 0){
		p->child1 = in();
		return p;
	}
	else if (tk.type.compare("PRINTtk") == 0){
		p->child1 = out();
		return p;
	}
	else if (tk.type.compare("STARTtk") == 0){
		p->child1 = block();
		return p;
	}
	else if (tk.type.compare("IFtk") == 0){
		p->child1 = op_if();
		return p;
	}
	else if (tk.type.compare("REPEATtk") == 0){
		p->child1 = loop();
		return p;
	}
	else if (tk.type.compare("IDtk") == 0){
		p->child1 = assign();
		return p;
	}
	else { cout << "Expected an READtk, PRINTtk, STARTtk, IFtk, REPEATtk, or IDtk on line " << tk.linenum << "\n";}
	
}

node_t* mStat(){
	node_t* p = newNode(p, "mStat"); 
	if (tk.type.compare("READtk") == 0 ||
		tk.type.compare("PRINTtk") == 0 ||
		tk.type.compare("STARTtk") == 0 ||
		tk.type.compare("IFtk") == 0 ||
		tk.type.compare("REPEATtk") == 0 ||
		tk.type.compare("IDtk") == 0){
	p->child1 = stat();
	p->child2 = mStat();
	return p;
	}	
	else { return p; }
}

node_t* in(){
	node_t* p = newNode(p, "in");
	if (tk.type.compare("READtk") == 0){
		tk = scanner(file);
		if (tk.type.compare("IDtk") == 0){
			p->tok = tk;
			tk = scanner(file);
			if (tk.type.compare("DECtk") == 0){
				tk = scanner(file);
				return p;
			}
			else { cout << "Expected a DECtk Failure on line: " << tk.linenum << " \n"; exit(0);}
		}
		else { cout << "Expected an IDtk Failure on line: " << tk.linenum << " \n"; exit(0);}
	}
	else {return p;}
}

node_t* out(){
	node_t* p = newNode(p, "out");
	if (tk.type.compare("PRINTtk") == 0){
		tk = scanner(file);
		p->child1 = expr();		
		if (tk.type.compare("DECtk") == 0){
			tk = scanner(file);
			return p;
		}
		else { cout << "Expected a DECtk Failure on line: " << tk.linenum << " \n"; exit(0);}
	}
	else {return p;}
}

node_t* op_if(){
	node_t* p = newNode(p, "op_if");
	if (tk.type.compare("IFtk")  == 0){
		tk = scanner(file);
		if (tk.type.compare("L_BRACEtk") == 0){
			p->child1 = expr();
			p->child2 = RO();
			p->child3 = expr();
			if (tk.type.compare("R_BRACEtk") == 0){
				p->child4 = block();
			}
			else { cout << "Expected a R_BRACEtk Failure on line: " << tk.linenum << "\n"; exit(0);}
		}
		else { cout << "Expected a L_BRACEtk Failure on line: " << tk.linenum << "\n"; exit(0);}
	}
}

node_t* loop(){
	node_t* p = newNode(p, "loop");
	if (tk.type.compare("REPEATtk") == 0){
		tk = scanner(file);
		if (tk.type.compare("L_BRACEtk") == 0){
			tk = scanner(file);
			p->child1 = expr();
			p->child2 = RO();
			p->child3 = expr();
			if (tk.type.compare("R_BRACEtk") == 0){
				tk = scanner(file);
				p->child4 = block();
			}
			else { cout << "Expected a R_BRACEtk Failure on line: " << tk.linenum << " \n"; exit(0);}
		}
		else { cout << "Expected a L_BRACEtk Failure on line: " << tk.linenum << " \n"; exit(0);}
	}
	else { return p;}
	
}
node_t* assign(){
	node_t* p = newNode(p, "assign");
	if (tk.type.compare("IDtk") == 0){
		p->tok = tk;
		tk = scanner(file);
		if (tk.type.compare("ASSIGNtk") == 0){
			p->child1 = expr();
			if (tk.type.compare("DECtk") == 0){
				tk = scanner(file);
				return p;
			}
			else { cout << "Expected DECtk Failure on line: " << tk.linenum << " \n"; exit(0);}
		}
		else { cout << "Expected an ASSIGNtk Failure on line: " << tk.linenum << " \n";exit(0);}
	}
	else {return p;}
}

node_t* expr(){
	node_t* p = newNode(p, "expr");
	p->child1 = T();
	if (tk.type.compare("MULTItk") == 0){
		p->tok = tk;
		tk = scanner(file);
		p->child2 = expr();
		return p;
	}	
	if (tk.type.compare("DIVIDEtk") == 0){
		tk = scanner(file);
		p->child2 = expr();
		return p;
	}
	else{ p->child2 = T(); return p;}
}

node_t* T(){
	node_t* p = newNode(p, "T");
	p->child1 = F();
	if (tk.type.compare("ADDtk") == 0){
		p->tok = tk;
		tk = scanner(file);
		p->child2 = T();
		return p;
	}
	if (tk.type.compare("SUBtk") == 0){
		p->tok = tk;
		tk = scanner(file);
		p->child2 = T();
		return p;
	}
	return p;
}

node_t* F(){
	node_t* p = newNode(p, "F");
	if (tk.type.compare("SUBtk") == 0){
		p->tok = tk;
		tk = scanner(file);
		p->child1 = F();
		return p;
	}
	return p;
}

node_t* R(){
	node_t* p = newNode(p, "R");
	if (tk.type.compare("L_PARENtk") == 0){
		tk = scanner(file);
		p->child1 = expr();
		if (tk.type.compare("R_PARENtk") == 0){
			tk = scanner(file);	
			return p;
		}
		else { cout << "Expected R_PARENtk Failure on line: " << tk.linenum << "\n"; exit(0);}
	}
	if (tk.type.compare("IDtk") == 0){
		p->tok = tk;
		tk = scanner(file);
		return p;
	}
	if (tk.type.compare("NUMtk") == 0){
		p->tok = tk;
		tk = scanner(file);
		return p;
	}
	else {return p;}
}

node_t* RO(){
	node_t* p = newNode(p, "RO");
	if (tk.type.compare("L_THANtk") == 0){
		p->tok = tk;
		tk = scanner(file);
		return p;
	}
	if (tk.type.compare("G_THANtk") == 0){
		p->tok = tk;
	        tk = scanner(file);
			        return p;
	} 
	if (tk.type.compare("OP_EQUALtk") == 0){
		p->tok = tk;
	        tk = scanner(file);
			        return p;
	} 
	if (tk.type.compare("LT_EQUALtk") == 0){
		p->tok = tk;
	        tk = scanner(file);
			        return p;
    } 
	if (tk.type.compare("GT_EQUALtk") == 0){
		p->tok = tk;
	        tk = scanner(file);
			        return p;
    } 
	if (tk.type.compare("EQ_BANGtk") == 0){
		p->tok = tk;
	        tk = scanner(file);
			        return p;
	} 
	else { cout << "Expected a Relational Operator Failure on line: " << tk.linenum << "\n"; exit(0);}
}


I'll post the rest of the code
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
#include <string>
#include <iostream>
#include <stdio.h>
#include <ostream>
#include "scanner.h"
#include "token.h"
#include "node.h"

extern token tk;
extern FILE *file;

node_t* parser(FILE *file);
node_t* newNode(node_t*, std::string);

node_t* s();
node_t* program();
node_t* var();
node_t* mvars();
node_t* block();
node_t* stats();
node_t* expr();
node_t* T();
node_t* F();
node_t* R();
node_t* mStat();
node_t* stat();
node_t* in();
node_t* out();
node_t* op_if();
node_t* loop();
node_t* assign();
node_t* RO();


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
#include "scanner.h"

int linenum = 1;

token scanner(FILE *file){
	int currentState = 0;
	int nextState = 0;
	token tk;
	char lookAhead = fgetc(file);
	if (isalpha(lookAhead)){
		lookAhead = tolower(lookAhead);
	}
	if (lookAhead == '#'){
		while (lookAhead != '\n'){
			lookAhead = fgetc(file);
		}
	}
	int stateTableColumnNumber = lookUpTable(lookAhead);
	while(!(currentState > 30)){
		nextState = stateTable[currentState][stateTableColumnNumber];
		//BREAK FOR FA ERROR
		if (nextState == -1){
			std::cout << "Hit ! bang error! at Line Number: " << linenum << "\n";
			std::exit(1);
		}
		//BREAK FOR UNEXPECTED CHARACTER
		if (nextState == 30){
			std::cout << "Hit an unexpected character! at Line Number: " << tk.linenum << "\n";
			std::exit(1);
		}
		if (stateTableColumnNumber == 33){
			while( lookAhead != '\n'){
				lookAhead = fgetc(file);
			}
			break;
		}
		//IF STATE IS NOT ENDSTAE OR EOF OR FAILURE GET NEXTSTATE			
		if (!(nextState > 49)){
			currentState = nextState;
			if (isalpha(lookAhead)){
				lookAhead = tolower(lookAhead);
			}
			if (!isspace(lookAhead)){
				tk.lexeme.append(1, lookAhead);
			}
			lookAhead = fgetc(file);
			stateTableColumnNumber = lookUpTable(lookAhead);
			}
		//BREAK IF EOF
		if (nextState == 24){
			break;
		}
		if (nextState > 49){
			if (lookAhead == '\n'){ linenum++;}
			ungetc(lookAhead, file);
			break;
		}
			
	}
	//GET TOKEN LINE NUMBER
	tk.linenum = linenum;
	//CHANGE CURRENT ENDSTATE NUM TO TOKEN TYPE
	switch (nextState){
		case 50 : tk.type = "IDtk";
			break;
		case 51 : tk.type = "NUMtk";
			break;
		case 52 : tk.type = "ASSIGNtk";
			break;
		case 53 : tk.type = "L_THANtk";
			break;
		case 54 : tk.type = "G_THANtk";
			break;
		case 55 : tk.type = "COLONtk";
			break;
		case 56 : tk.type = "ADDtk";
			break;
		case 57 : tk.type = "SUBtk";
			break;
		case 58 : tk.type = "MULTItk";
			break;
		case 59 : tk.type = "DIVIDEtk";
			break;
		case 60 : tk.type = "MODtk";
			break;
		case 61 : tk.type = "DECtk";
			break;	
		case 62 : tk.type = "L_PARENTHtk";
			break;
		case 63 : tk.type = "R_PARENTHtk";
			break;
		case 64 : tk.type = "COMMAtk";
			break;
		case 65 : tk.type = "L_CURLYtk";
			break;
		case 66 : tk.type = "R_CURLYtk";
			break;
		case 67 : tk.type = "S_COLONtk";
			break;
		case 68 : tk.type = "L_BRACEtk";
			break;
		case 69 : tk.type = "R_BRACEtk";
			break;
		case 70 : tk.type = "OP_EQUALtk";
			break;
		case 71 : tk.type = "LT_EQUALtk";
			break;
		case 72 : tk.type = "GT_EQUALtk";
			break;
		case 73 : tk.type = "EQ_BANGtk";
			break;
		case 24 : tk.type = "EOFtk";
			tk.lexeme = "EOF";
			break;
		}
		//LOOK UP SPECIFIC KEYWORD
		if (tk.type == "IDtk"){
			for (int i = 0; i < 15; i++){
				if (tk.lexeme == keywordTable[i].lexeme){
					tk.type = keywordTable[i].type;
				}
			}
		}
		return tk;
}


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
#include <cstdio>
#include <fstream>
#include <iostream>
#include "token.h"
#include "lookUpTable.h"
							   // L,  D,  =,  <,  >,  !,  :,  +,  -,  *,  /,  %,  .,  (,  ),  ,,  {,  },  ;,  [,  ], WS, EOF
const int stateTable[26][24] = {{  1,  2,  3,  4,  5, -1,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,  0, 24, 30},//row[0]
							   {  1, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 24, 30},//IDtk
							   { 51,  2, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 24, 30},//NUMtk
							   { 52, 52, 22, 23, 24,  6, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 24, 30},//ASSIGNtk
							   { 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 24, 30},//L_THANtk
							   { 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 24, 30},//G_THANtk
							   { -1, -1, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 24, 30},
							   { 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 24, 30},//COLONtk
							   { 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 24, 30},//ADDtk
							   { 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 24, 30},//SUBtk
							   { 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 24, 30},//MULTItk
							   { 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 24, 30},//DIVIDEtk
							   { 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 24, 30},//MODtk
							   { 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 24, 30},//DECtk
							   { 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 24, 30},//L_PARENtk
							   { 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 24, 30},//R_PARENtk
							   { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 24, 30},//COMMAtk
							   { 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 24, 30},//L_CURLYtk
							   { 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 24, 30},//R_CURLYtk
							   { 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 24, 30},//S_COLONtk
							   { 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 24, 30},//L_BRACEtk
							   { 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 24, 30},//R_BRACEtk
							   { 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 24, 30},//OP_EQUALtk
							   { 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 24, 30},//L_EQUALtk
							   { 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 24, 30},//G_EQUALtk
							   { 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 24, 30}};//EQ_BANGtk

const token keywordTable[15] = {{ 0, "start", "STARTtk" },
							    { 0, "finish","FINISHtk"},		
								{ 0, "then",  "THENtk"  },
								{ 0, "if",    "IFtk"    },
								{ 0, "repeat","REPEATtk"},
								{ 0, "var",   "VARtk"   },
								{ 0, "int",   "INTtk"   },
								{ 0, "float", "FLOATtk" },
								{ 0, "do",    "DOtk"    },
								{ 0, "read",  "READtk"  },
								{ 0, "print", "PRINTtk" },
								{ 0, "void",  "VOIDtk"  },
								{ 0, "return","RETURNtk"},
								{ 0, "dummy", "DUMMYtk" },
								{ 0, "program","PROGRAMtk"}};





								

token scanner(FILE *file);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef NODE_H
#define NODE_H
#include <string>
#include "token.h"

using namespace std;

struct node_t {
	string nodeID;	
	struct token tok;
	struct node_t* child1;
	struct node_t* child2;
	struct node_t* child3;
	struct node_t* child4;
};
#endif  


1
2
3
4
5
6
7
8
9
#include <string>
#ifndef TOKEN_H
#define TOKEN_H
struct token {
	int linenum;
	std::string lexeme;
	std::string type;
};
#endif 
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
#include "traversals.h"
 

/*void traverseInorder(node_t * root){
	if ( root ){
	traverseInorder(root->LEFTER);
	//for (int i = 0; i < count; i++){
	//	std::cout << "		";
	//}
	std::cout << root->l << " ";
	for (int i = 1; i < root->words.size(); i++){
		std::cout << root->words[i] << " "; 
	}
	std::cout << std::endl;
	traverseInorder(root->LEFT);
	traverseInorder(root->RIGHT);
	}

}*/

void traversePreorder(node_t * root){
	if (root){
		std::cout << "Node ID: " << root->nodeID << " ";
		std::cout << "Token type: " << root->tok.type << " ";
		std::cout << "Line Number: " << root->tok.linenum << "\n";
		traversePreorder(root->child1);
		traversePreorder(root->child2);
		traversePreorder(root->child3);
		traversePreorder(root->child4);
	}
}

/*void traversePostorder(node_t * root){
	if (root){
		traversePostorder(root->LEFTER);
		traversePostorder(root->LEFT);
		traversePostorder(root->RIGHT);
		std::cout << root->l << " ";
		for (int i = 1; i < root->words.size(); i++){
			std::cout << root->words[i] << " ";
		}
		std::cout << std::endl;
	}
}*/


1
2
3
4
5
6
#include <iostream>
#include "node.h"

//void traverseInorder(node_t * root);
void traversePreorder(node_t * root);
//void traversePostorder(node_t * root); 


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
#include <cctype> 
#include <cstdio>
#include "lookUpTable.h"

int lookUpTable(char currChar){
	typedef struct tableKey {
		int key;
		int val;
	};

	tableKey charTable[20] = {
		{'=',2},
		{'<',3},
		{'>',4},
		{'!',5},
		{':',6},
		{'+',7},
		{'-',8},
		{'*',9},
		{'/',10},
		{'%',11},
		{'.',12},
		{'(',13},
		{')',14},
		{',',15},
		{'{',16},
		{'}',17},
		{';',18},
		{'[',19},
		{']',20},
		{'#',33}
	};

	if(isalpha(currChar)){
		return 0;
	}
	else if (isdigit(currChar)){
		return 1;
	}
	else if (isspace(currChar)){
		return 21;
	}	
	else if (currChar == EOF){
		return 22;
	}
	else {
		for (int i = 0; i < 20; i++){
			if(charTable[i].key == currChar){
				return charTable[i].val;
			}
		}
	}
	return 30; //error number

}


int lookUpTable(char);

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
#include <cstdio>
#include <iostream>
#include <string>
#include <fstream>
#include "parser.h"
#include "node.h"
#include "traversals.h"

using namespace std;

int main(int argc, char* argv[]){
if (argc = 0){
	cout << "Please use a file";
	exit(0);
/*
	FILE *file;
	file = fopen("p1.dat", "r");
	while(fgets(buffer, sizeof(buffer), file)) {
		fprintf(file, "%s", buffer);
	}
	fclose(file);
	getTokens(file);
*/
}
else {
	char* newfilename;
	FILE *file;
	char* filename = argv[1];
	int filelen = sizeof(filename);
	/*if (filelen >  3 && filename[filelen-4] != 46){
		newfilename = strcat(filename, ".lan");
		cout << newfilename << "\n";
	}
	else {newfilename = strcat(filename, ".lan");
		cout << newfilename << "\n";
		}
	*/
	file = fopen(filename, "r");
	node_t* p = parser(file);
	cout << "Preorder Traversal :" << endl;
	traversePreorder(p);
	return 0;
	}
return 1;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
all: testParser 

testParser: main.o parser.o scanner.o lookUpTable.o traversals.o
	g++ -o testParser main.o parser.o scanner.o lookUpTable.o traversals.o

main.o: main.cpp node.h
	g++ -c main.cpp

lookUpTable.o: lookUpTable.cpp lookUpTable.h
	g++ -c lookUpTable.cpp

scanner.o: scanner.cpp scanner.h token.h lookUpTable.h
	g++ -c scanner.cpp 

parser.o: parser.cpp parser.h node.h
	g++ -c parser.cpp

traversals.o: traversals.cpp traversals.h node.h
	g++ -c traversals.cpp

clean:
	rm -f *o


And finally a little testing file I used for my input.

1
2
3
4
5
6
var x.
do
start
read x.
finish
return


Thanks for taking a look at my code, I really have no idea of how to fix this.
You are allocating memory for C++ objects with C allocation functions. Constructors and destructors are not invoked so those objects are always in invalid states.

if (argc = 0) always evaluates to false since you're setting the value of argc to 0, then checking to see if it is non-zero.


Thanks for the help! It seems now I am having a seg fault, but now with p->tok = tk;

Is this because I don't initially instantiate the token of the node? I'm not sure why I can't assign a token to the token type in the node when it was defined this way.
Okay so I have figured out that specifically the string attributes of my token are failing at being accessed no matter what. Is there a reason I can't do this? It works fine when I access them with tokens, but when I try to instantiate them with anything and then try to access them it seg faults every time. If I never assign anything to them it goes farther until I try to access them. Any insight would be greatly appreciated I'm really confused on why this fails.
Topic archived. No new replies allowed.