Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The purpose of this exercise is to write a syntax generator for a subset of the C++ programming language that will write random C++ programs

The purpose of this exercise is to write a syntax generator for a subset of the C++ programming language that will write random C++ programs to a file. By writing these random syntactically correct programs, you will further develop your understanding of the difference between syntax and semantics.

Consider the attached set of productions that define a subset of the C++ programming language:

image text in transcribed

image text in transcribed

Problem 1. Write a program in C, C++, C#, Java, or Python (your choice) that starts with the root non-terminal and generates a random syntactically correct C++ program using the productions defined above. You should follow the examples we saw in class where we expand non-terminals recursively until we obtain a sentence consisting of terminal tokens only. In the case where a production contains more than one expansion (i.e., right-hand-side expressions), your program should select one randomly (preferably with non-uniform weighting based on which expansions are more likely to occur in C++). Your program should write the random C++ code to an output file.

Here is the class that I am using:

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

};

In particular, I'm having trouble with the expand function and how to implement everything in my main function. The final product should create an output such as (manually tabbed):

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;

}

PLEASE NOTE: This assignment is NOT asking to output random numbers. Most of the answers on here for this question are just producing random numbers using rand. I've already asked this once and received such an answer. I've also received copy/pasted answers straight off google, so please don't bother. This program is supposed to create a syntactically correct C++ program. I don't need you to write the entire code, I just need some help getting in the right direction. I understand BNF grammar and parse trees, but have trouble translating that into code. Thank you.

stat> I Kampa stat kif stat> Kompa stat if else l if Kampd stat else stat l if Kampd stat else Kampd stat compd stat stats *type Kassgn. stat op Kop stat> I Kampa stat kif stat> Kompa stat if else l if Kampd stat else stat l if Kampd stat else Kampd stat compd stat stats *type Kassgn. stat op Kop

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 19th International Conference Dexa 2008 Turin Italy September 2008 Proceedings Lncs 5181

Authors: Sourav S. Bhowmick ,Josef Kung ,Roland Wagner

2008th Edition

3540856536, 978-3540856535

More Books

Students also viewed these Databases questions

Question

Select suitable tools to analyze service problems.

Answered: 1 week ago