简体   繁体   English

boost :: spirit:多维输入的迭代器和解析

[英]boost::spirit: iterator and parsing for multidimensional input

The input for the parser is similar to this example: 解析器的输入类似于以下示例:

struct Word{
    Word(std::string txt, int val)
    :text(txt)
    ,value(val)
    {}

    std::string text;
    int value;
};

int main()
{
    std::vector<Word> input;

    input.push_back(Word("This", 10));
    input.push_back(Word("is", 73));
    input.push_back(Word("the", 5));
    input.push_back(Word("input", 32));
}

The grammar for the parser is written for to the text variable of the Words and can look like this: 解析器的语法是为Words的text变量编写的,看起来像这样:

qi::rule<Iterator, int()> word = qi::string("This") | 
                                 qi::string("is") | 
                                 qi::string("the") | 
                                 qi::string("input"); 
qi::rule<Iterator, std::vector<int>()> start = +word;

Parsing the std::vector<Word> input should result in a vector containing the corresponding Integer values, for this example it would be 解析std::vector<Word> input应产生一个包含相应Integer值的向量,在本例中为

[10,73,5,32]
  1. Is this even possible with boost::spirit or should I take a different approach? 使用boost :: spirit甚至有可能还是我应该采取其他方法?

If this is could be a reasonable solution, 如果这是一个合理的解决方案,

  1. How can one implement an Iterator for this, how does it look like? 一个人如何为此实现一个Iterator ,它的外观如何?
  2. What should the semantic actions look like to create the corresponding synthesized attribute or do I need some other spirit "magic"? 语义动作应该如何创建相应的综合属性,或者我需要其他精神“魔术”吗?

I hope I have provided enough information for this, let me know if not. 希望我为此提供了足够的信息,如果没有,请告诉我。


EDIT: 编辑:

Looks like I asked not specific enough since I tried to keep this question as general as possible. 好像我问的不够具体,因为我试图使这个问题尽可能地笼统。 Sehe's solution should work for what I described, but I have the following limitations: Sehe的解决方案应该可以满足我的描述,但是我有以下限制:

  1. A Word can occur multiple times with different Integer values, there is no correlation between a Words text and its Integer value 一个Word可以使用不同的Integer值多次出现,Word文本与其Integer值之间没有关联
  2. The "text" (in this example "This is the input") needs to be parsed anyway to complete another task. 无论如何,都需要解析“文本”(在此示例中为“ This is the input”),以完成另一个任务。 I have already written everything to do so and it would be really easy for me to add what I need, if only I could access the Integer value from inside the semantic actions somehow. 我已经写了所有要做的事情,如果我只能以某种方式从语义动作内部访问Integer值,那么添加所需的内容对我来说真的很容易。

This appears superficially more related to lexing (aka tokenizing or scanning). 从表面上看,这与词汇化(又称为标记化或扫描)更相关。 See Boost Spirit Lex. 参见Boost Spirit Lex。

With Spirit Qi "magic", use symbols: 与精神气“魔术”一起使用符号:

Live On Coliru 生活在Coliru

#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;

struct tokens : qi::symbols<char, int>
{
    tokens() {
        add
            ("This",  10)
            ("is",    73)
            ("the",   5)
            ("input", 32);
    }
};

int main() {
    std::string const input("This is the input");

    std::vector<int> parsed;
    std::string::const_iterator f = input.begin(), l = input.end();
    bool ok = qi::phrase_parse(f, l, qi::no_case[ +tokens() ], qi::space, parsed);

    if (ok)
        std::cout << "Parse success: ";
    else
        std::cout << "Parse failed: ";

    std::copy(parsed.begin(), parsed.end(), std::ostream_iterator<int>(std::cout, " "));

    if (f!=l)
        std::cout << "\nRemaining input: '" << std::string(f,l) << "'\n";
}

Prints: 打印:

Parse success: 10 73 5 32 

See also qi::no_case and qi::symbols : 另请参见qi::no_caseqi::symbols

When symbols is used for case-insensitive parsing (in a no_case directive), added symbol strings should be in lowercase. symbols用于不区分大小写的解析时(在no_case指令中),添加的符号字符串应为小写。 Symbol strings containing one or more uppercase characters will not match any input when symbols is used in a no_case directive. no_case指令中使用符号时,包含一个或多个大写字符的符号字符串将不匹配任何输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM