How to create my own programming language?

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

badloy1135
Posts: 2
Joined: Sun May 12, 2013 11:53 am UTC

How to create my own programming language?

Postby badloy1135 » Mon May 13, 2013 12:59 am UTC

I have a project that I will create programming language but I don't know how to start... but I already have a little bit of knowledge about it.

User avatar
Tomlidich the second
Posts: 1230
Joined: Thu Mar 22, 2012 5:38 pm UTC

Re: How to create my own programming language?

Postby Tomlidich the second » Wed May 15, 2013 4:47 pm UTC

what languages do you already know?
you may end up finding there is already one that will suit your purposes, i would be very surprised if there wasn't.
Image

korona
Posts: 495
Joined: Sun Jul 04, 2010 8:40 pm UTC

Re: How to create my own programming language?

Postby korona » Wed May 15, 2013 11:43 pm UTC

I have developed two programming languages (i.e. one of them is pretty useable, it is complete enough to be self-hosted and I was able to write an operating system kernel and simple userland in this language; the other one is work in progress) so far because I did NOT find a language that included every feature I wanted. Doing that however is not trivial and requires MUCH work (the compiler stack,including a custom assembler and linker + the kernel is 35k sloc).

First read about parsers and context-free grammars. There are a few methods that work very well for hobby programming languages. There are parser generators like yacc (very fast) and parse expression grammars (very simple) and if you need a parser for arbitrary context-free grammars you can use Earley's algorithm (very powerful) which is O(n^3) in the worst case but is fast enough for most grammars.

Then think about code generation. Try to write an interpreter for brainfuck or another simple language first. Then write a brainfuck to C compiler. You have to choose a target language for your compiler. Good choices would be C, LLVM or assembly, in increasing level of usefulness but also complexity.
Write a compiler that accepts arithmetic expressions (e.g. 1+5*3) with correct operator precedence and then expand that to include variables and control structures and any construct you want in your programming language.

Code generation in general requires much thought but unless you don't want advance features as a C++-like template system it is not that hard. Generating efficient code is usually incredibly hard. Designing efficient register allocators and instruction scheduling algorithms is difficult. You can circumvent those problems if you compile your code to C and let the C compiler take care of optimizations. That is not always possible though, for example if you need custom exception handling or a tracing garbage collector (as the C compiler will not generate proper stack maps that are required for those techniques).

DeGuerre
Posts: 51
Joined: Mon Feb 04, 2008 6:41 am UTC

Re: How to create my own programming language?

Postby DeGuerre » Thu May 16, 2013 2:19 am UTC

I agree with korona's advice for your first programming language. I'd be tempted to invest in a copy of the dragon book (Aho, Sethi & Ullman's Compilers: Principles, Techniques, and Tools) or the tiger book (Appel's Modern Compiler Design in C/Java/ML; the C and Java versions make more sense if you translate the example code back into ML).

However, bear in mind that while you need to know about parsers and context-free languages, syntax isn't the most interesting part of a programming language. What's interesting is the features of the language. So for your second language, write abstract syntax first, and then work out how to represent it in plain text.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: How to create my own programming language?

Postby EvanED » Thu May 16, 2013 4:18 am UTC

I'm not a fan of the dragon book for learning the basics (as opposed to reference). But if you do get it, definitely get the second edition; this isn't your "nothing changed but the exercises" update. It's the most complete intro-to-intermediate compiler book I've seen (and I've taught a compiler course twice, so I've seen a lot) but I don't like the organization and don't think it's the best at explaining things. I'm also a proponent of LL grammars over LR grammars, thus I don't like how much they favor LR, and I think there is no coverage of LL parser generators.

However, I also don't have a great suggestion for what to actually use. The first time I taught it I suggested getting Cooper et al's Engineering A Compiler, but for the primary source of material (especially the second time) I used these notes written by a professor who commonly taught that course. That's not my page, and I made some somewhat heavy modifications to those notes*; if you want, I can make my versions available. IMO they are probably easier to follow than the dragon book, and there's also a lot less of them, but they are definitely less complete. (Still, they seem to suffice for purposes of a class during which the students create a working compiler.)

The Appel book also seems decent. I don't know it as well as the other two I've mentioned though. (Though I do have a copy of the Java version!)

* Just a warning, this link may go down soon. The semester it applies to is ending, and I'm not sure when our IT people update the course links for the next term.

DeGuerre
Posts: 51
Joined: Mon Feb 04, 2008 6:41 am UTC

Re: How to create my own programming language?

Postby DeGuerre » Thu May 16, 2013 7:01 am UTC

One more thing, while I think of it.

There's lots of decent open source these days. Finding a small but real interpreter (something like Squirrel or Lua would be decent choices these days) and trying to understand it will probably help.

alessandro95
Posts: 109
Joined: Wed Apr 24, 2013 1:33 am UTC

Re: How to create my own programming language?

Postby alessandro95 » Thu May 16, 2013 10:20 am UTC

DeGuerre wrote:One more thing, while I think of it.

There's lots of decent open source these days. Finding a small but real interpreter (something like Squirrel or Lua would be decent choices these days) and trying to understand it will probably help.


Never heard of squirrel, but I use a lot of Lua, I'm everything but an expert in C, but I looked a bit into the Lua source and it is very clean and organized so if you have good knowledge of C you may find it interesting ;) (well, it is an interpreter, not a compiler, but maybe it would be easier to start writing an interpreter than a compiler)
The primary reason Bourbaki stopped writing books was the realization that Lang was one single person.

