Skip to content

Commit 4a80d4a

Browse files
committed
Major memleaks removed
Errors now stored in ll
1 parent ada8be1 commit 4a80d4a

16 files changed

+140
-20
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77

88
all: all_dependencies machine_grammar
9-
gcc -o driver.exe *.o
9+
gcc -g -o driver.exe *.o # please don't remove -g
1010

1111
machine_grammar:
1212
python3 generate_grammar.py -o machine_grammar.txt --grammar-struct-out grammar_structs --json_in symbols.json grammar.txt
1313

1414
all_dependencies: machine_grammar
15-
gcc -c *.c
15+
gcc -g -c *.c
1616

1717
.PHONY: clean
1818
clean:

driver.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Group 36
77
*/
88

99
#include <stdlib.h>
10+
#include "gen_utils.h"
1011
#include "grammar.h"
1112
#include "tokenizer_structs.h"
1213
#include "parse_tree.h"
@@ -55,9 +56,9 @@ int main(int argc, char** argv){
5556
printf("\n\nMenu: \n");
5657
printf("--------------------------------------------------------\n");
5758
printf("0 - Exit\n");
58-
printf("1 - Create parse tree\n");
59+
printf("1 - print parse tree (without types syntax only)\n");
5960
printf("2 - Traverse parse tree, create Type expression table, display type errors\n");
60-
printf("3 - print parse tree\n");
61+
printf("3 - print parse tree (with types computed)\n");
6162
printf("4 - print type expression table\n");
6263
printf("--------------------------------------------------------\n");
6364

@@ -66,6 +67,8 @@ int main(int argc, char** argv){
6667

6768
switch(option){
6869
case 0:
70+
freeTokenStream(s);
71+
// freeGrammar(g);
6972
break;
7073
case 1:
7174
{
@@ -78,6 +81,7 @@ int main(int argc, char** argv){
7881
}
7982
else printf("parse tree created successfully\n\n");
8083
printParseTree(p,0);
84+
free_parse_tree(p);
8185
break;
8286
}
8387
case 2:
@@ -91,7 +95,11 @@ int main(int argc, char** argv){
9195
exit(1);
9296
}
9397
else printf("parse tree created successfully\n\n");
98+
init_errors();
9499
traverse_and_populate(t, p);
100+
print_all_errors();
101+
free_parse_tree(p);
102+
free_type_expression_table(t);
95103
break;
96104
}
97105
case 3:
@@ -105,9 +113,12 @@ int main(int argc, char** argv){
105113
}
106114
else printf("parse tree created successfully\n\n");
107115
type_exp_table *t = create_type_expression_table();
116+
init_errors();
108117
traverse_and_populate(t, p);
109118
printParseTree(p,0);
110119
printf("\n");
120+
free_parse_tree(p);
121+
free_type_expression_table(t);
111122
break;
112123
}
113124
case 4:
@@ -123,9 +134,14 @@ int main(int argc, char** argv){
123134
}
124135
else
125136
printf("parse tree created successfully\n\n");
137+
138+
init_errors();
126139
traverse_and_populate(t, p);
140+
print_all_errors();
127141
printf("Populated Type expression table successfully\n");
128142
print_type_exp_table(t);
143+
free_parse_tree(p);
144+
free_type_expression_table(t);
129145
/* printParseTree(p, 0); */
130146
break;
131147
}

gen_utils.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Group 36
66
2017B4A70740P Aditya Tulsyan
77
*/
88

9+
#include "linked_list.h"
910
#include <stdio.h>
1011

1112
#define KNRM "\x1B[0m"
@@ -23,6 +24,8 @@ Group 36
2324
#include "parse_tree.h"
2425
#include <stdlib.h>
2526

