[英]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]
If this is could be a reasonable solution, 如果这是一个合理的解决方案,
Iterator
for this, how does it look like? 一个人如何为此实现一个Iterator
,它的外观如何? 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的解决方案应该可以满足我的描述,但是我有以下限制:
This appears superficially more related to lexing (aka tokenizing or scanning). 从表面上看,这与词汇化(又称为标记化或扫描)更相关。 See Boost Spirit Lex. 参见Boost Spirit Lex。
With Spirit Qi "magic", use symbols: 与精神气“魔术”一起使用符号:
#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_case
和qi::symbols
:
When
symbols
is used for case-insensitive parsing (in ano_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 ano_case
directive. 在no_case
指令中使用符号时,包含一个或多个大写字符的符号字符串将不匹配任何输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.