重用Antlr对象作为新的输入字符串(C ++运行时)? [英] Reuse Antlr objects for a new input string (C++ runtime)?

查看:80
本文介绍了重用Antlr对象作为新的输入字符串(C ++运行时)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用C ++运行时演示构建了一个基本的解析器,并且工作正常.但是,我通常会解析很多输入字符串,是否可以修改代码以将现有对象重用于重复调用?如果是这样,有没有人举这个例子?

I've built a basic parser using the C++ runtime demo and it works fine. However I typically parse a lot of input strings, can the code be modified to reuse existing objects for repeated calls? If so, does anyone have an example of this?

推荐答案

是的,可以重用对象.解析调用的典型序列如下:

Yes, reusing the objects is possible. A typcial sequence for a parse call looks like this then:

input.load(newText);
errors.clear();
lexer.reset();
lexer.setInputStream(&input); // Not just reset(), which only rewinds the current position.
tokens.setTokenSource(&lexer);

parser.reset();
...

这可能是解析器服务类的一部分.在此类的c-tor中创建所有对象(解析器,词法分析器,令牌流,输入流),然后为每个解析操作调用以上代码.

This could be part of a parser service class. All the objects (parser, lexer, token stream, input stream) are created in the c-tor of this class and the code above is then called for each parse operation.

但是,通过重用这些对象并不会赢得太多收益.创建起来很便宜,并且大量数据是静态保存的,因此不需要在每次解析器创建时都重新创建它.

However, you don't win much by reusing these objects. Creation is cheap and the heavy data is held statically, so it doesn't need to be recreated on each parser creation.

这篇关于重用Antlr对象作为新的输入字符串(C ++运行时)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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