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:
|
| if (
| if (
| if (
| if (
| while (
|
| double
|
|
|
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
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
vector
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started