<?php

/*
  Generates random sentences based on the following BNF grammar:

    <sentence> ::= <simple_sentence> [ <conjunction> <sentence> ]...
    <simple_sentence> ::= <noun_part> <verb_part>
    <noun_part> ::= <proper_noun> | 
                    ( <determiner> [ <adjective> ] <noun> [ who <verb_part> ] )
    <verb_part> ::= <intransitive_verb> | ( <transitive_verb> <noun_part> )
    <conjunction> ::= and | or | but
    <proper_noun> ::= Fred | Jane | Tom | Mary |
                       Richard Nixon | Miss America | The President
    <determiner> ::= a | the | some | every 
    <noun> ::= dog | cat | man | woman | unicorn | fish | bird | wizard
    <intransitive_verb> ::= runs | walks | jumps | laughs | cries | sleeps |
                             knows that <simple_sentence> |
                             believes that <simple_sentence> |
                             sees that <simple_sentence>
    <transitive_verb> ::= loves | hates | knows | sees | is | finds | looks for
    <adjective> ::= big | laughing | hopeful | sad | bashful | stupid
*/

$maxdepth 10;
srand ((double) microtime() * 1000000); /* initialize random number generator */

print("<html><head><title>Random Sentence</title></head><body bgcolor=white>\n");
print(
"<p><big><font color='#CC0000'>A Random Sentence:</font></big></p>\n");

print(
"<h2 align=center>");

sentence(0);  /* Output a random sentence. */

print("</h2>");

print(
"<p><big><a href='random_sentence.php'>Show another one!</a></big></p>\n");
print(
"</body></html>");

function 
put($word) {
   print(
"$word<br>\n");
}

function 
sentence($depth) {
   global 
$maxdepth;
   
simple_sentence($depth+1);
   while (
$depth $maxdepth && rand(0,3) == 1) {
      
conjunction();
      
$depth $depth 1;
      
simple_sentence($depth+1);
   }
}

function 
simple_sentence($depth) {
   global 
$maxdepth;
   
noun_part($depth+1);
   
verb_part($depth+1);
}

function 
noun_part($depth) {
   global 
$maxdepth;
   if (
rand(0,2) == 0)
      
proper_noun();
   else {
      
determiner();
      if (
rand(0,3) == 0)
         
adjective();
      
noun();
      if (
$depth $maxdepth && rand(0,4) == 0) {
         
put("who");
         
verb_part($depth+1);
      }
   }
}

function 
verb_part($depth) {
   global 
$maxdepth;
   if (
rand(0,1) == 0)
      
intransitive_verb($depth+1);
   else {
      
transitive_verb($depth+1);
      
noun_part($depth+1);
   } 
}

function 
conjunction() {
   switch (
rand(0,3)) {
      case 
0: case 1put("and"); break;
      case 
2put("or"); break;
      case 
3put("but"); break;
   }
}

function 
proper_noun() {
   switch (
rand(0,6)) {
      case 
0put("Fred"); break;
      case 
1put("Jane"); break;
      case 
2put("Tom"); break;
      case 
3put("Mary"); break;
      case 
4put("Richard Nixon"); break;
      case 
5put("Miss America"); break;
      case 
6put("The President"); break;
   }
}

function 
determiner() {
   switch (
rand(0,9)) {
      case 
0: case 1: case 2put("a"); break;
      case 
3: case 4: case 5put("the"); break;
      case 
6: case 7put("some"); break;
      case 
8: case 9put("every"); break;
   }
}

function 
noun() {
   switch(
rand(0,7)) {
      case 
0put("dog"); break;
      case 
1put("cat"); break;
      case 
2put("man"); break;
      case 
3put("woman"); break;
      case 
4put("unicorn"); break;
      case 
5put("fish"); break;
      case 
6put("bird"); break;
      case 
7put("wizard"); break;
   }
}

function 
intransitive_verb($depth) {
   global 
$maxdepth;
   if (
$depth $maxdepth)
      
$r rand(0,8);
   else
      
$r rand(0,5);
   switch (
$r) {
      case 
0put("runs"); break;
      case 
1put("walks"); break;
      case 
2put("jumps"); break;
      case 
3put("laughs"); break;
      case 
4put("cries"); break;
      case 
5put("sleeps"); break;
      case 
6:
         
put("knows");
         
put("that");
         
simple_sentence($depth+1);
         break;
      case 
7:
         
put("believes");
         
put("that");
         
simple_sentence($depth+1);
         break;
      case 
8:
         
put("sees");
         
put("that");
         
simple_sentence($depth+1);
         break;
   }
}

function 
transitive_verb() {
   switch(
rand(0,6)) {
      case 
0put("loves"); break;
      case 
1put("hates"); break;
      case 
2put("knows"); break;
      case 
3put("sees"); break;
      case 
4put("is"); break;
      case 
5put("finds"); break;
      case 
6put("looks"); put("for"); break;
   }
}

function 
adjective() {
   switch(
rand(0,5)) {
      case 
0put("big"); break;
      case 
1put("laughing"); break;
      case 
2put("hopeful"); break;
      case 
3put("sad"); break;
      case 
4put("bashful"); break;
      case 
5put("stupid"); break;
   }
}

?>