-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.c
More file actions
45 lines (39 loc) · 862 Bytes
/
parse.c
File metadata and controls
45 lines (39 loc) · 862 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include "minstack.h"
int parse_expr(char * c, int i, Top t){
switch(c[i]){
case ')':
return (bs_pop(&t) == ')') ? parse_expr(c, i+1, t) : -1;
case ']':
return (bs_pop(&t) == ']') ? parse_expr(c, i+1, t) : -1;
case '}':
return (bs_pop(&t) == '}') ? parse_expr(c, i+1, t) : -1;
case '{':
bs_push('}', &t);
return parse_expr(c, i+1, t);
case '(':
bs_push(')', &t);
return parse_expr(c, i+1, t);
case '[':
bs_push(']', &t);
return parse_expr(c, i+1, t);
case '\0':
return (t->count == 0) ? 1 : -1;
default:
return parse_expr(c, i+1, t);
}
}
int main(int argv, char** argc){
char c[2048];
while(1){
int i = 0;
printf("input> ");
scanf("%s", c);
Top t = bs_init(0);
i = parse_expr(c, i, t);
printf("rvalue: %i\n", i);
}
return 0;
}