How do you write a compiler for a language in that

2019-04-20 11:48发布

Possible Duplicates:
How can a language's compiler be written in that language?
implementing a compiler in “itself”

I was looking at Rubinius, a Ruby implementation that compiles to bytecode using a compiler written in Ruby. I cannot get my head around this. How do you write a compiler for a language in the language itself? It seems like it would be just text without anything to compile it into an executable that could then compile the future code written in Ruby. I get confused just typing that sentence. Can anyone help explain this?

7条回答
走好不送
2楼-- · 2019-04-20 12:33

It's a 2 step process:

  1. write a Ruby compiler in some other lanaguage like C, assuming a Ruby compiler doesn't yet exist
  2. since you now have a Ruby compiler, you can write a Ruby program that is a (new) Ruby compiler

Since somebody already wrote a Ruby compiler (Matz), you "only" have to do the second part. Easier said than done.

查看更多
登录 后发表回答