-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemanticcheck.cpp
More file actions
52 lines (43 loc) · 1.07 KB
/
semanticcheck.cpp
File metadata and controls
52 lines (43 loc) · 1.07 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
#include <iostream>
#include <set>
#include <vector>
using namespace std;
// Represents a switch statement for semantic checking
class SwitchStatement {
private:
set<int> caseLabels;
bool hasDefault = false;
public:
// Add a case label
bool addCase(int value) {
if (caseLabels.count(value)) {
cout << "Semantic Error: Duplicate case label: " << value << endl;
return false;
}
caseLabels.insert(value);
return true;
}
// Add default label
bool addDefault() {
if (hasDefault) {
cout << "Semantic Error: Multiple default labels in switch statement" << endl;
return false;
}
hasDefault = true;
return true;
}
};
int main() {
SwitchStatement sw;
// Simulating switch cases
sw.addCase(1);
sw.addCase(2);
sw.addCase(3);
// Duplicate case (error)
sw.addCase(2);
// Default case
sw.addDefault();
// Duplicate default (error)
sw.addDefault();
return 0;
}