Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Using Eclipse Recursive Syntax The grammar of natural languages such as English exhibits a recursive structure. This structure can be expressed in syntax rules

Java Using Eclipse

Recursive Syntax

The grammar of natural languages such as English exhibits a recursive structure. This structure can be expressed in syntax rules written in the format known as BNF (Bachus-Naur Form, named after the people who invented it). You have probably seen BNF used to specify the syntax of programming languages. While BNF is ordinarily used as a guide for parsing (that is, determining whether and how a given string follows the syntax rules), it can also be used a guide for generating strings that follow the syntax rules. An example of this can be found in the sample program SimpleRandomSentences. In this example, each syntax rule -- except for the most basic ones -- is represented by a method that generates strings that follow that rule. Where one syntax rule refers to another rule, the method that represents the first rule calls the method that represents the second rule.

For the first exercise of the lab, you should write a similar program that implements the following rules:

::= [ ] ::= ::= | [ ]. [ who ] ::= | | is | believes that ::= and | or | but | because ::= Fred | Jane | Richard Nixon | Miss America ::= man | woman | fish | elephant | unicorn ::= a | the | every | some ::= big | tiny | pretty | bald ::= runs | jumps | talks | sleeps ::= loves | hates | sees | knows | looks for | finds

As in SimpleRandomSentences.java, you can use arrays to implement the last seven rules in this list. (You might improve on that program by writing a single method "void String randomItem(String[] listOfStrings)" for picking a random item from an array of strings.) You are welcome to add to or modify the items in the lists given here. For each of the first three rules, you should write a subroutine to represent that rule. Note that a choice of alternatives (represented in the rules by "|") can be implemented using a switch or if..else statement; the various choices don't necessarily have to have the same probability. An optional element (represented by brackets, "[ xxx ]") can be implemented by a simple if. And a repeated optional element (represented by brackets with dots, "[ xxx ]...") can be represented by a while loop. You should implement the first four rules exactly as stated here. The main routine should call the subroutine to generate random sentences. You have to be careful in this program to avoid infinite recursion in this program. Since it will use random choices, there is no guarantee that the recursion will ever end. If your probabilities of doing recursion and continuing loops are too high, it is possible for the program to get lost in recursive calls forever -- or to produce some finite but ridiculously long sentences. You should adjust your probabilities to make sure that this doesn't happen, but that you still get some interesting sentences.

SimpleRandomSentences.java

/*

The last verse of a well-known nursery rhyme:

This is the farmer sowing his corn

That kept the rooster that crowed in the morn

That waked the judge all shaven and shorn

That married the man all tattered and torn

That kissed the maiden all forlorn

That milked the cow with the crumpled horn

That tossed the dog

That worried the cat

That chased the rat

That ate the cheese

That lay in the house that Jack built.

Some rules that capture the syntax of this verse:

::= [ and ]

::= this is [ ] the house that Jack built

::= the [ ] that [ ]

::= farmer | rooster | judge | man | maiden | cow | dog | cat | cheese

::= kept | waked | married | milked | tossed | chased | lay in

::= that crowed in the morn | all shaven and shorn |

all tattered and torn | all forlorn | with the crumpled horn

This program implements these rules to generate random sentences. All the

verses of the rhyme can be generated, plus a lot of sentences that make no

sense (but still follow the syntax). Note that an optional item like

[ ] has a chance of being used, depending on the value of some

randomly generated number.

The program generates and outputs one random sentence every three seconds until

it is halted (for example, by typing Control-C in the terminal window where it is

running).

*/

public class SimpleRandomSentences {

static final String[] nouns = { "farmer", "rooster", "judge", "man", "maiden",

"cow", "dog", "cat", "cheese" };

static final String[] verbs = { "kept", "waked", "married",

"milked", "tossed", "chased", "lay in" };

static final String[] modifiers = { "that crowed in the morn", "sowing his corn",

"all shaven and shorn",

"all forlorn", "with the crumpled horn" };

public static void main(String[] args) {

while (true) {

randomSentence();

System.out.println(". ");

try {

Thread.sleep(3000);

}

catch (InterruptedException e) {

}

}

}

static void randomSentence() {

System.out.print("this is ");

if (Math.random() > 0.2)

randomNounPhrase();

System.out.print("the house that Jack built");

if (Math.random() > 0.75) {

System.out.print(" and ");

randomSentence();

}

}

static void randomNounPhrase() {

int n = (int)(Math.random()*nouns.length);

int v = (int)(Math.random()*verbs.length);

int m = (int)(Math.random()*modifiers.length);

System.out.print("the " + nouns[n]);

if (Math.random() > 0.75)

System.out.print(" " + modifiers[m]);

System.out.print(" that " + verbs[v] + " ");

if (Math.random() > 0.5)

randomNounPhrase();

}

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

Students also viewed these Databases questions

Question

What is the name of the following compounds: FeSO ClO HS(aq)

Answered: 1 week ago