/
cdecl.c
176 lines (151 loc) · 3.59 KB
/
cdecl.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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXTOKENLEN 100
#define MAXTOKENS 100
#define ungetchar(x) ungetc((x), stdin)
extern void exit(int);
void done();
const char IDENTIFIER = 'I',
QUALIFIER = 'Q',
TYPE = 'T',
END = 'E';
struct token {
char type;
char string[MAXTOKENLEN];
};
struct token token_stack[MAXTOKENS];
int stack_top = -1;
struct token curr_token;
/*******************************************
* Utility functions *
******************************************/
/*
Look at the current token and return a value of "type",
"qualifier" or "identifier"
*/
char classify_string() {
if(!strcmp(curr_token.string, "const")) return QUALIFIER;
if(!strcmp(curr_token.string, "volatile")) return QUALIFIER;
if(!strcmp(curr_token.string, "void")) return TYPE;
if(!strcmp(curr_token.string, "char")) return TYPE;
if(!strcmp(curr_token.string, "int")) return TYPE;
if(!strcmp(curr_token.string, "signed")) return TYPE;
if(!strcmp(curr_token.string, "unsigned")) return TYPE;
if(!strcmp(curr_token.string, "short")) return TYPE;
if(!strcmp(curr_token.string, "long")) return TYPE;
if(!strcmp(curr_token.string, "float")) return TYPE;
if(!strcmp(curr_token.string, "double")) return TYPE;
return IDENTIFIER;
}
/*
Read the next token into this.string
if it is alphanumeric, classify_string
else it must be a single character token
this.type = the token itself; terminate this.string
with a nul.
*/
void get_token() {
char c = 'a';
while((c = getchar()) != EOF) {
if(isspace(c)) continue;
else break;
}
if(c == EOF) {
curr_token.type = END;
return;
}
if(isalnum(c) || c == '_') {
int i = 0;
while(isalnum(c) || c == '_') {
curr_token.string[i++] = c;
c = getchar();
}
curr_token.string[i] = 0;
ungetchar(c);
curr_token.type = classify_string();
return;
}
curr_token.type = c;
if(c == '*') {
strcpy(curr_token.string, "pointer to ");
}
else {
curr_token.string[0] = c;
curr_token.string[1] = 0;
}
}
/*
get_token and push it onto the stack until the first
identifier is read.
Print "identifier is", this.string
get_token
*/
void read_to_first_identifier() {
get_token();
while(curr_token.type != END && curr_token.type != IDENTIFIER) {
token_stack[++stack_top] = curr_token;
get_token();
}
if(curr_token.type == END) {
printf("Bad syntax.\n");
done();
}
printf("%s is ", curr_token.string);
get_token();
}
/*******************************************
* Parsing functions *
******************************************/
void deal_with_function_args() {
char c;
while((c = getchar()) != ')')
putchar(c);
ungetchar(c);
printf("is a function returning ");
}
void deal_with_arrays() {
char c;
printf("an array[");
while((c = getchar()) != ']')
putchar(c);
ungetchar(c);
printf("] of ");
}
void deal_with_any_pointers() {
while(stack_top > -1 && token_stack[stack_top].type == '*') {
printf("%s ", token_stack[stack_top].string);
stack_top--;
}
}
void deal_with_declarator() {
if(curr_token.type == '[')
deal_with_arrays();
if(curr_token.type == '(')
deal_with_function_args();
deal_with_any_pointers();
while(stack_top > -1) {
if(token_stack[stack_top].type == '(') {
if(curr_token.type != ')') {
printf("Bad Syntax! Expected ')', got %s\n", curr_token.string);
done();
}
get_token();
stack_top--;
deal_with_declarator();
}
else {
printf("%s ", token_stack[stack_top].string);
stack_top--;
}
}
}
void done() {
printf("\n");
exit(0);
}
int main() {
read_to_first_identifier();
deal_with_declarator();
done();
}