Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following set of productions that define a subset of the C++ programming language: ::= int main() { return 0; } ::= | ::=

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

::= 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]

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.

Problem 2. Run your generator (possibly a few times) to generate a random syntactically correct C++ program. You will observe that, despite the correct syntax, there are other issues that prevent this program from being correct. Please discuss some of these errors. Suppose you wish to implement a checker for detecting these errors. What sort of approach or algorithm would you use to accomplish this? There is no need to write any code for this problem.

Hint 1: The following is a sample random program that was generated by my solution. I added some white spaces to make it more readable:

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 Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

More Books

Students also viewed these Databases questions