Example #1
0
// if(x) then ... else ...
void TrFun_If::CompileDeclaration(
                                   PM_Automat &pm,
                                   lem::Iridium::Macro_Parser &txtfile,
                                   TrFunctions &functions,
                                   TrKnownVars &known_vars,
                                   const TrBuiltInFunSignature *signature  
                                  )
{
 src_location = pm.GetDict().GetDebugSymbols().RegisterLocation( txtfile, txtfile.tellp() );

 // ”словие опционально может быть заключено в круглые скобочки.
 const bool req_paren = txtfile.probe( B_OROUNDPAREN );
 cond = functions.CompileCall( pm, txtfile, known_vars );
 if( req_paren )
  txtfile.read_it( B_CROUNDPAREN );

 txtfile.read_it( B_THEN );
 f_then = functions.CompileCall( pm, txtfile, known_vars );
 txtfile.probe( B_SEMICOLON );

 if( txtfile.pick().GetToken()==B_ELSE )
  {
   txtfile.read_it(B_ELSE);
   f_else = functions.CompileCall( pm, txtfile, known_vars );
   txtfile.probe( B_SEMICOLON );
  }

 return;
}
Example #2
0
void TreeScorerResult::LoadTxt( Dictionary & dict, lem::Iridium::Macro_Parser & txtfile, const TreeScorerMarkers & markers )
{
 // ќценка может быть отрицательной.
 if( txtfile.probe( B_SUB ) )
  {
   type=0;
   score = -txtfile.read_int();
  }
 else
  {
   if( lem::is_int( txtfile.pick().string() ) )
    {
     type=0;
     score = txtfile.read_int();
    }
   else
    {
     const lem::Iridium::BethToken & t = txtfile.read();
     id_fact = dict.GetLexAuto().GetKnowledgeBase().FindFacts( t.string() );
     if( id_fact==UNKNOWN )
      {
       // todo - тут могут быть другие варианты вызываемых вычислений.
       lem::Iridium::Print_Error(t,txtfile);
       dict.GetIO().merr().printf( "Unknown scoring expression starts with %us\n", t.string().c_str() );
       throw lem::E_BaseException();
      }

     txtfile.read_it( B_OROUNDPAREN );
     while( !txtfile.eof() )
     {
      if( txtfile.probe( B_CROUNDPAREN ) )
       break;

      if( !args.empty() )
       txtfile.read_it( B_COMMA );

      const lem::Iridium::BethToken & var = txtfile.read();
      lem::UCString upper_var = lem::to_upper(var.string());
      if( !markers.IsAlreadyBound(upper_var) )
       {
        lem::Iridium::Print_Error(var,txtfile);
        dict.GetIO().merr().printf( "variable %us not bound\n", var.string().c_str() );
        throw lem::E_BaseException();
       }

      args.push_back( upper_var );
     }

     type=1;
    }
  }

 return;
}
Example #3
0
void TreeScorers::LoadGroup( Dictionary & dict, lem::Iridium::Macro_Parser &txtfile )
{
 lem::Iridium::BethToken t_name = txtfile.read();
 lem::UCString gname = t_name.string();
 gname.to_upper();
 if( name2id.find(gname)!=name2id.end() )
  {
   lem::Iridium::Print_Error( t_name, txtfile );
   dict.GetIO().merr().printf( "Tree scorer group [%us] is already declared\n", t_name.string().c_str() );
   throw lem::E_ParserError();
  }

 TreeScorerGroupParams params;

 if( txtfile.probe( B_OFIGPAREN ) )
  {
   while( !txtfile.eof() )
   {
    if( txtfile.probe( B_CFIGPAREN ) )
     break;

    lem::Iridium::BethToken tparam = txtfile.read();
    if( tparam.string().eqi(L"allow_unmatched_children") )
     {
      txtfile.read_it( B_EQUAL );

      lem::Iridium::BethToken tbool = txtfile.read();
      if( tbool.string().eqi(L"true") )
       params.allow_unmatched_children = true;
      else if( tbool.string().eqi(L"false") )
       params.allow_unmatched_children = false;
      else
       {
        lem::Iridium::Print_Error( tbool, txtfile );
        dict.GetIO().merr().printf( "[%us] is not boolean value\n", tbool.string().c_str() );
        throw lem::E_ParserError();
       }
     }
    else
     {
      lem::Iridium::Print_Error( tparam, txtfile );
      dict.GetIO().merr().printf( "Unknown tree scorer group parameter [%us]\n", tparam.string().c_str() );
      throw lem::E_ParserError();
     }
   }
  }

 const int id = storage->StoreTreeScorerGroup( gname, params );
 name2id.insert( std::make_pair(gname,id) );

 return;
}
Example #4
0
void ViolationHandler::LoadTxt( lem::Iridium::Macro_Parser & txtfile )
{
 if( txtfile.probe( B_OSPAREN ) )
  {
   txtfile.read_it( B_SUB );
   violation_score = -txtfile.read_int();
   txtfile.probe( B_CSPAREN );
  }
 else
  {
   violation_score=0;
  }

 return;
}
void TreeScorerQuantification::LoadTxt(lem::Iridium::Macro_Parser & txtfile)
{
    if (txtfile.probe(B_OSPAREN))
    {
        lem::Iridium::BethToken t1 = txtfile.read();

        int n1 = 0;
        int n2 = MAX_COUNT;

        if (t1.GetToken() == B_NOT)
        {
            // Ветка [not] представляет собой требования отсутствия и хранится через
            // пару -1,-1
            min_count = max_count = -1;
        }
        else
        {
            if (lem::is_int(t1.string()))
            {
                n1 = lem::to_int(t1.string());
            }
            else
            {
                txtfile.seekp(t1); // [,1] эквивалентно [0,1]
            }

            if (txtfile.probe(B_COMMA))
            {
                lem::Iridium::BethToken t2 = txtfile.read();
                if (lem::is_int(t2.string()))
                    n2 = lem::to_int(t2.string());
                else
                    txtfile.seekp(t2);
            }
            else
            {
                n2 = n1; // [1] эквивалентно [1,1]
            }

            min_count = n1;
            max_count = n2;
        }

        txtfile.read_it(B_CSPAREN);
    }

    return;
}
Example #6
0
void TreeScorerResult::LoadBoundVars( Dictionary & dict, lem::Iridium::Macro_Parser & txtfile, const TreeScorerMarkers & markers )
{
 txtfile.read_it( B_OROUNDPAREN );
 while( !txtfile.eof() )
 {
  if( txtfile.probe( B_CROUNDPAREN ) )
   break;

  if( !args.empty() )
   txtfile.read_it( B_COMMA );

  const lem::Iridium::BethToken & var = txtfile.read();
  lem::UCString upper_var = lem::to_upper(var.string());
  if( !markers.IsAlreadyBound(upper_var) )
   {
    lem::Iridium::Print_Error(var,txtfile);
    dict.GetIO().merr().printf( "Variable %us is not bound\n", var.string().c_str() );
    throw lem::E_BaseException();
   }

  args.push_back( upper_var );
 }

 return;
}
Example #7
0
void TreeScorerResult::LoadTxt(
                               Dictionary & dict,
                               lem::Iridium::Macro_Parser & txtfile,
                               const TreeScorerMarkers & markers
                              )
{
 // ќценка может быть отрицательной.
 if( txtfile.probe( B_SUB ) )
  {
   type=NumberScoreType;
   score = -txtfile.read_int();
  }
 else
  {
   if( lem::is_int( txtfile.pick().string() ) )
    {
     type=NumberScoreType;
     score = txtfile.read_int();
    }
   else
    {
     const lem::Iridium::BethToken & t = txtfile.read();
     id_fact = dict.GetLexAuto().GetKnowledgeBase().FindFacts( t.string() );
     if( id_fact==UNKNOWN )
      {
       type = FuncScoreType;

       Solarix::TrKnownVars vars;

       Solarix::TrTreeType tree_type;
       for( lem::Container::size_type k=0; k<markers.CountMarkers(); ++k )
       {
        vars.RegisterVar( tree_type, markers.GetMarker(k) );
        args.push_back( markers.GetMarker(k) );
       }

       txtfile.seekp(t);
       score_fun = dict.GetLexAuto().GetFunctions().Get().CompileCall( dict.GetLexAuto(), txtfile, vars );
      }
     else
      {
       LoadBoundVars( dict, txtfile, markers );
       type=NGramScoreType;
      }
    }
  }

 return;
}
void PatternConstraints::LoadTxt(
    Dictionary &dict,
    lem::Iridium::Macro_Parser & txtfile,
    SynPatternCompilation & compilation_context
)
{
    txtfile.read_it(B_OFIGPAREN);

    lem::Iridium::BSourceState beg = txtfile.tellp();

    while (!txtfile.eof())
    {
        if (txtfile.probe(B_CFIGPAREN))
            break;

        PatternConstraint * c = new PatternConstraint();
        c->LoadTxt(dict, txtfile, compilation_context);
        constraints.push_back(c);
    }

    return;
}
Example #9
0
void TrType::LoadTxt(
    PM_Automat &pm,
    lem::Iridium::Macro_Parser &txtfile,
    const TrFunctions &funs
)
{
    is_const = txtfile.probe( L"const" );

    const lem::Iridium::BethToken& t = txtfile.read();

    if( t.string().eqi( L"bool" ) )
        type = Bool;
    else if( t.string().eqi( L"int" ) )
        type = Int;
    else if( t.string().eqi( L"string" ) )
        type = String;
    else if( t.string().eqi( L"tree" ) )
        type = Tree;
    else if( t.string().eqi( L"trees" ) )
        type = Trees;
    else if( t.string().eqi( L"fun" ) )
        type = Fun;
    else if( t.string().eqi( L"void" ) )
        type = Void;
    else if( t.string().eqi( L"variant" ) )
        type = Variant;
    else if( t.string().eqi( L"tuple" ) )
        type = Tuple;
    else
    {
        lem::Iridium::Print_Error( t, txtfile );
        pm.GetIO().merr().printf( "Unknown type [%us]\n", t.string().c_str() );
        throw E_ParserError();
    }

    return;
}
Example #10
0
void SynPattern::LoadTxt(
                         Dictionary &dict,
                         lem::Iridium::Macro_Parser & txtfile,
                         const SynPatterns &patterns,
                         WordEntrySet &wordentry_set,
                         const TrProcedureDeclaration &procs,
                         TrFunctions &functions
                        )
{
 lem::Iridium::BSourceState pattern_beginning = txtfile.tellp();

 id_src = dict.GetDebugSymbols().RegisterLocation( txtfile, txtfile.tellp() );

 if( dict.GetDebugLevel_ir()>=3 )
  {
   dict.GetIO().mecho().printf( "pattern " );
  }

 // ќпционально могут быть заданы целевой ¤зык и опции.

 while( !txtfile.eof() )
 {
  if( txtfile.probe( B_OFIGPAREN ) )
   break;

  if( txtfile.probe( B_LANGUAGE ) )
   {
    txtfile.read_it( B_EQUAL );
   
    lem::Iridium::BethToken lang = txtfile.read();
    id_language = dict.GetSynGram().Find_Language(lang.string());
    if( id_language==UNKNOWN )
     {
      lem::Iridium::Print_Error(lang,txtfile);
      dict.GetIO().merr().printf( "Unknown language name %us\n", lang.c_str() );
      throw lem::E_BaseException();
     }     
   }
  else if( txtfile.probe( L"incomplete" ) )
   {
    incomplete=true;
   }
  else
   {
    lem::Iridium::BethToken tname = txtfile.read();
    name = tname.string();
  
    if( dict.GetDebugLevel_ir()>=3 )
     {
      dict.GetIO().mecho().printf( "%vfE%us%vn ", name.c_str() );
     }
  
    if( !patterns.IsPatternName(name) )
     {
      dict.GetIO().merr().printf( "Patterns group [%us] is not declared\n", name.c_str() );
      lem::Iridium::Print_Error(tname,txtfile);
      throw lem::E_BaseException();
     }
  
    const SynPatternOptions & group_options = patterns.GetOptions(name);
    id_language = group_options.GetLanguageId();
  
    // —екци¤ export { ... } содержит объ¤влени¤ координат, которые паттерн выдает наружу
    // —начала попробуем вз¤ть содержимое экспорта по умолчанию, зарегистрированное в объ¤влении
    // группы паттернов.
  
    if( txtfile.probe(L"export") )
     {
      export_info.LoadTxt( dict, txtfile );
     }
    else
     {
      const SynPatternOptions & p_options = patterns.GetOptions(name);
      export_info = p_options.GetExport();
     }
  
    export_info.RegisterExport( *compilation_context );
  
    if( txtfile.probe( B_LANGUAGE ) )
     {
      txtfile.read_it( B_EQUAL );
      
      lem::Iridium::BethToken lang = txtfile.read();
      id_language = dict.GetSynGram().Find_Language(lang.string());
      if( id_language==UNKNOWN )
       {
        lem::Iridium::Print_Error(lang,txtfile);
        dict.GetIO().merr().printf( "Unknown language name %us\n", lang.c_str() );
        throw lem::E_BaseException();
       }     
     }
  
    txtfile.read_it( B_OFIGPAREN );
    break;
   }
 }

 // —писок опорных точек в фигурных скобочках
 lem::Iridium::BSourceState beg = txtfile.tellp();

 compilation_context->SetName( name );
 compilation_context->Set(&wordentry_set);

 while( !txtfile.eof() )
  {
   if( txtfile.pick().GetToken()==B_CFIGPAREN )
    {
     txtfile.read();
     break;
    }

   SlotProperties slot;
   slot.LoadTxt( dict, txtfile );

   SynPatternPoint *p = new SynPatternPoint;
   p->LoadTxt( dict, txtfile, patterns, *compilation_context, procs, functions );

   points.push_back(p);
   slots.push_back(slot);
   compilation_context->BeforeNextPointCompilation();
  }

 if( points.empty() )
  {
   lem::Iridium::Print_Error(beg,txtfile);
   dict.GetIO().merr().printf("Pattern must not be empty\n" );
   throw E_Solarix();
  }

 if( !compilation_context->PatternHasBeenCompiled(dict.GetSynGram()) )
  {
   lem::Iridium::Print_Error(pattern_beginning,txtfile);
   dict.GetIO().merr().printf("Some export items are not actually exported\n" );
   throw E_Solarix();
  }

 points.back()->Terminator();

 bool links_loaded=false, ngrams_loaded=false, predicates_loaded=false, constraints_loaded=false;

 while( !txtfile.eof() )
  if( txtfile.probe(B_COLON) )
   {
    lem::Iridium::BSourceState section_beg = txtfile.tellp();

    if( txtfile.probe( L"links" ) )
     {
      if( links_loaded )
       {
        lem::Iridium::Print_Error(section_beg,txtfile);
        dict.GetIO().merr().printf("Redefinition of 'links'\n" );
        throw E_Solarix();
       }
 
      LoadLinks( dict, txtfile, *compilation_context );
      links_loaded=true;
     }
    else if( txtfile.probe( L"ngrams" ) )
     {
      if( ngrams_loaded )
       {
        lem::Iridium::Print_Error(section_beg,txtfile);
        dict.GetIO().merr().printf("Redefinition of 'ngrams'\n" );
        throw E_Solarix();
       }

      LoadNGrams( dict, txtfile, *compilation_context );
      ngrams_loaded=true;
     }
    else if( txtfile.probe( L"predicates" ) )
     {
      if( predicates_loaded )
       {
        lem::Iridium::Print_Error(section_beg,txtfile);
        dict.GetIO().merr().printf("Redefinition of 'predicates'\n" );
        throw E_Solarix();
       }

      LoadPredicates( dict, txtfile, *compilation_context );
      predicates_loaded=true;
     }
/*
    else if( sparse && txtfile.probe( L"constraints" ) )
     {
      if( constraints_loaded )
       {
        lem::Iridium::Print_Error(section_beg,txtfile);
        dict.GetIO().merr().printf("Redefinition of 'constraints'\n" );
        throw E_Solarix();
       }

      LoadConstraints( dict, txtfile, *compilation_context );
      constraints_loaded=true;
     }*/
    else
     {
      lem::Iridium::Print_Error(txtfile);
      dict.GetIO().merr().printf("Unexpected token\n" );
      throw E_Solarix();
     }
   }
  else
   {
    break;
   }


 // ќпорные точки могут теперь выполнить внутренние оптимизации, в частности - учесть использование
 // маркировок и улучшить эффективность директивы @mark()
 for( lem::Container::size_type i=0; i<points.size(); ++i )
  points[i]->OptimizeAfterCompilation( *compilation_context );

 // Ѕезым¤нные паттерны, то есть правила самого верхнего уровн¤, должны быть прив¤заны к ¤зыку с помощью директивы { language=XXX }
 if( id_language==UNKNOWN && name.empty() )
  {
   lem::Iridium::Print_Error(pattern_beginning,txtfile);
   dict.GetIO().merr().printf("Pattern must be bound to a language\n" );
   throw E_Solarix();
  }

 if( dict.GetDebugLevel_ir()>=3 )
  {
   dict.GetIO().mecho().printf( "%vfAOK%vn\n" );
  }
 
 return;
}
Example #11
0
void SynPatternExport::LoadTxt(
                               Dictionary &dict,
                               lem::Iridium::Macro_Parser & txtfile
                              )
{
 txtfile.read_it( B_OFIGPAREN );

 while( !txtfile.eof() )
  {
   bool null_export = txtfile.probe( B_OROUNDPAREN );
   lem::Iridium::BethToken coord_name = txtfile.read();
 
   if( coord_name.GetToken()==B_CFIGPAREN )
    break;

   if( coord_name.string().eqi( L"node" ) )
    {
     txtfile.read_it( B_COLON );
     lem::Iridium::BethToken t_node_name = txtfile.read();
     lem::UCString node_name = t_node_name.string();
     node_name.to_upper();

     if( export_nodes.find(node_name)!=UNKNOWN )
      {
       dict.GetIO().merr().printf( "Wordform %us is already mentioned in export section\n", t_node_name.string().c_str() );
       lem::Iridium::Print_Error(t_node_name,txtfile);
       throw lem::E_BaseException();
      }

     export_nodes.push_back( node_name );
     null_export_nodes.push_back( null_export ? 1 : 0 );

     if( null_export )
      txtfile.read_it( B_CROUNDPAREN );

     continue;
    }

   const GramCoordAdr iglob_coord = dict.GetSynGram().FindCoord(coord_name.string());
 
   if( !iglob_coord.IsDefined() )
    {
     dict.GetIO().merr().printf( "Unknown coordinate %us\n", coord_name.c_str() );
     lem::Iridium::Print_Error(coord_name,txtfile);
     throw lem::E_BaseException();
    }
 
   if( export_coords.find( iglob_coord.GetIndex() )!=UNKNOWN )
    {
     dict.GetIO().merr().printf( "Coordinate %us is already mentioned in export section\n", coord_name.c_str() );
     lem::Iridium::Print_Error(coord_name,txtfile);
     throw lem::E_BaseException();
    }
 
   export_coords.push_back(iglob_coord.GetIndex());
   null_export_coords.push_back( null_export ? 1 : 0 );

   if( null_export )
    txtfile.read_it( B_CROUNDPAREN );
  }

 return;
}
Example #12
0
void KB_Facts::LoadTxt(Solarix::Dictionary &dict, lem::Iridium::Macro_Parser &txtfile)
{
    name = txtfile.read().string();

    txtfile.read_it(B_LANGUAGE);
    txtfile.read_it(B_EQUAL);

    lem::Iridium::BethToken lang = txtfile.read();
    id_language = dict.GetSynGram().Find_Language(lang.string());
    if (id_language == UNKNOWN)
    {
        lem::Iridium::Print_Error(lang, txtfile);
        dict.GetIO().merr().printf("Unknown language name %us\n", lang.c_str());
        throw lem::E_BaseException();
    }

    txtfile.read_it(B_OFIGPAREN);

    n_ret = 0;

    while (!txtfile.eof())
    {
        if (txtfile.probe(B_CFIGPAREN))
            break;

        lem::Iridium::BethToken t = txtfile.read();
        if (t.string().eqi(L"arity"))
        {
            txtfile.read_it(B_EQUAL);
            n_arg = txtfile.read_int();
        }
        else if (t.string().eqi(L"return"))
        {
            txtfile.read_it(B_EQUAL);

            lem::Iridium::BethToken t_ret = txtfile.read();
            if (t_ret.eqi(L"boolean"))
            {
                n_ret = 1;
                ret_type = 0;
            }
            else if (t_ret.eqi(L"integer"))
            {
                n_ret = 1;
                ret_type = 1;
            }
            else
            {
                lem::Iridium::Print_Error(t_ret, txtfile);
                dict.GetIO().merr().printf("Unknown return type %us\n", t_ret.c_str());
                throw lem::E_BaseException();
            }
        }
        else if (t.string().eqi(L"violation_score"))
        {
            txtfile.read_it(B_EQUAL);

            if (txtfile.probe(B_SUB))
                violation_score = -txtfile.read_int();
            else
                violation_score = txtfile.read_int();
        }
        else if (t.string().eqi(L"violation_handler"))
        {
            txtfile.read_it(B_EQUAL);

            violation_handler = new PredicateTemplate();
            PredicateTemplateParams_KB params(n_arg);
            violation_handler->LoadTxt(dict, txtfile, params);
        }
        else if (t.string().eqi(L"generic"))
        {
            allow_generic = true;
        }
        else
        {
            lem::Iridium::Print_Error(t, txtfile);
            dict.GetIO().merr().printf("Unknown property %us\n", t.c_str());
            throw lem::E_BaseException();
        }
    }

    return;
}