2 votes

Comment faire référence à une valeur de la table des symboles ?

Je n'arrive pas à trouver comment passer la valeur de la table des symboles dans la fonction.

template <typename Iterator>
class single_attribute_grammar : public qi::grammar<Iterator, AttributeData(), qi::blank_type>
{
public:
    single_attribute_grammar(const word_symbols &words) : single_attribute_grammar::base_type(single_attribute_rule)
    {
        auto attr_word = phx::bind(&AttributeData::word, qi::_val);
        auto grammar_word = phx::bind(&WordGrammar::word, qi::_1);
        auto attr_value = phx::bind(&AttributeData::value, qi::_val);
        single_attribute_rule = qi::lexeme[words[attr_word = grammar_word] > 
            qi::int_[attr_value = qi::_1] > (qi::space|qi::eoi)] >>
            qi::eps(phx::bind(verify_range, qi::_r1, qi::_val)); // <-- HERE is the problem
        BOOST_SPIRIT_DEBUG_NODE(single_attribute_rule);
    }

private:
    qi::rule<Iterator, AttributeData(), qi::blank_type> single_attribute_rule;
};

Je pense que je peux me référer à la valeur de la clé trouvée en utilisant qi::_r1 mais le code ne se compile pas :

main.cpp:72:31:   required from ‘single_attribute_grammar<Iterator>::single_attribute_grammar(const word_symbols&) [with Iterator = boost::spirit::classic::position_iterator2<boost::spirit::multi_pass<std::istreambuf_iterator<char, std::char_traits<char> > > >; word_symbols = boost::spirit::qi::symbols<char, WordGrammar>]’
main.cpp:87:16:   required from ‘all_attributes_grammar<Iterator>::all_attributes_grammar(const word_symbols&) [with Iterator = boost::spirit::classic::position_iterator2<boost::spirit::multi_pass<std::istreambuf_iterator<char, std::char_traits<char> > > >; word_symbols = boost::spirit::qi::symbols<char, WordGrammar>]’
main.cpp:130:62:   required from here
/usr/include/boost/spirit/home/support/context.hpp:180:13: error: static assertion failed: index_is_out_of_bounds
             BOOST_SPIRIT_ASSERT_MSG(
             ^
In file included from /usr/include/boost/spirit/home/qi/domain.hpp:18:0,
                 from /usr/include/boost/spirit/home/qi/meta_compiler.hpp:15,
                 from /usr/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /usr/include/boost/spirit/home/qi/action.hpp:14,
                 from /usr/include/boost/spirit/home/qi.hpp:14,
                 from /usr/include/boost/spirit/include/qi.hpp:16,
                 from main.cpp:11:
/usr/include/boost/spirit/home/support/context.hpp:186:13: error: no type named ‘type’ in ‘struct boost::fusion::result_of::at_c<boost::fusion::cons<AttributeData&, boost::fusion::nil_>, 1>’
             type;
             ^~~~

Au cas où cela serait utile, voici le MVCE.

#include <iomanip>
#include <string>
#include <vector>

#include <boost/variant.hpp>
#include <boost/optional/optional.hpp>

#define BOOST_SPIRIT_DEBUG

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_symbols.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp> // construct
#include <boost/spirit/include/support_multi_pass.hpp>
#include <boost/spirit/include/classic_position_iterator.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/phoenix/bind.hpp>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace classic = boost::spirit::classic;
namespace phx = boost::phoenix;
namespace fusion = boost::fusion;

struct AttributeData
{
    std::string word;
    int value;
};

using AttributeVariant = boost::variant<
    AttributeData
>;

struct WordGrammar
{
    std::string word;
    int range_from;
    int range_to;
};

BOOST_FUSION_ADAPT_STRUCT(
    AttributeData,
    (std::string, word)
    (int, value)
)

using word_symbols = qi::symbols<char, WordGrammar>;

bool verify_range(const WordGrammar &grammar, const AttributeData &data) 
{
    if(data.value < grammar.range_from || data.value > grammar.range_to)
    {
        return false;
    }

    return true;
}

template <typename Iterator>
class single_attribute_grammar : public qi::grammar<Iterator, AttributeData(), qi::blank_type>
{
public:
    single_attribute_grammar(const word_symbols &words) : single_attribute_grammar::base_type(single_attribute_rule)
    {
        auto attr_word = phx::bind(&AttributeData::word, qi::_val);
        auto grammar_word = phx::bind(&WordGrammar::word, qi::_1);
        auto attr_value = phx::bind(&AttributeData::value, qi::_val);
        single_attribute_rule = qi::lexeme[words[attr_word = grammar_word] > 
            qi::int_[attr_value = qi::_1] > (qi::space|qi::eoi)] >>
            qi::eps(phx::bind(verify_range, qi::_r1, qi::_val)); // <-- HERE is the problem
        BOOST_SPIRIT_DEBUG_NODE(single_attribute_rule);
    }

private:
    qi::rule<Iterator, AttributeData(), qi::blank_type> single_attribute_rule;
};

template <typename Iterator>
class all_attributes_grammar : public qi::grammar<Iterator, std::vector<AttributeVariant>(), qi::blank_type>
{
public:
    all_attributes_grammar(const word_symbols &words) : all_attributes_grammar::base_type(line_attribute_vec_rule)
    , sag(words)
    {
        line_attribute_rule = (
            sag
        );
        BOOST_SPIRIT_DEBUG_NODE(line_attribute_rule);
        line_attribute_vec_rule = (line_attribute_rule % *qi::blank) > qi::eoi;
        BOOST_SPIRIT_DEBUG_NODE(line_attribute_vec_rule);
    }

private:
    single_attribute_grammar<Iterator> sag;
    qi::rule<Iterator, AttributeVariant(), qi::blank_type> line_attribute_rule;
    qi::rule<Iterator, std::vector<AttributeVariant>(), qi::blank_type> line_attribute_vec_rule;
};

int main()
{
    std::vector<AttributeVariant> value;
    std::string data{"N100 X-100 AC5"};
    std::istringstream input(data);

    // iterate over stream input
    typedef std::istreambuf_iterator<char> base_iterator_type;
    base_iterator_type in_begin(input);

    // convert input iterator to forward iterator, usable by spirit parser
    typedef boost::spirit::multi_pass<base_iterator_type> forward_iterator_type;
    forward_iterator_type fwd_begin = boost::spirit::make_default_multi_pass(in_begin);
    forward_iterator_type fwd_end;

    // wrap forward iterator with position iterator, to record the position
    typedef classic::position_iterator2<forward_iterator_type> pos_iterator_type;
    pos_iterator_type position_begin(fwd_begin, fwd_end);
    pos_iterator_type position_end;

    word_symbols sym;
    sym.add
    ("N", {"N", 1, 9999})
    ("X", {"X", -999, 999})
    ("AC", {"AC", -99, 999})
    ;

    all_attributes_grammar<pos_iterator_type> all_attr_gr(sym);

    try
    {
        qi::phrase_parse(position_begin, position_end, all_attr_gr, qi::blank, value);
    }
    catch (const qi::expectation_failure<pos_iterator_type>& e)
    {
        const classic::file_position_base<std::string>& pos = e.first.get_position();
        std::cout <<
            "Parse error at line " << pos.line << " column " << pos.column << ":" << std::endl <<
            "'" << e.first.get_currentline() << "'" << std::endl <<
            std::setw(pos.column) << " " << "^- here" << std::endl;
    }

    return 0;
}

Des idées ?

2voto

sehe Points 123151

Beaucoup de choses à simplifier.

  • pourquoi les AttributeData sont-elles adaptées, alors que vous utilisez plutôt des actions sémantiques ? (voir Boost Spirit : "Les actions sémantiques sont mauvaises" ? )
  • Sauter les espaces blancs est beaucoup plus élégant lorsqu'on utilise un skipper. Il est également logiquement contradictoire de sauter des espaces à l'intérieur d'un lexème (cf. Problèmes de skipper de l'esprit de Boost )
  • En particulier, l'omission des espaces blancs ici :

    line_attribute_vec_rule = (line_attribute_rule % *qi::blank) > qi::eoi;

    est complètement impuissant (parce que les blancs sont déjà ignorés, la fonction *qi::blank ne correspondra jamais à aucun caractère). L'ensemble se réduit à +line_attribute_rule .

    line_attribute_vec_rule = +line_attribute_rule > qi::eoi;

