如何分析 Antlr 语法 [英] How to profile an Antlr grammar

查看:26
本文介绍了如何分析 Antlr 语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Antlr 语法,目前大约有 1200 行.它解析我想要的语言,但至少对于一种构造,即使对于较小的输入文件,它也非常慢.对于构造的每个添加元素,执行时间似乎呈指数增长.

I have an Antlr grammar that is currently about 1200 lines. It parses the language that I want, but for at least one construct it is prohibitively slow even for smaller input files. The execution time seems to be growing exponentially for each added element of the construct.

我想知道是否有任何关于调试/分析此类性能问题的好的指南.

I want to know if there are any good guidelines for debugging/profiling such performance problems.

我已经尝试过 VisualVM 并且给出了closureCheckingStopState 和closure_ 两种方法的名称,但这并不能更接近于找出语法有什么问题.

I have already tried with VisualVM and that gave be the name of the two methods closureCheckingStopState and closure_, but that does not bring be much closer to figure out what is wrong with the grammar.

推荐答案

我依靠两个主要项目来分析和改进语法的性能.

I rely on two primary items to analyze and improve the performance of a grammar.

  1. 最新版本的 ANTLRWorks 2 包括高级分析功能.当前的限制包括:

  1. The latest release of ANTLRWorks 2 includes advanced profiling capabilities. Current limitations include the following:

  • 探查器不支持需要自定义 CharStreamTokenStream(例如用于预处理输入)的语言.
  • 探查器不会在词法分析器或解析器中执行自定义嵌入操作,因此您的语法需要能够在不依赖于这些操作的情况下生成解析树.标准词法分析器命令,例如 ->跳过->channel(HIDDEN) 不会造成问题.
  • 分析器的输出是大多数 ANTLR 用户不容易理解的数字表格,特别是就知道您应该如何响应数字而言.
  • The profiler doesn't support languages which require a custom CharStream or TokenStream (e.g. for preprocessing the input).
  • The profiler doesn't execute custom embedded actions in the lexer or parser, so your grammar needs to be able to produce a parse tree without relying on these operations. Standard lexer commands such as -> skip or -> channel(HIDDEN) do not pose a problem.
  • The output of the profiler is tables of numbers which are not easily understood by most ANTLR users, especially in terms of knowing what you should do in response to the numbers.

我使用 主要版本的 fork,其中包括许多ANTLR 4 的参考版本中不存在优化.请注意,这些功能很少"记录在案,因为它们迄今为止的唯一目的是支持 ANTLRWorks 和 GoWorks 的内部开发.对于大多数语法,这个 fork 的性能大致相当于参考版本.但是,对于某些已知的语法,优化"版本的执行速度是参考版本的 200 倍以上.

I use a fork of the primary release which includes a number of optimizations not present in the reference release of ANTLR 4. Note that these features are "sparingly" documented as their only purpose to date was supporting the in-house development of ANTLRWorks and GoWorks. For most grammars, this fork performs roughly equivalent to the reference release. However, for some known grammars the "optimized" release performs over 200x as fast as the reference release.

如果你能提供语法和特别的输入,我可以运行分析并尝试解释结果的关键部分.

If you could provide the grammar and an input which is particularly, I could run the analysis and try to interpret the key pieces of the results.

ANTLRWorks 的最新版本通过官方 NetBeans 更新中心分发.只需运行 工具 →插件,转到可用插件并找到ANTLRWorks Editor.

The latest release of ANTLRWorks is distributed through the official NetBeans Update Center. Simply run Tools → Plugins, go to Available Plugins and locate ANTLRWorks Editor.

要运行分析器,请使用 Run →解释解析器... 命令.通过选择窗口→解析操作后,结果窗口可用.解析器调试器控制器.

To run the profiler, use the Run → Interpret Parser... command. The results window is available after the parsing operation by choosing Window → Parser Debugger Controller.

这篇关于如何分析 Antlr 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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