fork download
  1. /******** Name :- Md Firoz Islam ***********
  2. University :- Northern University Bangladesh
  3. ********** Dhaka, Bangladesh ***************/
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. #define ll long long
  7. #define yes cout<<"YES"<<endl;
  8. #define no cout<<"NO"<<endl;
  9. #define faster ios::sync_with_stdio(false),cin.tie(NULL);
  10.  
  11. bool isBracketBalanced(const string& s, stack<pair<char, int>>& bracketStack, int lineNumber)
  12. {
  13. for (int i = 0; i < s.size(); i++)
  14. {
  15. char ch = s[i];
  16. if (ch == '{' || ch == '(' || ch == '[')
  17. {
  18. bracketStack.push({ch, lineNumber});
  19. }
  20. else if (ch == '}' || ch == ')' || ch == ']')
  21. {
  22. if (bracketStack.empty())
  23. {
  24. cout << "Syntax Error: Unmatched closing bracket '" << ch << "' at line " << lineNumber << endl;
  25. return false;
  26. }
  27. char top = bracketStack.top().first;
  28. if ((ch == '}' && top != '{') || (ch == ')' && top != '(') || (ch == ']' && top != '['))
  29. {
  30. cout << "Syntax Error: Mismatched brackets at line " << lineNumber << endl;
  31. return false;
  32. }
  33. bracketStack.pop();
  34. }
  35. }
  36. return true;
  37. }
  38.  
  39. bool isSemicolonMissing(const string& line)
  40. {
  41. if(line=="int main()")
  42. return false;
  43. string s = line;
  44. s.erase(s.find_last_not_of(" \t\r\n") + 1);
  45. if (s.empty() || s.back() == ';' || s.back() == '{' || s.back() == '}')
  46. {
  47. return false;
  48. }
  49. return true;
  50. }
  51.  
  52. int main()
  53. {
  54. ifstream file("Firoz.txt");
  55. if (!file.is_open())
  56. {
  57. cout << "Unable to open the file" << endl;
  58. return 1;
  59. }
  60.  
  61. stack<pair<char, int>> bracketStack;
  62. vector<pair<string, int>> semicolonErrors;
  63. string line;
  64. int lineNumber = 0;
  65.  
  66. while (getline(file, line))
  67. {
  68. lineNumber++;
  69.  
  70. isBracketBalanced(line, bracketStack, lineNumber);
  71.  
  72. if (isSemicolonMissing(line))
  73. {
  74. semicolonErrors.push_back({line, lineNumber});
  75. }
  76. }
  77.  
  78. while (!bracketStack.empty())
  79. {
  80. auto unmatched = bracketStack.top();
  81. bracketStack.pop();
  82. cout << "Syntax Error: Unmatched opening bracket '" << unmatched.first << "' at line " << unmatched.second << endl;
  83. }
  84.  
  85. for (const auto& error : semicolonErrors)
  86. {
  87. cout << "Syntax Error: Missing semicolon at line " << error.second << ": " << error.first << endl;
  88. }
  89.  
  90. if (bracketStack.empty() && semicolonErrors.empty())
  91. {
  92. cout << "No Syntax Errors found." << endl;
  93. }
  94.  
  95. file.close();
  96. return 0;
  97. }
  98.  
  99.  
Success #stdin #stdout 0.03s 25872KB
stdin
Standard input is empty
stdout
/******** 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");
    if (!file.is_open())
    {
        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;
    }

    file.close();
    return 0;
}