Re : votre réponse

En effet, vous pouvez utiliser plus d'état dans la règle. Au lieu de cela, je simplifierais l'AST pour soutenir votre cas. Dites :

struct AttrDef {
    std::string word;
    std::pair<int,int> range;
};

struct AttributeData {
    AttrDef def;
    int value;

    bool is_valid() const {
        return std::minmax({value, def.range.first, def.range.second}) == def.range;
    }
};

BOOST_FUSION_ADAPT_STRUCT(AttributeData, def, value)

Maintenant, single_attribute_grammar ne se soucierait pas de sauter, et serait comme :

using attr_defs = qi::symbols<char, AttrDef>;

template <typename Iterator>
struct single_attribute_grammar : public qi::grammar<Iterator, AttributeData()> {
    single_attribute_grammar(const attr_defs &defs)
        : single_attribute_grammar::base_type(start) 
    {
        using namespace qi;
        attribute_data = defs >> int_;
        start         %= attribute_data [ _pass = is_valid_(_1) ];

        BOOST_SPIRIT_DEBUG_NODES((attribute_data));
    }

private:
    phx::function<std::function<bool(AttributeData const&)> > is_valid_ {&AttributeData::is_valid};
    qi::rule<Iterator, AttributeData()> attribute_data;
    qi::rule<Iterator, AttributeData()> start;
};

