SlideShare a Scribd company logo
I need help in parse additional types of expressions defined by the expanded grammar shown
below with the additions to the grammar highlighted in BOLD:
statement expression ',' assignments ';'
expression '( ' expressions ')'
expressions unary_expression | binary_expression | ternary_expression |
quaternary_expression
unary_expression expression '~'
binary_expression expression binary_operator expression
binary_operator '+' | '-' | '*' | '/' | '%' | '^' | '<' | '>' | '_'
ternary_expression expression '?' expression expression
quaternary_expression expression '#' expression expression expression
operand literal | variable | expression
assignments assignments ',' assignment | assignment
assignment variable '=' literal
Current C++ code:
parse.h // This file contains the function prototype of the parseName function whose body is
defined in parse.cpp.
string parseName(stringstream& in);
parse.cpp // characters until the next whitespace and returns the name that those characters form.
#include #include #include using namespace std; #include "parse.h"
string parseName(stringstream& in) {
main.cpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#include "expression.h"
#include "subexpression.h"
#include "symboltable.h"
#include "parse.h"
SymbolTable symbolTable;
void parseAssignments(stringstream& in);
int main() {
const int SIZE = 256;
Expression* expression;
char paren, comma, line[SIZE];
ifstream fin;
fin = ifstream("input.txt");
if (!(fin.is_open())) {
cout << "File did not open" << endl;
system("pause");
return 1;
}
while (true) {
fin.getline(line, SIZE);
if (!fin)
break;
stringstream in(line, ios_base::in);
in >> paren;
cout << line << " ";
try {
expression = SubExpression::parse(in);
in >> comma;
parseAssignments(in);
double result = expression->evaluate();
cout << "Value = " << result << endl;
}
catch (string message) {
cout << message << endl;
}
}
system("pause");
return 0;
}
void parseAssignments(stringstream& in) {
char assignop, delimiter;
string variable;
int value;
do {
variable = parseName(in);
in >> ws >> assignop >> value >> delimiter;
symbolTable.insert(variable, value);
}
while (delimiter == ',');
}
char alnum;
string name = "";
in >> ws;
while (isalnum(in.peek()))
{ in >> alnum; name += alnum;
}
return name; }
operand.h
class Operand: public Expression {
public: static Expression* parse(stringstream& in);
};
operand.cpp
using namespace std; #include "expression.h" #include "subexpression.h" #include "operand.h"
#include "variable.h" #include "literal.h" #include "parse.h"
Expression* Operand::parse(stringstream& in) {
char paren; int value;
in >> ws;
if (isdigit(in.peek())) {
in >> value; Expression* literal = new Literal(value); return literal;
} if (in.peek() == '(') {
in >> paren; return SubExpression::parse(in);
} else
return new Variable(parseName(in)); return 0;
}
subexpression.h
class SubExpression: public Expression {
public: SubExpression(Expression* left, Expression* right);
static Expression* parse(stringstream& in);
protected: Expression* left; Expression* right;
};
SubExpression.cpp
#include #include using namespace std; #include "expression.h" #include "subexpression.h"
#include "operand.h" #include "plus.h" #include "minus.h"
SubExpression::SubExpression(Expression* left, Expression* right) {
this->left = left; this->right = right;
} Expression* SubExpression::parse(stringstream& in) {
Expression* left; Expression* right; char operation, paren; left = Operand::parse(in); in >>
operation; right = Operand::parse(in); in >> paren; switch (operation) {
case '+': return new Plus(left, right);
case '-': return new Minus(left, right);
} return 0; }
plus.h
class Plus: public SubExpression {
public: Plus(Expression* left, Expression* right): SubExpression(left, right) { } double
evaluate() { return left->evaluate() + right->evaluate(); }
};
variable.h
class Variable: public Operand {
public: Variable(string name) { this->name = name; } double evaluate(); private: string name;
};
Variable.cpp
#include #include using namespace std; #include "expression.h" #include "operand.h" #include
"variable.h" #include "symboltable.h" extern SymbolTable symbolTable;
double Variable::evaluate() {
return symbolTable.lookUp(name);
}
minus.h
class Minus: public SubExpression {
public: Minus(Expression* left, Expression* right): SubExpression(left, right) { } double
evaluate() { return left->evaluate() - right->evaluate(); }
};
literal.h
class Literal: public Operand {
public: Literal(double value) { this->value = value; } double evaluate() { return value; } private:
double value; }; expression.h class Expression { public: virtual double evaluate() = 0;
};
symbolTable.h
class SymbolTable {
public: SymbolTable() {} void insert(string variable, double value); double lookUp(string
variable) const; private: struct Symbol { Symbol(string variable, double value) { this->variable =
variable; this->value = value; }
string variable; double value; };
vector elements;
};
symbolTable.cpp
#include #include using namespace std; #include "symboltable.h"
void SymbolTable::insert(string variable, double value) { const Symbol& symbol =
Symbol(variable, value); elements.push_back(symbol);
}
double SymbolTable::lookUp(string variable) const {
for (int i = 0; i < elements.size(); i++) if (elements[i].variable == variable) return
elements[i].value; return -1;
}

