-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_tree.cpp
More file actions
166 lines (149 loc) · 4.6 KB
/
parse_tree.cpp
File metadata and controls
166 lines (149 loc) · 4.6 KB
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
#include "parse_tree.h"
#include "lltable.h"
#include "lexer.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <string>
using namespace std;
void print_indent(FILE *fp, int n)
{
n *= 2;
for(int i = 0; i < n; i++)
fprintf(fp, " ");
}
void parse_tree(struct Grammar *grammar, struct LLTable *table,
map<string, string> &l_map, char *tok_path)
{
FILE *fp, *tree_txt;
int sym_id, consume, more;
char s1[50], s2[50], tok_type;
string str;
struct PLstack *top, *new_stk;
Symbol **symbol_table;
Symbol *sym, *top_sym;
struct Product_List *pl;
fp = fopen(tok_path, "r");
tree_txt = fopen("tree.txt", "w");
sym_id = symbol_id(grammar->hash_table, "S");
if(sym_id == -1){
printf("Can't find start symbol: S\n");
fclose(fp);
return;
}
symbol_table = grammar->symbol_table;
top = create_plstack(0, find_rule(grammar, sym_id)->pl);
while(fscanf(fp, "%s : %s", s1, s2) != EOF){
if(!strcmp("Line", s1))
continue;
more = 0;
// the id of the symbol read
sym_id = symbol_id(grammar->hash_table, s2);
if(sym_id == -1){
str = s2;
tok_type = lexer(l_map, str);
switch(tok_type){
case 'D':
{
more = 1;
strcpy(s1, s2);
strcpy(s2, "num");
sym_id = symbol_id(grammar->hash_table, s2);
} break;
case 'I':
{
more = 1;;
strcpy(s1, s2);
strcpy(s2, "id");
sym_id = symbol_id(grammar->hash_table, s2);
} break;
default:
printf("Illegal symbol: %s\n", s2);
fclose(fp);
return;
}
}
consume = 0;
while(!consume){
top_sym = symbol_table[top->p->id];
// the top of stack is a terminal
if(top_sym->terminal){
if(!strcmp(s2, top_sym->name)){
print_indent(tree_txt, top->depth);
fprintf(tree_txt, "%d %s\n", top->depth, top_sym->name);
if(more){
print_indent(tree_txt, top->depth + 1);
fprintf(tree_txt, "%d %s\n", top->depth + 1, s1);
}
top->p = top->p->next;
consume = 1;
}
else{
printf("Error: top of stack doesn't match input.\n");
printf("Read: %s, find: %s\n", s2, top_sym->name);
fclose(fp);
return;
}
}
// the top of stack is a nonterminal
else{
print_indent(tree_txt, top->depth);
fprintf(tree_txt, "%d %s\n", top->depth, top_sym->name);
// shift for the nonterminal
top->p = top->p->next;
// consult the LLTable
pl = table_get_pl(table, top_sym->id, sym_id);
if(pl){
new_stk = create_plstack(top->depth + 1, pl);
top = push_pl(top, new_stk);
}
else{
printf("Syntax error.\n");
while(top)
pop_pl(&top);
return;
}
}
while(1){
if(!top)
return;
// if the rule is not finished
if(top->p){
top_sym = symbol_table[top->p->id];
// and there's no epsilon
if(strcmp(top_sym->name, "epsilon"))
break;
else
pop_pl(&top);
}
else
pop_pl(&top);
}
}
}
fclose(fp);
fclose(tree_txt);
}
struct PLstack *push_pl(struct PLstack *top, struct PLstack *stk)
{
stk->next = top;
return stk;
}
void *pop_pl(struct PLstack **top)
{
struct PLstack *tmp = (*top)->next;
free(*top);
*top = tmp;
}
struct PLstack *create_plstack(int depth, struct Product_List *pl)
{
PLstack *plstack = (struct PLstack*) malloc(sizeof(struct PLstack));
if(!plstack)
return NULL;
plstack->depth = depth;
plstack->pl = pl;
plstack->p = pl->product;
plstack->next = NULL;
return plstack;
}