/******** Name :- Md Firoz Islam ***********
University :- Northern University Bangladesh
********** Dhaka, Bangladesh ***************/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define faster ios::sync_with_stdio(false),cin.tie(NULL);
bool isBracketBalanced(const string& s, stack<pair<char, int>>& bracketStack, int lineNumber)
{
for (int i = 0; i < s.size(); i++)
{
char ch = s[i];
if (ch == '{' || ch == '(' || ch == '[')
{
bracketStack.push({ch, lineNumber});
}
else if (ch == '}' || ch == ')' || ch == ']')
{
if (bracketStack
.empty()) {
cout << "Syntax Error: Unmatched closing bracket '" << ch << "' at line " << lineNumber << endl;
return false;
}
char top = bracketStack.top().first;
if ((ch == '}' && top != '{') || (ch == ')' && top != '(') || (ch == ']' && top != '['))
{
cout << "Syntax Error: Mismatched brackets at line " << lineNumber << endl;
return false;
}
bracketStack.pop();
}
}
return true;
}
bool isSemicolonMissing(const string& line)
{
if(line=="int main()")
return false;
string s = line;
s.erase(s.find_last_not_of(" \t\r\n") + 1);
if (s
.empty() || s
.back
() == ';' || s
.back
() == '{' || s
.back
() == '}') {
return false;
}
return true;
}
int main()
{
ifstream
file("Firoz.txt"); {
cout << "Unable to open the file" << endl;
return 1;
}
stack<pair<char, int>> bracketStack;
vector<pair<string, int>> semicolonErrors;
string line;
int lineNumber = 0;
while (getline
(file, line
)) {
lineNumber++;
isBracketBalanced(line, bracketStack, lineNumber);
if (isSemicolonMissing(line))
{
semicolonErrors.push_back({line, lineNumber});
}
}
while (!bracketStack
.empty()) {
auto unmatched = bracketStack.top();
bracketStack.pop();
cout << "Syntax Error: Unmatched opening bracket '" << unmatched.first << "' at line " << unmatched.second << endl;
}
for (const auto& error : semicolonErrors)
{
cout << "Syntax Error: Missing semicolon at line " << error.second << ": " << error.first << endl;
}
if (bracketStack
.empty() && semicolonErrors
.empty()) {
cout << "No Syntax Errors found." << endl;
}
return 0;
}