krz*_*ych 1 c++ parsing tokenize boost-spirit
我需要从c ++对象解析并生成一些文本.
语法是:
command #param #param #param
有一组命令,其中一些没有参数等.参数主要是数字.
问题是:我应该使用Boost Spirit来完成这项任务吗?或者只是简单地将每行评估函数标记为从字符串比较命令,读取其他参数并从中创建cpp对象?
如果你建议使用Spirit或任何其他解决方案,如果你能提供一些与我的问题类似的例子,那将是很好的.我已阅读并尝试了Boost Spirit doc中的所有示例.
我在之前对" 使用boost :: bind with boost :: function:retrieve binded variable type " 这一问题的回答中或多或少地实现了这一点.
使用Boost Spirit 的完整工作示例程序(期望非常相似的语法)在这里:https://gist.github.com/1314900.你只是想删除的/execute文字为你的语法,所以编辑41号线从
    if (!phrase_parse(f,l, "/execute" > (
至
    if (!phrase_parse(f,l, (
示例脚本
WriteLine "bogus"
Write     "here comes the answer: "
Write     42
Write     31415e-4
Write     "that is the inverse of" 24 "and answers nothing"
Shutdown  "Bye" 9
Shutdown  "Test default value for retval"
现在执行后会产生以下输出:
WriteLine('bogus');
Write(string: 'here comes the answer: ');
Write(double: 42);
Write(double: 3.1415);
Write(string: 'that is the inverse of');
Write(double: 24);
Write(string: 'and answers nothing');
Shutdown(reason: 'Bye', retval: 9)
Shutdown(reason: 'Test default value for retval', retval: 0)
出于档案目的:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <fstream>
namespace qi  = boost::spirit::qi;
namespace phx = boost::phoenix;
///////////////////////////////////
// 'domain classes' (scriptables)
struct Echo
{
    void WriteLine(const std::string& s) { std::cout << "WriteLine('"     << s << "');" << std::endl; }
    void WriteStr (const std::string& s) { std::cout << "Write(string: '" << s << "');" << std::endl; }
    void WriteInt (int i)                { std::cout << "Write(int: "     << i <<  ");" << std::endl; }
    void WriteDbl (double d)             { std::cout << "Write(double: "  << d <<  ");" << std::endl; }
    void NewLine  ()                     { std::cout << "NewLine();"                    << std::endl; }
} echoService;
struct Admin
{
    void Shutdown(const std::string& reason, int retval) 
    { 
        std::cout << "Shutdown(reason: '" << reason << "', retval: " << retval << ")" << std::endl;
        // exit(retval);
    }
} adminService;
void execute(const std::string& command)
{
    typedef std::string::const_iterator It;
    It f(command.begin()), l(command.end());
    using namespace qi;
    using phx::bind;
    using phx::ref;
    rule<It, std::string(), space_type> stringlit = lexeme[ '"' >> *~char_('"') >> '"' ];
    try
    {
        if (!phrase_parse(f,l, /*"/execute" >*/ (
                (lit("WriteLine")  
                    > stringlit  [ bind(&Echo::WriteLine, ref(echoService), _1) ])
              | (lit("Write") >> +(
                      double_    [ bind(&Echo::WriteDbl,  ref(echoService), _1) ] // the order matters
                    | int_       [ bind(&Echo::WriteInt,  ref(echoService), _1) ]
                    | stringlit  [ bind(&Echo::WriteStr,  ref(echoService), _1) ]
                ))
              | (lit("NewLine")  [ bind(&Echo::NewLine,   ref(echoService)) ])
              | (lit("Shutdown")  > (stringlit > (int_ | attr(0))) 
                                 [ bind(&Admin::Shutdown, ref(adminService), _1, _2) ])
            ), space))
        {
            if (f!=l) // allow whitespace only lines
                std::cerr << "** (error interpreting command: " << command << ")" << std::endl;
        }
    }
    catch (const expectation_failure<It>& e)
    {
        std::cerr << "** (unexpected input '" << std::string(e.first, std::min(e.first+10, e.last)) << "') " << std::endl;
    }
    if (f!=l)
        std::cerr << "** (warning: skipping unhandled input '" << std::string(f,l) << "')" << std::endl;
}
int main()
{
    std::ifstream ifs("input.txt");
    std::string command;
    while (std::getline(ifs/*std::cin*/, command))
        execute(command);
}
| 归档时间: | 
 | 
| 查看次数: | 379 次 | 
| 最近记录: |