-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path5-20.c
226 lines (171 loc) · 4.73 KB
/
5-20.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX_TOKEN 100
#define BUFFER_SIZE 100
enum { NAME, PARENS, BRACKETS };
enum { NO_ERROR, ERROR };
char buffer[100];
int buffer_index = 0;
void unget_character(int character) {
if (buffer_index >= BUFFER_SIZE)
printf("\nUngetch : Too many characters");
else if (character != EOF)
buffer[buffer_index++] = character;
}
int get_character() {
if (buffer_index > 0)
return buffer[--buffer_index];
else
return getchar();
}
int token_error = NO_ERROR;
void print_error(char *error) {
printf("%s", error);
token_error = ERROR;
}
int token_type;
char token[MAX_TOKEN];
char name[MAX_TOKEN];
char datatype[MAX_TOKEN];
char out[1000];
#define QUALIFIERS 5
char qualifier[QUALIFIERS][MAX_TOKEN] = {
"static",
"const",
"register",
"extern",
};
#define DATA_TYPES 5
char data_type[DATA_TYPES][MAX_TOKEN] = {
"int",
"void",
"char",
"float",
};
int qualifier_checker(char string[MAX_TOKEN]) {
for (int index = 0; index < QUALIFIERS; index++)
if (strcmp(string, qualifier[index]) == 0)
return 1;
return 0;
}
int data_type_checker(char string[MAX_TOKEN]) {
for (int index = 0; index < DATA_TYPES; index++)
if (strcmp(string, data_type[index]) == 0)
return 1;
return 0;
}
int get_token(void) {
int character = get_character();
char *ptr = token;
if (token_error == ERROR) {
token_error = NO_ERROR;
unget_character(character);
return token_type;
}
while (character == ' ' || character == '\t')
character = get_character();
if (character == '(') {
character = getchar();
if (character == ')') {
strcpy(token, "()");
return token_type = PARENS;
} else {
unget_character(character);
return token_type = '(';
}
} else if (character == '[') {
for (*ptr++ = character; (*ptr++ = get_character()) != ']'; );
*ptr = '\0';
return token_type = BRACKETS;
} else if (isalpha(character) || character == '_') {
for (*ptr++ = character; isalnum(character = get_character()) || character == '_'; )
*ptr++ = character;
*ptr = '\0';
unget_character(character);
return token_type = NAME;
} else
return token_type = character;
}
void declarator(void);
void specification_declarator(void) {
char temp[MAX_TOKEN];
temp[0] = '\0';
get_token();
do {
if (token_type != NAME) {
token_error = ERROR;
declarator();
} else if (data_type_checker(token)) {
strcat(temp, " ");
strcat(temp, token);
get_token();
} else if (qualifier_checker(token)) {
strcat(temp, " ");
strcat(temp, token);
get_token();
} else
print_error("Unknown parameter type\n");
} while (token_type != ',' && token_type != ')');
strcat(out, temp);
if (token_type == ',')
strcat(out, ",");
}
void parameter_declarator(void) {
do {
specification_declarator();
} while (token_type == ',');
if (token_type != ')')
print_error("Missing ) in function\n");
}
void direct_declarator(void) {
int type;
if (token_type == '(') {
declarator();
if (token_type != ')')
print_error("Error : Missing ')'\n");
} else if (token_type == NAME)
strcpy(name, token);
else
print_error("Error : Expected name or (declarator)\n");
type = get_token();
while (type == PARENS || type == BRACKETS || type == '(') {
if (type == PARENS)
strcat(out, " function returning");
else if (type == '(') {
strcat(out, " function expecting");
parameter_declarator();
strcat(out, " and returning");
} else {
strcat(out, " array");
strcat(out, token);
strcat(out, " of");
}
type = get_token();
}
}
void declarator(void) {
int ns = 0;
for ( ; get_token() == '*'; )
ns++;
direct_declarator();
while (ns-- > 0)
strcat(out, " pointer to");
}
int main() {
while (get_token() != EOF) {
if (qualifier_checker(token)) {
strcpy(datatype, token);
get_token();
strcat(datatype, " ");
strcat(datatype, token);
} else
strcpy(datatype, token);
out[0] = '\0';
declarator();
if (token_type != '\n')
print_error("Syntax Error\n");
printf("%s : %s %s\n", name, out, datatype);
}
return 0;
}