More Related Content

Similar to I need help in parse additional types of expressions defined by the ex.pdf

Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
Vineet Kumar Saini
 
Object Oriented Programming Using C++: C++ Namespaces.pptx
Object Oriented Programming Using C++: C++ Namespaces.pptxObject Oriented Programming Using C++: C++ Namespaces.pptx
Object Oriented Programming Using C++: C++ Namespaces.pptx
RashidFaridChishti
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operatorSAFFI Ud Din Ahmad
 
What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
Paulo Morgado
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondMario Fusco
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Array Cont
Array ContArray Cont
Tut1
Tut1Tut1
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
Acácio Oliveira
 
Python basic
Python basicPython basic
Python basic
Saifuddin Kaijar
 
Web Technology_10.ppt
Web Technology_10.pptWeb Technology_10.ppt
Web Technology_10.ppt
Aftabali702240
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
Bradley Holt
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016
Britta Alex
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functionsmussawir20
 
7 functions
7  functions7  functions
7 functions
MomenMostafa
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
Tomasz Kowalczewski
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
Chandrakant Divate
 
Bca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointersBca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointers
Rai University
 

Similar to I need help in parse additional types of expressions defined by the ex.pdf (20)

Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Object Oriented Programming Using C++: C++ Namespaces.pptx
Object Oriented Programming Using C++: C++ Namespaces.pptxObject Oriented Programming Using C++: C++ Namespaces.pptx
Object Oriented Programming Using C++: C++ Namespaces.pptx
 
Php & my sql
Php & my sqlPhp & my sql
Php & my sql
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Array Cont
Array ContArray Cont
Array Cont
 
Tut1
Tut1Tut1
Tut1
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 
Python basic
Python basicPython basic
Python basic
 
Web Technology_10.ppt
Web Technology_10.pptWeb Technology_10.ppt
Web Technology_10.ppt
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
 
7 functions
7  functions7  functions
7 functions
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
Bca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointersBca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointers
 
Function in C program
Function in C programFunction in C program
Function in C program
 

More from shreeaadithyaacellso

In c++ Polymorphism is one of the hallmarks of OOP languages- What are (1).pdf
In c++ Polymorphism is one of the hallmarks of OOP languages- What are (1).pdfIn c++ Polymorphism is one of the hallmarks of OOP languages- What are (1).pdf
In c++ Polymorphism is one of the hallmarks of OOP languages- What are (1).pdf
shreeaadithyaacellso
 
In Chapter 4 of LS- there is a case of Zappos and it describes how Zap.pdf
In Chapter 4 of LS- there is a case of Zappos and it describes how Zap.pdfIn Chapter 4 of LS- there is a case of Zappos and it describes how Zap.pdf
In Chapter 4 of LS- there is a case of Zappos and it describes how Zap.pdf
shreeaadithyaacellso
 
