grammar
                                
                                 grammar copied to clipboard
                                
                                    grammar copied to clipboard
                            
                            
                            
                        A random sentence generator based on a context-free grammar (golang)
grammar
Random sentence generator based on a context-free grammar.
grammar randomly generates sentences based on a user-supplied context-free grammar.
Format
The definition consists of a series of newline-separated "rules." Each rule corresponds to a part of speech, and follows the syntax <part> <definition>. For example:
| NP | Adj Noun | 
In this rule, a noun phrase ("NP") is defined as consisting of an adjective followed by a noun. Any part of speech which is not later defined by a rule is considered a word on its own. For example:
| NP | Adj Noun | 
| Adj | blue | 
| Adj | red | 
This set of rules will defines "Noun" as its own word since it is nowhere defined. However, since "Adj" is defined, it will resolve to either "blue" or "red," but never the literal "Adj." (Note that definitions are processed serially, and thus parts of speech must be seen before they are defined) This example illustrates another feature of rules, which is that multiple rules are allowed, and will be chosen among randomly when resolving a part of speech.
Finally, the first line of the definition does not follow the normal syntax, but instead simply defines the structure of a whole sentence. For example:
| Prep NP VP | |
| NP | Adj Noun | 
| VP | Adv Verb | 
| Prep | a | 
| Prep | the | 
| Adj | blue | 
| Adj | red | 
| Noun | bird | 
| Noun | dog | 
| Adv | quickly | 
| Adv | slowly | 
| Verb | ran | 
| Verb | flew | 
This set of rules will create sentences such as "the red dog quickly flew."
Of course, grammar definitions are as flexible as any context-free grammar. Consider adding features such as plural vs singular phrases, or make up your own nonsensical definitions.