如何开始编写一种非常简单的编程语言

发布于 2024-09-05 08:21:48 字数 434 浏览 1 评论 0原文

最近,我四处寻找关于今年夏天可以使用 C 构建什么的想法,我发现了这篇文章:学习 C 的有趣项目?

实现一种编程语言。这 不必非常困难 - 我 做了不应该的语言 命名 - 但它会迫使你学习 C 的很多重要部分。如果 你不想写一个词法分析器和/或 自己解析,可以使用 lex/flex 和 yacc/bison,但如果你打算 你可能想从 项目规模较小。

我对实现编程语言答案有点感兴趣,我想知道如何开始这个?我已经读完了整本 K&R 书,并且还做了一些练习。如果这很重要的话,我在 C++ 和 Java 方面也有一些经验。有什么建议吗?谢谢!

Recently, I was going around looking for ideas on what I can build using C this summer and I came across this post: Interesting project to learn C?

Implement a programming language. This
doesn't have to be terribly hard - I
did the language that must not be
named - but it will force you to learn
a lot of the important parts of C. If
you don't want to write a lexer and/or
parser yourself, you can use lex/flex
and yacc/bison, but if you plan on
that you might want to start with a
somewhat smaller project.

I was kinda intrigued about the implementing a programming language answer and I'm wondering how do I go about starting this? I've gone through the whole K&R book and I've done some of the exercises as well. I also have a bit of experience in C++ and Java if that matters. Any tips? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

清醇 2024-09-12 08:21:48

从一种非常简单的(玩具)语言开始;稍后您可以创建更复杂的语法。

您可以编写一个解释器来解析字符串,

integer x
integer y
set x, 2
set y, 5
add x, y // x = x + y
print x

并立即评估每一行。如果将行存储在向量中,则可以轻松使用 goto 命令实现循环。


示例,另一个世界(复古游戏)
脚本编辑器:

alt text

Start with a very simple (toy) language; later you can create a more complex syntax.

You could write an interpreter to parse strings like,

integer x
integer y
set x, 2
set y, 5
add x, y // x = x + y
print x

and evaluate each line immediately. If you store the lines in a vector it'd be easy to implement loops with goto command.


An example, Another World (vintage game)
Script editor:

alt text

长发绾君心 2024-09-12 08:21:48

我将从一个简单的桌面计算器程序开始,它可以读取以下内容:

5 + 10 * 3

并打印答案。然后您可以继续添加变量、控制流,甚至函数。

I'd start with a simple desk calculator program that can read things like:

5 + 10 * 3

and print the answer. Then you can progress it to add variables, control flow, even functions.

梦开始←不甜 2024-09-12 08:21:48

了解正则表达式、语法和优秀的解析器生成器。

即使您最终实现了自己的解析器,这些也是实现任何编程语言的基本概念。

Learn about regular expressions, grammars, and a good parser generator.

Even if you end up implementing your own parser, these are the fundamental concepts to implementing any programming language.

情话墙 2024-09-12 08:21:48

我只能说,我见过很多人问“我如何制作一种编程语言?”之类的问题。或者“制作一种编程语言有多难”,大多数答案只是告诉他们,你必须读几年大学,读1000页长的书。我在这里告诉大家,你可以发布这些答案,但这对他们开发编程语言的旅程没有任何帮助。我今年 16 岁,从事编程工作已经快两年了,我编写编程语言。面向对象的也相当高级,但我没有读过任何书,也没有读过8年大学。为了帮助人们入门,这里有一个用 C# 编写的简单编程语言:

string code = "print Hello World";
foreach (string a in code.Split('\n'))
{
    if (a.StartsWith("print "))
    {
        Console.WriteLine(a.Substring(6));
    }
}

任何了解基本 C# 的人都应该能够理解它。如果没有一些编程经验,你就无法开始编写编程语言。确保你学习了一门编程语言并确保你对它了解很多,然后开始编写简单的小代码,就像我发布的那样,通过实验和实践,你将开始编写一些复杂的编程语言时间 :)