In C++ Plz LAB- Playlist (output linked list) Given main()- complete.pdf
In C++ Plz  LAB- Playlist (output linked list) Given main()- complete.pdfIn C++ Plz  LAB- Playlist (output linked list) Given main()- complete.pdf
In C++ Plz LAB- Playlist (output linked list) Given main()- complete.pdf
shreeaadithyaacellso
 
In C Programming- not C++ Write a function which takes two formal pa.pdf
In C Programming- not C++   Write a function which takes two formal pa.pdfIn C Programming- not C++   Write a function which takes two formal pa.pdf
In C Programming- not C++ Write a function which takes two formal pa.pdf
shreeaadithyaacellso
 
In chapter 17 you learned about the big picture of circulation through.pdf
In chapter 17 you learned about the big picture of circulation through.pdfIn chapter 17 you learned about the big picture of circulation through.pdf
In chapter 17 you learned about the big picture of circulation through.pdf
shreeaadithyaacellso
 
In cell M11- enter a formula that will calculate the total amount due.pdf
In cell M11- enter a formula that will calculate the total amount due.pdfIn cell M11- enter a formula that will calculate the total amount due.pdf
In cell M11- enter a formula that will calculate the total amount due.pdf
shreeaadithyaacellso
 
In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdfIn C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
shreeaadithyaacellso
 
In C++ Complete the program with the bold requirements #include #i.pdf
In C++  Complete the program with the bold requirements   #include  #i.pdfIn C++  Complete the program with the bold requirements   #include  #i.pdf
In C++ Complete the program with the bold requirements #include #i.pdf
shreeaadithyaacellso
 
I am studying for a test tomorrow in my 494 Population Biology class-.pdf
I am studying for a test tomorrow in my 494 Population Biology class-.pdfI am studying for a test tomorrow in my 494 Population Biology class-.pdf
I am studying for a test tomorrow in my 494 Population Biology class-.pdf
shreeaadithyaacellso
 
I got the codes written down below- Basically- I am trying to implemen.pdf
I got the codes written down below- Basically- I am trying to implemen.pdfI got the codes written down below- Basically- I am trying to implemen.pdf
I got the codes written down below- Basically- I am trying to implemen.pdf
shreeaadithyaacellso
 
I am trying to write a program that will converts a 32bit float number.pdf
I am trying to write a program that will converts a 32bit float number.pdfI am trying to write a program that will converts a 32bit float number.pdf
I am trying to write a program that will converts a 32bit float number.pdf
shreeaadithyaacellso
 
I am stuck on this question- please show the solution step by step- Re.pdf
I am stuck on this question- please show the solution step by step- Re.pdfI am stuck on this question- please show the solution step by step- Re.pdf
I am stuck on this question- please show the solution step by step- Re.pdf
shreeaadithyaacellso
 
I et X and Y he two indenendent R(3n-1-3) random variables- Calculate.pdf
I et X and Y he two indenendent R(3n-1-3) random variables- Calculate.pdfI et X and Y he two indenendent R(3n-1-3) random variables- Calculate.pdf
I et X and Y he two indenendent R(3n-1-3) random variables- Calculate.pdf
shreeaadithyaacellso
 
I am working on a coding project- and it asked me to combine five sets.pdf
I am working on a coding project- and it asked me to combine five sets.pdfI am working on a coding project- and it asked me to combine five sets.pdf
I am working on a coding project- and it asked me to combine five sets.pdf
shreeaadithyaacellso
 
I am needing to do a Point Factor Job Evaluation for a IT Company whos.pdf
I am needing to do a Point Factor Job Evaluation for a IT Company whos.pdfI am needing to do a Point Factor Job Evaluation for a IT Company whos.pdf
I am needing to do a Point Factor Job Evaluation for a IT Company whos.pdf
shreeaadithyaacellso
 
i cant figure out this excel equation For the Year Ended December 31-.pdf
i cant figure out this excel equation For the Year Ended December 31-.pdfi cant figure out this excel equation For the Year Ended December 31-.pdf
i cant figure out this excel equation For the Year Ended December 31-.pdf
shreeaadithyaacellso
 
