wood burning stoves 2.0*
The moose likes Java in General and the fly likes Using syntax rules in my code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using syntax rules in my code" Watch "Using syntax rules in my code" New topic
Author

Using syntax rules in my code

Ilona Kovacs
Greenhorn

Joined: Feb 12, 2014
Posts: 1
I begin to write an interesting program, which returns funny statements, but I cannot continue, because I do not know how to implement the syntax rules in my code:
These are the 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>

And here is my started code, which I do not know how to continue. I know that I should use recursion and that <sentence> method should call <simple_sentence>, but I do not know how to code this.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30548
    
152

Ilona,
Welcome to CodeRanch!

It's a bit overwhelming to start thinking of this from the full grammar point of view. First try to implement the logic for this much simpler grammar:


When you are done, you'll have a working program with a main method and four grammar methods. (randomSentence, randomSimpleSentence, randomNounPhrase and randomVerbPhrase.) I know it may seem silly to separate these simple methods, but it will get the structure of your program all set up. You will be able to get random words from the relevant two lists.

Once you get this working, you can start adding pieces of the grammar one at a time. Testing your program after each new addition to make sure it still works. The most interesting ones will probably be implementing "or" and "optional" so do ask if you have questions about those parts specifically. Or anything else about this for that matter. Just post what point in the grammar you are up to and what code you have so far.

Note: It is Java convention to user all uppercase for constants (static final variables.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using syntax rules in my code