00001 /*************************************************************************** 00002 kygrammar.h - Definition of KYGrammar class 00003 ------------------- 00004 begin : June 21 2002 00005 copyright : (C) 2003 by Vojtìch Toman 00006 email : vtoman@lit.cz 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00025 #ifdef __GNUG__ 00026 # pragma interface 00027 #endif 00028 00029 #ifndef KYGRAMMAR_H 00030 #define KYGRAMMAR_H 00031 00032 00033 #include "defs.h" 00034 #include "grammarbase.h" 00035 #include "queue.h" 00036 #include "stack.h" 00037 #include "kydefs.h" 00038 #include "iodevice.h" 00039 #include "fibonacci.h" 00040 #include "contextbase.h" 00041 00042 #ifdef __WIN32__ 00043 # include <Winsock2.h> 00044 #else 00045 # include <sys/time.h> 00046 #endif 00047 00048 00049 00053 #define KY_GRAMMAR_FLUSH_STACK_SIZE 8192 00054 00055 00059 #define KY_GRAMMAR_RULE_STACK_SIZE 8192 00060 00061 00067 #define KY_GRAMMAR_MAX_SIZE 8388608 00068 00069 00075 #define KY_GRAMMAR_EATDATA_PERIODICITY 256 00076 00077 00084 class KYGrammar : public GrammarBase 00085 { 00086 public: 00088 KYGrammar(void); 00089 00091 virtual ~KYGrammar(void); 00092 00094 virtual void purge(void); 00095 00097 virtual void setAlphabetBaseSize(size_t size); 00098 00100 virtual void setContext(ContextBase *ctxt, bool useForOutput = true); 00101 00103 void setOutputDevice(IODevice *device); 00104 00106 virtual void append(TerminalValue); 00107 00109 virtual void appendToRootRule(RuleElement *); 00110 00112 virtual void flush(void); 00113 00115 virtual Rule *findRule(RuleId id); 00116 00118 virtual void print(void); 00119 00120 00122 virtual void reconstructInput(void); 00123 00124 00126 virtual void printDigrams(void); 00127 00128 00129 protected: 00131 ContextBase *context; 00132 00134 bool useContextForOutput; 00135 00137 long newSymbolToInstall; 00138 00144 long lastFixedContextSymbol; 00145 00146 00148 size_t size; 00149 00157 IODevice *outputDevice; 00158 00160 InputItem *inputFirst; 00161 00163 InputItem *inputLast; 00164 00166 TestedRules *testedRules; 00167 00168 00174 RuleSet *ruleSet; 00175 00181 Rule *rootRule; 00182 00188 TerminalDigrams *terminalDigrams; 00189 00195 VariableDigrams *variableDigrams; 00196 00197 00206 MatchingRule bestTestedRule; 00207 00213 bool I; 00214 00223 RuleId ruleCounter; 00224 00225 00233 Queue<RuleId> *availableRuleNumbers; 00234 00235 00236 // Stack<Rule> ruleStack; 00237 00243 unsigned long runCount; 00244 00245 00247 unsigned long inputQueueLength; 00248 00250 unsigned long maxInputQueueLength; 00251 00252 00254 size_t totalTime; 00255 00257 size_t totalSymbols; 00258 00260 size_t variablesEncoded; 00261 00263 size_t terminalsEncoded; 00264 00266 size_t eatDataPeriodicity; 00267 00269 virtual Rule *createRootRule(void); 00270 00271 00273 virtual Rule *createRule(RuleElement *, RuleElement *); 00274 00275 00277 virtual bool ruleMatchesInput(TestedRule *testedRule, bool flush = false); 00278 00280 virtual void eatData(bool = false); 00281 00283 virtual void reconstructRule(Rule *rule, IODevice *outputDevice); 00284 00286 virtual size_t getRuleLength(Rule *rule); 00287 00289 virtual void reductionRule1(Rule *); 00290 00292 virtual Rule *reductionRule2(RuleElement *, RuleElement *); 00293 00295 virtual Rule *reductionRule3(Rule *, RuleElement *, RuleElement *); 00296 00297 00299 virtual void initKYGrammar(void); 00300 00301 00303 RuleElement **flushStack; 00304 00306 int flushStackPos; 00307 00309 Rule **ruleStack; 00310 00312 int ruleStackPos; 00313 }; 00314 00315 00316 00317 #endif //KYGRAMMAR_H 00318