岛语法antlr3
antlr3中的“岛屿语法”是什么以及如何使用?
What are and how to use the "island grammar" in antlr3?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
antlr3中的“岛屿语法”是什么以及如何使用?
What are and how to use the "island grammar" in antlr3?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
岛语法是将语言的大部分视为一团文本(“水”),并挑选出感兴趣的语言部分以使用语法规则进行解析(“岛”)。例如,您可能选择构建一个岛语法来挑选 C# 程序中找到的所有表达式,并忽略变量/方法/类声明和语句语法(if、while、...)。
真正的问题是,“你应该使用岛屿语法吗?”。
积极的好处:
缺点:
挑选出感兴趣的源部分并不总是那么容易。例如,
你如何确保岛语法不会拾取被注释掉的代码块,除非你的岛语法详细地解释了所有注释?孤岛语法的要点是避免陷入这种细节,但在这里你必须这样做。
你只能使用孤岛语法来关注你现在所理解的问题。如果问题发生了变化,那么您的岛语法也可能必须改变,但这并不总是那么容易。
程序操作中最有趣的问题要求您不仅能够确定语法(例如“解析”)并构建某种树来操作,而且还能够确定符号的含义。使用岛屿语法,您实际上已经排除了这样做的可能性(除非您想包含块、声明等的所有语法......哎呀突然它不是岛屿语法而是小大陆语法) 。这确实限制了你能做的事情。
你可以说我有偏见,但我做这种事已经很久了。我相信岛屿语法确实没有那么有用。我建议的替代方案是基于共同基础的多种语言的摊销成本解析器,其称为 DMS 软件再造工具包。
YMMV。
An island grammar is one that treats most of a language as a blob of text ("water") and picks out the part of the langauge of interest to parse using grammar rules ("island"). For instance, you might choose to build an island grammar to pick out all the expressions found in a C# program, and ignore the variable/method/class declarations and the statement syntax (if, while, ...).
The real question is, "Should you use island grammars at all?".
The positive benefits:
The downside:
It isn't always easy to pick out the part of the source of interest. For example,
how do you ensure that the island grammar doesn't pick up a block of code that is commented out, unless your island grammar lexes all the comments in detail? The point of an island grammar was to avoid going into that kind of detail, and yet here you must.
You can only use the island grammar to focus on the problem as you understand it right now. If the problem moves, then your island grammar may have to shift, too, and that isn't always easy.
Most interesting problems in program manipulation require that you be able to determine not only the syntax (e.g., "parse") and build some kind of tree to manipulate, but that you also be able to determine the meaning of symbols. With an island grammar, you've effectively written off the possibility of doing that (unless you want to include all the syntax for blocks, declarations, etc. ... oops suddenly it isn't an island grammar but a small continent grammar). And that really limits what you can do.
Call me biased, but I've been doing this kind of stuff a long time. I believe that island grammars really aren't that useful. The alternative I propose is amortized-cost parsers over many languages based on common foundations, and its called DMS Software Reengineering Toolkit.
YMMV.
好吧,我不确定你的意思,但既然你还没有提到你到目前为止所写的内容,我就从这里开始:
http://www.antlr.org/wiki/display/ANTLR3/Island+Grammars+Under+Parser+Control
Well I'm not sure exactly what you mean but since you haven't mentioned what you've written so far I'd start here:
http://www.antlr.org/wiki/display/ANTLR3/Island+Grammars+Under+Parser+Control