-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr_parser.c
190 lines (150 loc) · 3.2 KB
/
expr_parser.c
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include<stdio.h>
#include<stdbool.h>
#include<assert.h>
//(((f) * f))
/*
* Grammar for Expressions:
*
* Expr -> Expr AOP Expr | Term
*
* Term -> Term MOP Term | Factor | (Expr)
*
* AOP -> + | -
*
* MOP -> * | /
*
* Factor -> 0-9
*/
int factor_arr[1024];
int ptr;
#define EXPR "is an expr"
#define N_EXPR "is not an expr"
#define EMIT(e) fprintf(stdout, e"\n")
struct expr
{
enum symbols *arr;
int num;
} expr1;
bool Mop(struct expr *);
bool Aop(struct expr *);
bool Term(struct expr *);
bool Expr(struct expr *);
#define NEXT(e) do {++e->num;}while(0)
#define CUR(e) e->arr[e->num]
enum symbols {
factor,
add,
sub,
mulp,
div,
openb,
closeb,
END
};
char str[] = {'(', ')', '+', '*'};
enum symbols str_sym[] = {openb, closeb, add, mulp};
bool Aop(struct expr *e)
{
enum symbols sym = CUR(e);
if (sym == add || sym == sub) {
NEXT(e);
return true;
}
return false;
}
bool Mop(struct expr *e)
{
enum symbols sym = CUR(e);
if (sym == mulp || sym == div) {
NEXT(e);
return true;
}
return false;
}
bool Term(struct expr *e)
{
enum symbols sym = CUR(e);
if (sym == factor) {
//EMIT("PSH %d", factor_arr[ptr++]);
fprintf(stdout,"PSH %d\n", factor_arr[ptr++]);
NEXT(e);
if (Mop(e) == true) {
bool ret = Term(e);
//EMIT("MUL");
fprintf(stdout, "MUL\n");
return ret;
}
return true;
}
if (sym == openb) {
NEXT(e);
if (Expr(e) == true) {
if (CUR(e) == closeb) {
NEXT(e);
if (Mop(e) == true) {
bool ret = Term(e);
//EMIT("MUL");
fprintf(stdout, "MUL\n");
return ret;
}
return true;
}
}
}
return false;
}
bool Expr(struct expr *e)
{
if (Term(e) == true) {
if (Aop(e) == true) {
bool ret = Expr(e);
//EMIT("ADD\n");
fprintf(stdout, "ADD\n");
return ret;
}
return true;
}
if (CUR(e) == END)
return true;
return false;
}
bool parse(struct expr *e)
{
bool ret = Expr(e);
if (e->arr[e->num] != END)
return false;
return ret;
}
int main()
{
enum symbols expr_test[1024];
//= {openb, openb, openb, factor, closeb, mulp, factor, closeb, closeb, END};
unsigned char c;
int i = 0;
while ((c = getchar()) && !feof(stdin)) {
int j;
bool is_factor = false;
/* This is LF character */
if (c == '\n')
continue;
for (j = 0; j < strlen(str); j++) {
if (c == str[j]) {
expr_test[i++] = str_sym[j];
break;
}
}
if (j == strlen(str)) {
expr_test[i++] = factor;
factor_arr[ptr++] = atoi(&c);
is_factor = true;
}
assert(j < strlen(str) || is_factor == true);
}
expr_test[i] = END;
ptr = 0;
expr1.arr = expr_test;
printf("%s\n", parse(&expr1) == true ? EXPR : N_EXPR);
}
//(((f*f-f/f) * f))
//(((f) * f))
//(((f)*f))