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

查看:14
本文介绍了将 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天全站免登陆