views:

724

answers:

17

I need to get around to writing that programming language I've been meaning to write. How do you kids do it these days? I've been out of the loop for over a decade; are you doing it any differently now than we did back in the pre-internet, pre-windows days? You know, back when "real" coders coded in C, used the command line, and quibbled over which shell was superior?

Just to clarify, I mean, not how do you DESIGN a language (that I can figure out fairly easily) but how do you build the compiler and standard libraries and so forth? What tools do you kids use these days?

+2  A: 

I'm pretty sure you do what's always been done.

Write some code, and show your results to the world.

As compared to the olden times, there are some tools to make your job easier though. Might I suggest ANTLR for parsing your language grammar?

Kevin Montrose
A: 

Real coders still code in C. Just that it's a litte sharper.
Hmmm... language design? or writing a compiler? If you want to write a compiler, you'd use Flex + Bison. (google)

Aviral Dasgupta
If you want to write a good compiler, you'll hand-roll your own recursive-descent parser, because if a moderately complex Bison parser you'll soon run into issues (if not getting the language to work, then getting the compiler/interpreter to report errors).
Chris Lutz
+1 for the way you referred to c#
RCIX
@chris Yeah, maybe, but only for LISP (ASM, Scheme...) Manually writing a proper full blown compiler is the last thing you want to do ... just because of the complexity involved.
Aviral Dasgupta
Not really. It's not terribly complicated, especially because there are tons of books and tutorials on the subject. And all major programming languages are written with hand-rolled parsers/lexers.
Chris Lutz
I believe that is because they are not regular grammars (I remember reading something about C++ and C# not being regular grammars??)
Aviral Dasgupta
You mean "context-free" grammars, and this is partially true - Flex/Bison can only handle context-free grammars, and attempts can be made to force them to handle grammars with context, but it's very limiting. But the main reason GCC switched it's C compiler from Flex/Bison to hand-rolled was for better error reporting. (Regular grammars are parsed by regular expressions, and are rather too limited for full language design, if perhaps useful for many tasks.)
Chris Lutz
+1  A: 

You should not accept wimpy solutions like using the latest tools. You should bootstrap the language by writing a minimal compiler in Visual Basic for Applications or a similar language, then write all the compilation tools in your new language and then self-compile it using only the language itself.

Also, what is the proposed name of the language?

I think recently there have not been languages with ALL CAPITAL LETTER names like COBOL and FORTRAN, so I hope you will call it something like MIKELANG with all capital letters.

Kinopiko
BASIC? I heard someone was doing research based on whether chimps hammering away at a keyboard were tidier than production level BASIC code. Guess...
Aviral Dasgupta
Cool idea. I had picked out "Complicity" several years ago but I like the idea of an ALLCAPS language! MIKTRAN, MOBOL, MIKEBASIC, MALEVOLENT, MALT, MARKV, MINGLE, MING, UNILANG...
Mike
A: 

Not an easy answer, but..

You essentially want to define a set of rules written in text (tokens) and then some parser that checks these rules and assembles them into fragments.

http://www.mactech.com/articles/mactech/Vol.16/16.07/UsingFlexandBison/

People can spend years on this, The above article talks about using two tools (Flex and Bison) That can be used to turn text into code you can feed to a compiler.

IanNorton
+2  A: 

Speaking as someone who just built a very simple assembly like language and interpreter, I'd start out with the .NET framework or similar. Nothing can beat the powerful syntax of C# + the backing of the entire .NET community when attempting to write most things. From here i designed a simple bytecode format and assembly syntax and proceeeded to write my interpreter + assembler.

Like i said, it was a very simple language.

RCIX
powerful syntax? c#? you're kidding me. But the .net framework and the community are nice though.
Thomas Danecker
+6  A: 

One consideration that's new since the punched card era is the existence of virtual machines already bountifully provided with "standard libraries." Targeting the JVM or the .NET CLR instead of ye olde "language walled garden" saves you a lot of bootstrapping. If you're creating a compiled language, you may also find Java byte code or MSIL an easier compile target than machine code (of course, if you're in this for the fun of creating a tight optimising compiler then you'll see this as a bug rather than a feature).

On the negative side, the idioms of the JVM or CLR may not be what you want for your language. So you may still end up building "standard libraries" just to provide idiomatic interfaces over the platform facility. (An example is that every languages and its dog seems to provide its own method for writing to the console, rather than leaving users to manually call System.out.println or Console.WriteLine.) Nevertheless, it enables an incremental development of the idiomatic libraries, and means that the more obscure libraries for which you never get round to building idiomatic interfaces are still accessible even if in an ugly way.

If you're considering an interpreted language, .NET also has support for efficient interpretation via the Dynamic Language Runtime (DLR). (I don't know if there's an equivalent for the JVM.) This should help free you up to focus on the language design without having to worry so much about the optimisation of the interpreter.

itowlson
alex tingle
Not true! since the libraries for the JVM and .NET platforms don't have to worry about idiosyncrasies of the plaforms they're on, they can be free to explore API design aspects that would otherwise be left untouched.
RCIX
Matt Joiner
A: 

First I spent a year or so to actually think how the language should look like. At the same time I helped in developing Ioke (www.ioke.org) to learn language internals.

I have chosen Objective-C as implementation platform as it's fast (enough), simple and rich language. It also provides test framework so agile approach is a go. It also has a rich standard library I can build upon.

Since my language is simple on syntactic level (no keywords, only literals, operators and messages) I could go with Ragel (http://www.complang.org/ragel/) for building scanner. It's fast as hell and simple to use.

Now I have a working object model, scanner and simple operator shuffling plus standard library bootstrap code. I can even run a simple programs - as long as they fit in one file that is :)

Bragi Ragnarson
+4  A: 

I've written two compilers now in Haskell for small domain-specific languages, and have found it to be an incredibly productive experience. The parsec library makes playing with syntax easy, and interpreters are very simple to write over a Haskell data structure. There is a description of writing a Lisp interpreter in Haskell that I found helpful.

If you are interested in a high-performance backend, I recommend LLVM. It has a concise and elegant byte-code and the best x86/amd64 generating backend you can find. There is an optional garbage collector, and some experimental backends that target the JVM and CLR.

You can write a compiler in any language that produces LLVM bytecode. If you are adventurous enough to learn Haskell but want LLVM, there are a set of Haskell-LLVM bindings.

David Crawshaw
+1  A: 

Not so much an implementation but a design decision which effects implementation - if you make every statement of your language have a unique parse tree without context, you'll get something that it's easy to hand-code a parser, and that doesn't require large amounts of work to provide syntax highlighting for. Similarly simple things like using a different symbol for module namespaces and object namespaces ( unlike Java which uses . for both package and class namespaces ) means you can parse the code without loading every module that it refers to.

Standard libraries - include the equivalent of everything in C99 standard libraries other than setjmp. Add whatever else you need for your domain. Work out an easy way to do this, either something like SWIG or an in-line FFI such as Ruby's [can't remember module name] and Python's ctypes.

