Created
January 1, 2025 18:12
-
-
Save wilyJ80/5315dee769e8ac3a2ca724740fe89e18 to your computer and use it in GitHub Desktop.
matched
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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