-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
241 lines (219 loc) · 6.62 KB
/
main.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// Copyright (c) 2023 Brian Sullender
// All rights reserved.
//
// This source code is licensed under the terms provided in the README file.
//
// https://github.com/b-sullender/expression-parser
//
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
// Define token types
typedef enum {
NUMBER,
PLUS,
MINUS,
MULTIPLY,
DIVIDE,
REMAINDER,
BIT_AND,
BIT_OR,
BIT_XOR,
BIT_LSHIFT,
BIT_RSHIFT,
LPAREN,
RPAREN,
END
} TokenType;
// The hint for the next token, for negative factors
typedef enum {
NONE,
OPERATOR,
FACTOR
} TokenHint;
// Define token structure
typedef struct {
TokenType type;
int value;
} Token;
// Define parser context
typedef struct {
Token token;
char* input;
} ParserContext;
// Function to get the next token
void getNextToken(ParserContext* context, int* precedence, TokenHint hint)
{
char c = *context->input++;
while (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
c = *context->input++;
}
if (c == '\0') {
context->token.type = END;
context->input--;
} else if (c >= '0' && c <= '9') {
context->token.type = NUMBER;
context->token.value = c - '0';
c = *context->input++;
while (c >= '0' && c <= '9') {
context->token.value = context->token.value * 10 + (c - '0');
c = *context->input++;
}
context->input--;
} else if (c == '+') {
context->token.type = PLUS;
*precedence = 700;
} else if (c == '-') {
if ((hint == FACTOR) && (*context->input >= '0' && *context->input <= '9'))
{
c = *context->input++;
context->token.type = NUMBER;
context->token.value = c - '0';
c = *context->input++;
while (c >= '0' && c <= '9') {
context->token.value = context->token.value * 10 + (c - '0');
c = *context->input++;
}
context->token.value = -context->token.value;
context->input--;
}
else
{
context->token.type = MINUS;
*precedence = 700;
}
} else if (c == '*') {
context->token.type = MULTIPLY;
*precedence = 800;
} else if (c == '/') {
context->token.type = DIVIDE;
*precedence = 800;
} else if (c == '%') {
context->token.type = REMAINDER;
*precedence = 800;
} else if (c == '&') {
context->token.type = BIT_AND;
*precedence = 500;
} else if (c == '|') {
context->token.type = BIT_OR;
*precedence = 300;
} else if (c == '^') {
context->token.type = BIT_XOR;
*precedence = 400;
} else if (c == '<' && *(context->input) == '<') {
context->token.type = BIT_LSHIFT;
*precedence = 600;
context->input++;
} else if (c == '>' && *(context->input) == '>') {
context->token.type = BIT_RSHIFT;
*precedence = 600;
context->input++;
} else if (c == '(') {
context->token.type = LPAREN;
} else if (c == ')') {
context->token.type = RPAREN;
}
}
// Forward declaration
int parseExpression(ParserContext* context, int* precedence, int minPrecedence);
// Function to parse a factor
int parseFactor(ParserContext* context, int* precedence)
{
int value;
if (context->token.type == NUMBER) {
value = context->token.value;
getNextToken(context, precedence, OPERATOR);
} else if (context->token.type == LPAREN) {
// Create a new precedence for operations in parentheses
int subPrecedence = 0;
// Parse the expression inside the parentheses
getNextToken(context, &subPrecedence, FACTOR);
value = parseExpression(context, &subPrecedence, 0);
// Check that we have a closing parenthesis
if (context->token.type != RPAREN) {
fprintf(stderr, "Error: Missing closing parenthesis\n");
exit(EXIT_FAILURE);
}
getNextToken(context, precedence, OPERATOR);
} else {
fprintf(stderr, "Error: Unexpected token\n");
exit(EXIT_FAILURE);
}
return value;
}
// Function to parse an expression
int parseExpression(ParserContext* context, int* precedence, int minPrecedence)
{
// Get the factor
int value = parseFactor(context, precedence);
// Check that we can continue processing this term
while ((context->token.type != END) && (context->token.type != RPAREN) && (*precedence >= minPrecedence))
{
// Check if we have a valid operator token
if (context->token.type == NUMBER) {
fprintf(stderr, "%s\n", "Error: expected operator, not variable");
exit(EXIT_FAILURE);
}
// Save current operator token
Token token = context->token;
// Get the factor/term to the right
getNextToken(context, precedence, FACTOR);
int term = parseExpression(context, precedence, *precedence);
// Perform the operation
if (token.type == PLUS) {
value += term;
} else if (token.type == MINUS) {
value -= term;
} else if (token.type == MULTIPLY) {
value *= term;
} else if (token.type == DIVIDE) {
value /= term;
} else if (token.type == REMAINDER) {
value %= term;
} else if (token.type == BIT_LSHIFT) {
value <<= term;
} else if (token.type == BIT_RSHIFT) {
value >>= term;
} else if (token.type == BIT_AND) {
value &= term;
} else if (token.type == BIT_XOR) {
value ^= term;
} else if (token.type == BIT_OR) {
value |= term;
}
}
// Return the result of the term
return value;
}
int main()
{
char expre[256];
printf("%s\n", "Enter 'q' to exit");
while (true)
{
printf("Enter expression: ");
fflush(stdout);
if (fgets(expre, 255, stdin) != expre) {
perror("Error reading expression\n");
return EXIT_FAILURE;
}
if (strcmp(expre, "q\n") == 0) {
return EXIT_SUCCESS;
}
char* pInput = expre;
ParserContext context;
memset(&context, 0, sizeof(ParserContext));
context.input = pInput;
int precedence = 0;
getNextToken(&context, &precedence, FACTOR);
int result = parseExpression(&context, &precedence, 0);
if (context.token.type != END) {
fprintf(stderr, "%s\n", "Error: Unexpected token\n");
continue;
}
printf("Result: %d\n", result);
}
return EXIT_SUCCESS;
}