CSC 240
Emcee Expressions Problem Description: Our Emcee language included expressions, as defined in the BNF below: expression constant identifier...
View the step-by-step solution to:

Question



I am having trouble with this homework assignment. Can someone help me? The classes can

be in separate files or can all be in one.








Expressions (1)-page-001.jpg




Expressions (1)-page-002.jpgExpressions (1)-page-003.jpg

Expressions (1)-page-001.jpg

Emcee Expressions
Problem Description:
Our Emcee language included expressions, as defined in the BNF below:
expression
constant
identifier
expression binary_op expression
unary_op expression
identifier LEFT_PAREN expression_list RIGHT PAREN
identifier LEFT_PAREN RIGHT_PAREN
LEFT PAREN expression RIGHT PAREN
constant
REAL CONST | INT CONST | STRING CONST
binary_op
: :=
PLUS | MINUS | MULT | DIVIDE | CARAT | MOD
unary_op
MINUS
As we prepare to complete our Emcee interpreter, you are to create C++ classes that
implement the portions highlighted above. For binary operators, you need only handle +
-, * and /. Your unary operator should handle -. Constants are always integers. The design
for these classes can be found below.
Declare all your C++ classes in the file "expression.h".
A reminder about expression notation: An infix expression places the operator between
two operands, or in front of a single operand (e.g. 1 + 2, or - 3). A postfix expression always
puts the operator after the operands (e.g. 1 2 +, or 3 u-). To avoid confusion, when printing
a unary - in postfix format, use the notation u- (as shown above and also in the example
below). When printing infix, use parentheses liberally (again, see example below)
To test your classes, run the main program defined below. This program creates the
following expression, evaluates it, and then prints it in infix and postfix formats:
- 3 * (4 + (9 / 3 ) )

Expressions (1)-page-002.jpg

Design:
expression
+eval(): int
+infix(): char *
+postfix(): char *
constant
unop
Binop
+constant([: int)
+unop(op: char, e: expression *)
+binop(op: char, el: expression *, e2: expression *)
+eval(): int
+eval(): int
+eval(): int
+infix(): char *
+infix(): char
+infix(): char
+postfix(): char *
+postfix(): char *
+postfix(): char *
Required Main:
#include "expression. h"
#include <iostream>
using namespace std;
int main (int argo, char *argv ) {
expression *e = new binop('*',
new unop (' -', new constant (3) ) ,
new binop ( 't',
new constant (4) ,
new binop ( ' / ',
new constant (9) ,
new constant ( 3)
cout << "Evaluate: " << e->eval () << endl;
cout << "Infix: " << e->infix () << endl;
cout << "Postfix: " << e->postfix () << endl;
return 0;

Expressions (1)-page-003.jpg

Notes:
Turn in the file expression.h file, and any other files required to run the program.
Use polymorphism in the class definitions.
.
If you prefer, you can use strings instead of char *.
sprintf() is a function like printf that uses a format string to create a string with a
desired layout. For example:
include <costdio> // Includes declaration of sprintf()
char expr str [100] ;
sprintf (expr_str, " (8s :c :s) ", el. infix(), op, e2. infix() ) ;
Required Input:
. None
Required Output:
Your output should look like the following:
Evaluate: -21
Infix: ((- 3) * (4 + (9 / 3) ) )
Postfix : 3 u- 4 9 3 / + *

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes