-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path220126_c_declaration_reader.cpp
221 lines (187 loc) · 6.1 KB
/
220126_c_declaration_reader.cpp
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
// Inspired by "Expert C Programming" Chapter 3
// usage: send c declaration to cin
// example:
// input: char *const *(*next)()
// interpreted: next is pointer to a function with parameters '()' that returns pointer to constant pointer to char
// input: char *(*c[10])(int **p)
// interpreted: c is array of 10 pointer to a function with parameters '(int**p)' that returns pointer to char
// input: int const *const (*(* const(*toxic[])())[10])()
// interpreted: toxic is array of pointer to a function with parameters '()' that returns constant pointer to array of 10 pointer to a function with parameters '()' that returns constant pointer to constant int
#include <iostream>
#include <iomanip>
#include <vector>
#include <cctype>
#include <algorithm>
// supported keywords:
// const, volatile
// char, int
// no struct, no union, no enum
constexpr std::array<const char*, 4> reserved = {
"const", "volatile",
"char", "int",
};
std::vector<std::string> parse(const std::string& input)
{
std::vector<std::string> ret;
if (input.empty())
return ret;
auto iter = input.begin();
do
{
if (std::isspace(*iter))
continue;
else if ('*' == *iter)
ret.push_back("*");
else if ('(' == *iter)
ret.push_back("(");
else if (')' == *iter)
ret.push_back(")");
else if ('[' == *iter)
ret.push_back("[");
else if (']' == *iter)
ret.push_back("]");
else if (',' == *iter)
ret.push_back(",");
else if (0 != std::isdigit(*iter))
{
// advance until end of number
auto num_begin = iter, num_end = iter;
while (input.end() != ++num_end && 0 != std::isdigit(*num_end));
iter = num_end - 1;
ret.push_back(input.substr(num_begin - input.begin(), num_end - num_begin));
}
else if (0 != std::isalpha(*iter))
{
// advance until end of alnum
auto alnum_begin = iter, alnum_end = iter;
while (input.end() != ++alnum_end && 0 != std::isalnum(*alnum_end));
iter = alnum_end - 1;
ret.push_back(input.substr(alnum_begin - input.begin(), alnum_end - alnum_begin));
}
else
{
throw std::runtime_error(std::string("unable to parse ") + *iter);
}
}
while (++iter != input.end());
return ret;
}
std::string process(const std::string& input)
{
if (input.empty())
return std::string();
std::cout << std::setw(20) << "input: " << input << '\n';
std::vector<std::string> words = parse(input);
/*
std::cout << std::setw(20) << "parsed: ";
std::for_each(words.begin(), words.end(), [](const auto& s){ std::cout << ' ' << s; });
std::cout << '\n';
*/
// find identifier
const auto iter_id = std::find_if(words.begin(), words.end(), [](const auto& s){
return std::isalpha(s.front())
&& std::find_if(reserved.begin(), reserved.end(), [&s](const auto& r){
return s == r; }) == reserved.end();
});
std::string ret = *iter_id + " is";
auto iter = iter_id - 1;
words.erase(iter_id, iter_id + 1);
int state = 0;
while (state != -1)
{
if (state == 0)
{
// look right
auto iter_right = iter + 1;
if (words.end() == iter_right)
{
// do nothing
}
else if ("(" == *iter_right)
{
ret += " a function with parameters '";
const auto iter_closing = std::find_if(iter_right, words.end(), [](const auto& s){ return ")" == s; });
std::string params;
std::for_each(iter_right, iter_closing + 1, [¶ms](const auto& s){ params += s; });
ret += params;
ret += "' that returns";
words.erase(iter_right, iter_closing + 1);
}
else if ("[" == *iter_right)
{
ret += " array of";
if ("]" == *(iter_right + 1))
{
words.erase(iter_right, iter_right + 2);
}
else
{
ret += " " + *(iter_right + 1); // must be a number
words.erase(iter_right, iter_right + 3);
}
continue;
}
state = 1;
}
else if (state == 1)
{
// look left
auto end_iter = iter;
if ("(" == *iter)
{
if (")" != *(iter + 1))
throw std::runtime_error(std::string("syntax error : closing brace expected but got ") + *(iter + 1));
end_iter = end_iter + 2;
state = 0;
}
else
{
if ("const" == *iter)
{
ret += " constant";
}
else if ("volatile" == *iter)
{
ret += " volatile";
}
else if ("*" == *iter)
{
ret += " pointer to";
}
else
{
ret += " " + *iter;
}
end_iter = end_iter + 1;
}
if (words.begin() == iter)
{
state = -1; // finished
words.clear();
}
else
{
--iter;
words.erase(iter + 1, end_iter);
}
}
/*
std::cout << std::setw(20) << "remaining: ";
std::for_each(words.begin(), words.end(), [](const auto& s){ std::cout << ' ' << s; });
std::cout << '\n';
*/
}
std::cout << std::setw(20) << "interpreted: " << ret << '\n';
return ret;
}
int main()
{
while (true)
{
std::string input;
std::getline(std::cin, input);
if (input.empty()) // finish program
break;
process(input);
}
}