27+
linked_list * all_errors = NULL;
28+
2629
void assert(bool condition, char *error_string) {
2730
if (!condition) {
2831
printf("ERROR: Assertion failed - %s\n", error_string);
@@ -69,6 +72,19 @@ char *getStmtType(Parse_tree_node *p) {
6972
return "UNKNOWN";
7073
}
7174

75+
void init_errors(){
76+
if(all_errors){
77+
ll_node* head = all_errors->head;
78+
while(head){
79+
all_errors->head = all_errors->head->next;
80+
free(head);
81+
head = all_errors->head;
82+
}
83+
free(all_errors);
84+
}
85+
all_errors = create_linked_list();
86+
}
87+
7288
bool assert_debug(bool condition, char *error_string, Parse_tree_node *p,
7389
char *t1, char *t2, char *operator, char *lex1,
7490
char *lex2) {
@@ -90,8 +106,20 @@ bool assert_debug(bool condition, char *error_string, Parse_tree_node *p,
90106
temp1 = temp1->child;
91107
}
92108
er->line_num = temp1->tok->line_no;
93-
printErrorEntries(er);
109+
//printErrorEntries(er);
110+
if(!all_errors)
111+
init_errors();
112+
ll_append(all_errors, er);
94113
return false;
95114
}
96115
return true;
97116
}
117+
118+
void print_all_errors(){
119+
if(!all_errors)
120+
return;
121+
printErrorsHeader();
122+
for(ll_node *head = all_errors->head; head; head=head->next){
123+
printErrorEntries((ErrorNode *)head->data);
124+
}
125+
}

gen_utils.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Group 36
1717
#include <stdlib.h>
1818
#include <math.h>
1919

20+
extern linked_list * all_errors;
21+
2022
typedef struct en
2123
{
2224
int line_num, depth;
@@ -26,7 +28,8 @@ typedef struct en
2628

2729
void printErrorsHeader();
2830
void printErrorEntries(ErrorNode *err);
29-
31+
void init_errors();
32+
void print_all_errors();
3033
void assert(bool condition, char *error_string);
3134
bool assert_debug(bool condition, char* error_string, Parse_tree_node * p, char* t1, char* t2, char* operator, char* lex1, char* lex2);
3235
#endif

grammar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ typedef struct grammar { // Final data structure for
3232
void printGrammar(Grammar* g);
3333
int readGrammar(char *filename, Grammar* g);
3434
char* replace_char(char* str, char find, char replace);
35+
void freeGrammar(Grammar *g);
3536

3637
#endif

hash_map.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ hash_map *create_hash_map(int num_buckets)
3737
return new_hm;
3838
}
3939

40+
void free_hash_map(hash_map *new_hm )
41+
{
42+
if(!new_hm){
43+
return;
44+
}
45+
for (int i = 0; i < new_hm->num_buckets; i++)
46+
{
47+
hm_node *temp = new_hm->buckets[i]->first;
48+
while(temp){
49+
new_hm->buckets[i]->first = new_hm->buckets[i]->first->next;
50+
free(temp);
51+
temp = new_hm->buckets[i]->first;
52+
}
53+
free(new_hm->buckets[i]);
54+
}
55+
free(new_hm->buckets);
56+
free(new_hm);
57+
}
58+
59+
4060
void add_to_bucket(hm_bucket *b, char *string, void *data)
4161
{
4262
hm_node *new_hm_node = (hm_node *)calloc(1, sizeof(hm_node));

hash_map.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ void *fetch_from_hash_map(hash_map *hm, char *string);
3939

4040
void destroy_hash_map(hash_map *hm);
4141

42+
void free_hash_map(hash_map *new_hm );
43+
4244
#endif

linked_list.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ linked_list *create_linked_list()
1717
return ll;
1818
}
1919

20+
void ll_free(linked_list *ll){
21+
ll_node *temp = ll->head;
22+
while( temp ){
23+
ll->head = ll->head->next;
24+
free(temp);
25+
temp = ll->head;
26+
}
27+
free(ll);
28+
}
29+
2030
void ll_add_at(linked_list *ll, void *data, int idx)
2131
{
2232
assert(idx <= ll->num_nodes && idx >= 0, "linked list insertion index valid");

linked_list.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ Group 36
1010
#define LINKED_LIST_H
1111

1212
#include <stdlib.h>
13-
#include "gen_utils.h"
13+
#include "grammar_structs.h"
14+
1415
typedef struct node {
1516
void *data;
1617
struct node *next;
@@ -23,10 +24,14 @@ typedef struct ll {
2324
int num_nodes;
2425
} linked_list;
2526

27+
void assert(bool condition, char *error_string);
28+
2629
linked_list *create_linked_list();
2730

2831
void ll_add_at(linked_list *ll, void *data, int idx);
2932

33+
void ll_free(linked_list *ll);
34+
3035
void ll_append(linked_list *ll, void *data);
3136

3237
void *ll_get(linked_list *ll, int idx);

parse_tree.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Parse_tree_node *getNodeFromIndex(Parse_tree_node *p, int index)
2020

2121
Parse_tree_node* createParseTree(TokenStream *s, Grammar *g, int *maxline){
2222
Symbol starting_symb;
23-
starting_symb.s = main_program;
23+
starting_symb.s = g->start_symb.s;
2424
starting_symb.is_terminal = false;
2525
return recursiveParseNonterminal(starting_symb, &(s->head), g, maxline, 0);
2626
}
@@ -95,7 +95,7 @@ Parse_tree_node *recursiveParseNonterminal(Symbol symb, Token ** tstr, Grammar *
9595
}
9696
}
9797
//printf("Failed to derive %s\n", toStringSymbol(symb));
98-
return new_node;
98+
return NULL;
9999
}
100100

101101
Parse_tree_node* new_parse_tree(Token *tok){
@@ -111,15 +111,22 @@ Parse_tree_node* new_parse_tree(Token *tok){
111111
}
112112
void free_parse_tree(Parse_tree_node *root){
113113

114-
Parse_tree_node* temp = root->child;
115114

115+
Parse_tree_node* temp = root->child;
116116
while(temp){
117117
root->child = root->child->next;
118118
free_parse_tree(temp);
119119
temp = root->child;
120-
root->num_children--;
121120
}
122-
//free(root->tok);
121+
/* while(temp){ */
122+
/* root->child = root->child->next; */
123+
/* free_parse_tree(temp); */
124+
/* temp = root->child; */
125+
/* //root->num_children--; */
126+
/* } */
127+
/* if(root->tok) */
128+
/* free(root->tok); */
129+
//root->tok = NULL;
123130
free (root);
124131
return ;
125132
}

0 commit comments

Comments
 (0)