簡體   English   中英

如何在boost :: spirit :: qi語法中實現#ifdef?

[英]How to implement #ifdef in a boost::spirit::qi grammar?

是否有一種很好的方法可以使語法非終結符的語法根據某些增強鳳凰功能的結果進行不同的解析?

在我的用例中,我有一個語法,其中包括CPP風格的#define指令和#ifdef #else #endif指令。 (雖然它實際上是C預處理程序,盡管它只是其他人的粗略模仿。)當我用qi解析它時,我將其語法(以ctor形式)傳遞給對“預處理程序數據庫”對象的引用,該對象適用於融合結構,並且我修改了phoenix函數,允許添加PP定義/檢查PP定義。 我這樣做是為了使#define指令具有語義動作,該動作可注冊新的定義。

當我嘗試實現#ifdef #else指令時,我不確定應該怎么做。 我能想到的唯一方法是在我所有語法非終結符的所有屬性類型中添加一個布爾標志,以標記它是否在丟棄的#ifdef分支中,並在解析完AST之后爬網再次,並扔了標記的家伙。 但這很不雅致,必須有更好的方法,對嗎?

如果可能的話,我希望能夠跟蹤原始行號(在解決ifdef之前)。

我希望問題很明確,如果不是這樣,我可以做一個簡單的例子來說明我要做什么,但是我的實際語法很大。

編輯:好的,我准備了一個SSCCE:

因此,這是一個程序,可解析非常簡單的對語法,並具有一些最少的預處理器語言,其中包括define和ifdef。 我了解如何使用語義動作,以使匹配的內容導致C ++回調被觸發,並且該部分似乎正在工作。 但是,我不明白的是如何使用回調將信息反饋到語法中,即“如果此phoenix函數返回false,則以不同的方式進行解析”。 我想知道如何說“如果此菲尼克斯函數作為此語義操作的一部分返回布爾假,然后任意聲明非終結符不匹配並回溯,那將足夠了”。 實際上,現在我正在編寫所有這些內容,我想我知道“微型XML”示例必須以某種方式執行此操作,因為它使用局部變量來強制啟動和關閉標記必須匹配? 所以我想我可以對它的工作方式進行逆向工程。 但是顯然我還沒有通過閱讀文檔/研究示例來弄清楚。

請注意,我認為這與您的第一個建議不同,只是略過語法即可。 問題是我也不知道如何使跳過語法的行為取決於boost phoenix函數的輸出,這又是同樣的問題。 我現在唯一知道的如何處理qi中的鳳凰是,觸發void回調,並使事情分配給屬性值。

#define BOOST_SPIRIT_USE_PHOENIX_V3

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/variant/recursive_variant.hpp>

#include <cassert>
#include <cmath>
#include <memory>
#include <string>
#include <utility>
#include <vector>

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

typedef std::string pp_sym;
typedef std::set<pp_sym> pp_data;

void add(pp_data & defines, const pp_sym & s) { defines.insert(s); }
void remove(pp_data & defines, const pp_sym & s) { defines.erase(s); }
bool search(pp_data & defines, const pp_sym & s) { return defines.count(s); }

BOOST_PHOENIX_ADAPT_FUNCTION(void, pp_add_define_, add, 2);
BOOST_PHOENIX_ADAPT_FUNCTION(void, pp_remove_define_, remove, 2);
BOOST_PHOENIX_ADAPT_FUNCTION(bool, pp_search_define_, search, 2);

typedef std::string Str;
typedef std::pair<Str, Str> Pair;
typedef std::vector<Pair> PairVec;

/***
 * Grammar definitions
 */

template <typename Iterator>
struct simple_grammar : qi::grammar<Iterator, PairVec()> {
    qi::rule<Iterator, PairVec()> main;
    qi::rule<Iterator, Pair()> pair;
    qi::rule<Iterator, Str()> first;
    qi::rule<Iterator, Str()> second;

    qi::rule<Iterator, pp_sym()> pp_symbol;
    qi::rule<Iterator> pp_directive;
    qi::rule<Iterator, pp_sym()> define_directive;
    qi::rule<Iterator, pp_sym()> undef_directive;
    qi::rule<Iterator, pp_sym()> if_directive;
    qi::rule<Iterator> else_directive;
    qi::rule<Iterator> endif_directive;

    qi::rule<Iterator> ws;

    simple_grammar(pp_data & preprocessor_data)
            : simple_grammar::base_type(main)
    {
        using qi::lit;
        using qi::char_;
        using namespace qi::labels;

        ws = char_(" \t\r\n");

        first = !lit('#') >> *(char_ - '=') >> lit('=');
        second = *(char_ - '\n') >> lit('\n');
        pair = first >> second;

        pp_symbol = +char_("A-Za-z_");

        pp_directive = &lit('#')
                >> ((define_directive [ pp_add_define_(ref(preprocessor_data), _1) ] )
                | (undef_directive [ pp_remove_define_(ref(preprocessor_data), _1) ] )
                | if_directive // [ ??? ]
                | else_directive
                | endif_directive)
                >> *(char_ - '\n') >> lit('\n');

        main = (pp_directive >> -main) | (pair >> -main);

        define_directive = lit("#define ") >> pp_symbol >> &ws;
        undef_directive  = lit("#undef ") >> pp_symbol >> &ws;
        if_directive     = lit("#ifdef ") >> pp_symbol >> &ws;
        else_directive   = lit("#else");
        endif_directive  = lit("#endif");
    }
};

