如何使用具有Boost Spirit的AST中只有一个属性的类?

时间:2011-10-30 19:18:58

标签: boost-spirit boost-spirit-qi

我想使用Boost Spirit将文件解析为AST。

我的AST的根是一个只有一个属性的类:

typedef boost::variant<FunctionDeclaration, GlobalVariableDeclaration> FirstLevelBlock;

struct Program {
    std::vector<FirstLevelBlock> blocks;
};

BOOST_FUSION_ADAPT_STRUCT(
    ::Program,
    (std::vector<eddic::FirstLevelBlock>, blocks)
)

如果我使用单一规则解析:

program %= *(function | globalDeclaration);

它没有编译,但是如果我向Program添加一个字符串名称,它运行良好。我可以使用向量作为根,但我想使用该类,因为我想在Program类中添加一些方法。

编辑:

如果我用括号包围我的程序,它运作良好:

program %= lexer.left_brace >> *(function | globalDeclaration) >> lexer.right_brace;

编译并正常工作,但是:

program %= *(function | globalDeclaration);

无法编译......

Boost Spirit中有什么东西阻止使用这么简单的规则吗?

1 个答案:

答案 0 :(得分:1)

编辑问题version 2

  

如果我用括号包围我的程序,它运行良好[...],但program %= *(function | globalDeclaration);无法编译......

     

Boost Spirit中是否存在阻止使用此类简单规则的内容?

首先,如果没有functionglobalDeclaration的定义,我们就无法说出来。

其次我试过,将我的PoC线改为

static const qi::rule<It, Program(), space_type> program  = *(function | global);
Program d = test("void test(); int abc; int xyz; void last();" , program); 

瞧,我得到你的编译错误了!现在我肯定同意这看起来非常像属性转换错误一样。此外,这是一个初步的解决方法:

program %= eps >> *(function | global);

如你所见,qi::eps救援


<子>

回答原始问题version 1

嗯。我认为你需要发布一个最小的工作样本。这是从你的问题开始的概念证明,它都运行得相当好。

请注意,我使用g++ -std=c++0x编译,以便在Attr函数上获取默认的test参数。 < / p>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/fusion/adapted.hpp>
#include <boost/strong_typedef.hpp>

// added missing bits
namespace eddic 
{
    typedef std::string FunctionDeclaration;
    typedef std::string GlobalVariableDeclaration;

    typedef boost::variant<FunctionDeclaration, GlobalVariableDeclaration> FirstLevelBlock;
}

using namespace eddic;
// end missing bits

struct Program {
    std::vector<FirstLevelBlock> blocks;
};

BOOST_FUSION_ADAPT_STRUCT(
    ::Program,
    (std::vector<eddic::FirstLevelBlock>, blocks)
)

namespace /*anon*/
{    
    using namespace boost::spirit::karma;

    struct dumpvariant : boost::static_visitor<std::ostream&>
    {
        dumpvariant(std::ostream& os) : _os(os) {}
        template <typename T> std::ostream& operator ()(const T& t) const
            { return _os << format(stream, t); }

      private: std::ostream& _os;
    };

    std::ostream& operator<<(std::ostream& os, const FirstLevelBlock& block)
    { 
        os << "variant[" << block.which() << ", ";
        boost::apply_visitor(dumpvariant(os), block);
        return os << "]";
    }

    std::ostream& operator<<(std::ostream& os, const std::vector<FirstLevelBlock>& blocks)
    { return os << format(-(stream % eol), blocks); }

    std::ostream& operator<<(std::ostream& os, const Program& program)
    { return os << "BEGIN\n" << program.blocks << "\nEND"; }
}

namespace qi = boost::spirit::qi;

template <typename Rule, typename Attr = typename Rule::attr_type>
    Attr test(const std::string& input, const Rule& rule)
{
    typedef std::string::const_iterator It;
    It f(input.begin()), l(input.end());

    Attr result;
    try
    {
        bool ok = qi::phrase_parse(f, l, rule, qi::space, result);
        if (!ok)
            std::cerr << " -- ERR: parse failed" << std::endl;
    } catch(qi::expectation_failure<It>& e)
    {
        std::cerr << " -- ERR: expectation failure at '" << std::string(e.first, e.last) << "'" << std::endl;
    }
    if (f!=l)
        std::cerr << " -- WARN: remaing input '" << std::string(f,l) << "'" << std::endl;
    return result;
}

int main()
{
    typedef std::string::const_iterator It;
    static const qi::rule<It, FunctionDeclaration(), space_type>        function = "void " > +~qi::char_("()") > "();";
    static const qi::rule<It, GlobalVariableDeclaration(), space_type>  global   = "int "  > +~qi::char_(";")  > ";";
    static const qi::rule<It, FirstLevelBlock(), space_type>            block    = function | global;
    static const qi::rule<It, Program(), space_type>                    program  = '{' >> *(function | global) >> '}';

    FunctionDeclaration       a = test("void test();", function); 
    std::cout << "FunctionDeclaration a         : " << a << std::endl;

    GlobalVariableDeclaration b = test("int abc;", global); 
    std::cout << "GlobalVariableDeclaration b   : " << b << std::endl;

    FirstLevelBlock c = test("void more();", block); 
    std::cout << "FirstLevelBlock c             : " << c << std::endl;

    /*FirstLevelBlock*/ c = test("int bcd;", block); 
    std::cout << "FirstLevelBlock c             : " << c << std::endl;

    Program d = test("{"
            "void test();"
            "int abc"
            ";"
            "int xyz; void last();"
            "}", program); 
    std::cout << "Program d                     : " << d << std::endl;
}

输出:

FunctionDeclaration a         : test
GlobalVariableDeclaration b   : abc
FirstLevelBlock c             : variant[1, more]
FirstLevelBlock c             : variant[1, bcd]
Program d                     : BEGIN
test
abc
xyz
last
END