编译器编程:什么是最根本的成分? [英] Compiler-Programming: What are the most fundamental ingredients?

查看:197
本文介绍了编译器编程:什么是最根本的成分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感兴趣的是写一个非常简约的编译器。

I am interested in writing a very minimalistic compiler.

我想写一个小软件(在C / C ++),满足以下标准:

I want to write a small piece of software (in C/C++) that fulfills the following criteria:


  • 在ELF格式输出(* nix中)

  • 输入是一个文本文件

  • 类似C的语法和句法

  • 任何链接

  • 没有preprocessor ​​

  • 很小(最大1-2 KLOC)

语言特点:


  • 本地数据类型:CHAR,INT和彩车

  • 阵列(所有本地数据类型)

  • 变量

  • 控制结构(if-else语句)

  • 功能

  • 循环(就好)

  • 简单的代数运算(DIV,加,分,MUL,布尔前pressions,位移位等)

  • 内联汇编(系统调用)

有人能告诉我怎么开始呢?我不知道一个编译器包含哪些部分(至少在我刚开始会马上下架的意义上)以及如何将它们编程。感谢您的想法。

Can anybody tell me how to start? I don't know what parts a compiler consists of (at least not in the sense that I just could start right off the shelf) and how to program them. Thank you for your ideas.

推荐答案

首先,你需要决定是否将会使编译器或一个跨preTER。编译器会将您的code到的东西,可以在硬件上直接运行,在一个相互preTER,或会被编译成另一种语言,然后以某种方式PTED间$ P $。这两种类型的语言图灵完备,使他们具有相同的前pressive能力。我建议你​​创建一个编译器编译它的code到任何.NET或Java字节code,因为它给你一个非常优化的跨preTER上运行,以及大量的标准库

Firstly, you need to decide whether you are going to make a compiler or an interpreter. A compiler translates your code into something that can be run either directly on hardware, in an interpreter, or get compiled into another language which then is interpreted in some way. Both types of languages are turing complete so they have the same expressive capabilities. I would suggest that you create a compiler which compiles your code into either .net or Java bytecode, as it gives you a very optimized interpreter to run on as well as a lot of standard libraries.

一旦你做出了决定有一些共同的步骤如下

Once you made your decision there are some common steps to follow


  1. 语言定义首先,你必须定义你的语言应该是什么样子语法。

  1. Language definition Firstly, you have to define how your language should look syntactically.

词法第二步是创建code,被称为令牌的关键字。在这里,我们谈论的是非常基本的元素,比如数字,加号,和字符串。

Lexer The second step is to create the keywords of your code, known as tokens. Here, we are talking about very basic elements such as numbers, addition sign, and strings.

解析下一步是要建立一个满足你的标记列表的语法。可以使用例如定义你的语法上下文无关文法。许多工具可以用这些语法之一喂养,并为您创建解析器。一般,所解析的令牌被组织成一个解析树。解析树是你的语法是一种数据结构,你可以在走动的再presentation。

Parsing The next step is to create a grammar that matches your list of tokens. You can define your grammar using e.g. a context-free grammar. A number of tools can be fed with one of these grammars and create the parser for you. Usually, the parsed tokens are organized into a parse tree. A parse tree is the representation of your grammar as a data structure which you can move around in.

编译或国米preting 最后一步是在您解析树运行一些逻辑。一个简单的方法,使自己的跨preTER是树中的创建与每个节点类型一些逻辑,并通过树或者自下而上或自上而下走。如果你想编译成另一种语言,你可以插入如何翻译的,而不是节点中的code中的逻辑。

Compiling or Interpreting The last step is to run some logic on your parse tree. A simple way to make your own interpreter is to create some logic associated to each node type in your tree and walk through the tree either bottom-up or top-down. If you want to compile to another language you can insert the logic of how to translate the code in the nodes instead.

维基百科是来学习,你可能要开始这里

Wikipedia is great for learning more, you might want to start here.

关于现实世界的阅读材料,我建议Java编程语言处理器由大卫瓦功放&;德里克·布朗˚F。我用这本书在我的编译器课程,并通过实例学习在这个领域是很大的。

Concerning real-world reading material I would suggest "Programming language processors in JAVA" by David A Watt & Deryck F Brown. I used that book in my compilers course and learning by example is great in this field.

这篇关于编译器编程:什么是最根本的成分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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