开始使用 ANTLR 并避免常见错误 [英] Getting started with ANTLR and avoiding common mistakes

查看:26
本文介绍了开始使用 ANTLR 并避免常见错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始学习 ANTLR,并且拥有 2007 年的书The Definitive ANTLR Reference"和 ANTLRWorks(一种用于创建语法的交互式工具).而且,作为那种人,我从第 3 章开始.(不耐烦的快速浏览").

这是一个相当痛苦的过程,尤其是因为有些错误相当难以理解(例如 ANTLR:在规则范围内缺少属性访问"问题,这对我来说只是意味着你做错了").此外,我还有一些非常简单的语法(仅 3-4 个产生式)和简单的输入(2 行),在运行时会出现OutOfMemory"错误.

ANTLR 站点很有用,但有些零散,一些 SO 用户发表了评论(https://stackoverflow.com/questions/278480/good-tutorial-for-antlr),这本书和教程期望高入门水平.因为这个,我一直不愿意接近 ANTLR 讨论列表.

稍后我们开始着手处理它.拥有可以温和扩展的简单可靠的示例会很有用.这当然值得掌握,因为我们已经在 ANTLR 的基础上重塑了很多我们的想法.

一个问题是 ANTLR V3 与 V2 相比有显着变化.SO(以及 ANTLR 页面)上的一个答案提到了不再可用的 V2 语法.

关于 SO 的一些 ANTLR 问题对我帮助很大,但找到它们有点临时.所以我想知道 SO 用户如何帮助减少学习过程的痛苦.(如果您参考参考书,指向特定页面会很有用).

编辑.@duffymo 和 @JamesAnderson 已经确认 ANTLR 是一项艰苦的工作 - 主要是因为解析器很困难.(FWIW 我已经通过 LEX/YACC 等,并且毫无疑问 ANTLR 更强大且更易于使用.)我认为拥有可以避免污染的区域仍然很有用,例如:

  • 确保变量名的大小写正确
  • 将包名添加到词法分析器和解析器
  • 注意规则的顺序,因为它会影响优先级

还有更多这样的东西会很有用.

解决方案

我同意 - ANTLR 不适合胆小的人.它确实期望较高的入门级,因为语法和解析器并非微不足道.

话虽如此,这里有一些建议:

  1. 忘记 v2.版本 3 是标准;甚至不要浪费时间考虑早期版本或其文档.
  2. OutOfMemoryError 告诉您在您定义的语法中有一些循环.
  3. IntelliJ 有一个出色的 IDE,可用于处理 ANTLR v3.它将为您提供语法的图形表示、逐步调试等.如果您要使用 ANTLR 进行大量工作,那么购买许可证就值几美元.

ANTLR 不容易掌握.这本书很好,但很厚.正如您所指出的,错误消息很神秘.如果这里有人能让事情变得简单,我会感到惊讶.

I have started to learn ANTLR and have both the 2007 book "The Definitive ANTLR Reference" and ANTLRWorks (an interactive tool for creating grammars). And, being that sort of person, I started at Chapter 3. ("A quick tour for the impatient").

It's a fairly painful process especially as some errors are rather impenetrable (e.g. ANTLR: "missing attribute access on rule scope" problem which just means to me "you got something wrong"). Also I have some very simple grammars (3-4 productions only) and simple input (2 lines) which when run give "OutOfMemory" error.

The ANTLR site is useful but somewhat fragmented and some SO users have commented (https://stackoverflow.com/questions/278480/good-tutorial-for-antlr) that the book and the tutorials expect a high entry level. I've been reluctant to approach the ANTLR discussion list because of this.

LATER We are beginning to get to grips with it. It would be useful to have simple reliable examples that could be gently expanded. It's certainly worth mastering as we have remodelled quite a lot of our thinking based on ANTLR.

One problem is that ANTLR V3 has signifcant changes from V2. One answer on SO (and on the ANTLR pages) refered to a V2 syntax that is no longer available.

Some of the ANTLR questions on SO have helped me a lot, but finding them is a bit ad hoc. So I'd like to know how SO users can help to make the learning process less painful. (If you refer to the reference book it would be useful to point to particular pages).

EDIT. @duffymo and @JamesAnderson have confirmed that ANTLR is hard work - largely because parsers are difficult. (FWIW I have been through LEX/YACC, etc. and there's no doubt that ANTLR is more powerful and easier to work with.) I think it would still be useful to have areas where it's possible to avoid fouling up such as:

  • ensure correct capitalisation of variable names
  • add package name to lexer as well as parser
  • take care over order of rules as it affects precedence

and more of these sort would be useful.

解决方案

I agree - ANTLR is not for the faint of heart. It does expect a high entry level, because grammars and parsers are not trivial.

With that said, here are a few suggestions:

  1. Forget about v2. Version 3 is the standard; don't even waste time considering the earlier version or its documentation.
  2. OutOfMemoryError is telling you that there's something circular in the grammar you've defined.
  3. IntelliJ has a wonderful IDE for working with ANTLR v3. It'll give you a graphical representation of your grammar, step-through debugging, etc. If you're going to be doing a lot of work with ANTLR it'd be worth a few dollars to buy a license.

ANTLR won't be easy to master. The book is good, but dense. The error messages are cryptic, as you've noted. I'd be surprised if anyone here could make it easy.

这篇关于开始使用 ANTLR 并避免常见错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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