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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
| #include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXTOKEN 100
#define BUFSIZE 100
enum {NAME, PARENS, BRACKETS};
enum {NO, YES};
void dcl(void); //parse a declarator
void dirdcl(void); //parse a direct declarator
int gettoken(void);
void paramdcl(void); //declarator analysis with parameters
void declspec(void); //specification declaration
int typespec(void); //type specifier
int typedesc(void); //descriptor type
int getch(void); //get a (possibly pushed-back) character
void ungetch(int c); //push character back on input
void errmesg(char *msg); //error message
int tokentype; //type of last token
char token[MAXTOKEN]; //last token string
char name[MAXTOKEN]; //identifier name
char datatype[MAXTOKEN]; //data type = char, int, etc.
char buf[BUFSIZE]; //buffer for ungetch;
char out[1000];
int errtoken = NO;
int errbrack = NO;
/* convert declaration to words */
int main()
{
while(gettoken() != EOF) //1st token on line
{
strcpy(datatype, token); //is the datatype
out[0] = '\0';
dcl(); //parse rest of line
if(tokentype != '\n')
printf("syntax error\n");
printf("%s: %s %s\n", name, out, datatype);
}
return 0;
}
/* dcl: parse a declarator */
void dcl(void)
{
int ns;
for(ns = 0; gettoken() == '*';) //count stars
ns++;
dirdcl();
while(ns-- > 0)
strcat(out, " pointer to");
}
/* dirdcl: parse a direct declarator */
void dirdcl(void)
{
int type;
if(tokentype == '(') //(dcl)
{
dcl();
if(tokentype != ')')
errmesg("error: missing )\n");
}
else if(tokentype == NAME) //variable name
{
if(name[0] == '\0')
strcpy(name, token);
}
else
errtoken = YES;
while((type = gettoken()) == PARENS || type == BRACKETS || type == '(')
if(type == PARENS)
strcat(out, " function returning");
else if(type == '(')
{
strcat(out, " function taking");
paramdcl();
strcat(out, " and returning");
}
else
{
strcat(out, " array");
strcat(out, token);
strcat(out, " of");
}
}
/* errmesg: error message */
void errmesg(char *msg)
{
printf("%s", msg);
errtoken = YES;
}
/* paramdcl: declarator analysis with parameters */
void paramdcl(void)
{
do
{
declspec(); //specification declaration
} while(tokentype == ',');
if(tokentype != ')')
errmesg("missing ) in declarator of parameters\n");
}
/* specification declaration */
void declspec(void)
{
char temp[MAXTOKEN];
temp[0] = '\0';
gettoken();
do
{
if(tokentype != NAME)
{
errtoken = YES;
dcl();
}
else if(typespec() == YES) // type specifier
{
strcat(temp, " ");
strcat(temp, token);
gettoken();
}
else if(typedesc() == YES) //descriptor type
{
strcat(temp, " ");
strcat(temp, token);
gettoken();
}
else
errmesg("error: unknown type in the parameter list\n");
} while(tokentype != ',' && tokentype != ')');
strcat(out, temp);
if(tokentype == ',')
strcat(out, ",");
}
/* type specifier */
int typespec(void)
{
static char *types[] = {"char", "int", "void"};
char *ptypes = token;
int result, i;
result = NO;
for(i = 0; i < 3; i++)
if(strcmp(ptypes, *(types + i)) != 0)
result = NO;
else
{
result = YES;
return result;
}
return result;
}
/* descriptor type */
int typedesc(void)
{
static char *typed[] = {"const", "volatile"};
char *ptd = token;
int result, i;
result = NO;
for(i = 0; i < 2; i++)
if(strcmp(ptd, *(typed + i)) != 0)
result = NO;
else
{
result = YES;
return result;
}
return result;
}
int gettoken(void)
{
int c;
char *p = token;
if(errtoken == YES)
{
errtoken = NO;
return tokentype;
}
while((c = getch()) == ' ' || c == '\t')
;
if(c == '(')
{
if((c = getch()) == ')')
{
strcpy(token, "()");
return tokentype = PARENS;
}
else
{
ungetch(c);
return tokentype = '(';
}
}
else if(c == '[')
{
for(*p++ = c; *p != ']';)
{
*p = getch();
if(*p != ']')
{
if(*p == '\n' || *p == ')' || *p == '(')
{
printf("error: missing ]\n");
ungetch(*p);
*p = ']';
}
else
p++;
}
}
*++p = '\0';
return tokentype = BRACKETS;
}
else if(isalpha(c))
{
for(*p++ = c; isalnum(c = getch());)
*p++ = c;
*p = '\0';
ungetch(c);
return tokentype = NAME;
}
else
return tokentype = c;
}
int bufp = 0;
int getch(void) // get a (possibly pushed-back) character
{
int bufp = 0; //next free position in buf
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c) // push character back on input
{
if(bufp >= BUFSIZE)
printf("ungetch: too many characnters\n");
else
buf[bufp++] = c;
} |