如何创建自己的编译器? [英] how to create our own compiler?

查看:100
本文介绍了如何创建自己的编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

有谁能告诉我们如何使用C#.NET创建自己的编译器,通过它我可以执行任何程序?

Hi
Can anyone tell how we can create our own compiler using C#.NET through which I can execute any program?

推荐答案

创建自己的编译器并不是一项简单的工作:如果你想在现实世界中使用它,那么它需要经过精心设计和编码。



有很多关于编译器设计的书籍: Google [ ^ ]可以帮助你开始。

有些东西是免费的 - 我不知道这是否意味着被盗 - 其中一些是背景材料,但应该足以让你开始思考。



但是不要指望这是一个一个周末的项目:一个现代编译器是由一个团队编写的,并且仍然需要数年才能生成!
Creating your own compiler is not a trivial job: it's a lot of careful design and coding, if you want it to be any use in the real world.

There are a lot of books on compiler design: Google[^] can help you get started.
Some of the stuff there is "Free" - I don't know if that means "stolen" - and some of it is background material, but there should be enough to get you started thinking anyway.

But don't expect this to be a "one weekend" project: a modern compiler is written by a team of people, and can still take years to produce!


这比使用针对本机平台的语言更容易,因为您需要生成机器代码。使用.NET,没有机器代码。您可以解析代码并将其转换为CodeDOM。请参阅:

http:// msdn .microsoft.com / zh-CN / library / y2k85ax6%28v = vs.110%29.aspx [ ^ ]。



换句话说,你可以实现此方法: http:/ /msdn.microsoft.com/en-us/library/system.codedom.compiler.codedomprovider.parse%28v=vs.110%29.aspx [ ^ ]。但你不必;你可以按照你想要的方式填充DOM,CodeDOM会为你生成程序集。



解析是一个单独的主题。那么,您可以从头开始创建自己的解析器,学习解析技术,或使用一些可用的编译器 - 编译器。使用编译器编译器,您需要正式定义输入语言;并将为您生成编译器组件。请参阅: http://en.wikipedia.org/wiki/Compiler-compiler [ ^ ]。



另见本文: http://msdn.microsoft.com/en-us/magazine/cc136756。 aspx [ ^ ]。



此CodeProject文章甚至建议使用.NET编译器构建工具包:反讽 - .NET编译器构建工具包 [ ^ ]。



-SA
This is easier than with the languages targeting native platform, because you would need to generate machine code. With .NET, there is no machine code. You can parse code and translate it to CodeDOM. Please see:
http://msdn.microsoft.com/en-us/library/y2k85ax6%28v=vs.110%29.aspx[^].

In other words, you can implement this method: http://msdn.microsoft.com/en-us/library/system.codedom.compiler.codedomprovider.parse%28v=vs.110%29.aspx[^]. But you don't have to; you can just populate DOM the way you want and CodeDOM will generated the assembly for you.

Parsing is a separate topic. Well, you can invent your own parser from scratch, learn the parsing techniques, or use some available compiler-compiler. With a compiler-compiler, you would need to formally define the input language; and the compiler components will be generated for you. Please see: http://en.wikipedia.org/wiki/Compiler-compiler[^].

See also this article: http://msdn.microsoft.com/en-us/magazine/cc136756.aspx[^].

This CodeProject article even suggests a Compiler Construction Kit for .NET: Irony - .NET Compiler Construction Kit[^].

—SA


这篇关于如何创建自己的编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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