Write a class called RandomArithmeticExpression that forms a randomly generated expression that follows the rules listed in the grammar shown below. Your code will be tested by RandomArithmeticGenerator, which is available on Site to get you started.

  • To avoid the possibility of infinite recursion, limit the depth of recursion to 16 levels, where a level corresponds to any production involving at least one non-terminal on the right of the production.
  • When printed, there should be a single space between each of the non-blank characters in the expression.

Grammar:

The initial expression is S. The non-terminal expressions are S and E. The values x, y, and z are terminals, that is, they cannot be replaced by any other symbol. An expression appearing on the left of a --> may be replaced by any expression of the right of that arrow as shown below.

S --> E
E --> E + E
E --> E – E
E --> E * E
E --> E / E
E --> ( E )
E --> x | y | z

For example, a derivation might be:

S --> E --> E + E --> E + ( E ) --> E * E + ( E ) --> x * E +( E ) --> x * E – E +( E ) --> x * E – E +( z ) --> x * E – z +( z ) --> x * y – z +( z )

Here are four randomly generated expressions:

( x + ( x + z / y + z / x * z / y * z / y ) - y - y / x ) x * ( ( y * ( x * y * y * y ) * y / z * z + x ) / x ) ( z ) y / y * y - x * z / ( x ) + x - z - z

Here is the test driver code: See image.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.