lgw
Posts: 437
Joined: Mon Apr 12, 2010 10:52 pm UTC

Re: How to create my own programming language?

Postby lgw » Thu May 16, 2013 9:49 pm UTC

There are some surprisingly different approaches to creating a programming language.
  • You can write one from scratch, which is fun as a learning experience.
  • You can start with a good flavor of Scheme and build your language by adding macros to that starting point.
  • You can write a "compiler" that emits C++, then realize that GCC is open source and just add a layer on top of it, then realize that that whole process has been done many times, and that CLANG is purpose-built for that.
  • You can create a new language on top of Microsoft's CLR, F#-style, which is neat (if you don't mind Microsoft) in that you get the whole .NET library for your language.
I personally would start by writing a Scheme interpreter in Scheme, as a learning exercise. The language spec is very short, and you get the boring parsing bit for free. When you finish you can try porting it to C, and eventually discover Rabbit, as a reference for the hard problems, which is about the most amazing journey you can take with programming languages. The Dragon Book is mostly about the boring parts, really. Guy Steele's dissertation is about transforming the abstract mathematics behind algorithms (the lambda calculus and combinator logic) into working object code by a set of beautifully simple transformations.

For me learning Scheme was "neat, this is amazingly different from languages I'm used to" and then understanding Rabbit was "oh, wow, not so different after all, once you get your head around it".
"In no set of physics laws do you get two cats." - doogly

badloy1135
Posts: 2
Joined: Sun May 12, 2013 11:53 am UTC

Re: How to create my own programming language?

Postby badloy1135 » Sat May 18, 2013 5:21 am UTC

thanks a lot for your advices. I know now to make my own code.

User avatar
snowyowl
Posts: 464
Joined: Tue Jun 23, 2009 7:36 pm UTC

Re: How to create my own programming language?

Postby snowyowl » Sat May 18, 2013 1:08 pm UTC

lgw wrote:
  • You can write one from scratch, which is fun as a learning experience.

What does this mean? One that compiles to assembly language or something like that?
The preceding comment is an automated response.

User avatar
thoughtfully
Posts: 2253
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN
Contact:

Re: How to create my own programming language?

Postby thoughtfully » Sat May 18, 2013 8:24 pm UTC

snowyowl wrote:
lgw wrote:
  • You can write one from scratch, which is fun as a learning experience.

What does this mean? One that compiles to assembly language or something like that?

http://xkcd.com/378/
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

lgw
Posts: 437
Joined: Mon Apr 12, 2010 10:52 pm UTC

Re: How to create my own programming language?

Postby lgw » Tue May 21, 2013 7:45 pm UTC

snowyowl wrote:
lgw wrote:
  • You can write one from scratch, which is fun as a learning experience.

What does this mean? One that compiles to assembly language or something like that?


You could use C to write a compiler for a (simple) language: you'd learn a heck of a lot that way, including why people don't do that much. But far more practically you could write an interpreter for an interpreted language using a low-level language, so that you don't need to mess with the platform-specific stuff. Start with a few operators and a read-eval-print loop, and build out from there.

If you like the abstract, and are comfortable with lambda, I strongly recommend writing a Scheme interpreter in Scheme first - it's a quick project to get you started.

If you like the concrete, I'd suggest writing a simple numerical equation engine (read "3*(4+5)", parse it into an abstract syntax tree, do the computation, and print "27"), then build towards C from there by adding variables, if statements, and so on.

I've had great fun doing both.
"In no set of physics laws do you get two cats." - doogly

User avatar
MHD
Posts: 630
Joined: Fri Mar 20, 2009 8:21 pm UTC
Location: Denmark

Re: How to create my own programming language?

Postby MHD » Fri Jun 07, 2013 5:43 pm UTC

If you want, learning Assembler and implementing a FORTH is a great excercise. Usually clocks in at only about 1-2k instructions.
EvanED wrote:be aware that when most people say "regular expression" they really mean "something that is almost, but not quite, entirely unlike a regular expression"

User avatar
Negrebskoh
Posts: 139
Joined: Fri Mar 01, 2013 11:49 pm UTC
Location: The Netherlands

Re: How to create my own programming language?

Postby Negrebskoh » Tue Jun 11, 2013 10:53 pm UTC

thoughtfully wrote:
snowyowl wrote:
lgw wrote:
  • You can write one from scratch, which is fun as a learning experience.

What does this mean? One that compiles to assembly language or something like that?

http://xkcd.com/378/

(Shameless plug) *cough*

User avatar
thoughtfully
Posts: 2253
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN
Contact:

Re: How to create my own programming language?

Postby thoughtfully » Wed Jun 12, 2013 1:26 am UTC

Negrebskoh wrote:
thoughtfully wrote:
snowyowl wrote:
lgw wrote:
  • You can write one from scratch, which is fun as a learning experience.

What does this mean? One that compiles to assembly language or something like that?

http://xkcd.com/378/

(Shameless plug) *cough*

Obligatory Link
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

User avatar
Sizik
Posts: 1243
Joined: Wed Aug 27, 2008 3:48 am UTC

Re: How to create my own programming language?

Postby Sizik » Wed Jun 12, 2013 5:20 pm UTC

thoughtfully wrote:
. . .

Obligatory Link


Nearly misread that first line as "If you're a programmer who's become fed up with software bloat, then may you find heroin the perfect antidote."
gmalivuk wrote:
King Author wrote:If space (rather, distance) is an illusion, it'd be possible for one meta-me to experience both body's sensory inputs.
Yes. And if wishes were horses, wishing wells would fill up very quickly with drowned horses.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 8 guests