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
|
/* See LICENSE file for copyright and license details. */
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(__GNUC__)
# pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
# pragma GCC diagnostic ignored "-Wsuggest-attribute=pure"
#endif
static char **tokens;
static size_t ntokens;
struct node {
int is_operand;
const char *symbol;
struct node *left;
struct node *right;
};
static struct node *
operand_node(const char *token)
{
struct node *ret = malloc(sizeof(*ret));
ret->is_operand = 1;
ret->symbol = token;
ret->left = NULL;
ret->right = NULL;
return ret;
}
static struct node *
operator_node(struct node *left, const char *operator, struct node *right)
{
struct node *ret = malloc(sizeof(*ret));
ret->is_operand = 0;
ret->symbol = operator;
ret->left = left;
ret->right = right;
return ret;
}
static void
print_node(struct node *node)
{
if (node->is_operand) {
printf("%s", node->symbol);
} else {
printf("(");
print_node(node->left);
printf(" %s ", node->symbol);
print_node(node->right);
printf(")");
}
}
static const char *
next_token(void)
{
static size_t token_i = 0;
return token_i == ntokens ? NULL : tokens[token_i++];
}
static struct node *
parse_operand(void)
{
const char *token = next_token();
if (!token)
exit(2); /* syntax error */
return operand_node(token);
}
static int
get_precedence(const char *token, int *ltr)
{
int prec = 0;
*ltr = 1;
if (!strcmp(token, ","))
return prec;
prec++;
*ltr = 0;
if (!strcmp(token, "=") || !strcmp(token, "+=") || !strcmp(token, "-=") || !strcmp(token, "*=") ||
!strcmp(token, "%=") || !strcmp(token, "<<=") || !strcmp(token, ">>=") || !strcmp(token, "&=") ||
!strcmp(token, "^=") || !strcmp(token, "|="))
return prec;
prec++;
*ltr = 1;
if (!strcmp(token, "||"))
return prec;
prec++;
if (!strcmp(token, "&&"))
return prec;
prec++;
if (!strcmp(token, "|"))
return prec;
prec++;
if (!strcmp(token, "^"))
return prec;
prec++;
if (!strcmp(token, "&"))
return prec;
prec++;
if (!strcmp(token, "==") || !strcmp(token, "!="))
return prec;
prec++;
if (!strcmp(token, "<=") || !strcmp(token, "<") || !strcmp(token, ">=") || !strcmp(token, ">"))
return prec;
prec++;
if (!strcmp(token, "<<") || !strcmp(token, ">>"))
return prec;
prec++;
if (!strcmp(token, "+") || !strcmp(token, "-"))
return prec;
prec++;
if (!strcmp(token, "*") || !strcmp(token, "/") || !strcmp(token, "%"))
return prec;
prec++;
*ltr = 0;
if (!strcmp(token, "**"))
return prec;
exit(2);
}
static struct node *
parse_expression_(int min_prec, const char **tokenp)
{
struct node *left, *right;
const char *operator;
int prec, ltr;
left = parse_operand();
*tokenp = next_token();
while (*tokenp) {
prec = get_precedence(*tokenp, <r);
if (ltr ? prec <= min_prec : prec < min_prec)
break;
operator = *tokenp;
*tokenp = NULL;
right = parse_expression_(prec, tokenp);
left = operator_node(left, operator, right);
*tokenp = *tokenp ? *tokenp : next_token();
}
return left;
}
static struct node *
parse_expression(void)
{
return parse_expression_(-1, &(const char *){NULL});
}
int
main(int argc, char *argv[])
{
struct node *node;
tokens = &argv[1];
ntokens = (size_t)argc - 1u;
if (!(node = parse_expression()))
return 1;
print_node(node);
printf("\n");
return 0;
}
|