如何使用boost :: spirit :: lex实现include指令? [英] How do I implement include directives using boost::spirit::lex?

查看:149
本文介绍了如何使用boost :: spirit :: lex实现include指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于spirit :: lex和spirit :: qi构建的简单配置文件解析器.当词法分析器到达模式include "path"时,我希望包含文件的文本.如您所知,spirit :: lexer :: begin()开始扫描过程:

I have a simple configuration file parser built from spirit::lex and spirit::qi. When the lexer reaches the pattern include "path" I want the text of the file to be included. As you may know, spirit::lexer::begin() starts the scanning process:

// Read file contents into a std::string
...

// _first and _last are const char*
_first = _contents.c_str();
_last  = &_first[_input.size()];

// _token is a lexer::iterator_type for the current token
_token = _lexer.begin(_first, _last);

我的想法是要有一个堆栈,用于存储以结构表示的词法分析器状态:

My idea is to have a stack that stores lexer state represented as a struct:

struct LexerState
{
    const char* first;
    const char* last;
    std::string contents;
};

将使词法分析器识别include "path"的模式,并在语义动作中提取包含文件的路径.然后,将当前的lexer状态压入堆栈,将该文件的内容加载到字符串中,然后使用lexer :: begin()像上面那样初始化新状态.

The lexer would be made to recognize the pattern for include "path" and in a semantic action extract the path to the include file. Then, the current lexer state is pushed on the stack, the file's contents are loaded into a string, and the new state is initialized as above using lexer::begin().

当词法分析器找到EOF字符时,将弹出堆栈,并使用以前的词法分析器状态变量调用lexer :: begin().

When the lexer finds the EOF character, the stack is popped and lexer::begin() is called using the previous lexer state variables.

像这样反复调用lexer :: begin()可以吗?如何获得lex :: lexer来识别include "path"模式和EOF字符而又不将标记返回到qi解析器?

Is it ok to repeatedly call lexer::begin() like this? How do I get lex::lexer to recognize the include "path" pattern and the EOF character without returning a token to the qi parser?

最后,有没有其他替代方法或更好的方法来完成此任务?

Finally, are there any alternative or better ways of accomplishing this?

推荐答案

看看

Have a look at how Boost Wave does things:

Wave C ++预处理程序库使用Spirit解析器构造库来实现具有符合ISO/ANSI标准的预处理功能的C ++词法分析器.它公开了一个迭代器接口,该接口从输入流返回当前经过预处理的令牌.在对预处理程序迭代器序列进行迭代时(在STL的术语中,这些迭代器为正向迭代器),它是在运行时动态生成的.

The Wave C++ preprocessor library uses the Spirit parser construction library to implement a C++ lexer with ISO/ANSI Standards conformant preprocessing capabilities. It exposes an iterator interface, which returns the current preprocessed token from the input stream. This preprocessed token is generated on the fly while iterating over the preprocessor iterator sequence (in the terminology of the STL these iterators are forward iterators).

关于功能:

C ++预处理器提供了四个单独的功能,您可以根据需要使用它们:

The C++ preprocessor provides four separate facilities that you can use as you see fit:

  • 包含头文件
  • 宏扩展
  • 有条件的编译
  • 线路控制
  • Inclusion of header files
  • Macro expansion
  • Conditional compilation
  • Line control

他们的快速入门示例显示了如何使用Boost Wave的lexer界面.

Their Quick Start Sample shows how you'd use Boost Wave's lexer interface.

这篇关于如何使用boost :: spirit :: lex实现include指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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