Building as much of the language in the language is an option, but projects which start out doing either give up (rubinius moved to using C++ for parts of its standard library), or is only for research purposes (Mozilla Narcissus)

Pete Kirkham
A: 

I am actually a kid, haha. I've never written an actual compiler before or designed a language, but I have finished The Red Dragon Book, so I suppose I have somewhat of an idea (I hope).

It would depend firstly on the grammar. If it's LR or LALR I suppose tools like Bison/Flex would work well. If it's more LL, I'd use Spirit, which is a component of Boost. It allows you to write the language's grammar in C++ in an EBNF-like syntax, so no muddling around with code generators; the C++ compiler compiles the grammar for you. If any of these fail, I'd write an EBNF grammar on paper, and then proceed to do some heavy recursive descent parsing, which seems to work; if C++ can be parsed pretty well using RDP (as GCC does it), then I suppose with enough unit tests and patience you could write entire compilers using RDP.

Once I have a parser running and some sort of intermediate representation, it then depends on how it runs. If it's some bytecode or native code compiler, I'll use LLVM or libJIT to process it. LLVM is more suited for general compilation, but I like the libJIT API and documentation better. Alternatively, if I'm really lazy, I'll generate C code and let GCC do the actual compilation. Another alternative, is to target an existing VM, like Parrot or the JVM or the CLR. Parrot is the VM being designed for Perl. If it's just an interpreter, I'll walk the syntax tree.

