Untitled
unknown
plain_text
2 years ago
897 B
5
Indexable
#include "token.h"
#include <stdio.h>
#include <stdlib.h>
extern Token* tokenize(const char* expr, int* tokenCount);
int main() {
const char* expression = "3 + 4.5 * 2 - (1 / 2)";
int tokenCount = 0;
Token* tokens = tokenize(expression, &tokenCount);
if (tokens == NULL) {
printf("Error tokenizing expression.\n");
return 1;
}
printf("Tokenized Expression:\n");
for (int i = 0; i < tokenCount; ++i) {
if (tokens[i].type == TOKEN_NUMBER) {
printf("Number: %f\n", tokens[i].value);
} else if (tokens[i].type == TOKEN_OPERATOR) {
printf("Operator: %c\n", tokens[i].op);
} else if (tokens[i].type == TOKEN_LPAREN) {
printf("Left Parenthesis\n");
} else if (tokens[i].type == TOKEN_RPAREN) {
printf("Right Parenthesis\n");
}
}
free(tokens);
return 0;
}
Editor is loading...
Leave a Comment