I am writing a program that will allow the user to move the crosshairs.pdf
I am writing a program that will allow the user to move the crosshairs.pdfI am writing a program that will allow the user to move the crosshairs.pdf
I am writing a program that will allow the user to move the crosshairs.pdf
shreeaadithyaacellso
 
I need simple java code for the below scenario- UML Diagrams(Class-Use.pdf
I need simple java code for the below scenario- UML Diagrams(Class-Use.pdfI need simple java code for the below scenario- UML Diagrams(Class-Use.pdf
I need simple java code for the below scenario- UML Diagrams(Class-Use.pdf
shreeaadithyaacellso
 
I need hlep I have posted this proble 4 times have gotten no help can.pdf
I need hlep I have posted this proble 4 times have gotten no help can.pdfI need hlep I have posted this proble 4 times have gotten no help can.pdf
I need hlep I have posted this proble 4 times have gotten no help can.pdf
shreeaadithyaacellso
 
I need question 1 answer in java language- question 1)Create an applic.pdf
I need question 1 answer in java language- question 1)Create an applic.pdfI need question 1 answer in java language- question 1)Create an applic.pdf
I need question 1 answer in java language- question 1)Create an applic.pdf
shreeaadithyaacellso
 

More from shreeaadithyaacellso (20)

In c++ Polymorphism is one of the hallmarks of OOP languages- What are (1).pdf
In c++ Polymorphism is one of the hallmarks of OOP languages- What are (1).pdfIn c++ Polymorphism is one of the hallmarks of OOP languages- What are (1).pdf
In c++ Polymorphism is one of the hallmarks of OOP languages- What are (1).pdf
 
In Chapter 4 of LS- there is a case of Zappos and it describes how Zap.pdf
In Chapter 4 of LS- there is a case of Zappos and it describes how Zap.pdfIn Chapter 4 of LS- there is a case of Zappos and it describes how Zap.pdf
In Chapter 4 of LS- there is a case of Zappos and it describes how Zap.pdf
 
In C++ Plz LAB- Playlist (output linked list) Given main()- complete.pdf
In C++ Plz  LAB- Playlist (output linked list) Given main()- complete.pdfIn C++ Plz  LAB- Playlist (output linked list) Given main()- complete.pdf
In C++ Plz LAB- Playlist (output linked list) Given main()- complete.pdf
 
In C Programming- not C++ Write a function which takes two formal pa.pdf
In C Programming- not C++   Write a function which takes two formal pa.pdfIn C Programming- not C++   Write a function which takes two formal pa.pdf
In C Programming- not C++ Write a function which takes two formal pa.pdf
 
In chapter 17 you learned about the big picture of circulation through.pdf
In chapter 17 you learned about the big picture of circulation through.pdfIn chapter 17 you learned about the big picture of circulation through.pdf
In chapter 17 you learned about the big picture of circulation through.pdf
 
In cell M11- enter a formula that will calculate the total amount due.pdf
In cell M11- enter a formula that will calculate the total amount due.pdfIn cell M11- enter a formula that will calculate the total amount due.pdf
In cell M11- enter a formula that will calculate the total amount due.pdf
 
In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdfIn C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
 
In C++ Complete the program with the bold requirements #include #i.pdf
In C++  Complete the program with the bold requirements   #include  #i.pdfIn C++  Complete the program with the bold requirements   #include  #i.pdf
In C++ Complete the program with the bold requirements #include #i.pdf
 
I am studying for a test tomorrow in my 494 Population Biology class-.pdf
I am studying for a test tomorrow in my 494 Population Biology class-.pdfI am studying for a test tomorrow in my 494 Population Biology class-.pdf
I am studying for a test tomorrow in my 494 Population Biology class-.pdf
 
I got the codes written down below- Basically- I am trying to implemen.pdf
I got the codes written down below- Basically- I am trying to implemen.pdfI got the codes written down below- Basically- I am trying to implemen.pdf
I got the codes written down below- Basically- I am trying to implemen.pdf
 
