-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractical_6.cpp
More file actions
64 lines (54 loc) · 1.31 KB
/
Copy pathPractical_6.cpp
File metadata and controls
64 lines (54 loc) · 1.31 KB
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
#include <iostream>
#include <vector>
using namespace std;
class RDP {
string input;
size_t ip;
bool flag;
public:
RDP(const string& str) : input(str), ip(0), flag(true) {}
void match(char expected) {
if (ip < input.length() && input[ip] == expected) {
ip++;
} else {
flag = false;
}
}
void S() {
if (ip < input.length() && input[ip] == '(') {
match('(');
L();
match(')');
} else if (ip < input.length() && input[ip] == 'a') {
match('a');
} else {
flag = false;
}
}
void L() {
S();
while (ip < input.length() && input[ip] == ',') {
match(',');
S();
}
}
bool parse() {
S();
return flag && ip == input.length();
}
};
int main() {
int n;
cout << "Enter number of strings: ";
cin >> n;
vector<string> inputs(n);
cout << "Enter " << n << " strings: " << endl;
for (auto &input : inputs) {
cin >> input;
}
for (const auto& input : inputs) {
RDP parser(input);
cout << (parser.parse() ? "Valid string: " : "Invalid string: ") << input << endl;
}
return 0;
}