• Post Reply Bookmark Topic Watch Topic
  • New Topic

help on recursion  RSS feed

Nelly Verryl
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is actually an assignment I am stuck with.
Please l appreciate your help and support.

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 :
< 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 .

I really need help with this. I just need to validate this course.
Thanks for even looking at it.

fred rosenberger
lowercase baba
Posts: 12563
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
posting your homework assignment and saying "Help me" won't get you very far around here.

Asking specific, focused, directed questions will get you more help than you could ever hope for.

We don't know if you need help downloading and installing the JDK or if you need help formatting some text on the screen - or something else entirely.

so, what SPECIFICALLY do you need help with? What have you done?
Nelly Verryl
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand the basics of java and can use eclipse well but the codes is where I am not good.
Matthew Brown
Posts: 4568
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That still isn't a focused question.

Don't start thinking about code. Think about the problem first:
- Do you understand what the question is asking?
- Would you be able to carry this out by hand? (e.g. using dice for the randomness)
- Would you be explain to someone else how to carry this out by hand?

Don't write a line of code until the answer to those questions is "yes".
Once it is, break it down into smaller problems. Start with a simplified version and write a program to do this. I'd suggest breaking the rules down into separate methods - pick one of the simplest (e.g. < conjunction >) and try to get that working. Then build up from there.

And welcome to the Ranch!
Nelly Verryl
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew,
I will do just that now and hopefully solve this
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!