const char * example_1 = ""
"#define FOO\n"
"led_zeppelin=9\n"
"the_shins=9\n"
"dead_mau5=6\n"
"portishead=10\n"
"#ifdef FOO\n"
"foo_fighters=7\n"
"#else\n"
"the_who=6\n"
"#endif\n"
"kanye_west=4\n"
"#undef FOO\n"
"#define BAR\n";

int main() {
    std::string temp{example_1};

    typedef std::string::const_iterator str_it;

    typedef simple_grammar<str_it> my_grammar;
    pp_data defines;
    my_grammar gram(defines); // Our grammar
    PairVec ast; // Our tree

    str_it it = temp.begin();
    str_it end = temp.end();

    bool b = qi::parse(it, end, gram, ast);

    assert(b);
    assert(defines.count("FOO") == 0);
    assert(defines.count("BAR") == 1);

    std::cout << "Parsed a list:\n\n";

    for( const auto & p : ast) {
        std::cout << p.first << "\n\t\t\t=\t" << p.second << std::endl;
    }
    return 0;
}

對我來說,上面的輸出是(如預期的那樣):

$ ./main 
Parsed a list:

led_zeppelin
            =   9
the_shins
            =   9
dead_mau5
            =   6
portishead
            =   10
foo_fighters
            =   7
the_who
            =   6
kanye_west
            =   4

但是,我想做的是讓ifdef部分執行您自然希望的操作,並允許嵌套的ifdef子句。

作為對問題中添加的“ SSCCE”代碼的回應:

AST

