-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.c
146 lines (136 loc) · 3.6 KB
/
parsing.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
# include "parsing.h"
struct s_tree *parse(char *equation)
{
char *rang, *sub_string;
int len;
struct s_tree *tree = NULL;
set_erreur(0);
rang = strchr(equation, '=');
if (rang == NULL)
set_erreur(1);
else if (rang != strrchr(equation, '='))
set_erreur(2);
else
{
tree = build_operator(*(rang));
len = rang - equation;
sub_string = calloc(len + 1, sizeof (char));
strncpy(sub_string, equation, len);
tree->left = _parse(sub_string);
free(sub_string);
sub_string = calloc(strlen(equation) - len + 1, sizeof (char));
strcpy(sub_string, rang + 1);
tree->right = _parse(sub_string);
free(sub_string);
}
return tree;
}
struct s_tree *_parse(char *equation)
{
char *sub_string;
int pos = get_pos_operator(equation);
struct s_tree *tree;
size_t lgth;
if (pos != -1)
{
tree = build_operator(*(equation + pos));
sub_string = calloc(pos + 2, sizeof (char));
if (pos > 0)
{
strncpy(sub_string, equation, pos);
tree->left = _parse(sub_string);
}
else
tree->left = NULL;
free(sub_string);
sub_string = calloc(strlen(equation) - pos, sizeof (char));
strcpy(sub_string, equation + pos + 1);
if(*(equation + pos) == '/')
{
tree->right = build_function("pow");
((struct s_function*) tree->right->data)->param = -1;
tree->right->left = _parse(sub_string);
}
else
tree->right = _parse(sub_string);
free(sub_string);
}
else if (comp_regex(equation,
"^[a-z]{2,}\\([0-9a-z\\+\\-\\*/().]+(,[0-9]+)?\\)$"))//Function
{
lgth = strcspn(equation, "(");
sub_string = calloc(lgth + 1, sizeof (char));
strncpy(sub_string, equation, lgth);
tree = build_function(sub_string);
free(sub_string);
equation += lgth + 1;
*(equation + strlen(equation) - 1) = '\0';
lgth = strcspn(equation, ",");
if(lgth != strlen(equation))
{
sub_string = calloc(strlen(equation + lgth + 1) + 1, sizeof (char));
strcpy(sub_string, equation +lgth + 1);
sscanf(sub_string, "%f", &((struct s_function*) tree->data)->param);
free(sub_string);
}
sub_string = calloc(lgth + 1, sizeof (char));
strncpy(sub_string, equation, lgth);
tree->left = _parse(sub_string);
free(sub_string);
}
else if (comp_regex(equation, "^\\([0-9a-z,\\+\\-\\*/().]+\\)$"))//parenthesys
{
sub_string = calloc(strlen(equation) - 1, sizeof (char));
strncpy(sub_string, equation + 1, strlen(equation) - 2);
struct s_tree *tmp = _parse(sub_string);
free(sub_string);
return tmp;
}
else if (comp_regex(equation, "^[a-z]$"))//variable
{
tree = build_variable(equation);
}
else if (comp_regex(equation, "^[0-9]+(\\.[0-9]+)?$"))//Number
{
tree = build_number(equation);
}
else
set_erreur(3);
return tree;
}
char *clean_string(char *string)
{
int lgt = strlen(string) + 1, cpt = 0, i;
char *new_s = calloc(lgt, sizeof (char));
for (i = 0; i < lgt; ++i)
{
char c = *(string + i);
if (c >= 'A' && c <= 'Z')
*(new_s + cpt++) = c + 32;
else if ((c >= '(' && c <= '9') || (c >= 'a' && c <= 'z') || c == '=')
*(new_s + cpt++) = c;
}
return new_s;
}
int comp_regex(char *string, char *reg)
{
regex_t regex;
int err_reg = 0;
err_reg = regcomp(®ex, reg, REG_NOSUB | REG_EXTENDED | REG_NEWLINE);
if (err_reg)
{
char *test = calloc(100,sizeof(char));
regerror(err_reg,®ex,test,100);
printf("%s\n",reg);
printf("%s\n",test);
err(1, "Error initialising function regex");
}
err_reg = regexec(®ex, string, 0, NULL, 0);
regfree(®ex);
if (!err_reg)
return 1;
else if (err_reg == REG_NOMATCH)
return 0;
else
err(1, "Error executing function regex");
}