如何从精神解析器输出原始未解析的代码(作为注释)

时间:2012-02-03 23:09:06

标签: c++ boost boost-spirit boost-spirit-qi

给定输入字符串:A = 23; B = 5,我目前得到(预期)输出:

Output: 0xa0000023
Output: 0xa0010005
-------------------------

我希望看到这一点:

Output: 0xa0000023           // A = 23
Output: 0xa0010005           // B = 5
-------------------------

核心代码是:

statement   = eps[_val = 0x50000000] >> identifier[_val += _1<<16] >>
                     "=" >> hex[_val += (_1 & 0x0000FFFF)];

其中identifier是qi :: symbols表查找。

我的其余代码如下所示:

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>

#include <iostream>
#include <iomanip>
#include <ios>
#include <string>
#include <complex>

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

struct reg16_ : qi::symbols<char,unsigned> {
    reg16_() {
        add ("A", 0) ("B", 1) ("C", 2) ("D", 3) ;
    }
} reg16;

template <typename Iterator>
struct dash_script_parser : qi::grammar<Iterator, std::vector<unsigned>(), ascii::space_type> {
    dash_script_parser() : dash_script_parser::base_type(start) {
        using qi::hex;
        using qi::_val;
        using qi::_1;
        using qi::eps;

        identifier %= reg16;

        start      %= (statement % ";" );
        statement   = eps[_val = 0x50000000] >> identifier[_val += _1<<16]>> "=" >> hex[_val += (_1 & 0x0000FFFF)];
    }
    qi::rule<Iterator, std::vector<unsigned>(), ascii::space_type> start;
    qi::rule<Iterator, unsigned(), ascii::space_type> statement;
    qi::rule<Iterator, unsigned()> identifier;
};

int
main()
{
    std::cout << "\t\tA parser for Spirit...\n\n" << "Type [q or Q] to quit\n\n";

    dash_script_parser<std::string::const_iterator> g;
    std::string str;
    while (getline(std::cin, str))

    {
        if (str.empty() || str[0] == 'q' || str[0] == 'Q') break;

        std::string::const_iterator iter = str.begin();
        std::string::const_iterator end = str.end();
        std::vector<unsigned> strs;
        bool r = phrase_parse(iter, end, g, boost::spirit::ascii::space, strs);
        if (r && iter == end) {
            for(std::vector<unsigned>::const_iterator it=strs.begin(); it<strs.end(); ++it)
                std::cout << "Output: 0x" << std::setw(8) << std::setfill('0') << std::hex <<*it << "\n";
        } else
            std::cout << "Parsing failed\n";
    }
    return 0;
}

1 个答案:

答案 0 :(得分:4)

更新更新的回答引起了我的注意(来自Boost Spirit Repository):

这基本上与下面相同,但没有“滥用”语义动作(使其更适合,尤其是自动属性传播。


我的直觉是,首先将语句隔离到原始源迭代器范围可能会更容易,然后单独解析语句。这样,您将在开始时拥有相应的源文本。

解决这个问题,这是我测试的方法,而不会过多地破坏您的示例代码:


1。使属性类型为struct

将原始iter_pos替换为包含源代码段 verbatim 的结构,作为unsigned

string

2。使解析器填充两个字段

好的是,你已经在使用语义动作了,所以它只是建立在那个上面。请注意,结果不是很漂亮,并且会从转换为(融合)仿函数中获益匪浅。但它非常清楚地显示了这项技术:

struct statement_t
{
    unsigned    value;
    std::string source;
};

BOOST_FUSION_ADAPT_STRUCT(statement_t, (unsigned, value)(std::string, source));

3。打印

因此,start %= (statement % ";" ); statement = qi::raw [ raw[eps] [ at_c<0>(_val) = 0x50000000 ] >> identifier [ at_c<0>(_val) += _1<<16 ] >> "=" >> hex [ at_c<0>(_val) += (_1 & 0x0000FFFF) ] ] [ at_c<1>(_val) = construct<std::string>(begin(_1), end(_1)) ] ; 对应at_c<0>(_val)statement::value对应at_c<1>(_val)。这个稍微修改过的输出循环:

statement::source

输出:

for(std::vector<statement_t>::const_iterator it=strs.begin(); it<strs.end(); ++it)
    std::cout << "Output: 0x" << std::setw(8) << std::setfill('0') << std::hex << it->value << " // " << it->source << "\n";

完整样本

Output: 0x50000023 // A = 23
Output: 0x50010005 // B = 5