Grammar* newGrammar(const CHAR* pszGrammarFile) { if (!pszGrammarFile) return NULL; // Read grammar from binary file Grammar* pGrammar = new Grammar(); if (!pGrammar->readBinary(pszGrammarFile)) { #ifdef DEBUG printf("Unable to read binary grammar file %s\n", pszGrammarFile); #endif delete pGrammar; return NULL; } return pGrammar; }
void gen_aux(const Grammar& g, const std::string& word, std::list<std::string>& ret) { if(!bracketed(word)) { ret.push_back(word); } else { Grammar::const_iterator it = g.find(word); if(it == g.end()) throw std::logic_error("empty rule"); const Rule_collection& c = it->second; const Rule& r = c[nrand(c.size())]; for(Rule::const_iterator i = r.begin(); i != r.end(); ++i) gen_aux(g, *i, ret); } }
void CylinderRule::execute(Grammar &grammar, Context &context, Shape &shape) { if (shape.getShapeType() != ST_Prism) { // La règle ne s'applique que sur un prisme throw std::runtime_error("build_cylinder can only be used on Prism primitives"); } Prism const& prism = static_cast<Prism const&>(shape); std::vector<glm::vec2> const &points = prism.poly.getPoints(); const int n = points.size(); // Find the centroid glm::vec2 centroid; for (int i = 0; i < n; i++) { centroid += points[i]; } centroid /= n; glm::vec3 centroid3d(centroid.x, 0, centroid.y); centroid3d = centroid3d + glm::vec3(0, context.terrain->getHeight(centroid), 0); // Build a cylinder of center 'centroid' of radius 'radius' and height 'height' float radius = GRandom.RandomNumber(radius_min, radius_max); float height = GRandom.RandomNumber(height_min, height_max); Circle2D circle(radius, centroid3d); Cylinder cyl(circle, height); // Successeurs if (successors.size() != 1) { throw std::runtime_error("build_cylinder operation must have only one successor"); } CSymbol *succ = *successors.begin(); grammar.resolve(succ, context, cyl); }
/******************************************************************** Читаем из текстового файла секции расширенного описания статьи. Под расширенным описанием подразумевается список координат-атрибутов, формы, грамматическая сеть и специфические для производных классов поля. *********************************************************************/ void Base_Entry::LoadTxtEx( Macro_Parser& txtfile, Grammar& gram ) { // *** Считываем описание статьи *** bool looping = true; BethToken token; CP_Array common; while (looping) { if (txtfile.eof()) { Print_Error(txtfile); gram.GetIO().merr().printf("End of file has been reached before entry body completely loaded\n"); throw E_ParserError(); } const BSourceState back = txtfile.tellp(); token = txtfile.read(); if (token.GetToken() == B_CFIGPAREN) { // Описание статьи закончено looping = false; break; } if (token.GetToken() == B_OTRIPAREN) { txtfile.seekp(token.GetBegin()); SkipNetSection(txtfile, gram); continue; } if (ProcessSection(txtfile, gram, token)) continue; txtfile.seekp(back); if (LoadAttribute(txtfile, gram)) continue; // Загружаем словоформу. LoadForm( txtfile, gram, common, GramCoordAdr(UNKNOWN, UNKNOWN), UNKNOWN, GramCoordAdr(UNKNOWN, UNKNOWN), UNKNOWN, UNKNOWN ); } // конец цикла считывания описания статьи return; }
//=========================================================================== static void normalize( Element & rule, Element const * parent, Grammar & rules ) { if (rule.elements.size() == 1) { Element & elem = rule.elements.front(); if (elem.type != Element::kTerminal || parent && parent->type == Element::kChoice && rule.m == 1 && rule.n == 1) { rule.m *= elem.m; rule.n = max({rule.n, elem.n, rule.n * elem.n}); rule.type = elem.type; rule.value = elem.value; vector<Element> tmp{move(elem.elements)}; rule.elements = move(tmp); return normalize(rule, parent, rules); } } if (rule.eventName.size()) rule.eventRule = rules.eventAlways(rule.eventName); for (auto && elem : rule.elements) normalize(elem, &rule, rules); switch (rule.type) { case Element::kChoice: normalizeChoice(rule); break; case Element::kSequence: normalizeSequence(rule); break; case Element::kRule: normalizeRule(rule, rules); break; case Element::kTerminal: break; } }
SAWYER_EXPORT std::string IfEq::eval(const Grammar &grammar, const std::vector<std::string> &args) { ASSERT_require(args.size() == 4); std::string v1 = grammar.unescape(args[0]); std::string v2 = grammar.unescape(args[1]); return grammar(v1==v2 ? args[2] : args[3]); }
void SG_DeclensionForm::ReadAdditionalInfo( Grammar &gram, const SG_DeclensionTable &table, Macro_Parser& txtfile ) { // :: flexer рег_выражение for регулярное_выражение while( !txtfile.eof() ) { BethToken t = txtfile.read(); if( t==B_FOR ) { UFString re = strip_quotes( txtfile.read().string() ).c_str(); if( table.GetClass()!=UNKNOWN ) { const SG_Class &cls = (const SG_Class&)gram.classes()[ table.GetClass() ]; const int id_lang = cls.GetLanguage(); if( id_lang!=UNKNOWN ) { const SG_Language &lang = gram.GetDict().GetSynGram().languages()[id_lang]; lang.SubstParadigmPattern(re); } } condition_str = re; condition = boost::wregex( re.c_str(), boost::basic_regex<wchar_t>::icase ); valid_condition = true; } else if( t==B_FLEXER ) { UFString re = strip_quotes( txtfile.read().string() ).c_str(); flexer_flags_str = re; flexer_flags = boost::wregex( re.c_str(), boost::basic_regex<wchar_t>::icase ); valid_flexer_flags = true; } else { txtfile.seekp(t); break; } } return; }
//=========================================================================== bool processOptions(Grammar & rules) { if (!*rules.optionString(kOptionRoot)) { logMsgError() << "Option not found, " << kOptionRoot; return false; } string prefix = rules.optionString(kOptionApiPrefix); if (prefix.empty()) { logMsgError() << "Option not found, " << kOptionApiPrefix; return false; } ensureOption(rules, kOptionApiParserClass, prefix + "Parser"); ensureOption(rules, kOptionApiBaseClass, prefix + "ParserBase"); auto & f = use_facet<ctype<char>>(locale()); f.tolower(prefix.data(), prefix.data() + prefix.size()); ensureOption(rules, kOptionApiParserHeader, prefix + "parse.h"); ensureOption(rules, kOptionApiParserCpp, prefix + "parse.cpp"); ensureOption(rules, kOptionApiBaseHeader, prefix + "parsebase.h"); return true; }
boost::shared_ptr<Shape> ShapeLOperator::apply(boost::shared_ptr<Shape>& shape, const Grammar& grammar, std::list<boost::shared_ptr<Shape> >& stack) { float actual_frontWidth; float actual_leftWidth; if (frontWidth.type == Value::TYPE_RELATIVE) { actual_frontWidth = shape->_scope.x * grammar.evalFloat(frontWidth.value, shape); } else { actual_frontWidth = shape->_scope.x * grammar.evalFloat(frontWidth.value, shape); } if (leftWidth.type == Value::TYPE_RELATIVE) { actual_leftWidth = shape->_scope.y * grammar.evalFloat(leftWidth.value, shape); } else { actual_leftWidth = shape->_scope.y * grammar.evalFloat(leftWidth.value, shape); } return shape->shapeL(shape->_name, actual_frontWidth, actual_leftWidth); }
/********************************************************************* Все атрибуты должны быть определены! Метод вызывается после чтения тела описания Статьи. **********************************************************************/ void Base_Entry::CheckAttr(Macro_Parser& txtfile, Grammar& gram) { if (is_quantor(GetClass())) return; const GramClass& cl = gram.classes()[GetClass()]; const int na = CastSizeToInt(cl.attrs().size()); for (int i = 0; i < na; i++) { const GramCoordAdr iglob = cl.attrs()[i]; const GramClass &cls = gram.classes()[GetClass()]; if (GetAttrState(iglob) == UNKNOWN) { lem::zbool def_unk; for (lem::Container::size_type j = 0; j < cl.attr_defaults.size(); ++j) { if (cl.attr_defaults[j].first == iglob && cl.attr_defaults[j].second == UNKNOWN) { def_unk = true; break; } } if (!def_unk) { // Состояние аттрибута не определено! Print_Error(txtfile); gram.GetIO().merr().printf( "Not defined attribute [%us] state\n", gram.coords()[iglob.GetIndex()].GetName()[iglob.GetVar()].c_str() ); throw E_ParserError(); } } } return; }
//=========================================================================== bool copyRules( Grammar & out, Grammar const & src, string_view rootname, bool failIfExists ) { auto root = src.element(rootname); if (!root) { logMsgError() << "Rule not found, " << rootname; return false; } auto & rules = const_cast<set<Element> &>(out.rules()); auto ib = rules.insert(*root); if (!ib.second) { if (failIfExists) { logMsgError() << "Rule already exists, " << rootname; return false; } return true; } auto & elem = *ib.first; if ((elem.flags & Element::fFunction) && (elem.flags & Element::fCharEvents) ) { logMsgError() << "Rule with both Function and Char, " << elem.value; return false; } if ((elem.flags & Element::fStartEvents) == Element::fStartEvents) { logMsgError() << "Rule with both Start and Start+, " << elem.value; return false; } if ((elem.flags & Element::fEndEvents) == Element::fEndEvents) { logMsgError() << "Rule with both End and End+, " << elem.value; return false; } if ((elem.flags & Element::fCharEvents) == Element::fCharEvents) { logMsgError() << "Rule with both Char and Char+, " << elem.value; return false; } return copyRequiredDeps(out, src, elem); }
void gen_aux(const Grammar& g, const string& word, vector<string>& ret) { if (!bracketed(word)) { ret.push_back(word); } else { // locate the rule that corresponds to the word Grammar::const_iterator it = g.find(word); if (it == g.end()) throw logic_error("empty rule"); // fetch the set of possible rules const Rule_collection& c = it->second; // from which we select one at random const Rule& r = c[nrand(c.size())]; // recursively expand the selected rule for (Rule::const_iterator i = r.begin(); i != r.end(); ++i) gen_aux(g, *i, ret); } }
Grammar *read_rule_set(string file) { Grammar *grammar = new Grammar(); ifstream in_file; in_file.open(file.c_str()); if (in_file.is_open()) { int rule_num; while (in_file >> rule_num) { string X, blank, Y, Z; bool is_unary; int dir; in_file >> is_unary; if (!is_unary) { in_file >> X >> Y >> Z >> dir; int nt_X = grammar->to_nonterm(X); int nt_Y = grammar->to_nonterm(Y); int nt_Z = grammar->to_nonterm(Z); BinaryRule rule(rule_num, nt_X, nt_Y, nt_Z, dir); grammar->add_rule(rule); } else {
void gen_aux (const Grammar &g, const std::string &word, std::vector<std::string> &result) { if (!bracketed (word)) { result.push_back (word); } else { Grammar::const_iterator it = g.find (word); if (g.end() == it) throw std::logic_error ("empty grammar rule"); const RuleCollection &rc = it->second; const Rule &r = rc[nrand(rc.size())]; for (Rule::const_iterator r_it = r.begin(); r_it != r.end(); ++r_it) gen_aux (g, *r_it, result); } }
static void PrintActionsNames(std::wostream& os, Grammar& g, bool virt) { os << L"//\n"; os << L"const char* " << g.GetLanguageName() << L"_Actions_Text("; os << L"" << g.GetLanguageName() << L"_Actions e)\n"; os << L"{\n"; os << L" switch(e)\n"; os << L" {\n"; for (int i = 0; i < g.GetNumberOfActions(); i++) { os << L" case " << g.GetActionName(i) << L": return \"" << g.GetActionName(i) << L"\";\n"; } os << L" }\n"; os << L" return \"\";\n"; os << L"};\n"; }
boost::shared_ptr<Shape> TranslateOperator::apply(boost::shared_ptr<Shape>& shape, const Grammar& grammar, std::list<boost::shared_ptr<Shape> >& stack) { float actual_x; float actual_y; float actual_z; if (x.type == Value::TYPE_RELATIVE) { actual_x = shape->_scope.x * grammar.evalFloat(x.value, shape); } else { actual_x = grammar.evalFloat(x.value, shape); } if (y.type == Value::TYPE_RELATIVE) { actual_y = shape->_scope.y * grammar.evalFloat(y.value, shape); } else { actual_y = grammar.evalFloat(y.value, shape); } if (z.type == Value::TYPE_RELATIVE) { actual_z = shape->_scope.z * grammar.evalFloat(z.value, shape); } else { actual_z = grammar.evalFloat(z.value, shape); } shape->translate(mode, coordSystem, actual_x, actual_y, actual_z); return shape; }
int main(int argc, char **args){ Grammar* g = init_g(); if ( argc == 1 ){ g->show(); g->showN(50); // g->unrank(1,11); // for(int i = 1 ; i <= g->getN(6) ; i++ ){ // std::cout << yield(g->unrank(i,6)," ") << std::endl; // } gmp_randclass r (gmp_randinit_default); int mytime = time(NULL); r.seed(mytime); mpz_class ind = r.get_z_range(g->getN(40)); ind = ind + 1; std::cout<< yield(g->unrank(ind,40)," ") << std::endl << mytime << std::endl; }else if( argc > 1 && strcmp(args[1],"random") == 0 ){ gmp_randclass r (gmp_randinit_default); int mytime = time(NULL); r.seed(mytime); mpz_class ind = r.get_z_bits(256); std::cout << ind; }else{ mpz_class ind (args[1]); std::cout<< yield(g->unrank(ind,43)," ") ; } }
void GrammarResolver::cacheGrammars() { RefHashTableOfEnumerator<Grammar> grammarEnum(fGrammarBucket, false, fMemoryManager); ValueVectorOf<XMLCh*> keys(8, fMemoryManager); unsigned int keyCount = 0; // Build key set while (grammarEnum.hasMoreElements()) { XMLCh* grammarKey = (XMLCh*) grammarEnum.nextElementKey(); keys.addElement(grammarKey); keyCount++; } // PSVI: assume everything will be added, if caching fails add grammar back // into vector fGrammarsToAddToXSModel->removeAllElements(); // Cache for (unsigned int i = 0; i < keyCount; i++) { XMLCh* grammarKey = keys.elementAt(i); /*** * It is up to the GrammarPool implementation to handle duplicated grammar */ Grammar* grammar = fGrammarBucket->get(grammarKey); if(fGrammarPool->cacheGrammar(grammar)) { // only orphan grammar if grammar pool accepts caching of it fGrammarBucket->orphanKey(grammarKey); } else if (grammar->getGrammarType() == Grammar::SchemaGrammarType) { // add it back to list of grammars not in grammar pool fGrammarsToAddToXSModel->addElement((SchemaGrammar*) grammar); } } }
void XSAXMLScanner::switchGrammar( const XMLCh* const uriStr , bool laxValidate) { Grammar* tempGrammar = 0; if (XMLString::equals(uriStr, SchemaSymbols::fgURI_SCHEMAFORSCHEMA)) { tempGrammar = fSchemaGrammar; } else { tempGrammar = fGrammarResolver->getGrammar(uriStr); } if (tempGrammar && tempGrammar->getGrammarType() == Grammar::SchemaGrammarType) { fGrammar = tempGrammar; fGrammarType = Grammar::SchemaGrammarType; fValidator->setGrammar(fGrammar); } else if(!laxValidate) { fValidator->emitError(XMLValid::GrammarNotFound, uriStr); } }
/********************************************************** Загрузка тела описания статьи из текстового файла Словаря. Само тело состоит из нескольких факультативных частей. ***********************************************************/ void Base_Entry::LoadBody( Macro_Parser &txtfile, Grammar& gram ) { if (is_realized) { // Если теперь встретится '{', то значит имеем расширенное // описание словарной статьи. const BSourceState prefig = txtfile.tellp(); const BethToken isfig = txtfile.read(); #if LEM_DEBUGGING==1 int nf = CountForms(); #endif if (isfig.GetToken() != B_OFIGPAREN) txtfile.seekp(prefig); else LoadTxtEx(txtfile, gram); if (CountForms() == 0) BeforeFirstForm(gram); CheckAttr(txtfile, gram); } #if defined SOL_DETAILED if (gram.GetDict().GetDebugLevel_ir() >= 3) { // Эхо-сообщение: закончили трансляцию статьи. gram.GetIO().mecho().printf("Ok\n"); } #endif return; }
// like libcg3's, but with a non-void grammar … CG3::Grammar *cg3_grammar_load(const char *filename, UFILE *ux_stdout, UFILE *ux_stderr, bool require_binary = false) { using namespace CG3; std::ifstream input(filename, std::ios::binary); if (!input) { u_fprintf(ux_stderr, "Error: Error opening %s for reading!\n", filename); return 0; } if (!input.read(&cbuffers[0][0], 4)) { u_fprintf(ux_stderr, "Error: Error reading first 4 bytes from grammar!\n"); return 0; } input.close(); Grammar *grammar = new Grammar; grammar->ux_stderr = ux_stderr; grammar->ux_stdout = ux_stdout; boost::scoped_ptr<IGrammarParser> parser; if (cbuffers[0][0] == 'C' && cbuffers[0][1] == 'G' && cbuffers[0][2] == '3' && cbuffers[0][3] == 'B') { parser.reset(new BinaryGrammar(*grammar, ux_stderr)); } else { if (require_binary) { u_fprintf(ux_stderr, "Error: Text grammar detected -- to compile this grammar, use `cg-comp'\n"); CG3Quit(1); } parser.reset(new TextualParser(*grammar, ux_stderr)); } if (parser->parse_grammar_from_file(filename, uloc_getDefault(), ucnv_getDefaultName())) { u_fprintf(ux_stderr, "Error: Grammar could not be parsed!\n"); return 0; } grammar->reindex(); return grammar; }
force_info_factory<Grammar> construct(Grammar& grammar, sbmt::lattice_tree const&, sbmt::property_map_type pmap) { using namespace sbmt; std::vector<indexed_token> c; if (type == constraint_type::lisp_etree) { c = lisp_tree_tags(constraint,grammar.dict()); } else if (type == constraint_type::treebank_etree) { c = tree_tags(constraint,grammar.dict()); } else { indexed_sentence sent = native_sentence(constraint,grammar.dict()); c = std::vector<indexed_token>(sent.begin(),sent.end()); } match = sbmt::substring_hash_match<indexed_token>(c.begin(),c.end()); std::size_t force_string = type == constraint_type::sentence ? pmap["estring"] : pmap["etree_string"] ; return force_info_factory<Grammar>(force_string,match,sbmt::span_t(0,c.size())); }
void gen_aux(const Grammar& g, const std::string& word, std::vector<std::string>& ret) { if (!bracketed(word)) { ret.push_back(word); } else { Grammar::const_iterator it = g.find(word); if (it == g.end()) { std::logic_error("Rule is empty"); } // get rule collection from grammar const Rule_collection& c = it->second; // select a new rule from collection at random const Rule& r = c[nrand(c.size())]; // expand rule for (Rule::const_iterator i = r.begin(); i != r.end(); ++i) { gen_aux(g, *i, ret); } } }
int Syntaxer::Reduce(int cpos) { Grammar* g = nullptr; const Condinate* cond = p_.env->grammars.GetTargetCondinate(cpos, &g); int length = cond->symbols.front() == NativeSymbols::epsilon ? 0 : cond->symbols.size(); std::string log = ">> [R] `" + Utility::Concat(stack_->symbols.end() - length, stack_->symbols.end()) + "` to `" + g->GetLhs().ToString() + "`. "; void* newValue = (cond->action != nullptr) ? cond->action->Invoke(stack_->values) : nullptr; stack_->pop(length); int nextState = p_.lrTable.GetGoto(stack_->states.back(), g->GetLhs()); Debug::Log(log + "Goto state " + std::to_string(nextState) + "."); if (nextState < 0) { Debug::LogError("empty goto item(" + std::to_string(stack_->states.back()) + ", " + g->GetLhs().ToString() + ")"); return nextState; } stack_->push(nextState, newValue, g->GetLhs()); return nextState; }
//=========================================================================== void functionTags(Grammar & rules, Element & rule, bool reset, bool mark) { if (reset || mark) { size_t maxId = 0; for (auto && elem : rules.rules()) { if (reset) const_cast<Element &>(elem).flags &= ~Element::fFunction; maxId = max((size_t)elem.id, maxId); } if (mark) { vector<bool> used(maxId, false); addFunctionTags(rules, rule, used); } } }
int main(int argc, char * argv[]) { // Main Function for ROSE Preprocessor ios::sync_with_stdio(); // Syncs C++ and C I/O subsystems! printf ("***************************************************************************************** \n"); printf ("Build the C++ grammar (essentially an automated generation of a modified version of SAGE) \n"); printf ("***************************************************************************************** \n"); // First build the C++ grammar std::string target_directory = "."; if(argc == 2) target_directory = std::string(argv[1]); // For base level grammar use prefix "Sg" to be compatable with SAGE Grammar sageGrammar ( /* name of grammar */ "Cxx_Grammar", /* Prefix to names */ "Sg", /* Parent Grammar */ "ROSE_BaseGrammar", /* No parent Grammar */ NULL, target_directory ); // Build the header files and source files representing the // grammar's implementation sageGrammar.buildCode(); // Support for output of constructors as part of generated documentation string documentedConstructorPrototypes = sageGrammar.staticContructorPrototypeString; printf ("documentedConstructorPrototypes = %s \n",documentedConstructorPrototypes.c_str()); printf ("Program Terminated Normally! \n"); return 0; }
void Form_Table::LoadBody(Macro_Parser &txtfile, Grammar& gram) { txtfile.read_it(B_OFIGPAREN); // *** Считываем описание Таблицы *** BethToken token; CP_Array common; FOREVER { if (txtfile.eof()) { Print_Error(txtfile); gram.GetIO().merr().printf("End of file has been reached before entry body completely loaded\n"); throw E_ParserError(); } const BSourceState back = txtfile.tellp(); token = txtfile.read(); if (token.GetToken() == B_CFIGPAREN) { // Описание Таблицы закончено. break; } if (ProcessSection(txtfile,gram,token)) continue; txtfile.seekp(back); if (LoadAttribute(txtfile,gram)) continue; LoadForm( txtfile, gram, common, GramCoordAdr(UNKNOWN,UNKNOWN), UNKNOWN, GramCoordAdr(UNKNOWN,UNKNOWN), UNKNOWN, UNKNOWN ); // Загружаем словоформу. } // конец цикла считывания описания статьи return; }
// **************************************************************************** // Function: main // // Programmer: Jeremy Meredith // Creation: April 5, 2002 // // **************************************************************************** int main(int argc, char *argv[]) { if (argc < 2) cerr << "Expecting output file name as argument. Testing only.\n"; Grammar *G = new ExprGrammar; G->SetPrinter(&cout); if (!G->Configure()) { cerr << "--------------------------\n"; cerr << " Error in configuration \n"; cerr << "--------------------------\n"; cerr << endl; return -1; } if (argc >= 2) { ofstream output(argv[1], ios::out); if (!output) { cerr << "---------------------------\n"; cerr << " Error creating file \n"; cerr << "---------------------------\n"; cerr << endl; return -1; } cerr << "Writing to file: " << argv[1] << endl; G->WriteStateInitialization("ExprGrammar", output); output.close(); } return 0; }
void Print(std::wostream& os, MMap& map, Grammar& g) { for (auto it = map.begin(); it != map.end(); it++) { os << L"[" << it->m_pNotTerminal->GetName() << L", " << it->m_pTerminal->GetName() << L"]"; //Print(os, it->m_pTerminal); os << L" : " ; const Production& production = g.GetProduction(it->m_RuleIndex); Print(os , production); os << std::endl; } }
MMap MakeLLTables(Grammar& g) { const GrammarSymbol* pMain = g.FindSymbol(L"Main"); if (pMain == nullptr) { throw std::exception("Main syntax must be defined!"); } if (g.GetStartSymbol() == nullptr) { throw std::exception("Main syntax must be defined! (start)"); } const FirstSets first = BuildFirstSets3(g); const FollowSets follow = BuildFolowSets3(g, first); PrintFirst(std::wcout, first); std::wcout << std::endl; PrintFollow(std::wcout, follow); MMap M = BuildMTable(first, follow, g); std::wcout << L"M table" << std::endl; Print(std::wcout, M, g); return M; }