| | 1 | /* |
| | 2 | * grammar test |
| | 3 | */ |
| | 4 | |
| | 5 | #include "tads.h" |
| | 6 | #include "t3.h" |
| | 7 | #include "dict.h" |
| | 8 | #include "gramprod.h" |
| | 9 | |
| | 10 | |
| | 11 | dictionary gDict; |
| | 12 | dictionary property noun, adjective, plural; |
| | 13 | |
| | 14 | indent(level) |
| | 15 | { |
| | 16 | for ( ; level != 0 ; --level) |
| | 17 | "\ \ "; |
| | 18 | } |
| | 19 | |
| | 20 | grammar command: predicate->pred_ |
| | 21 | | predicate->pred_ <cmdTerminator> * : object |
| | 22 | debugPrint(level) |
| | 23 | { indent(level); "command: pred\n"; pred_.debugPrint(level+1); } |
| | 24 | ; |
| | 25 | |
| | 26 | grammar command: cmdTerminator * : object |
| | 27 | debugPrint(level) { indent(level); "command (empty)\n"; } |
| | 28 | ; |
| | 29 | |
| | 30 | grammar cmdTerminator: 'then' | '.' | '!' | ';' | '?' | ',' | 'and' : object |
| | 31 | ; |
| | 32 | |
| | 33 | grammar predicate: 'say' tokString->str_ : object |
| | 34 | debugPrint(level) |
| | 35 | { indent(level); "predicate tadsSay(str_ = <<str_>>)\n"; } |
| | 36 | execute() |
| | 37 | { |
| | 38 | "Okay, \"<<str_>>\"\n"; |
| | 39 | } |
| | 40 | ; |
| | 41 | |
| | 42 | grammar predicate: 'take' nounPhrase->np_ : object |
| | 43 | debugPrint(level) |
| | 44 | { indent(level); "predicate take np:\n"; np_.debugPrint(level+1); } |
| | 45 | ; |
| | 46 | |
| | 47 | grammar predicate: 'give' nounPhrase->dobj_ 'to' nounPhrase->iobj_ |
| | 48 | | 'give' nounPhrase->iobj_ nounPhrase->dobj_ : object |
| | 49 | debugPrint(level) |
| | 50 | { |
| | 51 | indent(level); |
| | 52 | "predicate give dobj, iobj:\n"; |
| | 53 | dobj_.debugPrint(level+1); |
| | 54 | iobj_.debugPrint(level+1); |
| | 55 | } |
| | 56 | ; |
| | 57 | |
| | 58 | grammar nounPhrase: noun->noun_ : object |
| | 59 | debugPrint(level) |
| | 60 | { indent(level); "nounPhrase(noun = <<noun_>>)\n"; } |
| | 61 | resolveObjects() |
| | 62 | { |
| | 63 | /* return the objects matching my noun */ |
| | 64 | return gDict.findWord(noun_, &noun); |
| | 65 | } |
| | 66 | ; |
| | 67 | |
| | 68 | grammar nounPhrase: [badness 0+10] anyPhrase->any_: object |
| | 69 | debugPrint(level) |
| | 70 | { |
| | 71 | indent(level); |
| | 72 | "nounPhrase any:\n"; |
| | 73 | any_.debugPrint(level + 1); |
| | 74 | } |
| | 75 | ; |
| | 76 | |
| | 77 | grammar nounPhrase: adjective->adj_ nounPhrase->np_ : object |
| | 78 | debugPrint(level) |
| | 79 | { |
| | 80 | indent(level); |
| | 81 | "nounPhrase(adj = <<adj_>>) np:\n"; |
| | 82 | np_.debugPrint(level+1); |
| | 83 | } |
| | 84 | resolveObjects() |
| | 85 | { |
| | 86 | local match1; |
| | 87 | local match2; |
| | 88 | |
| | 89 | /* get the objects matching my adjective */ |
| | 90 | match1 = gDict.findWord(adj_, &adjective); |
| | 91 | |
| | 92 | /* get the objects matching the rest of the noun phrase */ |
| | 93 | match2 = np_.resolveObjects(); |
| | 94 | |
| | 95 | np_.adj_ = true;//$$$ test only - assign property of variable |
| | 96 | match1.adj_ = true; |
| | 97 | |
| | 98 | /* intersect the lists to yield objects matching all words */ |
| | 99 | return match1.intersect(match2); |
| | 100 | } |
| | 101 | ; |
| | 102 | |
| | 103 | grammar anyPhrase: tokWord->txt_: object |
| | 104 | debugPrint(level) { indent(level); "anyPhrase(<<txt_>>)\n"; } |
| | 105 | ; |
| | 106 | |
| | 107 | grammar anyPhrase: tokWord->txt_ anyPhrase->phrase_: object |
| | 108 | debugPrint(level) |
| | 109 | { |
| | 110 | indent(level); |
| | 111 | "anyPhrase(<<txt_>>) phrase:\n"; |
| | 112 | phrase_.debugPrint(level + 1); |
| | 113 | } |
| | 114 | ; |
| | 115 | |
| | 116 | class Item: object |
| | 117 | ; |
| | 118 | |
| | 119 | redBall: Item noun = 'ball' adjective = 'red'; |
| | 120 | blueBall: Item noun = 'ball' adjective = 'blue'; |
| | 121 | flashlight: Item noun = 'flashlight' ; |
| | 122 | |
| | 123 | main(args) |
| | 124 | { |
| | 125 | "Hello from the parser test!\b"; |
| | 126 | for (;;) |
| | 127 | { |
| | 128 | local str, toks; |
| | 129 | local match; |
| | 130 | |
| | 131 | /* read a line */ |
| | 132 | "\b>"; |
| | 133 | str = inputLine(); |
| | 134 | |
| | 135 | /* tokenize the string */ |
| | 136 | toks = Tokenizer.tokenize(str); |
| | 137 | |
| | 138 | /* if it's 'quit' or 'q', stop */ |
| | 139 | if (toks.length() == 1 |
| | 140 | && (getTokVal(toks[1]) is in ('q', 'quit'))) |
| | 141 | break; |
| | 142 | |
| | 143 | /* keep going until we parse all commands in the string */ |
| | 144 | for (;;) |
| | 145 | { |
| | 146 | local used; |
| | 147 | |
| | 148 | /* parse it */ |
| | 149 | match = command.parseTokens(toks, gDict); |
| | 150 | |
| | 151 | /* if we didn't get anything, say so */ |
| | 152 | if (match.length() == 0) |
| | 153 | { |
| | 154 | "That command is not recognized. "; |
| | 155 | break; |
| | 156 | } |
| | 157 | |
| | 158 | /* display the matches */ |
| | 159 | for (local i = 1, local cnt = match.length() ; i <= cnt ; ++i) |
| | 160 | { |
| | 161 | /* display this match */ |
| | 162 | "[match <<i>>: token count = <<match[i][1]>>\n"; |
| | 163 | match[i][2].debugPrint(1); |
| | 164 | "\b"; |
| | 165 | |
| | 166 | /* proceed from here */ |
| | 167 | used = match[i][1]; |
| | 168 | } |
| | 169 | |
| | 170 | /* if more follows, move on */ |
| | 171 | if (used < toks.length()) |
| | 172 | { |
| | 173 | /* remove the used parts from the lists */ |
| | 174 | toks = toks.sublist(used + 1); |
| | 175 | } |
| | 176 | else |
| | 177 | { |
| | 178 | /* we're done */ |
| | 179 | break; |
| | 180 | } |
| | 181 | } |
| | 182 | } |
| | 183 | } |