Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

After doing a bit of programming, I've become quite curious on language design itself. I'm still a novice (I've been doing it for about a year), so the majority of my code pertains to only two fields (GUI design in Python and basic algorithms in C/C++). I have become intrigued with how the actual languages themselves are written. I mean this in both senses. Such as how it was literally written (ie, what language the language was written in). As well as various features like white spacing (Python) or object orientation (C++ and Python).

Where would one start learning how to write a language? What are some of the fundamentals of language design, things that would make it a "complete" language?

share|improve this question

closed as too broad by Kilian Foth, thorsten müller, amon, ChrisF Jan 19 at 22:07

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
I saw a question on here a while ago similiar to this and the most common answer was: dont. Writing your own answer is a major ballache. –  benhowdle89 Mar 1 '11 at 10:24
    
Generally speaking, there are two major aspects: One is to define a language, its syntax, semantic etc; the other one is to write an actual compiler or interpreter for that language. With only one year of experience, you probably should not do either. –  user281377 Mar 1 '11 at 10:30
    
ammoQ I'm not aiming to write a language, right now at least. However, I'm intrigued by the process and what's involved. –  RectangleTangle Mar 1 '11 at 10:44
    
This is the book you want: dragonbook.stanford.edu –  Roger Alsing Mar 1 '11 at 10:57
    
There are 3 most difficult tasks in programming: compilers, kernels and pessimistic transaction coordinators. One should be really really unhappy with what he/she finds available to start new compiler. –  user7071 Jun 4 '12 at 2:38
show 1 more comment

6 Answers

As with "normal" programming, LOGO is probably the language you should start with.

If you have lots of time, you can start writing a naive LOGO interpreter first (you will probably run into some obstacles and commit some common mistakes, but that's the whole point of this exercise).

The next step is to look at how context-free parsing works, LL(k)/LR(k)/Earley parsers, ASTs and so on.

That's obviously just the first step of processing the source code, but once you have it, you can progress to symbol tables and could probably write a LOGO-to-C compiler (you could of course compile to machine code directly, but it wouldn't add much to the "feel" while making debugging a nightmare at this point).

You'll find your way easily from there. :)

share|improve this answer
add comment

If you want to do it the right way, I'd recommend you to start by reading the following books:

http://www.cs.cmu.edu/~jcr/tpl.html

http://www.cambridge.org/gb/knowledge/isbn/item1132720/?site_locale=en_GB

And please, do not start with the Dragon book. It is outdated and parsing-centric. You'd have to read it any way, but not now.

share|improve this answer
add comment

There are many ways in understand how compilers are built. In the simplest definition compilers are programs that take your source code and convert it to a form valid executable in a form or another(VM or machine language).

So in order to convert them, it has to first understand them. Its like the compiler program you write has to successfully understand millions of possible combination of valid programs that can be written in the language. There for in order to understand them it has to ...

a. Parse them : This step itself is composed of many steps. Since the program it self can have data and other stuff. It has to first recognize valid lexical tokens(What you call keywords). Doing this requires reading characters one by one, and then matching it against a template. Its like this, how do you recognize that a sentence is a valid English statement? You take the rules of English grammar and apply to the vocabulary in question.

A similar thing happens inside compilers. There is something called as a language grammar! Which fundamentally defines what is legal syntactically. Now writing a parser for each grammar that comes down the way is manually laborious and not practical. Hence there are parser generators. They work by, taking a grammar and then generating a parser for it. How does the parser look? There are many ways to do that, starting from using regular expressions to reading each character one by one and matching them until a valid lexical token is encountered.

b.Making sense of what has been read: You can make a grammatically valid sentence yet not make sense out of it. Same thing needs to be checked in a compiler too. What does the syntax notation mean... This is nothing but semantics.

c.You now what it means, now you want to mean the same in other languages: What is the equivalent of what I just parsed in Assembly. Its like you now perfectly parsed and recognized lets say a if statement. Now you take that convert it to equivalent of that in assembly.

d.Meanwhile optimize,what you can optimize: If there are any reasonable code optimizations can be done along the way, do it.

This is brief overview of compilers work in common language. Of course there is a lot to it. You can write volumes on it(And there are volumes on it, already written).

What you should do:

  1. Read some good theory on Compilers. Dragon book recommended.
  2. Download a opensource Compiler. Plenty available.
  3. Try to map what you have read from the book to the code.
  4. Break something and see how it works.
  5. Add something and see how it works.

Then:

  1. Write something on your own.
  2. Search for interesting problems to solve and solve them.
  3. Browse bug lists in those open source projects, Try sending patches to them.

Remember you can read a lot, but you will tested only when you write code. So write code. Fail often, learn from it. Use the feedback... Repeat the cycle again.

share|improve this answer
add comment

You can start with the implementation of Python if you want, its just C. If you want something simpler check out SIOD (Scheme in one Defun) its a very small footprint scheme implementation. Since Scheme has such a simple syntax it is very easy to understand it.

Actually now that I think about it Structure and Interpretation of Computer Programs includes a complete scheme implementation. So it might be a good place to start. And it will teach you a lot.

share|improve this answer
add comment

Very few of today's popular languages are the product of a comprehensive up-front design. Instead, there was an initial implementation by one or two people who were frustrated in some way with the available languages. Then some other people tried it and liked it, and over time, features were added. Eventually some desirable feature can't be added without breaking existing programs. Then someone creates a new language.

Completeness is a partially a matter of opinion, but not totally. Paul Graham wrote a fine article about the relative power of programming languages, introducing an idea now widely known as the Blub Paradox.

share|improve this answer
    
+1. I was thinking the same. Everyone who was really unhappy with something, tried to make a new language. The 1960-70 were full of unhappy people. –  user7071 Jun 4 '12 at 2:34
add comment

Sounds snobby, but if you have to ask, you really should not.

No More Turing Complete Languages

Like in conceptual art, you kind of need to learn everything that was done in the field since java inception (and I'd have said since C inception, a couple of years ago) to get a decent language out right.

And still you risk of emitting a useless behemoth that cannot be successfully applied to some interesting domain.

...or another lisp dialect, or another coffeescript. (see, those have practical applications and usually are conceptually sound, but then again, put in perspective, usually happen to raise more problems then what they solve, and making problems worse is not a good reason to write a new language)

share|improve this answer
1  
You may be right, but this still doesn't answer the question. –  Caleb Jun 4 '12 at 11:53
add comment

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