相关疑难解决方法(0)

使用boost :: spirit :: lex和whitespace来解决问题

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

这是词法分析器:

#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;
};
Run Code Online (Sandbox Code Playgroud)

这是我正在尝试运行的示例:

#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 …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-spirit boost-spirit-lex

5
推荐指数
1
解决办法
1151
查看次数

尽管令牌不完整,但提升精神意味着成功解析

我有一个非常简单的路径构造,我试图用boost spirit.lex解析.

我们有以下语法:

token := [a-z]+
path := (token : path) | (token)
Run Code Online (Sandbox Code Playgroud)

所以我们这里只讨论冒号分隔的小写ASCII字符串.

我有三个例子"xyz","abc:xyz","abc:xyz:".

前两个应被视为有效.第三个,有一个尾随结肠,不应被视为有效.不幸的是,解析器我已经认识到这三个都是有效的.语法不应该允许空令牌,但显然精神就是这样做的.为了让第三个被拒绝,我错过了什么?

此外,如果您阅读下面的代码,则在注释中还有另一个版本的解析器要求所有路径以分号结尾.当我激活那些线时,我可以得到适当的行为(即拒绝"abc:xyz:;"),但这不是我想要的.

有人有主意吗?

谢谢.

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>

#include <iostream>
#include <string>

using namespace boost::spirit;
using boost::phoenix::val;

template<typename Lexer>
struct PathTokens : boost::spirit::lex::lexer<Lexer>
{
      PathTokens()
      {
         identifier = "[a-z]+";
         separator = ":";

         this->self.add
            (identifier)
            (separator)
            (';')
            ;
      }
      boost::spirit::lex::token_def<std::string> identifier, separator;
};


template <typename Iterator>
struct PathGrammar 
   : boost::spirit::qi::grammar<Iterator> 
{
      template <typename TokenDef>
      PathGrammar(TokenDef const& tok)
         : PathGrammar::base_type(path)
      {
         using boost::spirit::_val; …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-spirit boost-spirit-lex

4
推荐指数
1
解决办法
904
查看次数

标签 统计

boost ×2

boost-spirit ×2

boost-spirit-lex ×2

c++ ×2