-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSyntaxCheck.h
201 lines (189 loc) · 6.44 KB
/
SyntaxCheck.h
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
#pragma once
#include<iostream>
#include<string>
#include "Stack.h"
using namespace std;
#define MAX 100
//it has functions to check keywords and parentheses
class SyntaxCheck
{
public:
//it checks if the content are brackets
static bool are_brackets(const string& expression)
{
for (char i : expression) {
if (i == '{' || i == '}'
|| i == ')' || i == '(')
return true;
}
return false;
}
//it compares the keywords
static bool keywords(string data[], int size)
{
if (data[0] != "Begin")
{
cout << "begin NOT found!" << endl;
exit(0);
}
string keywords[MAX];
int keyword_size = 0;
for (int i = 0; i < size; i++)
{
if (data[i].find("Begin") != std::string::npos)
keywords[keyword_size++] = "Begin";
if ((data[i].find("if") != std::string::npos)
&& data[i].find("endif") == std::string::npos
&& data[i].find("elseif") == std::string::npos)
{
keywords[keyword_size++] = "if";
}
if ((data[i].find("else") != std::string::npos)
&& data[i].find("elseif") == std::string::npos) {
keywords[keyword_size++] = "else";
}
if ((data[i].find("elseif") != std::string::npos))
keywords[keyword_size++] = "elseif";
if ((data[i].find("while") != std::string::npos)
&& data[i].find("endwhile") == std::string::npos) {
keywords[keyword_size++] = "while";
}
if ((data[i].find("for") != std::string::npos)
&& data[i].find("endfor") == std::string::npos) {
keywords[keyword_size++] = "for";
}
if (data[i].find("endif") != std::string::npos)
keywords[keyword_size++] = "endif";
if (data[i].find("endfor") != std::string::npos)
keywords[keyword_size++] = "endfor";
if (data[i].find("endwhile") != std::string::npos)
keywords[keyword_size++] = "endwhile";
if
(
(data[i].find("end") != std::string::npos)
&& data[i].find("endfor") == std::string::npos
&& data[i].find("endwhile") == std::string::npos
&& data[i].find("endif") == std::string::npos
)
{
keywords[keyword_size++] = "end";
}
}
Stack s;
string temp;
int else_ifs = 1;
for (int i = 0; i < keyword_size; i++) {
if (keywords[i] == "Begin"
|| keywords[i] == "if"
|| keywords[i] == "elseif"
|| keywords[i] == "else"
|| keywords[i] == "for"
|| keywords[i] == "while")
{
if (keywords[i] == "else" || keywords[i] == "elseif")
else_ifs++;
s.push(keywords[i]);
continue;
}
if (s.empty())
return false;
if (keywords[i] == "endif") {
for (int i = 0; i < else_ifs; i++) {
temp = s.Top();
s.pop();
if (temp == "for" || temp == "while" || temp == "Begin") {
cout << "Invalid if/endif declaration" << endl;;
exit(0);
}
}
}
else if (keywords[i] == "endwhile") {
temp = s.Top();
s.pop();
if (temp != "while") {
cout << "endwhile found but while was not!" << endl;
exit(0);
}
}
else if (keywords[i] == "endfor") {
temp = s.Top();
s.pop();
if (temp != "for") {
cout << "endfor found but for was not!" << endl;
exit(0);
}
}
else if (keywords[i] == "end") {
if (i != keyword_size - 1) {
cout << "Multiple end statements found!" << endl;
exit(0);
}
else if (s.Top() == "Begin")
s.pop();
}
}
return s.empty();
}
//it checks the brackets
static bool check_brackets(string* data, int size) {
string expr = "";
// Get all the brackets in the entire string array.
for (int i = 0; i < size; i++)
{
string f = data[i];
for (char j : f) {
if (j == '(')
expr += '(';
else if (j == ')')
expr += ')';
else if (j == '{')
expr += '{';
else if (j == '}')
expr += '}';
else if (j == '[')
expr += '[';
else if (j == ']')
expr += ']';
}
}
// Verifying the brackets
Stack s;
string parse;
char popped;
for (char i : expr)
{
if (i == '(' || i == '[' || i == '{') {
parse = i;
s.push(parse);
continue;
}
if (s.empty())
return false;
switch (i) {
case ')':
popped = s.Top()[0];
s.pop();
if (popped == '{' || popped == '[')
return false;
break;
case '}':
popped = s.Top()[0];
s.pop();
if (popped == '(' || popped == '[')
return false;
break;
case ']':
popped = s.Top()[0];
s.pop();
if (popped == '(' || popped == '{')
return false;
break;
}
}
return s.empty();
}
static bool substring(const string& toCheck, const string& subString)
{
return toCheck.find(subString) != std::string::npos;
}
};