| 1 | #charset "us-ascii" |
| 2 | #pragma once |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 2000, 2006 Michael J. Roberts |
| 6 | * |
| 7 | * This file is part of TADS 3. |
| 8 | * |
| 9 | * The header defines the GrammarProd intrinsic class and some associated |
| 10 | * properties and constants. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | /* include our base class definition */ |
| 15 | #include "systype.h" |
| 16 | |
| 17 | /* |
| 18 | * The GrammarProd intrinsic class is a specialized type that's designed |
| 19 | * for creating parsers. An object of this type is created automatically |
| 20 | * by the TADS 3 compiler for each 'grammar' statement. This class |
| 21 | * encapsulates the prototype token list and mapping information defined in |
| 22 | * a 'grammar' statement, and provides a method to match its prototype to |
| 23 | * an input token string. |
| 24 | * |
| 25 | * GrammarProd implements a "parser" in a limited computerese sense, which |
| 26 | * is essentially a program that produces "sentence diagrams" a la |
| 27 | * elementary school grammar lessons. GrammarProd is thus only a small |
| 28 | * part of what we think of as "the parser" in an IF context. The broader |
| 29 | * parser starts with the sentence diagrams that GrammarProd produces, and |
| 30 | * must then interpret their meanings and carry out the intentions they |
| 31 | * express. |
| 32 | * |
| 33 | * Parsers built with GrammarProd trees can handle grammars with left or |
| 34 | * right recursion, and can handle ambiguous grammars (meaning that a |
| 35 | * single input can have multiple ways of matching the grammar). This is |
| 36 | * especially important for natural language parsing, since virtually all |
| 37 | * natural languages have ambiguous grammars. When a match is ambiguous, a |
| 38 | * GrammarProd parser builds all of the possible match trees, allowing you |
| 39 | * to choose the best match based on context and semantic content. |
| 40 | * |
| 41 | * You can also create new productions dynamically with new GrammarProd(). |
| 42 | * Use addAlt() to populate a new production with token matching rules. |
| 43 | */ |
| 44 | intrinsic class GrammarProd 'grammar-production/030002': Object |
| 45 | { |
| 46 | /* |
| 47 | * Parse the token list, starting at this production, using the given |
| 48 | * dictionary to look up tokens. Returns a list of match objects. If |
| 49 | * there are no matches to the grammar, simply returns an empty list. |
| 50 | */ |
| 51 | parseTokens(tokenList, dict); |
| 52 | |
| 53 | /* |
| 54 | * Retrieve a detailed description of the production. This returns a |
| 55 | * list of GrammarAltInfo objects that describe the rule alternatives |
| 56 | * that make up this production. |
| 57 | */ |
| 58 | getGrammarInfo(); |
| 59 | |
| 60 | /* |
| 61 | * Add a new alternative set of alternatives to the rule list for this |
| 62 | * grammar production. |
| 63 | * |
| 64 | * 'alt' is a string with the token list specifying the alternative(s) |
| 65 | * to add. This uses the same syntax as the rule list in a static |
| 66 | * 'grammar' statement. You can use '|' symbols to add multiple |
| 67 | * alternatives. |
| 68 | * |
| 69 | * 'matchObj' is the match object class, which is the class of the |
| 70 | * object that parseTokens() reutrns in the match list to represent a |
| 71 | * match to this production. This corresponds to the base class |
| 72 | * defined in a static 'grammar' statement. |
| 73 | * |
| 74 | * 'dict' is an optional Dictionary object. Literal tokens used in the |
| 75 | * token list will be automatically added to the dictionary if they're |
| 76 | * not already defined. |
| 77 | * |
| 78 | * 'symtab' is an optional LookupTable with the global symbol table. |
| 79 | * This must be provided if any symbolic tokens are used in the rule |
| 80 | * list (property names, sub-production names, etc). In most cases |
| 81 | * this is simply the symbol table that t3GetGlobalSymbols() returns |
| 82 | * during preinit. |
| 83 | */ |
| 84 | addAlt(alt, matchObj, dict?, symtab?); |
| 85 | |
| 86 | /* |
| 87 | * Delete onen or more alternatives from the rule list for this grammar |
| 88 | * production. 'id' identifies the rule(s) to delete: |
| 89 | * |
| 90 | * - By tag: if 'id' is a string, the method deletes each alternative |
| 91 | * whose match object's grammarTag property equals 'id'. For static |
| 92 | * rules defined with 'grammar' statements, the compiler sets |
| 93 | * grammarTag to the tag used in the statement defining the rule. This |
| 94 | * makes it easy to delete all of the rules defined by a given |
| 95 | * 'grammar' statement. |
| 96 | * |
| 97 | * - By match object class: if 'id' is an object, the method deletes |
| 98 | * every alternative whose match object equals 'id' or is a subclass of |
| 99 | * 'id'. This makes it easy to delete a group of dynamically added |
| 100 | * rules that share a match object. |
| 101 | * |
| 102 | * - By index: if 'id' is an integer, it gives the index of the rule to |
| 103 | * delete. This corresponds to an index in the list returned by |
| 104 | * getGrammarInfo(). |
| 105 | * |
| 106 | * 'dict' is an optional Dictionary object to adjust for the deletion. |
| 107 | * If a non-nil 'dict' is given, we'll remove literals from the |
| 108 | * dictionary that were defined by the alternative and no longer used |
| 109 | * in the production. |
| 110 | */ |
| 111 | deleteAlt(id, dict?); |
| 112 | |
| 113 | /* |
| 114 | * Delete all alternatives from the rule list for this grammar |
| 115 | * production. This resets the rule to an empty production with no |
| 116 | * alternatives to match, which is convenient if you want to redefine |
| 117 | * the entire rule set with a subsequent addAlt() call. |
| 118 | * |
| 119 | * 'dict' is an optional Dictionary object to adjust for the deletion. |
| 120 | * If a non-nil 'dict' is given, we'll remove all of the production's |
| 121 | * literals from the dictionary. |
| 122 | */ |
| 123 | clearAlts(dict?); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Token slot types. Each token slot in an alternative has a type, which |
| 128 | * determines what it matches in an input token list. getGrammarInfo() |
| 129 | * returns these type codes in the GrammarAltTokInfo objects. |
| 130 | */ |
| 131 | |
| 132 | #define GramTokTypeProd 1 /* token matches a sub-production */ |
| 133 | #define GramTokTypeSpeech 2 /* token matches a specific part of speech */ |
| 134 | #define GramTokTypeLiteral 3 /* token matches a literal string */ |
| 135 | #define GramTokTypeTokEnum 4 /* token matches a token class enum */ |
| 136 | #define GramTokTypeStar 5 /* token matches all remaining input tokens */ |
| 137 | #define GramTokTypeNSpeech 6 /* matches any of several parts of speech */ |
| 138 | |
| 139 | |
| 140 | /* |
| 141 | * Properties for the first and last token indices, and the complete |
| 142 | * original token list. |
| 143 | * |
| 144 | * Each match tree object will have the firstTokenIndex and lastTokenIndex |
| 145 | * properties set to the bounding token indices for its subtree. Each |
| 146 | * match tree object will also have tokenList set to the original token |
| 147 | * list passed to the parseTokens() that created the match tree, and |
| 148 | * tokenMatchList set to a list of the Dictionary's comparator's |
| 149 | * matchValues() result for each token match. |
| 150 | */ |
| 151 | property firstTokenIndex, lastTokenIndex, tokenList, tokenMatchList; |
| 152 | export firstTokenIndex 'GrammarProd.firstTokenIndex'; |
| 153 | export lastTokenIndex 'GrammarProd.lastTokenIndex'; |
| 154 | export tokenList 'GrammarProd.tokenList'; |
| 155 | export tokenMatchList 'GrammarProd.tokenMatchList'; |
| 156 | |
| 157 | /* |
| 158 | * For alternatives created at run-time with addAlt(), the system maintains |
| 159 | * a list of all of the properties used in "->" expressions in all |
| 160 | * alternatives associated with a match object. This list of properties is |
| 161 | * stored in the grammarAltProps of the match object. |
| 162 | */ |
| 163 | property grammarAltProps; |
| 164 | export grammarAltProps 'GrammarProd.altProps'; |
| 165 |