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

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

问题描述

我在互联网上搜寻了有关开发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.

谢谢!

推荐答案

首先,您需要了解什么是解析,以及什么是抽象语法树。为此,您可以在抽象语法树上查阅 Wikipedia

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进行语法高亮显示(尽管这可能会用大锤杀死a)。大多数人没有想太多(但编译器书籍强调)的是在您拥有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天全站免登陆