A radical alternative is to use Prolog, which has syntax features which remarkably simulate EBNF. I have no experience with it though, and if I am not wrong (which I am almost certainly going to be), Prolog would be quite slow if used to parse heavy duty programming languages with a lot of syntactical constructs and quirks (read: C++ and Perl).

All this I'll do in C++, if only because I am more used to writing in it than C. I'd stay away from Java/Python or anything of that sort for the actual production code (writing compilers in C/C++ help to make it portable), but I could see myself using them as a prototyping language, especially Python, which I am partial towards. Of course, I've never actually done any of this before, so I'm not one to say.

blwy10
+2  A: 

What has changed considerably but hasn't been mentioned yet is IDE support and interoperability:

Nowadays we pretty much expect Intellisense, step-by-step execution and state inspection "right in the editor window", new types that tell the debugger how to treat them and rather helpful diagnostic messages. The old "compile .x -> .y" executable is not enough to create a language anymore. The environment is nothing to focus on first, but affects willingness to adopt.

Also, libraries have become much more powerful, noone wants to implement all that in yet another language. Try to borrow, make it easy to call existing code, and make it easy to be called by other code.

Targeting a VM - as itowlson suggested - is probably a good way to get started. If that turns out a problem, it can still be replaced by native compilers.

peterchen
"the old 'compile .x -> .y' executable is pretty much dead" - hahahaha! Tell me another one. Go on that was great!
alex tingle
alex: as in "all you need for a new language is...", I guess I should clarify that.
peterchen
A: 

Mike --

If you're interested in an efficient native-code-generating compiler for Windows so you can get your bearings -- without wading through all the unnecessary widgets, gadgets, and other nonsense that clutter today's machines -- I recommend the Osmosian Order's Plain English development system. It includes a unique interface, a simplified file manager, a friendly text editor, a handy hexadecimal dumper, the compiler/linker (of course), and a wysiwyg page-layout application for documentation. Written entirely in Plain English, it is a quick download (less than a megabyte), small enough to understand in short order (about 25,000 lines of Plain English code, with just 4,000 in the compiler/linker), yet powerful enough to reproduce itself on a bottom-of-the-line Dell in less than three seconds. Really: three seconds. And it's free to all who write and ask for a copy, including the source code and and a rather humorous tongue-in-cheek 100-page manual. See www.osmosian.com for details on how to get a copy, or write to me directly with questions or comments: [email protected]

Gerry Rzeppa
A: 

On lambda-the-ultimate there's a link to Create Your Own Programming Language by Marc-André Cournoyer, which appears to describe how to leverage some modern tools for creating little languages.

Pete Kirkham
A: 

Of course older techniques are still common (e.g. using Flex and Bison) many newer language implementations combine the lexing and parsing phase, by using a parser based on a parsing expression grammar (PEG). This works for recursive descent parsers created using combinators, or memoizing Packrat parsers. Many compilers are built using the Antlr framework also.

cdiggins
A: 

Just to clarify, I mean, not how do you DESIGN a language (that I can figure out fairly easily)

Just a hint: Look at some quite different languages first, before designing a new languge (i.e. languages with a very different evaluation strategy). Haskell and Oz come to mind. Though you should also know Prolog and Scheme. A year ago I also was like "hey, let's design a language that behaves exactly as I want", but fortunatly I looked at those other languages first (or you could also say unfortunatly, because now I don't know how I want a language to behave anymore...).

Thomas Danecker
+1  A: 

Before you start creating a language you should read this:

Hanspeter Moessenboeck, The Art of Niklaus Wirth

ftp://ftp.ssw.uni-linz.ac.at/pub/Papers/Moe00b.pdf

Jim Barker
Niklaus Wirth was terrible at compiler design. He violated Einsteins Law: Make everything as easy as possible but not easier. His languages were all way to easy to be productive. By the way, i like Modula3 which was not designed by him.
Lothar