开发抽象语法树 [英] Developing Abstract Syntax Tree

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

问题描述

我在互联网上搜索了一些关于开发 C# 抽象语法树的新手信息,但我只能找到已经知情"的人的信息.我是一名业务线应用程序开发人员,因此这些主题有点超出我的脑海,但这是为了我自己的教育,所以我愿意花时间学习任何必要的概念.

I've scoured the internet looking for some newbie information on developing a C# Abstract Syntax Trees but I can only find information for people already 'in-the-know'. I am a line-of-business application developer so topics like these are a bit over my head, but this is for my own education so I'm willing to spend the time and learn whatever concepts are necessary.

一般来说,我想了解从代码字符串开发代码的抽象表示背后的技术.更具体地说,我希望能够使用这个 AST 来进行 C# 语法高亮显示.(我意识到语法高亮不需要 AST,但这似乎是学习一些编译器"级技术的好机会.)

Generally, I'd like to learn about the techniques behind developing an abstract representation of code from a code string. More specifically, I'd like to be able to use this AST to do C# syntax highlighting. (I realize that syntax highlighting doesn't necessary need an AST, but this seems like a good opportunity to learn some "compiler"-level techniques.)

如果这个问题有点宽泛,我深表歉意,但我不知道还能怎么问.

I apologize if this question is a bit broad, but I'm not sure how else to ask.

谢谢!

推荐答案

首先你需要了解什么是解析,什么是抽象语法树.为此,您可以先参考维基百科关于抽象语法树.

First you need to understand what parsing is, and what abstract syntax trees are. For this, you can consult Wikipedia on abstract syntax trees for a first look.

你真的需要花一些时间看编译器教科书来理解抽象语法树是如何与解析相关的,并且可以在解析的同时构造;经典参考是 Aho/Ullman/Sethi 的编译器"一书(很容易在网上找到).您可能会在 是否有任何乐趣"?学习语言、语法、解析和编译器的方法?有指导意义.

You really need to spend some time with a compiler text book to understand how abstract syntax trees are related to parsing, and can be constructed while parsing; the classic reference is Aho/Ullman/Sethi's "Compilers" book (easily found on the web). You may find the SO answer to Are there any "fun" ways to learn about Languages, Grammars, Parsing and Compilers? instructive.

一旦您了解了如何为简单语法构建 AST,您就可以将注意力转向 C# 之类的东西.这里的问题是纯粹的规模;玩具有 20 条语法规则的玩具语言是一回事.处理几百或一千条规则的语法是另一回事.经验将小,将更容易理解大的如何组合在一起,以及如何与它们共存.

Once you understand how to build an AST for a simple grammar, you can then turn your attention to something like C#. The issue here is sheer scale; it is one thing to play with a toy language with 20 grammar rules. It is another to work with grammar of several hundred or a thousand rules. Experience will small ones will make it a lot easier to understand how the big ones are put together, and how to live with them.

您可能不想构建自己的 C# 语法(或实现 C# 标准中的语法);它的工作量相当大.您可以获得可用于处理 C# AST 的可用工具(已经提到了 Roslyn;ANTLR 有一个 C# 解析器,还有更多).

You probably don't want to build your own C# grammar (or implement the one from the C# standard); its quite a lot of work. You can get available tools that will hand you C# ASTs (Roslyn has already been mentioned; ANTLR has a C# parser, there are many more).

确实,您可以使用 AST 进行语法高亮显示(尽管这可能会用大锤杀死小虫).大多数人不怎么想(但编译器书籍强调),是在您拥有 AST 之后会发生什么;大多数情况下,它们本身并没有用.你实际上需要更多的机器来做任何有趣的事情.与其一遍又一遍地重复这个(我一直看到同样的问题),你可以看到我关于 解析后的生活了解更多详情.

It is true that you might use an AST for syntax highlighting (although that is probably killing a gnat with a sledgehammer). What most people don't think much about (but the compiler books emphasize), is what happens after you have an AST; mostly they aren't useful by themselves. You actually need a lot more machinery to do anything interesting. Rather than repeat this over and over (I keep seeing the same kind of questions), you can see my discussion on Life After Parsing for more details.

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

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