Can I just say, I have seen many people asking questions like "How do I make a programming language?" or "How hard is it to make a programming language" and most of the answers just tell them that you have to go through years of university and read books that are 1000 pages long. I am here to tell everyone that you may post those answers, but it doesn't help them at all in their journey to make a programming language. I am 16 and have been doing programming for almost 2 years and I write programming languages. Quite advanced object orientated ones as well, but I haven't read any books, no have I done 8 years of university. To get people started, here's a simple programming language written in C#:

string code = "print Hello World";
foreach (string a in code.Split('\n'))
{
    if (a.StartsWith("print "))
    {
        Console.WriteLine(a.Substring(6));
    }
}

anyone who knows basic C# should be able to understand this. You can't start making programming languages without having some programming experience. Make sure you learn a programming language and make sure you know a lot about it, then just start writing simple little bits of code, like I've posted, and with experimentation and practice, you'll start writing some complex programming languages in no time :)

清风疏影 2024-09-12 08:21:48

嗯,我认为这样的事情确实很难做到,但它也是一个很棒的宠物项目。您应该了解解析器、词法分析器、流程控制、范例(命令式、函数式、面向对象)和许多其他事物的概念。

许多人说 Dragon Book 是其中之一这方面最好的书。也许你可以看一下:)

祝你好运!

Well, I think something like that is really hard to do but also it would be a great pet project. You should have notions of parsers, lexers, flow control, paradigms (imperative, functional, OO) and many other things.

Many people says the Dragon Book is one of the best books for this. Maybe you can take a look at it :)

Good Luck!

请恋爱 2024-09-12 08:21:48

你可以阅读 Niklaus Wirth 写的一些写得很好的论文:

  • “编译器构造”(可参见 这里)是对构建编译器艺术的简短介绍。
  • “算法 + 数据结构 = 程序”(不幸的是已经绝版)在他的最后一章中介绍了一种更简单的语言(名为 PL/0)。

尽管这些论文主要是用 Pascal 编写的,但所公开的概念很容易转换为 C。

you can read some well-written papers by Niklaus Wirth:

  • "Compiler Construction" (available here) is a short, concise introduction to the art of building a compiler.
  • "Algorithms + Data Structure = Programs" (unfortunately out of print), presents a simpler language (named PL/0) in his last chapter.

although those papers are mainly written in Pascal, the concepts exposed are easily translated to C.

七七 2024-09-12 08:21:48

如果您会说法语,您可能会对我同事的一门课程感兴趣(免费提供)
http://matthieuamiguet.ch/scientifique/enseignement/langages-et-compilateurs 虽然他使用Python来解释语言构造和编译的概念。

PyCon 2010 的英文 PDF http://matthieuamiguet.ch/assets/files/scientifique /publis/TeachingCompilersWithPython_Paper.pdf

我可能需要和他谈谈将他的信息翻译成英语 8)

If you speak French you may be interested in one of my colleagues courses (freely available)
http://matthieuamiguet.ch/scientifique/enseignement/langages-et-compilateurs although he uses Python to explain the concepts of language construction and compilation.

English PDF from PyCon 2010 http://matthieuamiguet.ch/assets/files/scientifique/publis/TeachingCompilersWithPython_Paper.pdf

I may have to speak to him about translating his info to English 8)

冷心人i 2024-09-12 08:21:48

我前段时间用 Java 做了一个简单的语言解析器,基本上评估了数学表达式,替换了常量和变量,并提供了一些关于语法/类型错误的反馈。

我发现做这样的事情最简单的方法是创建一个解析树。这可以通过使用两个堆栈(一个运算符堆栈和一个结果堆栈)轻松完成。
之后,您可以使用 DFS 递归地解析它,如果您决定,可以使用 访问者模式用面向对象的语言实现它。

关于这些事情有很多话要说,如果您愿意,我可以更深入地解释它们,我没有这样做,因为我认为您想尝试自己实现上述内容,但如果您这样做,请通知我我们可以谈谈。

