Skip to content

Instantly share code, notes, and snippets.

@wilyJ80
Created January 1, 2025 18:12
Show Gist options
  • Save wilyJ80/5315dee769e8ac3a2ca724740fe89e18 to your computer and use it in GitHub Desktop.
Save wilyJ80/5315dee769e8ac3a2ca724740fe89e18 to your computer and use it in GitHub Desktop.
matched
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Exp_T {
const char *str;
int index;
int length;
int token;
};
struct Exp_T initExpT(char *argv[]) {
struct Exp_T exp_T = {
.str = argv[1], .index = 0, .length = (int)strlen(argv[1])};
return exp_T;
}
void getToken(struct Exp_T *exp) {
exp->token = exp->str[exp->index];
exp->index++;
}
void r(struct Exp_T *exp);
void s(struct Exp_T *exp) { r(exp); }
void r(struct Exp_T *exp) {
if (exp->token != '(') {
return;
}
getToken(exp);
s(exp);
if (exp->token != ')') {
fprintf(stderr, "Unmatched\n");
return;
}
getToken(exp);
s(exp);
}
/* Provide the parenthesized expression with CLI args */
int main(int argc, char *argv[]) {
struct Exp_T exp = initExpT(argv);
while (exp.index < exp.length) {
getToken(&exp);
s(&exp);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment