- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个词法分析器,基于该词法分析器,我现在想创建一个使用该词法分析器生成的标记的语法。我尝试改编我发现的一些示例,现在我有一些可以编译和工作的东西至少有一点,但我的一个应该失败的测试却没有。现在我想知道为什么,我也想知道我在那里实际做了什么(我想了解 - 我只是从一些示例中复制了一些代码,但这并没有真正 boost 理解力)。
词法分析器:
#include <boost/spirit/include/lex_lexertl.hpp>
namespace lex = boost::spirit::lex;
enum LexerIDs { ID_IDENTIFIER, ID_WHITESPACE, ID_INTEGER, ID_FLOAT, ID_PUNCTUATOR };
template <typename Lexer>
struct custom_lexer : lex::lexer<Lexer>
{
custom_lexer()
: identifier("[a-zA-Z_][a-zA-Z0-9_]*")
, white_space("[ \\t\\n]+")
, integer_value("[1-9][0-9]*")
, hex_value("0[xX][0-9a-fA-F]+")
, float_value("[0-9]*\\.[0-9]+([eE][+-]?[0-9]+)?")
, float_value2("[0-9]+\\.([eE][+-]?[0-9]+)?")
, punctuator("\\[|\\]|\\(|\\)|\\.|&>|\\*\\*|\\*|\\+|-|~|!|\\/|%|<<|>>|<|>|<=|>=|==|!=|\\^|&|\\||\\^\\^|&&|\\|\\||\\?|:|,")// [ ] ( ) . &> ** * + - ~ ! / % << >> < > <= >= == != ^ & | ^^ && || ? : ,
{
using boost::spirit::lex::_start;
using boost::spirit::lex::_end;
this->self.add
(identifier, ID_IDENTIFIER)
/*(white_space, ID_WHITESPACE)*/
(integer_value, ID_INTEGER)
(hex_value, ID_INTEGER)
(float_value, ID_FLOAT)
(float_value2, ID_FLOAT)
(punctuator, ID_PUNCTUATOR);
this->self("WS") = white_space;
}
lex::token_def<std::string> identifier;
lex::token_def<lex::omit> white_space;
lex::token_def<int> integer_value;
lex::token_def<int> hex_value;
lex::token_def<double> float_value;
lex::token_def<double> float_value2;
lex::token_def<> punctuator;
};
语法:
namespace qi = boost::spirit::qi;
namespace lex = boost::spirit::lex;
template< typename Iterator, typename Lexer>
struct custom_grammar : qi::grammar<Iterator, qi::in_state_skipper<Lexer>>
{
template< typename TokenDef >
custom_grammar(const TokenDef& tok) : custom_grammar::base_type(ges)
{
ges = qi::token(ID_INTEGER) | qi::token(ID_FLOAT);
BOOST_SPIRIT_DEBUG_NODE(ges);
debug(ges);
}
qi::rule<Iterator, qi::in_state_skipper<Lexer>> ges;
};
例子:
BOOST_AUTO_TEST_CASE(BasicGrammar)
{
namespace lex = boost::spirit::lex;
namespace qi = boost::spirit::qi;
std::string test("1234 56");
typedef lex::lexertl::token<char const*, lex::omit, boost::mpl::true_> token_type;
typedef lex::lexertl::lexer<token_type> lexer_type;
typedef custom_lexer<lexer_type>::iterator_type iterator_type;
custom_lexer<lexer_type> my_lexer;
custom_grammar<iterator_type, custom_lexer<lexer_type>::lexer_def> my_grammar(my_lexer);
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();
bool r = qi::phrase_parse(iter,end,my_grammar, qi::in_state( "WS" )[ my_lexer.self ]);
BOOST_CHECK(r);
}
我的假设是这会返回 true,因为空格被跳过了——因为 auf qi::in_state("WS")。真的吗?此外,我知道如何为空白输出额外的标记 - 但我不知道在 qi::in_stat 现在所在的位置放什么 - 没有它就无法工作。
有什么我可以改进结构的想法吗?为什么调试输出如此有趣?
<ges>
<try>[]</try>
<success></success>
<attributes>[]</attributes>
</ges>
感谢您的帮助。
问候
托比亚斯
最佳答案
您的解析器没有失败,但它也不是“默默地”跳过空格(无论如何它只解析一个非空格标记)。
事实上,*phrase_parse 系列 Spirit API 的一个属性是它可能无法匹配完整的输入。事实上,这就是它通过引用获取第一个迭代器的原因:在解析之后,迭代器将指示解析停止的位置。
我做了一些改动,因此您可以通过在 lexer_tokens 上使用 lex::tokenize_and_phrase_parse
而不是 qi::phrase_parse
轻松访问源迭代器:
Iterator first = test.c_str();
Iterator last = &first[test.size()];
bool r = lex::tokenize_and_phrase_parse(first,last,my_lexer,my_grammar,qi::in_state( "WS" )[ my_lexer.self ]);
std::cout << std::boolalpha << r << "\n";
std::cout << "Remaining unparsed: '" << std::string(first,last) << "'\n";
输出是:
Remaining unparsed: '56'
这是一个完整的工作示例(注意我还将语法类的第二个参数直接更改为 Skipper,这对于 Spirit 语法来说更为典型):
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
namespace qi = boost::spirit::qi;
namespace lex = boost::spirit::lex;
enum LexerIDs { ID_IDENTIFIER, ID_WHITESPACE, ID_INTEGER, ID_FLOAT, ID_PUNCTUATOR };
template <typename Lexer>
struct custom_lexer : lex::lexer<Lexer>
{
custom_lexer()
: identifier ("[a-zA-Z_][a-zA-Z0-9_]*")
, white_space ("[ \\t\\n]+")
, integer_value ("[1-9][0-9]*")
, hex_value ("0[xX][0-9a-fA-F]+")
, float_value ("[0-9]*\\.[0-9]+([eE][+-]?[0-9]+)?")
, float_value2 ("[0-9]+\\.([eE][+-]?[0-9]+)?")
, punctuator ("\\[|\\]|\\(|\\)|\\.|&>|\\*\\*|\\*|\\+|-|~|!|\\/|%|<<|>>|<|>|<=|>=|==|!=|\\^|&|\\||\\^\\^|&&|\\|\\||\\?|:|,")// [ ] ( ) . &> ** * + - ~ ! / % << >> < > <= >= == != ^ & | ^^ && || ? : ,
{
using boost::spirit::lex::_start;
using boost::spirit::lex::_end;
this->self.add
(identifier , ID_IDENTIFIER)
/*(white_space , ID_WHITESPACE)*/
(integer_value, ID_INTEGER)
(hex_value , ID_INTEGER)
(float_value , ID_FLOAT)
(float_value2 , ID_FLOAT)
(punctuator , ID_PUNCTUATOR);
this->self("WS") = white_space;
}
lex::token_def<std::string> identifier;
lex::token_def<lex::omit> white_space;
lex::token_def<int> integer_value;
lex::token_def<int> hex_value;
lex::token_def<double> float_value;
lex::token_def<double> float_value2;
lex::token_def<> punctuator;
};
template< typename Iterator, typename Skipper>
struct custom_grammar : qi::grammar<Iterator, Skipper>
{
template< typename TokenDef >
custom_grammar(const TokenDef& tok) : custom_grammar::base_type(ges)
{
ges = qi::token(ID_INTEGER) | qi::token(ID_FLOAT);
BOOST_SPIRIT_DEBUG_NODE(ges);
}
qi::rule<Iterator, Skipper > ges;
};
int main()
{
std::string test("1234 56");
typedef char const* Iterator;
typedef lex::lexertl::token<Iterator, lex::omit, boost::mpl::true_> token_type;
typedef lex::lexertl::lexer<token_type> lexer_type;
typedef qi::in_state_skipper<custom_lexer<lexer_type>::lexer_def> skipper_type;
typedef custom_lexer<lexer_type>::iterator_type iterator_type;
custom_lexer<lexer_type> my_lexer;
custom_grammar<iterator_type, skipper_type> my_grammar(my_lexer);
Iterator first = test.c_str();
Iterator last = &first[test.size()];
bool r = lex::tokenize_and_phrase_parse(first,last,my_lexer,my_grammar,qi::in_state( "WS" )[ my_lexer.self ]);
std::cout << std::boolalpha << r << "\n";
std::cout << "Remaining unparsed: '" << std::string(first,last) << "'\n";
}
关于c++ - 如何结合 boost::spirit::lex 和 boost::spirit::qi?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13395453/
假设我们要解析一个内积表达式并得到结果。 "SUM({1, 2, 3} .* {4, 5, 6})" qi::_1 和 qi::_2 非常方便地在解析器中引用第 i 个属性。 void Test(st
无法弄清楚为什么这条规则 unary_msg 不起作用,它说属性类型是 qi::unused_type 但这对我来说毫无意义。为什么 boost 会这样折磨我? template struct g3:
从测试中我可以得到 qi::uint_parser()与 qi::uint_ 相同.他们从 0 解析整数至 UINT_MAX . 我不明白的是qi::uint_parser需要 std::numeri
bool EqnExprEvaluator::SetEqn(const std::string& eqnStr) { typedef std::string::const_iterator
我一直在使用 Boost 库中的 Spirit Qi 用 C++ 编写语法。作为该语言的新手,很难习惯该库的语法和怪癖,但现在我有点了解它的工作原理。 我在使用 qi::lexeme 时遇到错误。我的
我需要跟踪一些项目在我使用提升灵气解析的文本中的位置。我找到了 this example并适应这样: #include #include #include #include #include
我想做的是在运行时从 ABNF 语法文件创建一个解析器。我已经在 qi::grammar 中实现了所有 ABNF 规则,如下所示: typedef /*qi::rule or struct conta
我在boost_1.59中使用spirit来解析类c语言(名为stone,最初由java编写)。但是我在使用 boost::spirit::qi 解析 c 风格的字符串时遇到了麻烦。整个代码位于 co
我正在用 Boost.Spirit 做一个 IRC 消息解析器,但是当我尝试解析一个输入时我遇到了一个(很长的)错误。我遵循了“Roman Numerals”示例。此外,我将 g++4.7 与 -st
如何解析可能包含 double 或 int 的字符串,具体取决于是否设置了点。例如。 6.0是double类型,6是int类型。规则是 rule,skipper> r = qi::double_|qi
在 boost::spirit::traits::transform_attribute 中指示解析失败的正确方法是什么?我可以抛出任何旧的异常,还是它要我做的特定事情? namespace boos
我正在尝试使用 qi 创建通用解析器元素,因为不幸的是(必须支持 MSVC)无法使用 X3。这个想法是有一个模板化的结构: template struct parse_type; 我可以这样使用: t
我正在尝试创建一个返回 function 的规则通过柯里化(Currying) Phoenix 表达式构造。例如, start = int_[_val = xxx]; rule start; 应该做什
我正在解析一个文本文件,可能有几 GB 的大小,由以下几行组成: 11 0.1 14 0.78 532 -3.5 基本上,每行一个整数和一个 float 。整数应该是有序的并且是非负的。我想验证数据是
我想提出一个让我陷入困境的主题,并提出了一个关于齐::符号。 这一切都始于我查看新的野兽图书馆并阅读 atutorial example 它以一个从 http 路径猜测 mime 类型的函数开始扩展。
我有一段 boost::spirit::qi 代码可以匹配 "M7.x.y.z" 或 "M7.x.y.z ",但我想要一个在前一个输入上失败的解析器。 我想我需要在其中插入 qi::lexeme[]
我正在尝试为一种语言的多个子集定义一些 Boost::spirit::qi 解析器,代码重复最少。为此,我创建了一些基本的规则构建函数。原来的解析器工作正常,但一旦我开始使用组合函数,我的解析器似乎就
假设我有一个简单的类型foo: struct foo { void bar(int) { // do something } }; 我想使用 Boost.Qi 来解析字符串中的整数字段,并使用结果值调
(免责声明,我正在学习提升精神) 我正在尝试解析这样的表达式:F( 1 ) 并希望将 1 作为字符串(“1”而不是数字(与 qi::int_ 一起使用))。 我试过这样的事情(这是错误的,但也许我的方
假设我有两个用逗号分隔的 double 来解析返回它们的总和。在 Haskell 中我可能会这样做: import Data.Attoparsec.Text import Data.Text (pac
我是一名优秀的程序员,十分优秀!