-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
189 lines (149 loc) · 3.78 KB
/
Copy pathparser.c
File metadata and controls
189 lines (149 loc) · 3.78 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdio.h>
#include <malloc.h>
#include <memory.h>
#include <stdarg.h>
#include "parser.h"
#include "grammar.tab.h"
char * semantics[] = {
"ERROR_SEM",
"FLOAT_LITERAL",
"INT_LITERAL",
"ASSIGNMENT_SEM",
"ADD_SEM",
"REST_SEM",
"MULT_SEM",
"DIV_SEM",
"MODULO_SEM",
"BITWISE_AND_SEM",
"BITWISE_OR_SEM",
"NEG_SEM",
"VAR_VAL",
"VAR_REF"
};
void error( const char* format, ... )
{
va_list arglist;
printf( "Error: " );
va_start( arglist, format );
vfprintf( stderr, format, arglist );
va_end( arglist );
}
LinkedList * list_create(void *element, size_t n)
{
// We allocate memory for the list node + the data
LinkedList * new_list = (LinkedList *) malloc(sizeof(LinkedList)+n);
if (new_list){
//the element is always sizeof(LinkedList) bytes after the node
new_list->element = new_list+sizeof(LinkedList);
memcpy(new_list->element, element, n);
new_list->n = n;
new_list->next = NULL;
}
return new_list;
}
int len(LinkedList * head)
{
int n=1;
LinkedList * current = head;
if (!head)
return 0;
while(current->next!=NULL){
n++;
current = current->next;
}
return n;
}
LinkedList * append(LinkedList * head, void *element, size_t n)
{
LinkedList * current = head;
if (!head)
return NULL;
while (current->next != NULL)
current=current->next;
current->next = list_create(element,n);
return current->next;
}
LinkedList * element_at(LinkedList * head, int i)
{
int j=0;
LinkedList * current = head;
if (!head)
return NULL;
while (j < i && current->next != NULL){
current = current->next;
j++;
}
return current;
}
LinkedList * insert_after(LinkedList * node, void *element, size_t n)
{
LinkedList * new_node;
if (!node)
return NULL;
new_node = list_create(element,n);
if (!new_node)
return NULL;
new_node->next = node->next;
node->next = new_node;
return new_node;
}
LinkedList * insert_before(LinkedList * node, void *element,size_t n)
{
void * curr_element;
LinkedList * new_node;
if (!node)
return NULL;
curr_element = node->element;
new_node = list_create(curr_element,n);
if (!new_node)
return NULL;
new_node->next = node->next;
node->next = new_node;
node->element = element;
return new_node;
}
ExprType* expr_create(unsigned int op, LinkedList *items)
{
ExprType * new_node = (ExprType *) malloc(sizeof(ExprType));
if (new_node){
new_node->items = items;
new_node->op = op;
}
return new_node;
}
void expr_preorder(ExprType *root, int depth){
if (root==NULL)
return;
// printf ("root is not null\n");
LinkedList * current = root->items;
while(current!=NULL){
//printf ("doing element\n");
expr_preorder((ExprType*) current->element, depth+1);
current = current->next;
}
for(int i=0;i <depth; i++){
printf("-");
}
int op = root->op - 97;
printf("semantic is %d, %s\n", root->op, semantics[op]);
}
void expr_inorder(ExprType *root, int depth){
if (root==NULL)
return;
// printf ("root is not null\n");
for(int i=0;i <depth; i++){
printf("-");
}
int op = root->op - 97;
printf("semantic is %d, %s\n", root->op, semantics[op]);
LinkedList * current = root->items;
while(current!=NULL){
//printf ("doing element\n");
expr_preorder((ExprType*) current->element, depth+1);
current = current->next;
}
}
int main(int argc, char*argv[])
{
yyparse();
}