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.
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.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps