Created
July 7, 2024 09:10
-
-
Save sulincix/0d3e150d2f20ceb5936e4d008d87b9ad to your computer and use it in GitHub Desktop.
Simple parenthesis parser.
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 <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
void parse(char* in); | |
void main(){ | |
char* in="12+(13-22)+(2-4/(33-456))"; | |
parse(in); | |
} | |
void parse(char* in){ | |
int level=0; | |
int begin=0; | |
int end=0; | |
puts(in); | |
for(int i=0;i<strlen(in);i++){ | |
//printf("%d %c\n", i, in[i]); | |
if (in[i] == '('){ | |
if(level == 0){ | |
begin=i; | |
} | |
level++; | |
continue; | |
} else if (in[i] == ')'){ | |
if(level == 1){ | |
char* n = strndup(in+begin+1, i-begin-1); | |
parse(n); | |
} | |
level--; | |
continue; | |
} | |
} | |
if(begin == 0) { | |
// run expression and return some stuff. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment