如何用c#语言编写简单的编译器? [英] How to make simple compilier by c# language?

查看:335
本文介绍了如何用c#语言编写简单的编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要使用c#语言编译器将代码拆分为令牌并在此代码中查找错误

need to make compiler in c# language that split code to tokens and find errors in this codes

推荐答案

这比使用针对本机平台的语言更容易,因为你需要生成机器代码。使用.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


这篇关于如何用c#语言编写简单的编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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