使用Boost.Spirit.Qi定制词法分析器 [英] Using Boost.Spirit.Qi with custom lexer

查看:364
本文介绍了使用Boost.Spirit.Qi定制词法分析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整个文档,无法找到一个例子。所有的例子要么解析字符数据或使用Spirit.Lex。原谅我,如果我错过了什么。

能有人给了一个例子,或点上,如何使用Boost.Spirit.Qi与我自定义的词法分析器教程?例如:

 矢量<&MyTokenType GT;令牌= GetTokens();
//使用升压精神与MyTokenType上运行每个令牌粒度


您将不得不做的事情sevaral:

一个)暴露标记序列作为范围迭代的,这将有要传递给解析/ phrase_parse
B)添加默认的转换操作符令牌类型暴露令牌ID

 结构令牌
{
    运营商INT()const的{返回ID; }
};

这允许使用齐:: char_(ID)作为一个解析器组件相匹配使用令牌ID令牌 ID

集成属性(标记值)是更复杂,看Spirit.Lex它可以怎么做。

I dug through the whole documentation and couldn't find an example. All the examples either parse character data or use Spirit.Lex. Forgive me if I missed something.

Can someone give an example for, or point to a tutorial on, how to use Boost.Spirit.Qi with my custom lexer? E.g.:

vector<MyTokenType> tokens = GetTokens();
// use boost spirit to work with MyTokenType on per-token granularity

解决方案

You will have to do sevaral things:

a) expose the token sequence as a range of iterators, which will have to be passed to parse/phrase_parse b) add a default conversion operator to your token type exposing the token id

struct token
{
    operator int() const { return id; }
};

that allows to use qi::char_(ID) as a parser component matching a token with the token id ID.

Integrating attributes (token values) is more involved, look at Spirit.Lex how it can be done.

这篇关于使用Boost.Spirit.Qi定制词法分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