<legend id='5rrof'><style id='5rrof'><dir id='5rrof'><q id='5rrof'></q></dir></style></legend>

      <small id='5rrof'></small><noframes id='5rrof'>

      • <bdo id='5rrof'></bdo><ul id='5rrof'></ul>

    1. <tfoot id='5rrof'></tfoot>
      <i id='5rrof'><tr id='5rrof'><dt id='5rrof'><q id='5rrof'><span id='5rrof'><b id='5rrof'><form id='5rrof'><ins id='5rrof'></ins><ul id='5rrof'></ul><sub id='5rrof'></sub></form><legend id='5rrof'></legend><bdo id='5rrof'><pre id='5rrof'><center id='5rrof'></center></pre></bdo></b><th id='5rrof'></th></span></q></dt></tr></i><div id='5rrof'><tfoot id='5rrof'></tfoot><dl id='5rrof'><fieldset id='5rrof'></fieldset></dl></div>

    2. boost::spirit::lex &amp; 问题空白

      Troubles with boost::spirit::lex amp; whitespace(boost::spirit::lex amp; 问题空白)

        1. <i id='i9vkl'><tr id='i9vkl'><dt id='i9vkl'><q id='i9vkl'><span id='i9vkl'><b id='i9vkl'><form id='i9vkl'><ins id='i9vkl'></ins><ul id='i9vkl'></ul><sub id='i9vkl'></sub></form><legend id='i9vkl'></legend><bdo id='i9vkl'><pre id='i9vkl'><center id='i9vkl'></center></pre></bdo></b><th id='i9vkl'></th></span></q></dt></tr></i><div id='i9vkl'><tfoot id='i9vkl'></tfoot><dl id='i9vkl'><fieldset id='i9vkl'></fieldset></dl></div>
          <legend id='i9vkl'><style id='i9vkl'><dir id='i9vkl'><q id='i9vkl'></q></dir></style></legend>
            <tbody id='i9vkl'></tbody>

          <small id='i9vkl'></small><noframes id='i9vkl'>

        2. <tfoot id='i9vkl'></tfoot>
            <bdo id='i9vkl'></bdo><ul id='i9vkl'></ul>

                本文介绍了boost::spirit::lex &amp; 问题空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我尝试学习使用 boost::spirit.为此,我想创建一些简单的词法分析器,将它们组合起来,然后开始使用精神进行解析.我尝试修改示例,但它没有按预期运行(结果 r 不正确).

                I try learning to use boost::spirit. To do that, I wanted to create some simple lexer, combine them and then start parsing using spirit. I tried modifying the example, but it doesn't run as expected (the result r isn't true).

                这是词法分析器:

                #include <boost/spirit/include/lex_lexertl.hpp>
                
                namespace lex = boost::spirit::lex;
                
                template <typename Lexer>
                struct lexer_identifier : lex::lexer<Lexer>
                {
                    lexer_identifier()
                        : identifier("[a-zA-Z_][a-zA-Z0-9_]*")
                        , white_space("[ \t\n]+")
                    {
                        using boost::spirit::lex::_start;
                        using boost::spirit::lex::_end;
                
                        this->self = identifier;
                        this->self("WS") = white_space;
                    }
                    lex::token_def<> identifier;
                    lex::token_def<> white_space;
                    std::string identifier_name;
                };
                

                这是我尝试运行的示例:

                And this is the example I'm trying to run:

                #include "stdafx.h"
                
                #include <boost/spirit/include/lex_lexertl.hpp>
                #include "my_Lexer.h"
                
                namespace lex = boost::spirit::lex;
                
                int _tmain(int argc, _TCHAR* argv[])
                {
                    typedef lex::lexertl::token<char const*,lex::omit, boost::mpl::false_> token_type;
                    typedef lex::lexertl::lexer<token_type> lexer_type;
                
                    typedef lexer_identifier<lexer_type>::iterator_type iterator_type;
                
                    lexer_identifier<lexer_type> my_lexer;
                
                    std::string test("adedvied das934adf dfklj_03245");
                
                    char const* first = test.c_str();
                    char const* last = &first[test.size()];
                
                    lexer_type::iterator_type iter = my_lexer.begin(first, last);
                    lexer_type::iterator_type end = my_lexer.end();
                
                    while (iter != end && token_is_valid(*iter))
                    {
                        ++iter;
                    }
                
                    bool r = (iter == end);
                
                    return 0;
                }
                

                r 为真,只要字符串中只有一个标记.为什么会这样?

                r is true as long as there is only one token inside the string. Why is this the case?

                问候托比亚斯

                推荐答案

                您已经创建了第二个词法分析器状态,但从未调用过它.

                You have created a second lexer state, but never invoked it.

                在大多数情况下,获得预期效果的最简单方法是在可跳过的标记上使用带有 pass_ignore 标志的单状态词法分析:

                For most cases, the easiest way to have the desired effect would be to use single-state lexing with a pass_ignore flag on the skippable tokens:

                    this->self += identifier
                                | white_space [ lex::_pass = lex::pass_flags::pass_ignore ];
                

                请注意,这需要一个 actor_lexer 来允许语义操作:

                Note that this requires an actor_lexer to allow for the semantic action:

                typedef lex::lexertl::actor_lexer<token_type> lexer_type;
                

                完整样本:

                #include <boost/spirit/include/lex_lexertl.hpp>
                #include <boost/spirit/include/lex_lexertl.hpp>
                namespace lex = boost::spirit::lex;
                
                template <typename Lexer>
                struct lexer_identifier : lex::lexer<Lexer>
                {
                    lexer_identifier()
                        : identifier("[a-zA-Z_][a-zA-Z0-9_]*")
                        , white_space("[ \t\n]+")
                    {
                        using boost::spirit::lex::_start;
                        using boost::spirit::lex::_end;
                
                        this->self += identifier
                                    | white_space [ lex::_pass = lex::pass_flags::pass_ignore ];
                    }
                    lex::token_def<> identifier;
                    lex::token_def<> white_space;
                    std::string identifier_name;
                };
                
                int main(int argc, const char *argv[])
                {
                    typedef lex::lexertl::token<char const*,lex::omit, boost::mpl::false_> token_type;
                    typedef lex::lexertl::actor_lexer<token_type> lexer_type;
                
                    typedef lexer_identifier<lexer_type>::iterator_type iterator_type;
                
                    lexer_identifier<lexer_type> my_lexer;
                
                    std::string test("adedvied das934adf dfklj_03245");
                
                    char const* first = test.c_str();
                    char const* last = &first[test.size()];
                
                    lexer_type::iterator_type iter = my_lexer.begin(first, last);
                    lexer_type::iterator_type end = my_lexer.end();
                
                    while (iter != end && token_is_valid(*iter))
                    {
                        ++iter;
                    }
                
                    bool r = (iter == end);
                    std::cout << std::boolalpha << r << "
                ";
                }
                

                印刷品

                true
                

                WS"作为船长状态

                <小时>

                您也有可能遇到过使用第二个解析器状态作为船长的示例 (lex::tokenize_and_phrase_parse).让我花一分钟或 10 分钟来为此创建一个工作示例.

                "WS" as a Skipper state


                It is also possible you came across a sample that uses the second parser state for the skipper (lex::tokenize_and_phrase_parse). Let me take a minute or 10 to create a working sample for that.

                更新 花了我 10 多分钟 (waaaah) :) 这是一个对比测试,展示了词法分析器状态如何交互,以及如何使用 Spirit Skipper 解析来调用第二个解析器状态:

                Update Took me a bit more than 10 minutes (waaaah) :) Here's a comparative test, showing how the lexer states interact, and how to use Spirit Skipper parsing to invoke the second parser state:

                #include <boost/spirit/include/qi.hpp>
                #include <boost/spirit/include/lex_lexertl.hpp>
                namespace lex = boost::spirit::lex;
                namespace qi  = boost::spirit::qi;
                
                template <typename Lexer>
                struct lexer_identifier : lex::lexer<Lexer>
                {
                    lexer_identifier()
                        : identifier("[a-zA-Z_][a-zA-Z0-9_]*")
                        , white_space("[ \t\n]+")
                    {
                        this->self       = identifier;
                        this->self("WS") = white_space;
                    }
                    lex::token_def<> identifier;
                    lex::token_def<lex::omit> white_space;
                };
                
                int main()
                {
                    typedef lex::lexertl::token<char const*, lex::omit, boost::mpl::true_> token_type;
                    typedef lex::lexertl::lexer<token_type> lexer_type;
                
                    typedef lexer_identifier<lexer_type>::iterator_type iterator_type;
                
                    lexer_identifier<lexer_type> my_lexer;
                
                    std::string test("adedvied das934adf dfklj_03245");
                
                    {
                        char const* first = test.c_str();
                        char const* last = &first[test.size()];
                
                        // cannot lex in just default WS state:
                        bool ok = lex::tokenize(first, last, my_lexer, "WS");
                        std::cout << "Starting state WS:	" << std::boolalpha << ok << "
                ";
                    }
                
                    {
                        char const* first = test.c_str();
                        char const* last = &first[test.size()];
                
                        // cannot lex in just default state either:
                        bool ok = lex::tokenize(first, last, my_lexer, "INITIAL");
                        std::cout << "Starting state INITIAL:	" << std::boolalpha << ok << "
                ";
                    }
                
                    {
                        char const* first = test.c_str();
                        char const* last = &first[test.size()];
                
                        bool ok = lex::tokenize_and_phrase_parse(first, last, my_lexer, *my_lexer.self, qi::in_state("WS")[my_lexer.self]);
                        ok = ok && (first == last); // verify full input consumed
                        std::cout << std::boolalpha << ok << "
                ";
                    }
                }
                

                输出是

                Starting state WS:  false
                Starting state INITIAL: false
                true
                

                这篇关于boost::spirit::lex &amp; 问题空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to limit the number of running instances in C++(C++中如何限制运行实例的数量)
                Using boost::asio::async_read with stdin?(将 boost::asio::async_read 与 stdin 一起使用?)
                How to find out what dependencies (i.e other Boost libraries) a particular Boost library requires?(如何找出特定 Boost 库需要哪些依赖项(即其他 Boost 库)?)
                What#39;s the purpose of a leading quot;::quot; in a C++ method call(引导“::的目的是什么?在 C++ 方法调用中)
                Boost Spirit x3: parse into structs(Boost Spirit x3:解析为结构体)
                How boost auto-linking makes choice?(boost自动链接如何做出选择?)
                <i id='rIZsH'><tr id='rIZsH'><dt id='rIZsH'><q id='rIZsH'><span id='rIZsH'><b id='rIZsH'><form id='rIZsH'><ins id='rIZsH'></ins><ul id='rIZsH'></ul><sub id='rIZsH'></sub></form><legend id='rIZsH'></legend><bdo id='rIZsH'><pre id='rIZsH'><center id='rIZsH'></center></pre></bdo></b><th id='rIZsH'></th></span></q></dt></tr></i><div id='rIZsH'><tfoot id='rIZsH'></tfoot><dl id='rIZsH'><fieldset id='rIZsH'></fieldset></dl></div>
                • <bdo id='rIZsH'></bdo><ul id='rIZsH'></ul>
                    <legend id='rIZsH'><style id='rIZsH'><dir id='rIZsH'><q id='rIZsH'></q></dir></style></legend>

                      <small id='rIZsH'></small><noframes id='rIZsH'>

                          <tbody id='rIZsH'></tbody>
                        1. <tfoot id='rIZsH'></tfoot>