-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() {
}
看看JavaCC的,野牛和其他编译器编译器。写起来会容易得多。 –
如果你想DIY,看看递归正常的解析器。 – CodeChimp
有人吗?其他一些想法? – user3266734