如何着手制作自己的编程语言? [英] How to go about making your own programming language?

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

问题描述

可能的重复:
学习编写编译器

我环顾四周,试图了解有关编程语言开发的更多信息,但在网上找不到很多.我找到了一些教程视频,但对于文本指南、常见问题解答、建议等内容并不多.我真的很好奇如何构建自己的编程语言.它让我问:

I looked around trying to find out more about programming language development, but couldn't find a whole lot online. I have found some tutorial videos, but not much for text guides, FAQs, advice etc. I am really curious about how to build my own programming language. It brings me to SO to ask:

您如何着手制作自己的编程语言?

How can you go about making your own programming language?

我想构建一种非常基本的语言.我不打算拥有一门非常好的语言,也不认为它会被任何人使用.我只是想用自己的语言来学习更多关于操作系统、编程的知识,并在所有方面变得更好.

I would like to build a very basic language. I don't plan on having a very good language, nor do I think it will be used by anyone. I simply want to make my own language to learn more about operating systems, programming, and become better at everything.

从哪里开始?构建语法?构建编译器?需要什么技能?对操作系统有很多组装和了解?大多数编译器和语言内置哪些语言?我假设 C.

Where does one start? Building the syntax? Building a compiler? What skills are needed? A lot of assembly and understanding of the operating system? What languages are most compilers and languages built in? I assume C.

推荐答案

我想说的是,在你开始之前,你可能想看看 龙之书 和/或 编程语言语用学.这将使您了解编程语言的理论.这些书籍涵盖了编译和解释,并使您能够构建制作基本编程语言所需的所有工具.

I'd say that before you begin you might want to take a look at the Dragon Book and/or Programming Language Pragmatics. That will ground you in the theory of programming languages. The books cover compilation, and interpretation, and will enable you to build all the tools that would be needed to make a basic programming language.

我不知道你知道多少汇编语言,但除非你对汇编语言编程的某些方言相当熟悉,否则我建议你不要尝试编写一个编译成汇编代码的编译器,因为它相当有点挑战.您之前提到您对 C 和 C++ 都很熟悉,因此也许您可以编写一个编译器,将其编译为 C 或 C++,然后使用 gcc/g++ 或任何其他 C/C++ 编译器将代码转换为本地可执行文件.这就是 Vala 编程语言所做的(它将 Vala 语法转换为使用 GObject 库的 C 代码).

I don't know how much assembly language you know, but unless you're rather comfortable with some dialect of assembly language programming I'd advise you against trying to write a compiler that compiles down to assembly code, as it's quite a bit of a challenge. You mentioned earlier that you're familiar wtih both C and C++, so perhaps you can write a compiler that compiles down to C or C++ and then use gcc/g++ or any other C/C++ compiler to convert the code to a native executable. This is what the Vala programming language does (it converts Vala syntax to C code that uses the GObject library).

至于你可以用什么来编写编译器,你有很多选择.您可以用 C 或 C++ 手动编写它,或者为了简化开发,您可以使用更高级别的语言,以便您可以专注于编译器的编写,而不是内存分配以及处理字符串所需的此类内容在 C.

As for what you can use to write the compiler, you have a lot of options. You could write it by hand in C or C++, or in order to simplify development you could use a higher level language so that you can focus on the writing of the compiler more than the memory allocations and the such that are needed for working with strings in C.

您可以简单地生成语法并拥有 FlexBison 生成解析器和词法分析器.这非常有用,因为它允许您进行迭代开发以快速获得工作编译器.

You could simply generate the grammars and have Flex and Bison generate the parser and lexical analyser. This is really useful as it allows you to do iterative development to quickly work on getting a working compiler.

您拥有的另一个选择是使用 ANTLR 来生成您的解析器,这样做的好处是您可以获得ANTLR 可以编译成许多目标语言.我从来没有用过这个,但我听说过很多.

Another option you have is to use ANTLR to generate your parser, the advantage to this is that you get lots of target languages that ANTLR can compile to. I've never used this but I've heard a lot about it.

此外,如果您想更好地了解在编程语言编译器/扫描器/解析器构造中经常使用的模型,您应该获得一本关于计算模型的书.我推荐计算理论导论.

Furthermore if you'd like a better grounding on the models that are used so frequently in programming language compiler/scanner/parser construction you should get a book on the Models of Computation. I'd recommend Introduction to the Theory of Computation.

您似乎也有兴趣了解操作系统.我会说这是与编程语言设计分开的东西,应该分开进行.现代操作系统原理这本书很不错开始学习的地方.您可以从创建 shell 之类的小项目开始,或者编写一个模拟 ls 命令的程序,然后再进行更底层的事情,这取决于您对 C 中的系统调用的了解程度.

You also seem to show an interest in gaining an understanding of operating systems. This I would say is something that is separate from Programming Language Design, and should be pursued separately. The book Principles of Modern Operating Systems is a pretty good starting place for learning about that. You could start with small projects like creating a shell, or writing a programme that emulates the ls command, and then go into more low level things, depending on how through you are with the system calls in C.

希望能帮到你.

自从我写下这个答案以来,我学到了很多.我正在参加布朗大学提供的编程语言在线课程在那里看到了这个答案.教授非常正确地指出,这个答案谈论了很多关于解析器的内容,但几乎没有涉及其他所有内容.如果您想更好地了解如何创建编程语言,我真的建议您观看课程视频和练习.

I've learnt a lot since I write this answer. I was taking the online course on programming languages that Brown University was offering when I saw this answer featured there. The professor very rightly points out that this answer talks a lot about parsers but is light on just about everything else. I'd really suggest going through the course videos and exercises if you'd like to get a better idea on how to create a programming language.

这篇关于如何着手制作自己的编程语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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