ANTLR入门,避免常见错误 [英] Getting started with ANTLR and avoiding common mistakes

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

问题描述

我已经开始学习ANTLR,并且同时拥有2007年的著作权威ANTLR参考"和ANTLRWorks(用于创建语法的交互式工具).而且,作为这样的人,我从第3章开始.(不耐烦的快速浏览").

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

ANTLR站点很有用,但有些支离破碎,一些SO用户对此发表了评论( https://stackoverflow. com/questions/278480/good-tutorial-for-antlr ),则该书和教程希望入门水平很高.因此,我一直不愿进入ANTLR讨论列表.

LATER 我们开始逐渐了解它.拥有一些可以逐步扩展的简单可靠的示例将非常有用.当然,由于我们已经基于ANTLR重塑了很多思维方式,因此当然值得掌握.

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

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

编辑. @duffymo和@JamesAnderson已经确认ANTLR是艰苦的工作-主要是因为解析器很困难. (FWIW我曾经经历过LEX/YACC等,毫无疑问,ANTLR功能更强大,更容易使用.)我认为,在可能的地方避免积垢仍然很有用,例如:

  • 确保变量名正确大写
  • 将软件包名称添加到lexer和解析器中
  • 请注意规则顺序,因为它会影响优先级

更多此类有用.

解决方案

我同意-ANTLR不适合胆小的人.它确实希望入门水平很高,因为语法和解析器并不琐碎.

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

  1. 忘记v2.第3版是标准;甚至不用浪费时间考虑早期版本或其文档.
  2. OutOfMemoryError告诉您定义的语法中存在一些循环.
  3. IntelliJ具有与ANTLR v3配合使用的出色IDE.它将为您提供语法的图形表示,逐步调试等.如果您要与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天全站免登陆