Question: Write a C++ program that will make use of the following grammar rules: ::= int main() { return 0; } ::= | ::= | |

Write a C++ program that will make use of the following grammar rules:

::= int main() { return 0; }

::= |

::= | | |

|

::= { }

::= if ( )

| if ( ) | if ( ) else

| if ( ) else

| if ( ) else

| if ( ) else ::= while ( )

| while ( ) ::= = ;

::= ;

| ::= | | ::= + | - | * | /

::= int

| double

::=

::=

::= [empty]

|

|

::= [empty]

|

::= [A-Z] | [a-z] | _

::= [0-9]

Each terminal (the outputs on the left hand side) expand into one of the outputs on the right hand side, ultimately forming a syntactically correct C++ program, if the grammar is followed correctly (can be hand drawn in an expanse tree to get a visual). Write a C++ program that will store these in a vector and "cout" them to form a unique C++ code. Some of the expansions should not be a uniform probability, seeing as for example can go right back into -> and back into making the program too long to compile. Make the probabilities reflect this.

For example the program could output something like this:

int main()

{

int F0Z = 0262;

if (22682 / 525)

double S1;

else

S = U;

while (8 - 594873)

{

while (97942 / 6871573097 * 7261055)

{

while (9307 * M / 4 / 2 + 4 - 7 / K)

{

double A;

}

}

}

return 0;

} Even though, this has semantic errors, as long as it is syntactically correct. Each run of the program should "cout" a unique and new program. Hint: This class could be used to get started however you can do it any way you like.

class Production

{

private:

string lhs;

vector rhs_options; // list of options for expansion

vector trans_probs; // list of probabilities associated // with each choice

public:

Production();

Production(string);

void add_rhs(string, double); // adds new rhs to the production

string expand() const; // returns one of the rhs choices using

// a random number generator

};

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!