Possible Duplicate:
How do I create my own programming language and a compiler for it

I want to create a new general purpose language that will compile to JavaScript and I'd like to be able to write it in that same JavaScript.

I wouldn't go much into detail about why, but how can I do it? Currently I have professionally coded in JS, Java, Groovy, PL/SQL, PHP among the rest, so I'm covered on the language user side of the story, but I have no theoretical or practical knowledge from the create language side.

I have browsed the web for the past week and noticed a soup of acronyms and/or names like RR, LALR, BNF, AST etc. so I figure I need two things:

  • Good Book(s) about theory
  • Working projects or experiments that I can use to jump start my learning (something free like that narcissus)


No, this question does not cover the EXACTLY SAME content and it is not on the same topic even worse, even though there are two good answers, they don't answer how I can practically USE JS TO BUILD THE TOOLS (JS TRANSPILER) I NEED FOR THE NEW LANGUAGE.

Any ideas?

  • 1
    So you want to learn how to write a compiler. Ever done something like that before?
    – user1249
    Aug 15, 2012 at 7:47
  • No. Just created some DSL's in Groovy.
    – Azder
    Aug 15, 2012 at 7:53
  • 4
    Great. Compilers are fun :)
    – user1249
    Aug 15, 2012 at 11:14
  • I have read that one before posting. Too general, not helpful.
    – Azder
    Aug 16, 2012 at 5:26
  • 1
    @Coder, you're so terribly wrong! Everyone have to design languages. Just literally everyone. Because using the DSLs is the most powerful technique out there.
    – SK-logic
    Aug 27, 2012 at 8:41

2 Answers 2


First, forget about LALR and the other intimidating words associated with the dragons. There is a nice and friendly one, behold: PEG. A ready to use implementation for JavaScript is available.

After you've done with the parsing you will have to transform your Abstract Syntax Tree in one or several steps into an AST of JavaScript and serialise it into text. There are many schools of thought in this area, but my personal preference is to use the very simple and obvious term rewriting model. You can draw your inspiration from things like this.

An unmodified pure JavaScript is not quite suitable for implementing compilers straight away, since it does not provide any decent pattern matching facilities, so I'd recommend to implement a eDSL layer as above first before starting to implement your language transforms.

Depending on what kind of type system your source language will have, you may want to implement various kinds of typing algorithms. For some of the practical strict type systems, an embedded Prolog might help a lot. E.g., implementing a Hindley-Milner algorithm in Prolog is trivial. Otherwise, a simple type propagation implemented with trivial term rewriting will be sufficient.

If you want to perform some optimisations, consider employing intermediate representations such as SSA or CPS. You may possibly need a decent graph handling library for it.

  • 2
    there is some good stuff here, but overall I fear it may be a bit intimidating, of course you could argue that's because the problem itself deserves a bit of fear and respect.
    – jk.
    Aug 15, 2012 at 7:19
  • 1
    Well, I'm not intimidated, I wouldn't have asked otherwise :)
    – Azder
    Aug 15, 2012 at 7:53
  • 1
    @SK-logic "really, really, easy" might be over-simplifying a bit.
    – user1249
    Aug 15, 2012 at 11:24
  • 2
    I think the hardest thing about a compiler is providing useful error messages. Or maybe the developers just don't care? (Except Martin Odersky and the Clang crew.) Aug 15, 2012 at 11:55
  • 1
    @Giorgio, Limited grammars, presence of a dedicated lexing step, lack of functionality for error reporting and error recovery, not possible to implement symmetric pretty-printing, autocomplete and other forms of online hinting, etc. It is easier to say what is not wrong with the old automata-based parsing approaches: performance. Everything else is just pathetic.
    – SK-logic
    Aug 24, 2012 at 13:28

It is told that Jeremy Ashkenas was inspired to create CoffeeScript after reading this very book. Since CoffeeScript is one of the most popular languages for javascript as target language, I guess you also can find some useful information there.

Not the answer you're looking for? Browse other questions tagged or ask your own question.