Hello my question is if i could get assistance in this program I have 2 psuedocodes to check for syntax errors and I dont know how to take the input from the user of the either file name and check for syntax errors. I understand the stack of syntax checker I am just having trouble with the variables of the fstream library. Thank you #include #include #include #include using namespace std; int main() { stack s;/*created a stack object with char data type*/ ifstream testFile; cout << "Enter the file name: "; testFile.open("testFile", ios::in); //taking file as input /* is this where the variables for the stack are initialized? */ for (int i = 0; i < (int)testFile.length(); i++) /* or is this were it is intialized? */ { if (testFile[i] == '[' || testFile[i] == '(') /* depending on what is initialized is testFile index correct use for the stack? */ { s.push(testFile[i]); // test file? /* push if [ or ( are found*/ } if (testFile[i] == ']') //testfile? { if (s.empty()) { s.push(testFile[i]); //testfile? } if (s.top() == '[') { s.pop(); /*pop if ] found an top is [*/ } } if (testFile[i] == ')') //testfile? { if (s.empty()) { s.push(testFile[i]); // testfile? } if (s.top() == '(') { s.pop(); /*pop if ) found an top is (*/ } } } if (s.empty()) { cout << "\n No Syntax Error Occured."; } else { cout << "\nSyntax Error Occured."; } return 0; }
Hello my question is if i could get assistance in this program I have 2 psuedocodes to check for syntax errors and I dont know how to take the input from the user of the either file name and check for syntax errors. I understand the stack of syntax checker I am just having trouble with the variables of the fstream library.
Thank you
#include <iostream>
#include <fstream>
#include<stack>
#include<string>
using namespace std;
int main()
{
stack<char> s;/*created a stack object with char data type*/
ifstream testFile;
cout << "Enter the file name: ";
testFile.open("testFile", ios::in);
//taking file as input /* is this where the variables for the stack are initialized? */
for (int i = 0; i < (int)testFile.length(); i++) /* or is this were it is intialized? */
{
if (testFile[i] == '[' || testFile[i] == '(') /* depending on what is initialized is testFile index correct use for the stack? */
{
s.push(testFile[i]); // test file?
/* push if [ or ( are found*/
}
if (testFile[i] == ']') //testfile?
{
if (s.empty())
{
s.push(testFile[i]); //testfile?
}
if (s.top() == '[')
{
s.pop();
/*pop if ] found an top is [*/
}
}
if (testFile[i] == ')') //testfile?
{
if (s.empty())
{
s.push(testFile[i]); // testfile?
}
if (s.top() == '(')
{
s.pop();
/*pop if ) found an top is (*/
}
}
}
if (s.empty())
{
cout << "\n No Syntax Error Occured.";
}
else
{
cout << "\nSyntax Error Occured.";
}
return 0;
}
Step by step
Solved in 4 steps with 3 images