Comme vous pouvez le voir, il n'y a pas d'état parce que je n'ai pas utilisé eps . C'est vrai, j'ai volé en plein dans le visage de ma propre directive ("éviter les actions sémantiques") pour la simple raison qu'elle évite l'état explicite (au lieu d'utiliser l'exsting qi::_pass ).

En faisant beaucoup de simplifications dans main (spécifiquement, en utilisant boost::spirit::istream_iterator plutôt qu'une adaptation multi-pass), j'arriverais à ceci :

<a href="http://coliru.stacked-crooked.com/a/bcbfd5dba283603e" rel="nofollow noreferrer">Live On Coliru</a>

//#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/classic_position_iterator.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <iomanip>

namespace qi      = boost::spirit::qi;
namespace phx     = boost::phoenix;

struct AttrDef {
    std::string word;
    std::pair<int,int> range;
};

struct AttributeData {
    AttrDef def;
    int value;

    bool is_valid() const {
        return std::minmax({value, def.range.first, def.range.second}) == def.range;
    }
};

BOOST_FUSION_ADAPT_STRUCT(AttributeData, def, value)

static inline std::ostream& operator<<(std::ostream& os, const AttrDef& def) {
    return os << "AttrDef(" << def.word << ", " << def.range.first << ", " << def.range.second << ")";
}

using attr_defs = qi::symbols<char, AttrDef>;

template <typename Iterator>
struct single_attribute_grammar : public qi::grammar<Iterator, AttributeData()> {
    single_attribute_grammar(const attr_defs &defs)
        : single_attribute_grammar::base_type(start) 
    {
        using namespace qi;
        attribute_data = defs >> int_;
        start         %= attribute_data [ _pass = is_valid_(_1) ];

        BOOST_SPIRIT_DEBUG_NODES((attribute_data));
    }

private:
    phx::function<std::function<bool(AttributeData const&)> > is_valid_ {&AttributeData::is_valid};
    qi::rule<Iterator, AttributeData()> attribute_data;
    qi::rule<Iterator, AttributeData()> start;
};

using AttributeVariant = boost::variant<AttributeData>;

template <typename Iterator>
class all_attributes_grammar : public qi::grammar<Iterator, std::vector<AttributeVariant>(), qi::blank_type>
{
public:
    all_attributes_grammar(const attr_defs &defs)
        : all_attributes_grammar::base_type(line_attribute_vec_rule),
          sag(defs)
    {
        line_attribute_rule = (
            sag
        );
        line_attribute_vec_rule = +line_attribute_rule > qi::eoi;

        BOOST_SPIRIT_DEBUG_NODES((line_attribute_rule)(line_attribute_vec_rule));
    }

private:
    single_attribute_grammar<Iterator> sag;
    qi::rule<Iterator, AttributeVariant()>                              line_attribute_rule;
    qi::rule<Iterator, std::vector<AttributeVariant>(), qi::blank_type> line_attribute_vec_rule;
};

int main() {
    constexpr bool fail = false, succeed = true;
    struct _ { bool expect; std::string data; } const tests[] = {
         { succeed, "N100 X-100 AC5" },
         { fail,    ""               },
         { fail,    "  "             },
         { succeed, "N1"             },
         { succeed, " N1"            },
         { succeed, "N1 "            },
         { succeed, " N1 "           },
         { fail,    "N 1"            },
         { fail,    "N0"             },
         { succeed, "N9999"          },
         { fail,    "N10000"         },
    };

    for (auto test : tests) {
        std::istringstream input(test.data);

        typedef boost::spirit::classic::position_iterator2<boost::spirit::istream_iterator> pos_iterator_type;
        pos_iterator_type position_begin(boost::spirit::istream_iterator{input >> std::noskipws}, {}), position_end;

        attr_defs sym;
        sym.add
            ("N",  {"N", {1, 9999}})
            ("X",  {"X", {-999, 999}})
            ("AC", {"AC", {-99, 999}})
            ;

        all_attributes_grammar<pos_iterator_type> all_attr_gr(sym);

        try {
            std::vector<AttributeVariant> value;
            std::cout << " --------- '" << test.data << "'\n";

            bool actual = qi::phrase_parse(position_begin, position_end, all_attr_gr, qi::blank, value);

            std::cout << ((test.expect == actual)?"PASS":"FAIL");

            if (actual) {
                std::cout << "\t";
                for (auto& attr : value)
                    std::cout << boost::fusion::as_vector(boost::get<AttributeData>(attr)) << " ";
                std::cout << "\n";
            } else {
                std::cout << "\t(no valid parse)\n";
            }
        }
        catch (const qi::expectation_failure<pos_iterator_type>& e) {
            auto& pos = e.first.get_position();
            std::cout <<
                "Parse error at line " << pos.line << " column " << pos.column << ":" << std::endl <<
                "'" << e.first.get_currentline() << "'" << std::endl <<
                std::setw(pos.column) << " " << "^- here" << std::endl;
        }

        if (position_begin != position_end)
            std::cout << " -> Remaining '" << std::string(position_begin, position_end) << "'\n";
    }
}

Imprimés :

 --------- 'N100 X-100 AC5'
PASS    (AttrDef(N, 1, 9999) 100) (AttrDef(X, -999, 999) -100) (AttrDef(AC, -99, 999) 5) 
 --------- ''
PASS    (no valid parse)
 --------- '  '
PASS    (no valid parse)
 -> Remaining '  '
 --------- 'N1'
PASS    (AttrDef(N, 1, 9999) 1) 
 --------- ' N1'
PASS    (AttrDef(N, 1, 9999) 1) 
 --------- 'N1 '
PASS    (AttrDef(N, 1, 9999) 1) 
 --------- ' N1 '
PASS    (AttrDef(N, 1, 9999) 1) 
 --------- 'N 1'
PASS    (no valid parse)
 -> Remaining 'N 1'
 --------- 'N0'
PASS    (no valid parse)
 -> Remaining 'N0'
 --------- 'N9999'
PASS    (AttrDef(N, 1, 9999) 9999) 
 --------- 'N10000'
PASS    (no valid parse)
 -> Remaining 'N10000'

1voto

doqtor Points 3748

Après avoir étudié un peu plus l'esprit de boost, j'ai trouvé que la solution est d'utiliser les attributs locaux et hérités :

template <typename Iterator>
class single_attribute_grammar : public qi::grammar<Iterator, AttributeData(), qi::locals<WordGrammar>, qi::blank_type>
{
public:
    single_attribute_grammar(const word_symbols &words) : single_attribute_grammar::base_type(single_attribute_rule)
    {
        auto attr_word = phx::bind(&AttributeData::word, qi::_val);
        auto grammar_word = phx::bind(&WordGrammar::word, qi::_1);
        auto attr_value = phx::bind(&AttributeData::value, qi::_val);
        word_rule = words;
        BOOST_SPIRIT_DEBUG_NODE(word_rule);
        range_check_rule = qi::eps(phx::bind(verify_range, qi::_r1, qi::_r2));
        BOOST_SPIRIT_DEBUG_NODE(range_check_rule);
        single_attribute_rule = qi::lexeme[word_rule[attr_word = grammar_word,qi::_a=qi::_1] >
            qi::int_[attr_value = qi::_1] > (qi::space|qi::eoi)] >>
            range_check_rule(qi::_a, qi::_val);
        BOOST_SPIRIT_DEBUG_NODE(single_attribute_rule);
    }

private:
    qi::rule<Iterator, WordGrammar()> word_rule;
    qi::rule<Iterator, void(const WordGrammar&, const AttributeData&), qi::blank_type> range_check_rule;
    qi::rule<Iterator, AttributeData(), qi::locals<WordGrammar>, qi::blank_type> single_attribute_rule;
};

Aussi parce que j'utilise BOOST_SPIRIT_DEBUG_NODE surcharge de l'opérateur de flux pour WordGrammar est nécessaire :

std::ostream& operator<<(std::ostream& ostr, const WordGrammar& grammar)
{
    ostr << "WordGrammar(" << grammar.word << ", " << grammar.range_from << ", " << grammar.range_to << ")";
    return ostr;
}

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X