forked from coolsidd/Compiler-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_tree.c
147 lines (136 loc) · 5.25 KB
/
parse_tree.c
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
/*
Group 36
2017B4A70495P Manan Soni
2017B4A70549P Siddharth Singh
2017B4A70636P Nayan Khanna
2017B4A70740P Aditya Tulsyan
*/
#include "parse_tree.h"
#include <stdio.h>
#define MAXRULES 100
Parse_tree_node *getNodeFromIndex(Parse_tree_node *p, int index)
{
for(int i = 0; i < index; i++){
p = p->next;
}
return p;
}
Parse_tree_node* createParseTree(TokenStream *s, Grammar *g, int *maxline){
Symbol starting_symb;
starting_symb.s = g->start_symb.s;
starting_symb.is_terminal = false;
return recursiveParseNonterminal(starting_symb, &(s->head), g, maxline, 0);
}
Parse_tree_node *recursiveParseNonterminal(Symbol symb, Token ** tstr, Grammar *g, int *maxline, int depth) {
//printf("Trying to derive %s:\n", toStringSymbol(symb));
Parse_tree_node * new_node = NULL;
for(int i=0; i<g->num_rules; i++){
if(g->rules[i].lhs.s == symb.s){
////printf("trying to apply rule no. %d\n", i);
//printf("Trying \"%s -> ",toStringSymbol(g->rules[i].lhs) );
/* for(RuleNode* check_rule = g->rules[i].rhs; check_rule; check_rule = check_rule->next){ */
/* //printf("%s ", toStringSymbol(check_rule->s)); */
/* } */
//printf("\"\n");
bool flag_successful = true;
Token *temp_tstr = *tstr;
Token *tempToken = (Token*)malloc(sizeof(Token));
tempToken->token_name = toStringSymbol(symb);
tempToken->lexeme = symb;
tempToken->line_no = -1;
tempToken->next = NULL;
Parse_tree_node* new_node = new_parse_tree(tempToken);
new_node->depth = depth;
for(RuleNode* check_rule = g->rules[i].rhs; check_rule; check_rule = check_rule->next){
if(check_rule->s.is_terminal && temp_tstr->lexeme.is_terminal){
if(check_rule->s.s == temp_tstr->lexeme.s){
//printf("Found %s at line %d next\n", toStringSymbol(check_rule->s), temp_tstr->line_no);
Parse_tree_node *tempChildNode = new_parse_tree(temp_tstr);
tempChildNode->depth = depth+1;
add_parsed_child(new_node, tempChildNode);
temp_tstr = temp_tstr->next;
continue;
}
else{
//printf("Expected %s found %s at line %d\n", toStringSymbol(check_rule->s), temp_tstr->token_name, temp_tstr->line_no);
flag_successful = false;
break;
}
}else if(!(check_rule->s.is_terminal)){
Parse_tree_node *tempChildNode = recursiveParseNonterminal(check_rule->s, &temp_tstr, g, maxline, depth+1);
if(tempChildNode){
add_parsed_child(new_node, tempChildNode);
}else{
//printf("%s failed \n", toStringSymbol(check_rule->s));
flag_successful = false;
break;
}
}else{
//printf("Expected %s found %s at line %d\n", toStringSymbol(check_rule->s), temp_tstr->token_name, temp_tstr->line_no);
flag_successful = false;
break;
}
}
if(flag_successful){
//printf("%s successfully derived\n", toStringSymbol(symb));
if(temp_tstr){
//printf("Parsing till line %d done\n", temp_tstr->line_no);
if (temp_tstr->line_no > *maxline) *maxline = temp_tstr->line_no;
*tstr = temp_tstr;
}
return new_node;
}else{
free(tempToken);
free_parse_tree(new_node);
continue;
}
}
}
//printf("Failed to derive %s\n", toStringSymbol(symb));
return NULL;
}
Parse_tree_node* new_parse_tree(Token *tok){
Parse_tree_node* new_tree = (Parse_tree_node*)malloc(sizeof(Parse_tree_node));
new_tree->num_children=0;
new_tree->child = NULL;
new_tree->last_child = NULL;
new_tree->next = NULL;
new_tree->tok = tok;
new_tree->parent = NULL;
new_tree->txp = NULL;
return new_tree;
}
void free_parse_tree(Parse_tree_node *root){
Parse_tree_node* temp = root->child;
while(temp){
root->child = root->child->next;
free_parse_tree(temp);
temp = root->child;
}
/* while(temp){ */
/* root->child = root->child->next; */
/* free_parse_tree(temp); */
/* temp = root->child; */
/* //root->num_children--; */
/* } */
/* if(root->tok) */
/* free(root->tok); */
//root->tok = NULL;
free (root);
return ;
}
void add_parsed_child(Parse_tree_node *root, Parse_tree_node *node){
//printf("Adding child %s -> %s\n",toStringSymbol(root->tok->lexeme), toStringSymbol(node->tok->lexeme));
if(!root->child){
node->parent = root;
root->child = node;
root->last_child = node;
root->num_children = 1;
return;
}
node->parent = root;
root->last_child->next = node;
root->num_children++;
root->last_child = root->last_child->next;
return;
}