Recursive Syntax The recursive structure i.e of natural language like English can be expressed in syntax rules written in the format known as BNF (Bachus-Naur Form). While BNF is ordinarily used as a guide for parsing (that is, determining whether and how a given string follows the syntax rules), An example of this can be found in the sample program SimpleRandomSentences. 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(".\n\n"); 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(); } } Concluding part is attached.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Recursive Syntax

The recursive structure i.e of natural language like English can be expressed in syntax rules written in the format known as BNF (Bachus-Naur Form). While BNF is ordinarily used as a guide for parsing (that is, determining whether and how a given string follows the syntax rules), An example of this can be found in the sample program SimpleRandomSentences.

You should write a similar program that implements the following rules:

<sentence> ::= <simple_sentence> [ <conjunction> <sentence> ]

<simple_sentence> ::= <noun_phrase> <verb_phrase>

<noun_phrase> ::= <proper_noun> |
<determiner> [ <adjective> ]. <common_noun> [ who <verb_phrase> ]

<verb_phrase> ::= <intransitive_verb> |
<transitive_verb> <noun_phrase> |
is <adjective> |
believes that <simple_sentence>

<conjunction> ::= and | or | but | because

<proper_noun> ::= Fred | Jane | Richard Nixon | Miss America

<common_noun> ::= man | woman | fish | elephant | unicorn

<determiner> ::= a | the | every | some

<adjective> ::= big | tiny | pretty | bald

<intransitive_verb> ::= runs | jumps | talks | sleeps

<transitive_verb> ::= 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 <sentence> 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:

<sentence> ::= <simple_sentence> [ and <sentence> ]

<simple_sentence> ::= this is [ <noun_phrase> ] the house that Jack built

<noun_phrase> ::= the <noun> [ <modifier> ] that <verb> [ <noun_phrase> ]
<noun> ::= farmer | rooster | judge | man | maiden | cow | dog | cat | cheese

<verb> ::= kept | waked | married | milked | tossed | chased | lay in

<modifier> ::= 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
[ <modifier> ] 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(".\n\n");
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();
}
}

Concluding part is attached. 

Last part of SimpleRandomSentences.java
static void randomNounPhrasel) {
int n= (int)(Math.random()*nouns.length);
int v = (int) (Mathrandom()*verbs.length);
int m = (int)(Math.random()*nodfiersJength);
Systemout.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();
}
}
Transcribed Image Text:Last part of SimpleRandomSentences.java static void randomNounPhrasel) { int n= (int)(Math.random()*nouns.length); int v = (int) (Mathrandom()*verbs.length); int m = (int)(Math.random()*nodfiersJength); Systemout.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(); } }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education