I am trying to write a program that will converts a 32bit float number.pdf
I am trying to write a program that will converts a 32bit float number.pdfI am trying to write a program that will converts a 32bit float number.pdf
I am trying to write a program that will converts a 32bit float number.pdf
 
I am stuck on this question- please show the solution step by step- Re.pdf
I am stuck on this question- please show the solution step by step- Re.pdfI am stuck on this question- please show the solution step by step- Re.pdf
I am stuck on this question- please show the solution step by step- Re.pdf
 
I et X and Y he two indenendent R(3n-1-3) random variables- Calculate.pdf
I et X and Y he two indenendent R(3n-1-3) random variables- Calculate.pdfI et X and Y he two indenendent R(3n-1-3) random variables- Calculate.pdf
I et X and Y he two indenendent R(3n-1-3) random variables- Calculate.pdf
 
I am working on a coding project- and it asked me to combine five sets.pdf
I am working on a coding project- and it asked me to combine five sets.pdfI am working on a coding project- and it asked me to combine five sets.pdf
I am working on a coding project- and it asked me to combine five sets.pdf
 
I am needing to do a Point Factor Job Evaluation for a IT Company whos.pdf
I am needing to do a Point Factor Job Evaluation for a IT Company whos.pdfI am needing to do a Point Factor Job Evaluation for a IT Company whos.pdf
I am needing to do a Point Factor Job Evaluation for a IT Company whos.pdf
 
i cant figure out this excel equation For the Year Ended December 31-.pdf
i cant figure out this excel equation For the Year Ended December 31-.pdfi cant figure out this excel equation For the Year Ended December 31-.pdf
i cant figure out this excel equation For the Year Ended December 31-.pdf
 
I am writing a program that will allow the user to move the crosshairs.pdf
I am writing a program that will allow the user to move the crosshairs.pdfI am writing a program that will allow the user to move the crosshairs.pdf
I am writing a program that will allow the user to move the crosshairs.pdf
 
I need simple java code for the below scenario- UML Diagrams(Class-Use.pdf
I need simple java code for the below scenario- UML Diagrams(Class-Use.pdfI need simple java code for the below scenario- UML Diagrams(Class-Use.pdf
I need simple java code for the below scenario- UML Diagrams(Class-Use.pdf
 
I need hlep I have posted this proble 4 times have gotten no help can.pdf
I need hlep I have posted this proble 4 times have gotten no help can.pdfI need hlep I have posted this proble 4 times have gotten no help can.pdf
I need hlep I have posted this proble 4 times have gotten no help can.pdf
 
I need question 1 answer in java language- question 1)Create an applic.pdf
I need question 1 answer in java language- question 1)Create an applic.pdfI need question 1 answer in java language- question 1)Create an applic.pdf
I need question 1 answer in java language- question 1)Create an applic.pdf
 

Recently uploaded

Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 

Recently uploaded (20)

Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 