正確處理嵌套定義的唯一方法(包括條件塊包含#define / #undef指令的情況!)是使用表示塊樹¹的AST:

namespace Common {
    typedef std::string pp_sym;
}

namespace Ast {
    using Common::pp_sym;

    typedef std::string Str;
    typedef std::pair<Str, Str> Pair;
    typedef std::vector<Pair> Pairs;

    struct ConditionalBlock;

    namespace tag {
        struct define;
        struct undefine;
    }

    template <typename Tag> struct Directive {
        pp_sym name;
    };

    typedef Directive<tag::define> Define; 
    typedef Directive<tag::undefine> Undef; 

    typedef boost::make_recursive_variant<
                Pairs,
                boost::recursive_wrapper<ConditionalBlock>,
                Define,
                Undef
            >::type Block;

    typedef std::vector<Block> Blocks;

    struct ConditionalBlock {
        pp_sym required;
        Blocks if_, else_;
    };
}

為了便於在不使用語義動作的情況下進行解析:

BOOST_FUSION_ADAPT_TPL_STRUCT((Tag), (Ast::Directive)(Tag), name)
BOOST_FUSION_ADAPT_STRUCT(Ast::ConditionalBlock, required, if_, else_)

做完了

解析中

由於上述工作,我們現在可以完全按照需要定義解析器了!

筆記:

  • 現在使用船長來避免需要一定數量的硬編碼的空格或不容忍的空格
  • 現在使用seek[eol]忽略直到一行結束
  • 現在使用了distinct來解析標識符(請參見boost :: spirit :: qi關鍵字和標識符
  • 現在使#else的外觀為可選(請參閱-else
  • 刪除所有語義動作
  • 無需任何其他工作即可啟用調試信息

     start = skip(blank) [ blocks ]; blocks = *block; block = define | undef | conditional_block | +pair; pair = !char_("#") >> +~char_("=\\r\\n") >> '=' >> *(char_ - eol) >> *eol; pp_symbol = qr::distinct(char_("A-Za-z_")) [ +char_("A-Za-z_") ]; define = '#' >> distinct(alnum | '_') [ "define" ] >> pp_symbol >> seek[*eol]; undef = '#' >> distinct(alnum | '_') [ "undef" ] >> pp_symbol >> seek[*eol]; else_ = '#' >> distinct(alnum | '_') [ "else" ] >> seek[*eol]; endif = '#' >> distinct(alnum | '_') [ "endif" ] >> seek[*eol]; conditional_block = ('#' >> distinct(alnum | '_') [ "ifdef" ] >> pp_symbol >> seek[*eol]) >> *(!(else_|endif) >> block) >> -else_ >> *(!endif >> block) >> endif ; BOOST_SPIRIT_DEBUG_NODES((start)(blocks)(block)(pair)(pp_symbol)(define)(undef)(else_)(endif)(conditional_block)) 

我想說的很清楚,它會導致ast包含您以后想要使用的所有信息

處理邏輯

現在我們已經將處理與解析分離了,處理就是對樹的單次訪問。 我們使用單個函數對象Logic::Preprocessor兼作變體訪問者:

Logic::Preprocess pp({{"EXTERNAL"}} , "    ");
pp(ast);

在此示例中,我們從定義了預處理程序符號EXTERNAL開始(就像在命令行上一樣,它是“外部”定義的)。

訪問者的實現非常簡單,但是讓我展示一下操作位,即采取條件而忽略分支的位置。 為了使事情變得非常完整的我甚至遍歷不滿意 ,只是為了顯示完整的AST有分支機構,但與EN isolated的函數對象的實例,這樣是沒有效果的:

    void operator()(Ast::ConditionalBlock const& cb) const {
        bool const satisfied = ctx.defined.count(cb.required);

        auto old_indent = indent;
        indent += "\t";
        std::cout << old_indent << "#ifdef " << cb.required << " // " << std::boolalpha << satisfied << "\n";

        Preprocess isolated{ctx, indent+"// "}; // prevent changes to ctx to affect us for the non-matching branch

        (satisfied? *this : isolated)(cb.if_);
        std::cout << old_indent << "#else " << " // ifdef " << cb.required << "\n";
        (satisfied? isolated : *this)(cb.else_);

        std::cout << old_indent << "#endif " << " // ifdef " << cb.required << "\n";
        indent.resize(indent.size()-1);
    }
    void operator()(Ast::Define const& directive) const {
        ctx.defined.insert(directive.name);

        std::cout << indent << "#define\t" << directive.name;
        report();
    }
    void operator()(Ast::Undef const& directive) const {
        ctx.defined.erase(directive.name);

        std::cout << indent << "#undef\t" << directive.name;
        report();
    }

演示版

觀察如何正確解釋該文檔,該文檔甚至嵌套條件塊並從條件分支內(因此,有條件地)定義符號:

#define FOO
led_zeppelin=9
the_shins=9
dead_mau5=6
portishead=10
#ifdef FOO
foo_fighters=7
#define ZOO
#else
the_who=6
#define QUX
#endif

#ifdef EXTERNAL

#ifdef ZOO
zoowasdefined=yes
#else
zoowasdefined=no
#endif

#ifdef QUX
quxwasdefined=yes
#else
quxwasdefined=no
#endif
#endif

kanye_west=4
#undef FOO
#define BAR

我們的演示程序打印: Live On Coliru

Preprocess results:

    #define FOO // effective: EXTERNAL FOO 
    led_zeppelin=9
    the_shins=9
    dead_mau5=6
    portishead=10
    #ifdef FOO // true
        foo_fighters=7
        #define ZOO // effective: EXTERNAL FOO ZOO 
    #else  // ifdef FOO
        // the_who=6
        // #define  QUX // effective: EXTERNAL FOO QUX 
    #endif  // ifdef FOO
    #ifdef EXTERNAL // true
        #ifdef ZOO // true
            zoowasdefined=yes
        #else  // ifdef ZOO
            // zoowasdefined=no
        #endif  // ifdef ZOO
        #ifdef QUX // false
            // quxwasdefined=yes
        #else  // ifdef QUX
            quxwasdefined=no
        #endif  // ifdef QUX
    #else  // ifdef EXTERNAL
    #endif  // ifdef EXTERNAL
    kanye_west=4
    #undef  FOO // effective: EXTERNAL ZOO 
    #define BAR // effective: BAR EXTERNAL ZOO 


Defines still in effect: BAR EXTERNAL ZOO 

完整清單

生活在Coliru

#define BOOST_SPIRIT_USE_PHOENIX_V3
//#define BOOST_SPIRIT_DEBUG

#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/repository/include/qi_distinct.hpp>
#include <boost/spirit/repository/include/qi_seek.hpp>
#include <boost/variant.hpp>

#include <cassert>

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

namespace Common {
    typedef std::string pp_sym;
}

namespace Ast {
    using Common::pp_sym;

    typedef std::string Str;
    typedef std::pair<Str, Str> Pair;
    typedef std::vector<Pair> Pairs;

    struct ConditionalBlock;

    namespace tag {
        struct define;
        struct undefine;
    }

    template <typename Tag> struct Directive {
        pp_sym name;
    };

    typedef Directive<tag::define> Define; 
    typedef Directive<tag::undefine> Undef; 

    typedef boost::make_recursive_variant<
                Pairs,
                boost::recursive_wrapper<ConditionalBlock>,
                Define,
                Undef
            >::type Block;

    typedef std::vector<Block> Blocks;

    struct ConditionalBlock {
        pp_sym required;
        Blocks if_, else_;
    };
}

BOOST_FUSION_ADAPT_TPL_STRUCT((Tag), (Ast::Directive)(Tag), name)
BOOST_FUSION_ADAPT_STRUCT(Ast::ConditionalBlock, required, if_, else_)

/***
 * Grammar definitions
 */

template <typename Iterator>
struct simple_grammar : qi::grammar<Iterator, Ast::Blocks()> {

    simple_grammar() : simple_grammar::base_type(start)
    {
        using namespace qi;
        using qr::distinct;
        using qr::seek;

        start   = skip(blank) [ blocks ];
        blocks  = *block;
        block   = define | undef | conditional_block | +pair; 

        pair      = +~char_("=\r\n") >> '=' >> *(char_ - eol) >> *eol;
        pp_symbol = qr::distinct(char_("A-Za-z_")) [ +char_("A-Za-z_") ];

        define = '#' >> distinct(alnum | '_') [ "define" ] >> pp_symbol >> seek[*eol];
        undef  = '#' >> distinct(alnum | '_') [ "undef"  ] >> pp_symbol >> seek[*eol];

        else_  = '#' >> distinct(alnum | '_') [ "else"   ] >> seek[*eol];
        endif  = '#' >> distinct(alnum | '_') [ "endif"  ] >> seek[*eol];

        conditional_block = 
            ('#' >> distinct(alnum | '_') [ "ifdef" ] >> pp_symbol >> seek[*eol])
            >> *(!(else_|endif) >> block) 
            >> -else_
            >> *(!endif >> block)
            >> endif
            ;

        BOOST_SPIRIT_DEBUG_NODES((start)(blocks)(block)(pair)(pp_symbol)(define)(undef)(else_)(endif)(conditional_block))
    }

private:
    using Skipper = qi::blank_type;

    qi::rule<Iterator, Ast::Blocks()> start;

    qi::rule<Iterator, Ast::Blocks(), Skipper> blocks;
    qi::rule<Iterator, Ast::Block(),  Skipper> block;

    // directive
    qi::rule<Iterator, Ast::ConditionalBlock(), Skipper> conditional_block;
    qi::rule<Iterator, Ast::Define(),           Skipper> define;
    qi::rule<Iterator, Ast::Undef(),            Skipper> undef;
    // empty directives
    qi::rule<Iterator, Skipper> else_, endif;

    // lexeme
    qi::rule<Iterator, Ast::Pair()>   pair;
    qi::rule<Iterator, Ast::pp_sym()> pp_symbol;
};

namespace Logic {
    using Common::pp_sym;

    typedef std::set<pp_sym> pp_syms;

    struct context {
        pp_syms defined;
    };

    struct Preprocess : boost::static_visitor<void> {
        context ctx;
        std::string indent;

        Preprocess(context ctx = {}, std::string indent = "") 
            : ctx(std::move(ctx)), indent(std::move(indent))
        { }

        void operator()(Ast::Blocks const& blocks) {
            for (auto& b : blocks)
                boost::apply_visitor(*this, b);
        }
        void operator()(Ast::Block const& block) {
            boost::apply_visitor(*this, block);
        }
        void operator()(Ast::Pairs const& pairs) {
            for (auto& p : pairs)
                std::cout << indent << p.first << "=" << p.second << "\n";
        }
        void operator()(Ast::ConditionalBlock const& cb) {
            bool const satisfied = ctx.defined.count(cb.required);

            auto old_indent = indent;
            indent += "\t";
            std::cout << old_indent << "#ifdef " << cb.required << " // " << std::boolalpha << satisfied << "\n";

            Preprocess isolated{ctx, indent+"// "}; // prevent changes to ctx to affect us for the non-matching branch

            (satisfied? *this : isolated)(cb.if_);
            std::cout << old_indent << "#else " << " // ifdef " << cb.required << "\n";
            (satisfied? isolated : *this)(cb.else_);

            std::cout << old_indent << "#endif " << " // ifdef " << cb.required << "\n";
            indent.resize(indent.size()-1);
        }
        void operator()(Ast::Define const& directive) {
            ctx.defined.insert(directive.name);

            std::cout << indent << "#define\t" << directive.name;
            report();
        }
        void operator()(Ast::Undef const& directive) {
            ctx.defined.erase(directive.name);

            std::cout << indent << "#undef\t" << directive.name;
            report();
        }

      private:
        void report() const {
            std::cout << "\t// effective: ";
            for (auto& sym : ctx.defined) std::cout << sym << " ";
            std::cout << "\n";
        }
    };

}

int main() {
    typedef boost::spirit::istream_iterator It;

    typedef simple_grammar<It> my_grammar;

    my_grammar gram; // Our grammar
    Ast::Blocks ast; // Our tree

    It it(std::cin >> std::noskipws), end;

    bool b = qi::parse(it, end, gram, ast);

    if (it != end)
        std::cout << "Remaining input: '" << std::string(it, end) << "'\n";

    assert(b);

    std::cout << "Preprocess results:\n\n";

    Logic::Preprocess pp({{"EXTERNAL"}} , "    ");
    pp(ast);

    std::cout << "\n\nDefines still in effect: ";
    for (auto& sym : pp.ctx.defined) std::cout << sym << " ";
}

獎勵:調試信息

啟用調試信息除了上面的輸出以外,還會產生以下詳細的跟蹤信息:

<start>
  <try>#define FOO\nled_zepp</try>
  <blocks>
    <try>#define FOO\nled_zepp</try>
    <block>
      <try>#define FOO\nled_zepp</try>
      <define>
        <try>#define FOO\nled_zepp</try>
        <pp_symbol>
          <try>FOO\nled_zeppelin=9\nt</try>
          <success>\nled_zeppelin=9\nthe_</success>
          <attributes>[[F, O, O]]</attributes>
        </pp_symbol>
        <success>led_zeppelin=9\nthe_s</success>
        <attributes>[[[F, O, O]]]</attributes>
      </define>
      <success>led_zeppelin=9\nthe_s</success>
      <attributes>[[[F, O, O]]]</attributes>
    </block>
    <block>
      <try>led_zeppelin=9\nthe_s</try>
      <define>
        <try>led_zeppelin=9\nthe_s</try>
        <fail/>
      </define>
      <undef>
        <try>led_zeppelin=9\nthe_s</try>
        <fail/>
      </undef>
      <conditional_block>
        <try>led_zeppelin=9\nthe_s</try>
        <fail/>
      </conditional_block>
      <pair>
        <try>led_zeppelin=9\nthe_s</try>
        <success>the_shins=9\ndead_mau</success>
        <attributes>[[[l, e, d, _, z, e, p, p, e, l, i, n], [9]]]</attributes>
      </pair>
      <pair>
        <try>the_shins=9\ndead_mau</try>
        <success>dead_mau5=6\nportishe</success>
        <attributes>[[[t, h, e, _, s, h, i, n, s], [9]]]</attributes>
      </pair>
      <pair>
        <try>dead_mau5=6\nportishe</try>
        <success>portishead=10\n#ifdef</success>
        <attributes>[[[d, e, a, d, _, m, a, u, 5], [6]]]</attributes>
      </pair>
      <pair>
        <try>portishead=10\n#ifdef</try>
        <success>#ifdef FOO\nfoo_fight</success>
        <attributes>[[[p, o, r, t, i, s, h, e, a, d], [1, 0]]]</attributes>
      </pair>
      <pair>
        <try>#ifdef FOO\nfoo_fight</try>
        <fail/>
      </pair>
      <success>#ifdef FOO\nfoo_fight</success>
      <attributes>[[[[l, e, d, _, z, e, p, p, e, l, i, n], [9]], [[t, h, e, _, s, h, i, n, s], [9]], [[d, e, a, d, _, m, a, u, 5], [6]], [[p, o, r, t, i, s, h, e, a, d], [1, 0]]]]</attributes>
    </block>
    <block>
      <try>#ifdef FOO\nfoo_fight</try>
      <define>
        <try>#ifdef FOO\nfoo_fight</try>
        <fail/>
      </define>
      <undef>
        <try>#ifdef FOO\nfoo_fight</try>
        <fail/>
      </undef>
      <conditional_block>
        <try>#ifdef FOO\nfoo_fight</try>
        <pp_symbol>
          <try>FOO\nfoo_fighters=7\n#</try>
          <success>\nfoo_fighters=7\n#def</success>
          <attributes>[[F, O, O]]</attributes>
        </pp_symbol>
        <else_>
          <try>foo_fighters=7\n#defi</try>
          <fail/>
        </else_>
        <endif>
          <try>foo_fighters=7\n#defi</try>
          <fail/>
        </endif>
        <block>
          <try>foo_fighters=7\n#defi</try>
          <define>
            <try>foo_fighters=7\n#defi</try>
            <fail/>
          </define>
          <undef>
            <try>foo_fighters=7\n#defi</try>
            <fail/>
          </undef>
          <conditional_block>
            <try>foo_fighters=7\n#defi</try>
            <fail/>
          </conditional_block>
          <pair>
            <try>foo_fighters=7\n#defi</try>
            <success>#define ZOO\n#else\nth</success>
            <attributes>[[[f, o, o, _, f, i, g, h, t, e, r, s], [7]]]</attributes>
          </pair>
          <pair>
            <try>#define ZOO\n#else\nth</try>
            <fail/>
          </pair>
          <success>#define ZOO\n#else\nth</success>
          <attributes>[[[[f, o, o, _, f, i, g, h, t, e, r, s], [7]]]]</attributes>
        </block>
        <else_>
          <try>#define ZOO\n#else\nth</try>
          <fail/>
        </else_>
        <endif>
          <try>#define ZOO\n#else\nth</try>
          <fail/>
        </endif>
        <block>
          <try>#define ZOO\n#else\nth</try>
          <define>
            <try>#define ZOO\n#else\nth</try>
            <pp_symbol>
              <try>ZOO\n#else\nthe_who=6\n</try>
              <success>\n#else\nthe_who=6\n#de</success>
              <attributes>[[Z, O, O]]</attributes>
            </pp_symbol>
            <success>#else\nthe_who=6\n#def</success>
            <attributes>[[[Z, O, O]]]</attributes>
          </define>
          <success>#else\nthe_who=6\n#def</success>
          <attributes>[[[Z, O, O]]]</attributes>
        </block>
        <else_>
          <try>#else\nthe_who=6\n#def</try>
          <success>the_who=6\n#define QU</success>
          <attributes>[]</attributes>
        </else_>
        <else_>
          <try>#else\nthe_who=6\n#def</try>
          <success>the_who=6\n#define QU</success>
          <attributes>[]</attributes>
        </else_>
        <endif>
          <try>the_who=6\n#define QU</try>
          <fail/>
        </endif>
        <block>
          <try>the_who=6\n#define QU</try>
          <define>
            <try>the_who=6\n#define QU</try>
            <fail/>
          </define>
          <undef>
            <try>the_who=6\n#define QU</try>
            <fail/>
          </undef>
          <conditional_block>
            <try>the_who=6\n#define QU</try>
            <fail/>
          </conditional_block>
          <pair>
            <try>the_who=6\n#define QU</try>
            <success>#define QUX\n#endif\n\n</success>
            <attributes>[[[t, h, e, _, w, h, o], [6]]]</attributes>
          </pair>
          <pair>
            <try>#define QUX\n#endif\n\n</try>
            <fail/>
          </pair>
          <success>#define QUX\n#endif\n\n</success>
          <attributes>[[[[t, h, e, _, w, h, o], [6]]]]</attributes>
        </block>
        <endif>
          <try>#define QUX\n#endif\n\n</try>
          <fail/>
        </endif>
        <block>
          <try>#define QUX\n#endif\n\n</try>
          <define>
            <try>#define QUX\n#endif\n\n</try>
            <pp_symbol>
              <try>QUX\n#endif\n\n#ifdef E</try>
              <success>\n#endif\n\n#ifdef EXTE</success>
              <attributes>[[Q, U, X]]</attributes>
            </pp_symbol>
            <success>#endif\n\n#ifdef EXTER</success>
            <attributes>[[[Q, U, X]]]</attributes>
          </define>
          <success>#endif\n\n#ifdef EXTER</success>
          <attributes>[[[Q, U, X]]]</attributes>
        </block>
        <endif>
          <try>#endif\n\n#ifdef EXTER</try>
          <success>#ifdef EXTERNAL\n\n#if</success>
          <attributes>[]</attributes>
        </endif>
        <endif>
          <try>#endif\n\n#ifdef EXTER</try>
          <success>#ifdef EXTERNAL\n\n#if</success>
          <attributes>[]</attributes>
        </endif>
        <success>#ifdef EXTERNAL\n\n#if</success>
        <attributes>[[[F, O, O], [[[[f, o, o, _, f, i, g, h, t, e, r, s], [7]]], [[Z, O, O]]], [[[[t, h, e, _, w, h, o], [6]]], [[Q, U, X]]]]]</attributes>
      </conditional_block>
      <success>#ifdef EXTERNAL\n\n#if</success>
      <attributes>[[[F, O, O], [[[[f, o, o, _, f, i, g, h, t, e, r, s], [7]]], [[Z, O, O]]], [[[[t, h, e, _, w, h, o], [6]]], [[Q, U, X]]]]]</attributes>
    </block>
    <block>
      <try>#ifdef EXTERNAL\n\n#if</try>
      <define>
        <try>#ifdef EXTERNAL\n\n#if</try>
        <fail/>
      </define>
      <undef>
        <try>#ifdef EXTERNAL\n\n#if</try>
        <fail/>
      </undef>
      <conditional_block>
        <try>#ifdef EXTERNAL\n\n#if</try>
        <pp_symbol>
          <try>EXTERNAL\n\n#ifdef ZOO</try>
          <success>\n\n#ifdef ZOO\nzoowasd</success>
          <attributes>[[E, X, T, E, R, N, A, L]]</attributes>
        </pp_symbol>
        <else_>
          <try>#ifdef ZOO\nzoowasdef</try>
          <fail/>
        </else_>
        <endif>
          <try>#ifdef ZOO\nzoowasdef</try>
          <fail/>
        </endif>
        <block>
          <try>#ifdef ZOO\nzoowasdef</try>
          <define>
            <try>#ifdef ZOO\nzoowasdef</try>
            <fail/>
          </define>
          <undef>
            <try>#ifdef ZOO\nzoowasdef</try>
            <fail/>
          </undef>
          <conditional_block>
            <try>#ifdef ZOO\nzoowasdef</try>
            <pp_symbol>
              <try>ZOO\nzoowasdefined=ye</try>
              <success>\nzoowasdefined=yes\n#</success>
              <attributes>[[Z, O, O]]</attributes>
            </pp_symbol>
            <else_>
              <try>zoowasdefined=yes\n#e</try>
              <fail/>
            </else_>
            <endif>
              <try>zoowasdefined=yes\n#e</try>
              <fail/>
            </endif>
            <block>
              <try>zoowasdefined=yes\n#e</try>
              <define>
                <try>zoowasdefined=yes\n#e</try>
                <fail/>
              </define>
              <undef>
                <try>zoowasdefined=yes\n#e</try>
                <fail/>
              </undef>
              <conditional_block>
                <try>zoowasdefined=yes\n#e</try>
                <fail/>
              </conditional_block>
              <pair>
                <try>zoowasdefined=yes\n#e</try>
                <success>#else\nzoowasdefined=</success>
                <attributes>[[[z, o, o, w, a, s, d, e, f, i, n, e, d], [y, e, s]]]</attributes>
              </pair>
              <pair>
                <try>#else\nzoowasdefined=</try>
                <fail/>
              </pair>
              <success>#else\nzoowasdefined=</success>
              <attributes>[[[[z, o, o, w, a, s, d, e, f, i, n, e, d], [y, e, s]]]]</attributes>
            </block>
            <else_>
              <try>#else\nzoowasdefined=</try>
              <success>zoowasdefined=no\n#en</success>
              <attributes>[]</attributes>
            </else_>
            <else_>
              <try>#else\nzoowasdefined=</try>
              <success>zoowasdefined=no\n#en</success>
              <attributes>[]</attributes>
            </else_>
            <endif>
              <try>zoowasdefined=no\n#en</try>
              <fail/>
            </endif>
            <block>
              <try>zoowasdefined=no\n#en</try>
              <define>
                <try>zoowasdefined=no\n#en</try>
                <fail/>
              </define>
              <undef>
                <try>zoowasdefined=no\n#en</try>
                <fail/>
              </undef>
              <conditional_block>
                <try>zoowasdefined=no\n#en</try>
                <fail/>
              </conditional_block>
              <pair>
                <try>zoowasdefined=no\n#en</try>
                <success>#endif\n\n#ifdef QUX\nq</success>
                <attributes>[[[z, o, o, w, a, s, d, e, f, i, n, e, d], [n, o]]]</attributes>
              </pair>
              <pair>
                <try>#endif\n\n#ifdef QUX\nq</try>
                <fail/>
              </pair>
              <success>#endif\n\n#ifdef QUX\nq</success>
              <attributes>[[[[z, o, o, w, a, s, d, e, f, i, n, e, d], [n, o]]]]</attributes>
            </block>
            <endif>
              <try>#endif\n\n#ifdef QUX\nq</try>
              <success>#ifdef QUX\nquxwasdef</success>
              <attributes>[]</attributes>
            </endif>
            <endif>
              <try>#endif\n\n#ifdef QUX\nq</try>
              <success>#ifdef QUX\nquxwasdef</success>
              <attributes>[]</attributes>
            </endif>
            <success>#ifdef QUX\nquxwasdef</success>
            <attributes>[[[Z, O, O], [[[[z, o, o, w, a, s, d, e, f, i, n, e, d], [y, e, s]]]], [[[[z, o, o, w, a, s, d, e, f, i, n, e, d], [n, o]]]]]]</attributes>
          </conditional_block>
          <success>#ifdef QUX\nquxwasdef</success>
          <attributes>[[[Z, O, O], [[[[z, o, o, w, a, s, d, e, f, i, n, e, d], [y, e, s]]]], [[[[z, o, o, w, a, s, d, e, f, i, n, e, d], [n, o]]]]]]</attributes>
        </block>

....
</start>

¹否則您在解析時就應該有一個相當復雜的樹來進行匹配。 如有疑問, 請將解析與處理分開 這與Boost Spirit緊密相關:“語義行為是邪惡的”?

只需定義一個語法並實現匹配的規則即可。

您要做什么取決於您要對結果做什么。 如果目標是忽略該塊,則只需將語法添加到船長(例如'#ifdef' >> spirit::repository::qi::seek[ qi::eol >> "#endif" >> qi::eol ]或類似的東西)

考慮使用Boost Wave,它是用Spirit編寫的成熟的預處理器,已經與Boost一起提供。

通過閱讀精神文檔,我認為解決基本問題的正確方法(引用自己)

是否有一種很好的方法可以使語法非終結符的語法根據某些增強鳳凰功能的結果進行不同的解析?

要使用boost::spirit::qi::eps 從文檔( http://www.boost.org/doc/libs/1_41_0/libs/spirit/doc/html/spirit/qi/reference/auxiliary/eps.html ):

語義謂詞

語義謂詞使您可以在語法中的任何位置附加條件函數。 在此角色下,epsilon會采用Lazy Argument(惰性參數),該參數返回true或false。 惰性參數通常是一種測試,用於解決語法中的歧義。 當惰性參數結果評估為false時,將報告解析失敗。 否則將報告為空匹配。 通用形式為:

eps(f) >> rest;

調用惰性參數f進行語義測試(例如,檢查符號表中是否有符號)。 如果測試返回true,則將評估其余部分。 否則,生產將以無與倫比的方式提早返回,而不會碰到任何休息。

嘗試使用此技術擴展SSCCE並很快編輯此答案...


好的,這就是我最后得到的。 我認為它仍然會有一些不足之處,例如它不能完全正確地處理嵌套的ifdef,並且我的語法有一些代碼重復。 我認為簡短的答案是,即使語法非常簡單,也不要嘗試在任何中等復雜的語法中實現ifdef,您應該始終進行某種兩階段處理,即使語法非常簡單,也可能會產生很多問題。 但是無論如何,我認為這是使用精神的很好的練習。

#define BOOST_SPIRIT_USE_PHOENIX_V3

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/variant/recursive_variant.hpp>

#include <cassert>
#include <cmath>
#include <memory>
#include <string>
#include <utility>
#include <vector>

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

typedef std::string pp_sym;
typedef std::set<pp_sym> pp_data;

void add(pp_data & defines, const pp_sym & s) { /*std::cout << "Parser: #define " << s << std::endl;*/ defines.insert(s); }
void remove(pp_data & defines, const pp_sym & s) { /*std::cout << "Parser: #undef " << s << std::endl;*/ defines.erase(s); }
bool search(pp_data & defines, const pp_sym & s) { /*std::cout << "Parser: #ifdef " << s << std::endl;*/ return defines.count(s); }

BOOST_PHOENIX_ADAPT_FUNCTION(void, pp_add_define_, add, 2);
BOOST_PHOENIX_ADAPT_FUNCTION(void, pp_remove_define_, remove, 2);
BOOST_PHOENIX_ADAPT_FUNCTION(bool, pp_search_define_, search, 2);

typedef std::string Str;
typedef std::pair<Str, Str> Pair;
typedef std::vector<Pair> PairVec;

/***
 * Grammar definitions
 */

template <typename Iterator>
struct simple_grammar : qi::grammar<Iterator, PairVec()> {
    qi::rule<Iterator, PairVec()> main;
    qi::rule<Iterator, PairVec(), qi::locals<std::string>> if_block;
    qi::rule<Iterator, PairVec()> if_true_block;
    qi::rule<Iterator, PairVec()> if_false_block;
    qi::rule<Iterator, Pair()> pair;
    qi::rule<Iterator, Str()> first;
    qi::rule<Iterator, Str()> second;

    qi::rule<Iterator, pp_sym()> pp_symbol;
    qi::rule<Iterator> pp_directive;
    qi::rule<Iterator, pp_sym()> define_directive;
    qi::rule<Iterator, pp_sym()> undef_directive;
    qi::rule<Iterator, pp_sym()> if_directive;
    qi::rule<Iterator> else_directive;
    qi::rule<Iterator> endif_directive;

    qi::rule<Iterator> ws;
    qi::rule<Iterator> skip_to_eol;

    simple_grammar(pp_data & preprocessor_data)
            : simple_grammar::base_type(main)
    {
        using qi::lit;
        using qi::char_;
        using qi::omit;
        using qi::eps;
        using namespace qi::labels;

        ws = char_(" \t\r\n");

        first = !lit('#') >> *(char_ - '=') >> lit('=');
        second = *(char_ - '\n') >> lit('\n');
        pair = first >> second;

        pp_symbol = +char_("A-Za-z_");

        skip_to_eol = *(char_ - '\n') >> lit('\n');

        pp_directive = &lit('#')
                >> ((define_directive [ pp_add_define_(ref(preprocessor_data), _1) ] )
                | (undef_directive [ pp_remove_define_(ref(preprocessor_data), _1) ] )
                | else_directive
                | endif_directive)
                >> skip_to_eol;

        main = (if_block >> -main) | (pp_directive >> -main) | (pair >> -main);

        define_directive = lit("#define ") >> pp_symbol >> &ws;
        undef_directive  = lit("#undef ") >> pp_symbol >> &ws;
        if_directive     = lit("#ifdef ") >> pp_symbol >> &ws;
        else_directive   = lit("#else");
        endif_directive  = lit("#endif");


        if_block = omit[if_directive[_a = _1] ] >> skip_to_eol
                    >> ((eps( pp_search_define_(ref(preprocessor_data), _a) ) > if_true_block ) | if_false_block)
                    >> endif_directive >> skip_to_eol;
        if_false_block = omit[ *(char_ - else_directive - endif_directive) ] >> -(else_directive >> skip_to_eol >> if_true_block);
        if_true_block = !endif_directive >> 
                ( (else_directive >> skip_to_eol >> if_false_block) 
                | (if_block >> -if_true_block)
                | (pp_directive >> -if_true_block)
                | (pair >> -if_true_block)); 
    }
};

#define CHECK(C) \
do { \
    if (!(C)) { \
        std::cout << "Check \"" << #C << "\" failed!" << std::endl; \
    } \
} while(0)

#define CHECK_ITS(STR, IT, END) \
do { \
    if (IT != END) { \
        std::cout << "Failed to fully parse \"" << STR << "\"\n"; \
        std::cout << "Stopped at \"" << std::string(IT, END) << "\"" << std::endl; \
    } \
} while(0)

typedef std::string::const_iterator str_it;
typedef simple_grammar<str_it> my_grammar;

void unit_test() {
    std::cout << " --- unit tests ---" << std::endl;

    pp_data defines;
    my_grammar gram(defines); // Our grammar

    {
        std::cout << "test 1\n";

        std::string temp = "#define ZED\n";
        str_it it = temp.begin();
        str_it end = temp.end();

        std::string ast;
        bool check1 = qi::parse(it, end, gram.define_directive >> gram.skip_to_eol, ast);
        CHECK(check1);
        CHECK_ITS(temp, it, end);
        CHECK(ast == "ZED");
    }

    {
        std::cout << "test 2\n";

        std::string temp = "#define ZED\n";
        str_it it = temp.begin();
        str_it end = temp.end();

        bool check1 = qi::parse(it, end, gram.pp_directive);
        CHECK(check1);
        CHECK_ITS(temp, it, end);
        CHECK(defines.count("ZED") == 1);
    }

    {
        std::cout << "test 3\n";

        std::string temp = "#undef ZED\n";
        str_it it = temp.begin();
        str_it end = temp.end();

        bool check1 = qi::parse(it, end, gram.pp_directive);
        CHECK(check1);
        CHECK_ITS(temp, it, end);
        CHECK(defines.count("ZED") == 0);
    }

    std::cout << " --- end unit tests ---" << std::endl;
}

std::ostream & operator << (std::ostream & ss, const PairVec & pv) {
    ss << "Parsed a list:\n\n";

    for( const auto & p : pv) {
        ss << p.first << "\n\t\t\t=\t" << p.second << std::endl;
    }
    return ss;
}

PairVec test_case(pp_data & defines, int & result, const std::string & temp) {
    my_grammar gram(defines); // Our grammar
    PairVec ast; // Our tree

    str_it it = temp.begin();
    str_it end = temp.end();

    bool parse_successful = qi::parse(it, end, gram, ast);
    CHECK(parse_successful);
    CHECK_ITS(temp, it, end);

    std::cout << ast;

    result |= parse_successful ? 0 : 1;
    return ast;
}

bool have_name(const PairVec & pv, const Str & name) {
    return pv.end() != std::find_if(pv.begin(), pv.end(), [&](const Pair & p) { return p.first == name; });
}

int main() {
    unit_test();

    int result = 0;
    {
        std::cout << "Test case 1" << std::endl;
        pp_data defines;
        PairVec ast = test_case(defines, result, ""
"#define FOO\n"
"led_zeppelin=9\n"
"the_shins=9\n"
"dead_mau5=6\n"
"portishead=10\n"
"#ifdef FOO\n"
"foo_fighters=7\n"
"#else\n"
"the_who=6\n"
"#endif\n"
"kanye_west=4\n"
"#undef FOO\n"
"#define BAR\n");

        CHECK(defines.count("FOO") == 0);
        CHECK(defines.count("BAR") == 1);
        if (!have_name (ast, "foo_fighters")) { std::cout << "error no foo" << std::endl;}
    }

    {
        std::cout << "Test case 2" << std::endl;
        pp_data defines;
        PairVec ast = test_case(defines, result, ""
"#define WOO\n"
"led_zeppelin=9\n"
"the_shins=9\n"
"dead_mau5=6\n"
"portishead=10\n"
"#ifdef FOO\n"
"foo_fighters=7\n"
"#else\n"
"the_who=6\n"
"#endif\n"
"kanye_west=4\n"
"#undef FOO\n"
"#define BAR\n"
"#define ZED\n");

        CHECK(defines.count("FOO") == 0);
        CHECK(defines.count("BAR") == 1);
        CHECK(defines.count("WOO") == 1);
        CHECK(defines.count("ZED") == 1);
        CHECK(defines.count("GOO") == 0);
        CHECK(!have_name(ast, "foo_fighters"));
        CHECK(have_name(ast, "the_who"));
    }

    return result;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM