在这篇关于提升精神语义行为的文章中提到了这一点
实际上还有2个参数被传递:解析器上下文和对布尔"命中"参数的引用.仅当语义操作附加到规则右侧的某个位置时,解析器上下文才有意义.我们很快就会看到更多相关信息.在语义操作中,布尔值可以设置为false,从而使回溯中的匹配失效,从而使解析器失败.
一切都很好,但我一直试图找到一个示例传递函数对象作为语义动作使用其他参数(解析器上下文和命中布尔)但我还没有找到任何.我很想看到一个使用常规函数或函数对象的例子,因为我几乎无法理解凤凰伏都教
我创建了一个测试应用程序来说明我的问题.它解析前面带有"a ="或"b ="的整数列表,并用"\ r \n"分隔.该列表按任何顺序包含多个这些字段.
#include <string>
#include <vector>
#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
typedef std::vector<unsigned int> uint_vector_t;
std::ostream& operator<<(std::ostream& out, const uint_vector_t &data)
{
for (unsigned int i(0); i < data.size(); i++)
{
out << data[i] << '\n';
}
return out;
}
struct MyStruct
{
uint_vector_t m_aList;
uint_vector_t m_bList;
};
BOOST_FUSION_ADAPT_STRUCT
(
MyStruct,
(uint_vector_t, m_aList)
(uint_vector_t, m_bList)
)
;
template<typename Iterator>
struct MyParser : public boost::spirit::qi::grammar<Iterator,
MyStruct()>
{
MyParser() :
MyParser::base_type(Parser, "Parser")
{
using boost::spirit::qi::uint_;
using …Run Code Online (Sandbox Code Playgroud) 要实现对typedef的支持,当lexer识别标识符并返回不同的标记时,您需要查找符号表.这在flex lexer中很容易完成.我试图使用boost Spirit来构建解析器并在示例中查看,但它们都没有在词法分析器和解析器之间传递任何上下文信息.在mini c编译器教程示例中,最简单的方法是什么?
这很长,有很多代码,所以我希望Stack Overflow能够应对它.:P
我正在尝试用Boost Spirit编写一个SVG解析器.我有一个语法,用"Contours"填充一个矢量,它是"BezierPoints"的矢量,可以用bezier控件表示常规点或点.
到目前为止我有这个(还没有处理相对绘图命令):
#ifndef SVG_PARSER_HPP
#define SVG_PARSER_HPP
#include <vector>
#include "boost/spirit/include/qi.hpp"
#include "boost/spirit/include/phoenix.hpp"
#include "boost/fusion/include/adapt_struct.hpp"
#include "boost/fusion/include/std_pair.hpp"
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
namespace ascii = boost::spirit::ascii;
struct Point
{
Point(const double nx = 0.0, const double ny = 0.0) : x(nx), y(ny)
{}
double x;
double y;
};
BOOST_FUSION_ADAPT_STRUCT(
Point,
(double, x)
(double, y)
)
struct BezierPoint
{
BezierPoint(const double x = 0.0, const double y = 0.0) :
point(x, y), control1(0.0, 0.0), control2(0.0, 0.0) …Run Code Online (Sandbox Code Playgroud) 所以,我正在使用boost 1.47.0并且我一直在尝试组合一个词法分析器.
我的目标是为我的词法分析器的一些标记添加一个包含语义的动作
spirit::lex::_pass = spirit::lex::pass_flags::pass_ignore.但是,我无法编译任何语义操作.我在下面添加了一个(或多或少)最小的例子.我正在编译OS X 10.7.1上的clang.
clang++ -DBOOST_SPIRIT_DEBUG -DBOOST_SPIRIT_LEXERTL_DEBUG -DBOOST_SPIRIT_USE_PHOENIX_V3 -c input.cc
Run Code Online (Sandbox Code Playgroud)
#include <boost/phoenix.hpp>
#include <boost/spirit/home/lex.hpp>
#include <boost/spirit/home/lex/lexer/lexertl/lexer.hpp>
namespace phoenix = boost::phoenix;
namespace spirit = boost::spirit;
struct vhdl_lexer : spirit::lex::lexer< spirit::lex::lexertl::lexer<> > {
vhdl_lexer() {
num = "[0-9]";
any = ".";
this->self
= num
| any
;
}
spirit::lex::token_def<> num, any;
};
Run Code Online (Sandbox Code Playgroud)
#include <boost/phoenix.hpp>
#include <boost/spirit/home/lex.hpp>
#include <boost/spirit/home/lex/lexer/lexertl/lexer.hpp>
namespace phoenix = boost::phoenix;
namespace spirit = boost::spirit;
struct vhdl_lexer : spirit::lex::lexer< spirit::lex::lexertl::lexer<> > {
vhdl_lexer() …Run Code Online (Sandbox Code Playgroud) 我正在查看一些与n3960标准提案有关的代码,并注意到一些函数具有没有名称的参数,但具有完整的函数定义.有人可以解释这是怎么回事吗?
例:
template <typename ExPolicy, typename IteratorTag>
void test_for_each(ExPolicy const& policy, IteratorTag) //just IteratorTag, no name?
{
BOOST_STATIC_ASSERT(hpx::parallel::is_execution_policy<ExPolicy>::value);
typedef std::vector<std::size_t>::iterator base_iterator;
typedef test::test_iterator<base_iterator, IteratorTag> iterator;
std::vector<std::size_t> c(10000);
std::iota(boost::begin(c), boost::end(c), std::rand());
hpx::parallel::for_each(policy,
iterator(boost::begin(c)), iterator(boost::end(c)),
[](std::size_t& v) {
v = 42;
});
// verify values
std::size_t count = 0;
std::for_each(boost::begin(c), boost::end(c),
[](std::size_t v) {
HPX_TEST_EQ(v, std::size_t(42));
++count;
});
HPX_TEST_EQ(count, c.size());
}
Run Code Online (Sandbox Code Playgroud)