-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.h
169 lines (150 loc) · 5.53 KB
/
node.h
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
/** @file node.h
* @version 1.1
*/
#ifndef _NODE_H_
#define _NODE_H_
#include <stdio.h>
typedef int Node_type;
/* Seria de constantes que servirao para definir tipos de nos (na arvore),
* a partir da etapa 4 - irrelevante por enquanto. */
#define program_node 299
#define idf_node 300
#define int_node 301
#define real_node 302
#define str_node 303
#define empty_node 304
#define proc_node 305
#define param_node 306
#define decl_node 307
#define decl_list_node 308
#define op_node 309
#define nop_node 310
#define return_node 311
#define if_node 312
#define while_node 313
#define print_node 314
#define cond_node 315
#define affect_node 316
#define or_node 317
#define and_node 318
#define eq_node 319
#define neq_node 320
#define inf_node 321
#define sup_node 322
#define inf_eq_node 323
#define sup_eq_node 324
#define plus_node 325
#define minus_node 326
#define mult_node 327
#define div_node 328
#define mod_node 329
#define umenos_node 330
#define not_node 331
#define char_node 332
#define bloc_node 333
#define true_node 335
#define false_node 336
#define semicolon_node 337
#define double_node 338
#define colon_node 339
#define comma_node 340
#define int_lit_node 341
#define listadupla_node 342
#define abre_col_node 343
#define fecha_col_node 344
#define tipolista_node 345
#define acoes_node 346
#define atrib_node 347
#define comando_node 348
#define lvalue_node 349
#define listaexpr_node 350
#define expr_node 351
#define abre_par_node 352
#define fecha_par_node 353
#define f_lit_node 354
#define chamaproc_node 355
#define then_node 356
#define enunciado_node 357
#define abre_cha_node 358
#define fecha_cha_node 359
#define end_node 360
#define else_node 361
#define fiminstcontrole_node 362
#define expbool_node 363
/** Estrutura de dados parcial para o no da arvore.
* Trata-se de uma arvore generalizada: qualquer no pode ter de 0 ateh
* MAX_CHILDREN_NUMBER filhos. */
#define MAX_CHILDREN_NUMBER 10
typedef struct _node {
int num_line; /**< numero de linha (irrelevante por enquanto).*/
int id; /**< rótulo do nó. Cada nó deve ter um 'id' distinto. */
char* lexeme; /**< irrelevante por enquanto. */
Node_type type; /**< Um dos valores definidos acima pelos # defines. */
void* attribute;/**< Qualquer coisa por enquanto. */
/* Fim das informacoes armazenadas em cada no.
* A seguir, completar essa estrutura de dados com o necessário para
* a implementacao dos metodos especificados.
*/
int num_child; // Número de filhos do nodo
struct _node **child; // Lista dos filhos do nodo
// struct _node *father; // Ponteiro para o pai do nodo... não precisa
} Node;
/** Constructor of a Node.
*
* @param nl : line number of the instruction that originates the node.
* @param t : node type (one of the values # define'd above). Must return an
* error if the type in not correct.
* @param lexema : whatever string you want to associate to the node.
* @param att : a semantical attribute.
* @param nbc : number of children nodes (<= MAX_CHILDREN_NUMBER and >= 0). Must
* return an error if nbc it not correct.
* @param children : array of children nodes (of size 'nbc').
* @return a (pointer on a) new Node.
*/
Node* create_node(int nl, Node_type t, char* lexema,
void* att, int nbc, Node** children) ;
/** Constructor of a leaf Node (without any child).
* @param nl : line number of the instruction that originates the node.
* @param t : node type (one of the values # define'd above). Must return an
* error if the type in not correct.
* @param lexema : whatever string you want to associate to the node.
* @param att : a semantica attribute (can be NULL for now).
* @return a (pointer) on a new Node.
*/
Node* create_leaf(int nl, Node_type t, char* lexema, void* att) ;
/** accessor to the number of children of a Node.
* Must return en error if 'n' is NULL.
*/
int nb_of_children(Node* n);
/** accessor to the i'th child of a Node.
* @param n : the node to be consulted. Must return en error if 'n' is NULL.
* @param i : the number of the child that one wants. Must be strictly lower
* than n->num_children and larger than 0. Must return an error if i is
* not correct.
* @return a pointer on a Node.
*/
Node* child(Node* n, int i) ;
/** Pushes 'n' Nodes on bottom of an array of Node* which contains originally
* 'cur_size' entries. Returns the new number of entries (ie 'cur_size+1') in
* the array 'array_of_nodes'.
* This function is convenient to be used with create_node (see its last
* argument).
* On the first call, 'array_of_nodes' should not be allocated, and 'cur_size'
* should be zero.
* Implementation limit: since these Node* structures are meant to be used
* in a compiler, one expects an upper limit of MAX_CHILDREN_NUMBER nodes
* to be packed. Trying to pack more than this limit must raise an error.
* Typical use: Node** children; pf1 = create_leaf(1, int_node, "1", NULL);
* pf3 = create_leaf(1, int_node, "2", NULL); pack_nodes(&children, 0, pf1);
* pack_nodes(&children, 1, pf3);
*/
int pack_nodes(Node*** array_of_nodes, int cur_size, Node* n);
/** Destructor of a Node. Desallocates (recursively) all the tree rooted at
* 'n'.
*/
int deep_free_node(Node* n) ;
/** returns the height of the tree rooted by 'n'.
* The height of a leaf is 1.
*/
int height(Node *n) ;
#endif