This repository has been archived by the owner on Oct 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar_ressources.c
61 lines (55 loc) · 1.95 KB
/
grammar_ressources.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* grammar_ressources.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mgautier <mgautier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/12 18:23:50 by mgautier #+# #+# */
/* Updated: 2017/11/25 11:58:14 by mgautier ### ########.fr */
/* */
/* ************************************************************************** */
#include "grammar_defs.h"
#include "sym_interface.h"
#include "libft.h"
#include <stdlib.h>
static void sym_destroy(void **to_destroy)
{
destroy_symbol((t_symbol**)to_destroy);
}
void destroy_grammar(t_grammar **to_destroy)
{
t_grammar *gram;
gram = *to_destroy;
if (gram != NULL)
{
gram->start_symbol = NULL;
f_fifo_destroy(&gram->sym_list, &sym_destroy);
f_fifo_destroy(&gram->tokens_list, &sym_destroy);
free(gram);
*to_destroy = NULL;
}
}
t_grammar *create_grammar(void)
{
t_grammar *new_gram;
t_symbol *end_of_input_token;
new_gram = malloc(sizeof(t_grammar));
if (new_gram != NULL)
{
end_of_input_token = create_symbol("END_OF_INPUT");
new_gram->start_symbol = NULL;
new_gram->sym_list = f_fifo_create();
new_gram->tokens_list = f_fifo_create();
if (new_gram->sym_list == NULL
|| new_gram->tokens_list == NULL
|| end_of_input_token == NULL)
{
destroy_grammar(&new_gram);
destroy_symbol(&end_of_input_token);
}
else if (NULL == f_fifo_add(new_gram->tokens_list, end_of_input_token))
destroy_grammar(&new_gram);
}
return (new_gram);
}