Using Boost.Spirit.Qi with custom lexer

放肆的年华 提交于 2019-12-12 19:56:16

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/6236824/using-boost-spirit-qi-with-custom-lexer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!