岛文语法antlr3 [英] Island grammar antlr3

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

问题描述

antlr3 中的孤岛语法"是什么以及如何使用?

What are and how to use the "island grammar" in antlr3?

推荐答案

孤岛语法是将大部分语言视为一团文本(水")并挑选出感兴趣的语言部分进行解析的语法使用语法规则(岛").例如,您可能会选择构建一个孤岛语法来挑选出在 C# 程序中找到的所有表达式,而忽略变量/方法/类声明和语句语法(if、while、...).

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?".

积极的好处:

  • 您不必为要处理的语言编写完整的语法.

缺点:

  • 挑选出感兴趣的来源部分并不总是那么容易.例如,你如何确保 island 语法不会拾取被注释掉的代码块,除非你的 island 语法对所有注释进行了详细的词法分析?孤岛语法的目的是避免进入那种细节,但在这里你必须.

  • 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.

说我有偏见,但我做这种事情已经很长时间了.我相信岛屿语法真的没有那么有用.我提出的替代方案是基于共同基础的多种语言的摊销成本解析器,其名为 DMS 软件再造工具包.

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.

天啊.

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

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