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

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

问题描述

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



我想写一个小软件(在C / C ++中),符合以下标准: / p>


  • 以ELF格式输出(* nix)

  • 输入是单个文本文件

  • C样语法和语法

  • 无链接器

  • 无预处理器
  • 小(最大1-2 KLOC)


语言特性:




  • 原生数据类型:char,int和float

  • 数组(适用于所有原生数据类型)

  • >
  • 控制结构(if-else)

  • 功能

  • $ b
  • 简单代数(div,add,sub,mul,boolean expressions,bit-shift等)

  • inline asm $ b


任何人都可以告诉我如何开始?我不知道编译器组成什么部分(至少不是在我的意义上,我可以从现成的开始),以及如何编写它们。谢谢您的想法。

解决方案

首先,您需要决定是要编译器还是解释器。编译器将您的代码翻译成可以直接在硬件上,解释器中运行,或者编译成另一种语言,然后以某种方式解释。两种类型的语言都是完美的,因此它们具有相同的表达能力。我建议你创建一个编译器,将你的代码编译成.net或Java字节码,因为它给你一个非常优化的解释器,以及许多标准库。




  • Lexer 第二步是创建您的语言的关键字代码,称为令牌。


  • 解析下一步是创建与您的令牌列表匹配的语法。您可以使用例如一个上下文无关的语法。许多工具都可以使用这些语法之一来为你创建解析器。通常,解析的令牌被组织成解析树。分析树是您的语法表示为您可以移动的数据结构。


  • 编译或解释步骤是在你的解析树上运行一些逻辑。一个简单的方法来创建自己的解释器是创建一些逻辑关联到你的树中的每个节点类型,并通过树自下而上或自上而下。




  • 如果您要编译为其他语言,您可以插入如何翻译节点代码的逻辑。 >维基百科非常适合学习更多内容,您可以在这里开始。



    关于现实世界的阅读材料,我建议David A Watt& Sons的Programming language processors in JAVA Deryck F布朗。我在我的编译器课程中使用这本书,通过例子学习在这个领域是很好的。


    I am interested in writing a very minimalistic compiler.

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

    • output in ELF format (*nix)
    • input is a single textfile
    • C-like grammar and syntax
    • no linker
    • no preprocessor
    • very small (max. 1-2 KLOC)

    Language features:

    • native data types: char, int and floats
    • arrays (for all native data types)
    • variables
    • control structures (if-else)
    • functions
    • loops (would be nice)
    • simple algebra (div, add, sub, mul, boolean expressions, bit-shift, etc.)
    • inline asm (for system calls)

    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.

    解决方案

    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. Language definition Firstly, you have to define how your language should look syntactically.

    2. 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.

    3. 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.

    4. 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.

    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天全站免登陆