简单的解析器和生成器

时间:2012-07-09 13:58:48

标签: c++ parsing tokenize boost-spirit

我需要解析并从c ++对象生成一些文本。

语法为:

command #param #param #param

有一些命令,其中一些没有参数等。 参数主要是数字。

问题是:我应该使用Boost Spirit来完成这项任务吗?或者只是简单地将每行评估函数标记为从字符串比较命令,读取其他参数并从中创建cpp对象?

如果您建议使用Spirit或任何其他解决方案,那么如果您能提供一些与我的问题类似的示例将会很好。我已阅读并尝试了Boost Spirit doc中的所有示例。

2 个答案:

答案 0 :(得分:5)

我在问题“in a previous answer”中或多或少准确地实现了此Using boost::bind with boost::function: retrieve binded variable type

使用Boost Spirit的完整工作示例程序(期望非常相似的语法)在这里: https://gist.github.com/1314900 。您只想删除语法的/execute文字,因此请从

修改Line 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);
}

答案 1 :(得分:1)

对于简单格式化,易于测试的输入,标记化应该足够了 标记化时,您可以从输入中读取一行并将其放入字符串流(iss)中。从iss开始,您读取第一个单词并将其传递给命令工厂,该工厂为您创建正确的命令。然后你可以将iss传递给new命令的readInParameters函数,这样每个命令都可以解析它自己的参数并检查所有参数是否有效。

未经过测试的代码示例:

std::string line;
std::getline(inputStream, line);
std::istringstream iss(line);
std::string strCmd;
iss >> strCmd;
try
{
  std::unique_ptr<Cmd> newCmd = myCmdFactory(strCmd);
  newCmd->readParameters(iss);
  newCmd->execute();
  //...
}
catch (std::exception& e) 
{ 
  std::cout << "Issue with received command: " << e.what() << "\n"; 
}