2014-02-12 59 views
-1

我开始写一个有趣的节目,它返回滑稽的语句,但我不能继续下去,因为我不知道如何实现我的代码的语法规则:运用语法规则

:: = [ ]

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

我有一个线索,我应该如果使用/ while语句中的编码规则,但我不能在代码中这样写::: = []

这里是我的启动代码:

import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 


public class RecursiveSyntax2 { 

static final String[] conjunction = {"and", "or", "but", "because"}; 

static final String[] proper_noun = {"Fred", "Jane", "Richard Nixon", "Miss America"}; 

static final String[] common_noun = {"man", "woman", "fish", "elephant", "unicorn"}; 

static final String[] determiner = {"a", "the", "every", "some"}; 

static final String[] adjective = {"big", "tiny", "pretty", "bald"}; 

static final String[] intransitive_verb = {"runs", "jumps", "talks", "sleeps"}; 

static final String[] transitive_verb = {"loves", "hates", "sees", "knows", "looks for"}; 



    public static void main(String[] args) { 

     List<String[]> arrayList = new ArrayList<>(); 
     arrayList.add(conjunction); 
     arrayList.add(proper_noun); 
     arrayList.add(common_noun); 
     arrayList.add(determiner); 
     arrayList.add(adjective); 
     arrayList.add(intransitive_verb); 
     arrayList.add(transitive_verb); 

     Random random = new Random(); 
     for(String[] currentArray : arrayList){ 
      String chosenString = currentArray[random.nextInt(currentArray.length)]; 
      System.out.println(chosenString); 
     } 


     while (true) { 
     randomSentence(); 
     System.out.println(".\n\n"); 
     try { 
      Thread.sleep(3000); 
     } 
     catch (InterruptedException e) { 
     } 
     } 

} 

    static void randomSentence() { 

}

+0

看看JavaCC的,野牛和其他编译器编译器。写起来会容易得多。 –

+1

如果你想DIY,看看递归正常的解析器。 – CodeChimp

+0

有人吗?其他一些想法? – user3266734

回答

0
static void randomSentence() { 
     if (Math.random() > 0.2) 
      randomNounPhrase(); 
       randomVerbPhrase(); 
     if (Math.random() > 0.75){ 
      System.out.print("" + randomItem(conjunction)); 
      randomSentence(); 
     } 
    } 

    static void randomNounPhrase() { 
     if (Math.random()> 0.75) 
      System.out.print(""+ randomItem(proper_noun)); 
     else 
     { 
      System.out.print(""+ randomItem(determiner)); 
      if (Math.random()> 0.5) 
     System.out.print(""+ randomItem(adjective)+"."); 
      System.out.print(""+ randomItem(common_noun)); 
      if (Math.random() > 0.5){ 
       System.out.print(" who"); 
       randomVerbPhrase(); 
      } 
     } 
    } 

    static void randomVerbPhrase() { 

     if (Math.random() > 0.75) 
      System.out.print(""+ randomItem(intransitive_verb)); 
     else if (Math.random() > 0.50){ 
      System.out.print("" + randomItem(transitive_verb)); 
      randomNounPhrase(); 
     } 
     else if (Math.random() > 0.25) 
      System.out.print("is" + randomItem(adjective)); 
     else{ 
      System.out.print("believes that"); 
      randomNounPhrase(); 
      randomVerbPhrase(); 
     } 
    } 
    static String randomItem(String[] listOfStrings){ 
     return listOfStrings[(int)(Math.random()*listOfStrings.length)]; 
    } 
} 
+0

请检查您的代码。 'randomSentence()'中的第一个if语句可能缺少大括号{}'。 – Keugyeol