Question

IMPLEMENT IN C++ Implement a symbol balance checker function for the Pascal programming language. Pascal allows...

IMPLEMENT IN C++

Implement a symbol balance checker function for the Pascal programming language. Pascal allows for the following pairs: {}, (), [], begin end . All programs will begin with the word "begin" and end with the word "end". Your function should receive an ifstream object which is already open and will return true, all of the symbols match, or false, they do not. You do not have to worry about comments in the program but you do have to avoid other parts of the program's code such as assignment statements (x=y) and other expressions.

THIS IS HOW THE CODE WILL BE TESTED:

You should test with a test Pascal input program that you can validate is correct manually, and another that you can validate is incorrect. If it passes these tests, it should pass ours.

Every [ should have a matching ]

Every { should have a matching }

Every begin should have a matching end

Every ( should have a matching )

These can be nested in any order, and with any number of begins and ends.

So for instance, a valid test program would be:

begin

   { ignore this

      begin

          ignore this too

      end

   }

end

We are not going to be testing your code with Pascal code that has single bracket characters in strings like "}", so don't worry about that.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

main.cpp:

#include<iostream>
#include<fstream>
#include<stack>
using namespace std;

// return true if file contains all currect matching otherwise return false
bool isCorrect(ifstream &ifs){
   stack<string> stk;
   string line;
   do {
       getline(ifs, line);
       if (line.find("begin") != string::npos){
           stk.push("begin");
       } else if (line.find("{") != string::npos){
           stk.push("{");
       } else if (line.find("(") != string::npos){
           stk.push("(");
       } else if (line.find("[") != string::npos){
           stk.push("[");
       } else if (line.find("end") != string::npos){
           if (stk.top() != "begin")
               return false;
           stk.pop();
       } else if (line.find("}") != string::npos){
           if (stk.top() != "{")
               return false;
           stk.pop();
       } else if (line.find(")") != string::npos){
           if (stk.top() != "(")
               return false;
           stk.pop();
       } else if (line.find("]") != string::npos){
           if (stk.top() != "]")
               return false;
           stk.pop();
       }

   } while (!ifs.eof());
  
   if (stk.empty()){
       return true;
   }
   return false;
}

int main(){
   ifstream ifs;
   ifs.open("input.txt");
  
   // check input.txt file have currect matching of not
   if (isCorrect(ifs)){
       cout<<"file contains all correct matching.";
   } else {
       cout<<"file not contains all correct matching.";
   }
  
   ifs.close();
}

input.txt:

begin

   { ignore this

      begin

          ignore this too

      end

   }

end

Output:

file contains all correct matching.

Add a comment
Know the answer?
Add Answer to:
IMPLEMENT IN C++ Implement a symbol balance checker function for the Pascal programming language. Pascal allows...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Pascal programming language Implement a symbol balance checker function for the Pascal programming language. Pascal allows...

    Pascal programming language Implement a symbol balance checker function for the Pascal programming language. Pascal allows for the following pairs: {}, (), [], begin end . All programs will begin with the word "begin" and end with the word "end". Your function should receive an ifstream object which is already open and will return true, all of the symbols match, or false, they do not. You do not have to worry about comments in the program but you do have...

  • In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to...

    In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to extract all matching patterns (substrings) from a given input DNA sequence string. The alphabet for generating DNA sequences is {A, T, G, C}. Write a regular expression that represents all DNA strings that contains at least two ‘A’s. Note: assume empty string is not a valid string. Design a deterministic finite automaton to recognize the regular expression. Write a program which asks the user...

  • Overview: The goal of this assignment is to implement a simple spell checker using a hash...

    Overview: The goal of this assignment is to implement a simple spell checker using a hash table. You will be given the basic guidelines for your implementation, but other than that you are free to determine and implement the exact classes and methods that you might need. Your spell-checker will be reading from two input files. The first file is a dictionary containing one word per line. The program should read the dictionary and insert the words into a hash...

  • In this assignment you will implement the second version of your spell checker. Using the randomi...

    In this assignment you will implement the second version of your spell checker. Using the randomized dictionary (random_dictionary.txt) given, read in the dictionary into an array of 26 Binary Search Trees (BST) , one for each letter of the alphabet. Thus the first BST would contain only those words starting with letter 'a', while the last would contain only those words starting with letter 'z'. Then, when you read in the book (oliver.txt), you examine the first character of each...

  • Please do the following project in C++ programming language. You can use a bag to create...

    Please do the following project in C++ programming language. You can use a bag to create a spell checker. The bag serves as a dictionary and contains a collection of correctly of correctly spelled workds. To see whether a word is spelled correctly, you see whether it is contained in the dictionary. Use this scheme to create a spell checker for the words in an external file. To simplify your task, restrict your dictionary to a manageable size. The dictionary...

  • Implement a deterministic finite automata (DFA) using C++ programming language to extract matchin...

    Implement a deterministic finite automata (DFA) using C++ programming language to extract matching patterns from a given input DNA sequence string. Design a deterministic finite automata to recognize the regular expression A(A+T+G+C)*A + T(A+T+G+C)*T over the alphaber {A,T,G,C}. This regular expression recognize any string that starts and ends with ‘A’ or starts and ends with ‘T’. Write a program which asks the user to input a DNA sequence. The program should be able to extract all the patterns (substrings present...

  • create a hash table to implement spell checker in java 1. Create a file of 100...

    create a hash table to implement spell checker in java 1. Create a file of 100 words of varying length (max 8 characters). 2. All the words are in lower case. 3. design a hash table. 4. enter each word in the hash table. Once the hash table is created, run it as a spell checker.enter a word (interactive), find the word in the hash table. If not found enter an error message. Then prompt for next word. End the...

  • CAN SOMEONE PLEASE HELP ME WRITE THIS CODE IN C++, PLEASE HAVE COMMENTS IN THE CODE...

    CAN SOMEONE PLEASE HELP ME WRITE THIS CODE IN C++, PLEASE HAVE COMMENTS IN THE CODE IF POSSIBLE!! General Description: Write a program that allows the user to enter data on their friends list. The list holds a maximum of 10 friends, but may have fewer. Each friend has a name, phone number, and email address. The program first asks the user to enter the number of friends (1-10). You are not required to validate the entry, but you may...

  • In python, Implement a function studentID() which allows the user to enter the 7-digit student ID....

    In python, Implement a function studentID() which allows the user to enter the 7-digit student ID. The program will keep prompting the user for a last name and first name. If the student does not have a student ID on record, the program will then ask for the student ID, and store that information. If the student already has a student ID, the program will display it, and ask for confirmation whether a new studentID should be assigned (and, if...

  • In c++ Write a program that allows the user to type in any one-line question and then answers tha...

    in c++ Write a program that allows the user to type in any one-line question and then answers that question. Your program won't really respond to the question, rather it will read a random response from a file. Requirements Your program should have a function called getQuestion() which prompts the user for a question and returns the number of characters in the question (including spaces and punctuation, not including the ending newline). If the user enters an empty string they...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT