创建解释性语言的过程是怎样的?

发布于 2024-09-02 21:20:48 字数 67 浏览 5 评论 0原文

我想创建一种非常简单的实验性编程语言。我可以查看哪些资源来概述创建解释语言的过程。我将使用 C++ 来构建和编译解释器。

I want to create a very simple experimental programming language. What are some resources can i check out to get an overview of the process of creating an interpreted language. I will be using c++ to build and compile the interpreter.

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

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

发布评论

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

评论(4

小苏打饼 2024-09-09 21:21:00

看一下 boost 库“spirit”LL 解析器。

Take a look at the boost library "spirit" LL parser.

淡莣 2024-09-09 21:20:57

要创建解释性语言,您需要创建两件事:

  • 语言语法的正式定义
  • 可以读取和解释语言的解析器

定义了语言本身后,可以使用多种工具来帮助创建语言解析器。经典工具是 lexyacc 及其开源版本 flexbison

To create an interpreted language, you need to create two things:

  • A formal definition of the language's grammar
  • A parser that can read and interpret the language

Once you have defined the language itself, there are several tools available to assist in creating a language parser. The classic tools are lex and yacc, and their open-source versions flex and bison.

感受沵的脚步 2024-09-09 21:20:56

几个步骤:

首先,构建词法分析器和解析器。使用 lex 和 yacc 等常用工具,或者使用 Antlr 等更现代的框架(这是我推荐的),这确实很容易做到。这些工具将为您的目标语言生成源代码,然后您可以对其进行编译并包含在您的项目中。

词法分析器和解析器将构建源文件的内部表示。有几种不同的方法可以解决此问题:

  1. 字节码模型中,源文件被编译为低级内部语言,您可以为其编写一个字节码解释器直接执行操作。例如,这就是 Perl 和 .NET 语言的工作方式。
  2. 对象树模型中,源文件被编译成对象树,其中每个对象都知道如何执行自身。解析完成后,您只需在根对象上调用 Exec()(根对象又在其子对象上调用 Exec() 等)。这基本上就是我用于解释型域特定语言 Phonix 的方法。

A few steps:

First, build the lexer and parser. This is really easy to do with common tools such as lex and yacc, or using a more modern framework such as Antlr (which is what I recommend). These tools will generate source code for your target language that you can then compile and include in your project.

The lexer and parser will build the internal representation of the source file. There are a few different ways of approaching this:

  1. In the bytecode model, the source file is compiled into a low-level internal language, for which you write a bytecode interpreter that directly executes the operations. This is the way that Perl and the .NET languages work, for example.
  2. In the object tree model, the source file is compiled into an object tree where every object knows how to execute itself. Once parsing is completed, you just call Exec() on the root object (which in turn calls Exec() on its children, etc.). This is basically the method that I use for my interpreted domain-specific language Phonix.
碍人泪离人颜 2024-09-09 21:20:54

您需要实现解析器和解释器。

有一本很棒的免费教科书,名为“编程语言:应用程序和解释”,它使用方案来构建越来越复杂的解释器。它还可以很好地介绍编程语言的功能。

在这里查看:http://www.cs.brown.edu/ ~sk/Publications/Books/ProgLangs/

如果您不喜欢Scheme,那么它可能值得研究一下。

You need to implement both a parser and an interpreter.

There is a great free text book called "Programming Languages: Application and Interpretation" that uses scheme to build increasingly more complex interpreters. It also serves as a great introduction to programming language features.

Check it out here: http://www.cs.brown.edu/~sk/Publications/Books/ProgLangs/

If Scheme isn't your cup of tea it may be worth looking into.

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