I need help in parse additional types of expressions defined by the ex.pdf

  • 1. I need help in parse additional types of expressions defined by the expanded grammar shown below with the additions to the grammar highlighted in BOLD: statement expression ',' assignments ';' expression '( ' expressions ')' expressions unary_expression | binary_expression | ternary_expression | quaternary_expression unary_expression expression '~' binary_expression expression binary_operator expression binary_operator '+' | '-' | '*' | '/' | '%' | '^' | '<' | '>' | '_' ternary_expression expression '?' expression expression quaternary_expression expression '#' expression expression expression operand literal | variable | expression assignments assignments ',' assignment | assignment assignment variable '=' literal Current C++ code: parse.h // This file contains the function prototype of the parseName function whose body is defined in parse.cpp. string parseName(stringstream& in); parse.cpp // characters until the next whitespace and returns the name that those characters form. #include #include #include using namespace std; #include "parse.h" string parseName(stringstream& in) { main.cpp #include <iostream> #include <fstream> #include <sstream> #include <string> #include <vector> using namespace std;
  • 2. #include "expression.h" #include "subexpression.h" #include "symboltable.h" #include "parse.h" SymbolTable symbolTable; void parseAssignments(stringstream& in); int main() { const int SIZE = 256; Expression* expression; char paren, comma, line[SIZE]; ifstream fin; fin = ifstream("input.txt"); if (!(fin.is_open())) { cout << "File did not open" << endl; system("pause"); return 1; } while (true) { fin.getline(line, SIZE); if (!fin) break; stringstream in(line, ios_base::in); in >> paren; cout << line << " "; try { expression = SubExpression::parse(in); in >> comma; parseAssignments(in); double result = expression->evaluate(); cout << "Value = " << result << endl; } catch (string message) { cout << message << endl; } } system("pause"); return 0; } void parseAssignments(stringstream& in) { char assignop, delimiter; string variable;
  • 3. int value; do { variable = parseName(in); in >> ws >> assignop >> value >> delimiter; symbolTable.insert(variable, value); } while (delimiter == ','); } char alnum; string name = ""; in >> ws; while (isalnum(in.peek())) { in >> alnum; name += alnum; } return name; } operand.h class Operand: public Expression { public: static Expression* parse(stringstream& in); }; operand.cpp using namespace std; #include "expression.h" #include "subexpression.h" #include "operand.h" #include "variable.h" #include "literal.h" #include "parse.h" Expression* Operand::parse(stringstream& in) { char paren; int value; in >> ws; if (isdigit(in.peek())) { in >> value; Expression* literal = new Literal(value); return literal;
  • 4. } if (in.peek() == '(') { in >> paren; return SubExpression::parse(in); } else return new Variable(parseName(in)); return 0; } subexpression.h class SubExpression: public Expression { public: SubExpression(Expression* left, Expression* right); static Expression* parse(stringstream& in); protected: Expression* left; Expression* right; }; SubExpression.cpp #include #include using namespace std; #include "expression.h" #include "subexpression.h" #include "operand.h" #include "plus.h" #include "minus.h" SubExpression::SubExpression(Expression* left, Expression* right) { this->left = left; this->right = right; } Expression* SubExpression::parse(stringstream& in) { Expression* left; Expression* right; char operation, paren; left = Operand::parse(in); in >> operation; right = Operand::parse(in); in >> paren; switch (operation) { case '+': return new Plus(left, right); case '-': return new Minus(left, right); } return 0; } plus.h class Plus: public SubExpression {
  • 5. public: Plus(Expression* left, Expression* right): SubExpression(left, right) { } double evaluate() { return left->evaluate() + right->evaluate(); } }; variable.h class Variable: public Operand { public: Variable(string name) { this->name = name; } double evaluate(); private: string name; }; Variable.cpp #include #include using namespace std; #include "expression.h" #include "operand.h" #include "variable.h" #include "symboltable.h" extern SymbolTable symbolTable; double Variable::evaluate() { return symbolTable.lookUp(name); } minus.h class Minus: public SubExpression { public: Minus(Expression* left, Expression* right): SubExpression(left, right) { } double evaluate() { return left->evaluate() - right->evaluate(); } }; literal.h class Literal: public Operand { public: Literal(double value) { this->value = value; } double evaluate() { return value; } private: double value; }; expression.h class Expression { public: virtual double evaluate() = 0; }; symbolTable.h class SymbolTable {
  • 6. public: SymbolTable() {} void insert(string variable, double value); double lookUp(string variable) const; private: struct Symbol { Symbol(string variable, double value) { this->variable = variable; this->value = value; } string variable; double value; }; vector elements; }; symbolTable.cpp #include #include using namespace std; #include "symboltable.h" void SymbolTable::insert(string variable, double value) { const Symbol& symbol = Symbol(variable, value); elements.push_back(symbol); } double SymbolTable::lookUp(string variable) const { for (int i = 0; i < elements.size(); i++) if (elements[i].variable == variable) return elements[i].value; return -1; }