views:

1853

answers:

10

What is the best language to write a compiler in (not for)?

+2  A: 

The one with the best libraries for supporting that sort of task. In this case, probably C/C++, but I would look at the following for doing such things:

ANTLR and YACC.

I would also note that your answer would depend on the environment/platform you are targeting. When targeting the Java platform using bytecode, the tools might take a different tack. When using the CLR/.NET, your answer will differ there as well.

I like the answer of using your own language, perhaps read about bootstrapping before doing so.

altCognito
I would look at something like ANTLR (http://www.antlr.org/) rather than lex/yacc...
Denis Troller
Specialized tools such as lex and yacc are not always necessary, depending on the language you work in. It's so easily to write reasonably complex lexers and parsers directly in Haskell (either using Parsec or rolling your own) that a separate tool to do that seems rather pointless.
Curt Sampson
A: 

Brainf*ck

Not Sure
worth a lol ;)
Greg B
A: 

Many compilers are written in C or ML type languages. Which one is the best is subjective.

Unknown
Why the downvote? Most languages really are written in those languages, at least for a bootstrap. Its either that or assembly. My answer is practically the same as accepted answer...
Unknown
It was downvoted because there *are* serious advantages on the ML side, which are discussed in detail in the accepted answer. Perhaps there are some for C, as well. This answer contains very little useful information, beyond mentioning ML.
Curt Sampson
@Curt, and yet the accepted answer did little more than to say: "ML GOOD because I use it and some Yale professor did it one semester. Also Me like ML languages, so here is a list of them"
Unknown
That was Norman Ramsey. He has a track record.
Stephan Eggermont
+1  A: 

It depends on what language you're targeting. Microsoft makes it much easier to write a compiler targeting .NET using .NET itself (C#) as seen here. There are also books specifically for .NET language design. If you are targeting a compiled language like C though, it is usually a popular choice to write the compiler in C itself, and the lower level bits in assembler. This article called "Lets build a compiler" may be of interest to you, it covers compiler theory using the Pascal language although the ideas expressed are easily convertible to any language.

John T
One of the books this post is referering to is "Build Your Own .NET Language and Compiler" which, I think, is a really terrible book. I will not state why, but read both US Amazon and UK Amazon reviews. You will found out, that the writer himself is giving the book 4/5 in US Amazon and 5/5 in UK Amazon - nice ;)
lasseespeholt
+12  A: 

If the language you're designing is sufficiently capable, then building the compiler in its own language is a fantastic choice. There's nothing quite like ensuring your language is completely robust than working with it every day to solve a complex problem.

C compilers are typically written in C; C++ compilers are written in C++, the C# compiler is written in C#, and the list goes on from there.

Greg Hewgill
I don't think the MS C# compiler is written in C# yet - is in C++ and they are working on a C# rewrite now. Same thing for VB, I think (currently C++, will rewrite in VB). The F# compiler is written in F# though.
Brian
A: 

Look at http://www.dabeaz.com/ply/

S.Lott
A: 

I would say to use the language of which you are most familiar. If your grammar is small you can probably hand code it in any language, I build a small graphics processing language in Delphi by hand becuase the language was simple and the toolset we were using was built around delphi.

If I was builidng a larger language system, lexer/parser, code generator, linker/loader etc. I would probably use C becuase of the library support for those types of activities and the executiong speed means source text cna be processed pretty quickly. However, with enough determination you could do this with C#, java, python or ruby if you understand the concepts of building a compiler/translator.

MikeJ
+37  A: 

Building compilers is essentially rewriting trees. This is the killer app for the ML family of languages (Standard ML and Objective Caml and, to a lesser extent, Haskell. (Note: I love Haskell, but if all the questioner wants to do is write a compiler, I think ML is easier for beginners because the time and space behavior of the compiler is much more predictable, and it is possible to build the compiler without having to grok the IO monad, type classes, and other advanced ideas.)

Professor Zhong Shao of Yale University reported that when he changed Yale's compiler class to use Standard ML (from C I believe), the class was able to complete a full compiler, which normally took a semester, before Thanksgiving, leaving the rest of the term for advanced topics.

After Dave Hanson published his superb book with Chris Fraser on engineering lcc, I asked him what he and Chris had learned after spending ten years carefully designing and implementing lcc. His response:

C is a lousy language to write a compiler in.

I personally have written compilers in both Standard ML and Objective Caml. I have worked extensively on other people's compilers written in C, Haskell, Modula-3, and Standard ML. While today I have over 20 years of experience and am very happy programming in Haskell, for your first compiler I recommend Standard ML. Objective Caml is also a very good choice. The two languages grow from similar roots, and you can read about the differences on Stackoverflow.

Norman Ramsey
+1 for awesome :)
altCognito
Some time ago I reimplemented some DSL compiler from SML into Java; funny, but Java implementation was more compact than SML one. So I think any high-level language will go (not C of course).
Do you recommend I write my first compiler in ML over Haskell even if I know Haskell fairly well and I'd have to learn ML? From the earlier part of your answer, it sounds as if the two would be equivalant, with the exception that if you know neither, or know only ML, you'd have to learn more Haskell-specific stuff to use Haskell effectively.
Curt Sampson
@Curt: if you already know Haskell, go for it. I've written one compiler in Haskell and am working on another, and it's a blast.
Norman Ramsey
+1  A: 

This is probably not what others will suggest, but you might want to check out the Parrot virtual machine and its Parrot Compiler Toolkit for languages targeting it. There are already implementations of several languages using Parrot as their runtime/compiler, including APL, JavaScript, Python, Scheme, Perl 1, and (the reason Parrot was started in the first place) Perl 6. It's cross-platform, too - several of the developers are on Windows, Linux, the various BSDs, etc.

Links:

Parrot homepage

Parrot development wiki

List of language implementations (with links)

eternaleye
+2  A: 

I second the ML idea. I like F# (available on Mono or .NET) as an ML variant that gives you lots of good framework support. For a parser, I've been a fan of http://www.antlr.org/. If the language you're writing is relatively general purpose, writing a compiler in it is a good exercise in making sure your compiler is full featured and bug-free. But of course if you're writing a language meant to do something very different, you're barking up the wrong tree.

Sebastian Good