-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
177 lines (144 loc) · 2.91 KB
/
main.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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXTOKENLEN 64
#define MAXTOKENS 128
struct token {
char type;
char string[MAXTOKENLEN];
};
enum token_type {
TYPE,
QUALIFIER,
IDENTIFIER
};
/* main token stack */
struct token stack[MAXTOKENS];
int stack_index = -1;
#define push(A) stack[++stack_index] = (A)
#define pop stack[stack_index--]
#define current stack[stack_index]
/* current token */
struct token this;
/* returns the type of the current token */
enum token_type
classify_string()
{
char *str = this.string;
if ( !strcmp(str, "void")
|| !strcmp(str, "char")
|| !strcmp(str, "short")
|| !strcmp(str, "int")
|| !strcmp(str, "long")
|| !strcmp(str, "signed")
|| !strcmp(str, "unsigned")
|| !strcmp(str, "float")
|| !strcmp(str, "double")
|| !strcmp(str, "struct")
|| !strcmp(str, "union")
|| !strcmp(str, "enum")
) return TYPE;
if (!strcmp(str, "volatile"))
return QUALIFIER;
if (!strcmp(str, "const")) {
strcpy(str, "readonly");
return QUALIFIER;
}
return IDENTIFIER;
}
/* reads the next token into `this` and classify it */
void
get_token()
{
char *str = this.string;
/* discard whitespace */
do {
*str = getchar();
} while (*str == ' ' || *str == '\t');
if (isalnum(*str)) {
/* read the rest */
while (isalnum(*++str = getchar())) ;
ungetc(*str, stdin);
*str = '\0';
this.type = classify_string();
return;
}
/* for other not alnum the token type is itself */
this.type = *str;
*++str = '\0';
return;
}
void
read_to_first_identifier()
{
do {
get_token();
push(this);
} while (this.type != IDENTIFIER);
printf("%s is ", this.string);
pop;
get_token();
}
/* --- Parsing Routines ---
*
* Work on the current token and always
* read the next one at the end.
*/
void
deal_with_function_args()
{
/* read past closing `)` */
while (this.type != ')') get_token();
get_token();
printf("function returning ");
}
void
deal_with_arrays()
{
printf("array ");
get_token(); /* read size or `]` */
/* print size if given */
if (isdigit(*this.string)) {
printf("with size %s ", this.string);
get_token(); /* read `]` */
}
printf("of ");
get_token();
}
/* prints pointers on stack, if any */
void
deal_with_pointers()
{
while (current.type == '*') {
printf("pointer to ");
pop;
}
}
void
deal_with_declarator()
{
if (this.type == '[')
deal_with_arrays();
if (this.type == '(')
deal_with_function_args();
/* while there is content on the stack */
while (stack_index >= 0) {
deal_with_pointers();
if (current.type == '(') {
/* simple parenthesis */
pop;
get_token(); /* should be the matching `)` */
deal_with_declarator();
} else {
printf("%s ", pop.string);
}
}
}
int
main(int argc, char **argv)
{
read_to_first_identifier();
deal_with_declarator();
printf("\n");
return 0;
}