从源码到 AST 再到 CodeDom

发布于 2024-09-04 14:32:52 字数 513 浏览 5 评论 0原文

我正在阅读《语言实现模式》一书(http:// pragprog.com/book/tpdsl/language-implementation-patterns)以及其他一些内容混合在一起以澄清概念以及偶尔的网站。我正在尝试制作一个工具来读取简单的编程语言并对其进行一些基本分析。

我陷入了这个工具的设计阶段。我构建了一个简单的手写递归体面解析器,可以很好地验证源文件。然而,使用 CodeDom 树执行源操作会很有用。

问题:

1) 像这样的工具所采取的逻辑步骤是:解析并构建文本树和匹配的符号表,然后将其转换为 CodeDom?

2)构建文本树时,最方便的是 AST,更容易转换为 CodeDom ..但是重构工具会维护语句中所有嵌入标记的列表,以便保留内联注释以及它们如何跟踪此内容在他们的树上?

I am reading the book Language Implementation Patterns (http://pragprog.com/book/tpdsl/language-implementation-patterns) amongst a few others mixed in to clarify concepts as well as the occasional website. I am trying to make a tool that reads a trivial programming language and performs some basic analysis on it.

I am getting stuck in the design phase of this tool. I have constructed a simple handwritten recursive decent parser that validates a source file just fine. However, to perform source manipulations having a CodeDom tree would be useful.

The questions:

1) Are the logical steps a tool like this takes: Parse and build a textual tree and matching symbol table and then convert this to a CodeDom?

2) When building a textual tree, the most convenient would be a AST, easier to convert to a CodeDom .. but do Refactoring tools maintain a list of all embedded tokens in a statement in order to preserve inline comments and how do they track this in their tree?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呢古 2024-09-11 14:32:52

您可以构建自己的解析器、自己的树构建器、自己的树遍历器、自己的分析器、自己的漂亮打印机......但这需要大量工作。

您可能会考虑为您提供所有这些机制的工具。

我们的 DMS 软件再工程工具包就是此类工具之一。

给定一个语法,DMS将解析并自动构建一棵树;是的,它会自动捕获“微令牌”(例如评论)并将它们附加到适当的树节点。它可以在转换之前或之后将树漂亮地打印出来。您必须提供对符号表的支持,因为这是一种语义,而不是语法结构,但 DMS 提供了通用符号表和范围管理工具作为构建的库。 DMS 还提供了用于控制和数据流分析的完整库,如果您想要进行认真的代码转换或重构,则需要这些库。

DMS 最好的特性之一是能够应用使用语法语法声明的转换,例如,“如果您看到this(用我的语言),则将其替换为that ”。

您可以查看 定义词法分析器、解析器、prettyprinter 和定义 9 年级代数的转换规则的示例和一些微积分。 重写
规则用于对代数公式进行简化和计算符号导数。

You can build your own parser, your own tree builder, your own tree walker, your own analyzers, your own prettyprinters... but its a lot of work.

You might consider tools that provide all this machinery for you.

One such tool is our DMS Software Reengineering Toolkit.

Given a grammar, DMS will parse and automatically build a tree; yes, it automatically captures "microtokens" such as comments and attaches them to appropriate tree nodes. It can prettyprint the tree back out, before or after transformations. You have to provide support for symbol tables since that's a semantic, not a syntactic construct, but DMS provides generic symbol tables and scope management tools as a library to build upon. DMS also provides complete libraries for control and data flow analysis, which is needed if you want to do serious code transformations or refactorings.

One of DMS's nicest properties is the ability to apply transformations stated using the syntax of your grammar, e.g., "if you see this (in my language) then replace it by that".

You can see an example of defining lexer, parser, prettyprinter and transformation rules that define 9th grade algebra and a bit of calculus. The rewrite
rules are used to carry out simplifications and computing symbolic derivatives on algebraic formulas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文