2

A few years ago I started working on a few ideas about a programming language and as I was so excited about them I just wanted to see how they would works, so I decide to write a very simple compiler for that. As I'm more comfortable and more experienced in PHP, I just took a look at to see if it's possible to write a compiler in PHP and very soon after that I found that yes, it's possible. So I start making that and fortunately everything was okay and now it's working fine.

Those days I just wanted a place to start working on my ideas, and as PHP was fast in development (no need to compile), I had MySQL on my hands also, and debugging was really easy, etc.

Now I want to extend this simple compiler, and that's where I need your advise. My main question is "Is PHP a right tool for a compiler project?". Just suppose that I'm not gonna release it publicly, so just think about the PHP abilities to handle the task, not further problems like distribution.

I believe that it has some advantages. It's fast in development, I just edit the code and press the F5 on browser and I had my binary output right after that. I also made a text box there where I could write my simple codes, press the submit and then I had my binary output again. It was also fast to keeping and working on the parsed data in MySQL. But now I'm more worried about the script timeout for example. If it's gonna compile 10,000 lines of source code, it would timeout I guess. I know I can increase the timeout, but still worried about that. Also I know that PHP as a scripting language is (as I heard) 10 times slower rather a compiled-application.

So, do you think I have to switch-off to the C? (which I'm also okay with that also) ... or do you have any ideas if I could continue with my PHP back-end, but to handle more serious things and without facing critical mistakes?

Update:

Yes! the project is personal and for fun. You may consider that also!

Regarding application for a PHP-based compiler, yes, it's not a real-world compiler, but imagine if you want to share your ideas with others, it would be great if you gave them a web form to write their code, press the button and download the binary code. It's not my goal, however, I just wondering about that.

Regarding Lex/Yacc, my ideas was more about optimizing the final binary code, so I needed something more than just generating a binary code via Lex/Yacc.

4

4 回答 4

7

PHP 不是编写编译器的理想环境。可行吗?当然。你应该这样做吗?我强烈反对用 PHP 这样的高级语言编写编译器。我也反对不必要地重新发明轮子。

如果是为了好玩,我会说去吧,但我目前还没有看到 PHP 驱动的编译器有任何实际用途。

于 2012-10-03T06:12:52.940 回答
2

可以用任何语言编写编译器(甚至可以使用一些非图灵完备的语言)。但是为了让生活更轻松,您需要 PHP 中缺少的某些语言功能,并且由于它是一种非常低级的语言,因此不太可能将这些功能添加到该语言中。

用于实现编译器的体面语言必须包含:

  • 某种形式的代数数据类型,甚至与完全动态的语言(如 Lisp)相关
  • 模式匹配,越强大和表现力越好 - 越好。在没有模式匹配的情况下编写 AST 转换是一种痛苦。

这是最低限度的。对图形有一些不错的原生支持是一个优势。具有对解析的嵌入式支持非常有用(但解析通常并不那么重要)。在运行时访问 Prolog 或 Datalog 非常有用(但在 PHP 中实现您自己的 Prolog 应该很容易)。

于 2012-10-03T07:40:51.253 回答
1

Webjawns 说:但目前我没有看到 PHP 驱动的编译器有任何实际用途。所以????Smarty - 是一个编译器!!!

如果您的编译器只是为了好玩或特定的 php 相关用途 - 将 php 作为环境是个好主意。

但是,如果您的编译器有性能要求或内存限制 - 使用 PHP 是非常糟糕的主意。

于 2012-10-03T07:27:22.797 回答
0

有一个计算机程序员的噱头听起来像:

菜鸟:为什么C被认为比Java“运行得更快”/“使用更少的内存”/“在此处插入其他优化比较”?
导师:因为 Java 编译器是用 C 编写的,而 C 编译器是用 C 编写的(带有用于优化的汇编痕迹)。

几乎所有的编译器都是用 C 编写的。同样的事情也适用于几乎都是用 C 编写的操作系统。几乎所有高级语言使用的主要预打包库都是用 C 编写的。几乎所有的高负载服务器(Apache & MySQL 包括)是用 C 编写的。基本上几乎每个关键任务软件都是用 C 编写的。

考虑到这一点,id 说:

为了好玩,我会在 PHP 中编写一个 Tokenizer -> Parser -> Transcoder 链——甚至可能是一个带有数据库存储的链;但我会输出文本(另一种语言的源代码)。

我不会用 PHP 编写编译器,因为我不想从 PHP 输出二进制文件——不是因为它不能,而是因为我不喜欢使用字符串进行二进制算术。

我用 C 编写的编译器。

于 2012-10-03T06:31:21.077 回答