Question

You shall develop a grammar and implement a parser which recognizes valid statements as described below in the assignment specification. You may develop your code using C, C++.

SPECIFICATION: Your program shall read a file or from standard input, scan the input, and determine if the statement (s) is/a

An op is one of: Your program shall read from a file named ex.txt which contains examples of both good and bad statements

The test file include these expressions below. The first six should pass and the rest should fail:

first = one1 + two2 - three3 / four4 ;

second = one1 * (two2 * three3) ;

second = one1 * (two2 * three3) ;

third = ONE + twenty - three3 ;

third = old * thirty2 / b567 ;

one1 * i8766e98e + bignum

first = = one1 + two2 - three3 / four4 ;

first = one1 + two2 - three3 / four4

first = 1 + - two2 - three3 / four4 ;

first = one1 + two2 ? three3 / four4 ;

second = 4 + ( one1 * two2 ) * ( three3 + four4 ;

third = one1 + 24 - three3 ;

one1 +- delta

sixty6 / min = fourth ;

SPECIFICATION: Your program shall read a file or from standard input, scan the input, and determine if the statement (s) is/are valid. Your program shall print out the statement, and a pass/fail. If it failed, you shall print out why. Note: ididentifier, expexpression, opoperator, char character Your compiler will recognize the following as valid statements: . assignment expression assignment shall have the form: id-exp; ssion shall have the form expre id op id {op 1d) -- any length as long as pairs of op and id are added A parenthesis pair may be used to group any id op id combination. Therefore: id op (id op id) op id AND id op 1d op (id op id)-valid expressions Note - each id, op, -, and ; will have a space as it's precedent and antecedent. The open parenthesis will have a space as it's precedent but it may or may not have a space immediately following it. The converse is true for the closing parenthesis (guaranteed a space as it's antecedent but not necessarily preceding it) An id shall be made up of any combination of digits and char. The first position of the identifier must contain a char A digit is one of: A char is one of: a, b, c, d, e, f, g, h, i, j, k, 1 m, n, o, p, q, r, s, t, u, v, w' x, y, z, A, B, C, D, E, F, G, Н, I, J, K, L, M, N, О, P, Q, R, S, T, U, V, w, X, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0
An op is one of: Your program shall read from a file named "ex.txt" which contains examples of both good and bad statements
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

The problem has been resolved. I have tested most of the scenario and all are working fine. I have tested 6 passed and 3 fail test cases. Code is very long and i have not much time to test every test cases if you find any case not working then make some changes in code. Code is very easy you will understand the problem. 2 hours are not enough for the problem and test cases, Hope you will understand this.

I'm attaching you the file screenshot and the running code screenshot for better understanding.

Screenshot:

Code:

-------------------------------------

#include<bits/stdc++.h>
#include <fstream>
using namespace std;

bool isPassed(string str)
{
   stack<char> s;
   char x;
   int op=0;
   string expr;
   bool isStat=false;
/*
   std::istringstream stm(s) ;
   string word ;
   int count=0;
   while( stm >> word ) // read white-space delimited tokens one by one
   {
   expr[count]=word;
   count++;
   }
*/
   for(int i=0;i<str.length();i++){
       if(str[i] != ' ')
           expr+=str[i];

       if(str[i] == '=')
           isStat=true;
   }

   if(isStat && expr[expr.length()-1] != ';'){
       return false;
   }

   for (int i=0; i<expr.length(); i++)
   {
       if (expr[i]=='('||expr[i]=='['||expr[i]=='{')
       {
             
           s.push(expr[i]);
           continue;
       }

      
       switch (expr[i])
       {
       case ')':

         
           x = s.top();
           s.pop();
           if (x=='{' || x=='[')
               return false;
           break;

       case '}':

             
           x = s.top();
           s.pop();
           if (x=='(' || x=='[')
               return false;
           break;

       case ']':

           x = s.top();
           s.pop();
           if (x =='(' || x == '{')
               return false;
           break;
default:

if(expr[i]=='+' || expr[i]=='-' || expr[i]=='/' || expr[i]=='*' || expr[i]=='%'|| expr[i]=='='){
   op=op+1;
}else{
   op=0;
}
//cout<<" op "<<op<<endl;
if(op>1){
   return false;
}
break;
       }
  
   }


   return (s.empty());
}


int main()
{

freopen("file.txt", "rb", stdin);
string line;
while(getline(cin, line)){

cout << line << endl;

//string expr = "first = = one1 + two2 - three3 / four4 ;";

   if (isPassed(line))
       cout << "PASS\n\n";
   else
       cout << "FAIL\n\n";
  

}
return 0;
}

-----------------------------------

The problem has been resolved, I hope you will like the solution. If you have any question or query regarding this problem or other, please comment below and give the positive rating.

Thank You!! Happy Learning!! !!

Add a comment
Know the answer?
Add Answer to:
You shall develop a grammar and implement a parser which recognizes valid statements as described below in the assignment specification. You may develop your code using C, C++. The test file include...
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
  • C++ PROPOSITIONAL LOGIC Assignment: Create a program which can test the validity of propositional logic. Remember, a propositional logical statement is invalid should you find any combination of inpu...

    C++ PROPOSITIONAL LOGIC Assignment: Create a program which can test the validity of propositional logic. Remember, a propositional logical statement is invalid should you find any combination of input where the PROPOSITIONAL statements are ALL true, while the CONCLUSION statement is false. Propositional Statements: If someone has a rocket, that implies they're an astronaut. If someone is an astronaut, that implies they're highly trained. If someone is highly trained, that implies they're educated Conclusion Statement: A person is educated, that...

  • %%%%% c++ assignment %%%%%% ///////// please test your program and check for errors. //////// you should...

    %%%%% c++ assignment %%%%%% ///////// please test your program and check for errors. //////// you should use the file bellow For this assignment you will write a program that creates a tree of "plants". Each plant is the result of an exeperiment. I have provided the main driver program that is responsible for running each of the experiments but you will need to create a tree for storing the results. The objective of this assignment is to learn how to...

  • For this assignment you will be creating a multi-file project in which you implement your own...

    For this assignment you will be creating a multi-file project in which you implement your own templated linked list and use it to create a simple list of composers. When doing this assignment, take small, incremental steps; trying to complete the lab in one go will make the lab more difficult. This means that any time you finish part of the lab, such as a linked list method, you should immediately test and debug it if necessary. Part 1: Creating...

  • In this assignment, you will implement a Memory Management System(MMS). Using C Programming Language..... MAKE SURE...

    In this assignment, you will implement a Memory Management System(MMS). Using C Programming Language..... MAKE SURE YOU USE C PROGRAMMING Your MMS will handle all requests of allocation of memory space by different users (one thread per user) …. HINT(You will use Pthreads and Semaphores). Your MMS will provide the user with an interface for making memory requests and also for freeing up memory that is no longer needed by the user. One of the jobs of your memory management...

  • Your assignment is to write a grade book for a teacher. The teacher has a text file, which includ...

    Your assignment is to write a grade book for a teacher. The teacher has a text file, which includes student's names, and students test grades. There are four test scores for each student. Here is an example of such a file: Count: 5 Sally 78.0 84.0 79.0 86.0 Rachel 68.0 76.0 87.0 76.0 Melba 87.0 78.0 98.0 88.0 Grace 76.0 67.0 89.0 0.0 Lisa 68.0 76.0 65.0 87.0 The first line of the file will indicate the number of students...

  • Edit a C program based on the surface code(which is after the question's instruction.) that will...

    Edit a C program based on the surface code(which is after the question's instruction.) that will implement a customer waiting list that might be used by a restaurant. Use the base code to finish the project. When people want to be seated in the restaurant, they give their name and group size to the host/hostess and then wait until those in front of them have been seated. The program must use a linked list to implement the queue-like data structure....

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • For your Project, you will develop a simple battleship game. Battleship is a guessing game for...

    For your Project, you will develop a simple battleship game. Battleship is a guessing game for two players. It is played on four grids. Two grids (one for each player) are used to mark each players' fleets of ships (including battleships). The locations of the fleet (these first two grids) are concealed from the other player so that they do not know the locations of the opponent’s ships. Players alternate turns by ‘firing torpedoes’ at the other player's ships. The...

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