I've made a simple language parser in Java some time ago, basically evaluated mathematical expressions, replaced constants and variables and provided some feedback on syntax/type errors.

The easiest way I found to do such a thing was to make a parse tree. This can be done easily by using two stacks, an operator stack and a result stack.
Afterwards you could just parse it recursively using a DFS, maybe use the visitor pattern if you decide to implement this in a object oriented language.

There is a lot to say about these things and if you want to I can explain them more in-depth, I didn't because I thought you'd want to try implementing the above mentioned yourself, but if you do, just notify me and we can talk.

巷雨优美回忆 2024-09-12 08:21:48

一个旧的编译器教程是这个。虽然它是 Pascal 语言,但它是一个非常好的信息来源。如果您想要更新的内容,您应该查看 ANTLR

One old compiler tutorial is this one. Though it is in Pascal it is a very good source of information. If you want something more recent you should have a look at ANTLR.

哽咽笑 2024-09-12 08:21:48

从头开始的方案是关于用 C 实现方案的一系列不错的博客文章。代码非常可读,每个版本都以易于理解的方式构建在前一个版本的基础上。

这是第一部分:v0.1 - 整数

Scheme from Scratch is a nice series of blog posts about implementing Scheme in C. The code is very readable, and each version builds on the previous one in a way that's easy to follow.

Here is the first installment: v0.1 - Integers.

小伙你站住 2024-09-12 08:21:48

另一种选择是构建一种语言而不考虑其他任何东西。弄清楚你可以轻松做什么,然后从那里开始。例如,您可以将表达式解析为标记列表,用空格分隔,并使用前缀表示法(这很容易处理)。这种事情非常有趣,你可以从实验中学到很多东西。

Another alternative is to build a language without looking at anything else. Figure out what you can do easily, and go from there. For example, you could parse expressions into a list of tokens, separating with whitespace, and use prefix notation (which is quite simple to deal with). This sort of thing is a huge amount of fun, and you can learn a lot from experimenting.

你对谁都笑 2024-09-12 08:21:48

为了简单起见,我建议实现一种简单的后缀语言。 FORTH 或 PostScript 的核心部分将是不错的选择。

To keep things simple, I recommend implementing a simple postfix language. FORTH or the core part of PostScript would be great choices.

后知后觉 2024-09-12 08:21:48

阅读 usenet 新闻组 comp.compilers 上的帖子,可以通过 Google 网上论坛访问。它有许多与构建语言、构建编译器、lex/yacc、语法等相关的讨论。当然,你得对编译器方面的书籍中的龙书、虎书等经典书籍非常熟悉,还有算法、数据结构方面的好书。

原始 C 编译器正在被赋予新的生命。大部分内容都在重写,其代码库很小,只需一个暑假就可以阅读和理解。考虑阅读代码以及用于编写此编译器或任何工作编译器的代码的论文,我相信您会了解从哪里开始等。

Read through posts on the usenet newsgroup comp.compilers, it is accessible through Google Groups. It has many discussions related to building a language, building a compiler, lex/yacc, grammars and the like. Of course, you'd have to have good familiarity with the classics such as the dragon book, the tiger book among many books on compilers and, good books on algorithms and data structures.

The Original C Compiler is being given a new life. Most of it is being rewritten, and its code base is small enough to be read and understood in a summer vacation. Consider reading the code along with the papers that were used to write the code of this or any working compiler and I'm sure you'd get ideas about where to start, etc.

静谧幽蓝 2024-09-12 08:21:48

让其他人为您做脏活,即词法分析器和解析器。使用 cup、yacc 或 bison 来处理语法。这将使您专注于更重要的语言设计决策。甚至还有许多语言的示例解析器定义,您可以将其用作您的模板。

Let someone else do the dirty work for you, namely, the lexer and the parser. Use cup, yacc, or bison to handle the syntax. This will let you focus on the more important language design decisions. There are even example parser definitions for many languages that you can use as a template for yours.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文