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.

I often hear the term that language A is written in language B. For example, PHP has been written C, C# is written in C++.

Can someone please explain what does that mean and if it is even correct? Does that have anything to do with the compiler of interpreter used by the language?

In addition what are the factors on which the choice of the implementing language is built upon?

share|improve this question
18  
Strictly speaking, "PHP has been written in C" is wrong. A language is per se a formal definition, therefore it isn't written in another programmer language (but rather in English); only the compiler, interpreter and/or library can be written in C, C++ or whatever. In practice, for many languages there is one dominant compiler or interpreter, and the distinction between language definition and implementation isn't made. –  user281377 Nov 20 '12 at 11:41
2  
Can the down-voter please explain why? –  Songo Nov 20 '12 at 14:59
5  
PHP "per se", is not a formal definition. It is a C program. –  Kaz Nov 20 '12 at 16:28
8  
s/written/implemented/ and it's much clearer. –  TMN Nov 20 '12 at 17:02
1  
@ugoren There were plenty of C compilers written in assembly. Not so much this century, though. –  Ross Patterson Nov 21 '12 at 2:36

7 Answers 7

up vote 20 down vote accepted

Most programming languages fall in two categories: interpreted, and compiled languages.

A compiled language is translated by a compiler into machine code, the language the CPU directly executes step by step. An interpreted language, on the other hand, uses an intermediary, an interpreter, to run the language code. The interpreter is itself another program, usually itself compiled to machine code.

PHP is an interpreted language. You need a separate program to run PHP code, the computer does not run the program directly. That separate program, the PHP interpreter, is itself written in C.

C# is a compiled language, but it is not compiled to machine code. Instead, it is compiled to a specialist language, byte code, to be run on a virtual machine. Java is another example of such a setup. You could see it as a hybrid between compilation and interpretation, where the virtual machine is an interpreter. The virtual machine for C# (the CLI, or Common Language Infrastructure) is written in C++.

Other examples are:

  • Python: The Python interpreter compiles Python code to Python bytecode, then interprets the bytecode. The interpreter itself is written in C. New implementations have since been added, including one that compiles python to run on the same CLI used for C#, called IronPython, and one that runs on the Java virtual machine, Jython. To complete the circle, there is Python version written in Python, PyPy.
  • Ruby: Ruby started out as a pure interpreted language, but the most recent version switched to using bytecode. For Ruby, too, there is a project that compiles to the CLI, named IronRuby, and one for the Java VM, JRuby.
share|improve this answer
    
I'm sorry, how is a virtual machine any different than an interpreter? I don't see how using one is a halfway point to compilation. Are you saying that bytecode is half compiled? –  Philip Nov 20 '12 at 17:37
1  
@Philip: Byte code is not machine code; so instead of providing the CPU with direct instructions you still need an interpreter to take the byte code and interpret that, translate it into machine instructions. The advantage is that the virtual machine is simpler to port to other architectures, and you can apply tricks such as JIT compilation. –  Martijn Pieters Nov 20 '12 at 17:41
    
Does anyone feel like the term "compiled" has been diluted for marketing purposes? –  Philip Nov 20 '12 at 19:28
1  
Whoa! I take that back. I was going down the wrong road there for a little while. I presumed that "compiled" meant turning into machine code and only machine code, which isn't actually true. It's just a term for translating code into other code. Be that machine code, bytecode, or whatever language you want. Also, it turns out there are PHP compilers out there, so you can only say it's "typically" interpreted. –  Philip Nov 20 '12 at 20:14

You are basically right. If it is said that Ruby is written in C, this means that the language interpreter and parts of the core library are written in C.

So the Ruby interpreter is a C program that takes a text file as input, processes it and then calls functions that are either in another text file (if written in Ruby) or that are compiled C code, as much of the basic functionality that needs to directly access system resources like memory, the file system and more. And some functions that require very high performance.

So you have different parts of a language that can or have to be written in other languages. Nothing would keep you from writing the interpreter in C and the libraries in C++ (though maybe making a few things more difficult). You could even have multiple steps and use a language that is very good at text processing to generate some intermediate data which then is processed by some C code.

Factors for the decision may be just the same as for other complex applications. Performance is one. The ability to write code that can access system resources directly another. So in most cases it has to be a compiled language (though in theory you could write a Ruby interpreter in Python). Availability on different systems is important if you want your language to run on Linux, Win, OS X and others.

share|improve this answer
    
Does anybody know why I see three upvotes for my answer the moment I posted it? –  thorsten müller Nov 20 '12 at 11:44
1  
I see four now, but I'm not really sure what you're asking? Did the upvotes appear too fast? If so, well, lots of eyes on the question (three almost simultaneous answer), and your answer is good. –  Yannis Rizos Nov 20 '12 at 11:49
    
Hmm, yes. Maybe I had saved it and then edited, saved again and forgot about the first save (I'm getting old). For me it appeared as if I got the first three upvotes the moment I posted. –  thorsten müller Nov 20 '12 at 11:56
    
@thorstenmüller +1 for "Nothing would keep you from writing the interpreter in C and the libraries in C++" I was just about to ask you about this. Is there any famous implementations for this where the interpreter/compiler is in one language while the core libraries is in another language? –  Songo Nov 20 '12 at 17:48
    
@thorstenmüller I've had that happen on occasion a few times. If several someones were viewing the question as you posted, there'd be a small message saying "A new answer has been posted" within a second or two of you hitting submit, so they could have skimmed the entire answer and upvoted within 10 seconds of your posting it. Additionally, edits made within 5 minutes of posting the answer don't show up in the edit history, which could have further caused minor confusion on your part. –  Izkata Nov 20 '12 at 18:07

It simply means that most of the core of language A is written in language B. What "core of language A" might differ from language to language, but in general terms you guess right, it means it's compiler or interpreter. The deciding factor on picking a language to write another language in is, as with almost every project, what languages the developers are more familiar with.

That said, "language A is written in language B" is an oversimplification for most modern languages. If we take Python as an example, while the reference implementation, CPython, was indeed written in C there are implementations written in other languages, like Jython (written in Java), IronPython (written in C#), PyPy (writen in Python), CLPython (written in Common Lisp), Stackless Python (written in C and Python) and Unladen Swallow (written in C++).

A programming language is a definition, and as the Python example shows, there aren't really any restrictions on what languages its compiler, interpreter and libraries can be written in. And of course it's also possible for a language to be written in itself, through a process called bootstrapping.

share|improve this answer
2  
I wouldn't call Psyco another implementation, since it runs as an extension to CPython. –  Martijn Pieters Nov 20 '12 at 12:01
    
@MartijnPieters It's also a dead project, according to its site. Removed. –  Yannis Rizos Nov 20 '12 at 12:19
    
@YannisRizos Isn't Unladen Swallow dead as well? –  Andres F. Nov 20 '12 at 17:27
1  
@Songo: Other than that string.lower(s) is a python function that delegates that to return s.lower(), that is correct. The CPython 3.3 string case operations are implemented in C. –  Martijn Pieters Nov 23 '12 at 11:59
3  

A very similar phrasing with completely different meaning is "writing language A in language B", e.g. "writing C in Java".

This describes code that is syntactically correct in one language, but uses structures, idioms and conventions from another language. In the "writing C in Java" example, signs of this would be declaring all local variables on top of each method, using integer constants instead of enums, using identifiers_with_underscores, etc.

Typically this happens when someone has worked with one language for a long time (especially when they have worked only with that language) and is very new to the current language (or not interested in writing clean code).

share|improve this answer
    
"CPython is written in C" definitely does not mean "this user writes in Python like it was C". It means CPython (Python.exe on windows, /usr/bin/python on Unix) is written in C. –  Warren P Nov 20 '12 at 19:30
    
@Warren P: sure, but the phrases are very similar, so people not familiar with either one could easily end up here looking for an explanation. –  Michael Borgwardt Nov 21 '12 at 9:42

Technology is an inherently iterative process. We start with simple tools and then use those tools to make better ones. The first assembly languages were pretty much 1:1 translations of the standardized instruction bytecodes for the chip; the 8086 architecture and its assembler became dominant over other architectures like Z80, RISC, etc, and so we began to develop languages that could be digested into 8086 assembly, like FORTRAN, COBOL, Pascal and C. The program that interprets the source code of these languages has to be written in something more primitive, otherwise you end up in a chicken-and-egg argument; if the source code for the first C compiler was written in C, then what compiled that C source code, and wouldn't that, by definition, be the first C compiler?

Basically, "C# is written in C++" should be taken to mean that the first and/or most popular compiler and runtime/core libraries that obey the specification of the C# language (those being Microsoft's .NET Framework, and the command-line compiler program CSC.exe) are written in C++.

share|improve this answer

From the perspective of using a programming language, a programming language is just a program. It might be a compiler, or it might be an interpreter, or it might be some sort of virtual machine. All of those things are just computer programs, and thus can be written in any language.

So, if you wanted to create your own version of PHP, you might start out with whatever language you are most fluent in. You would then write a program that can read PHP-formatted code and do whatever the PHP spec says your program should do. You are thus creating the PHP language in language X.

share|improve this answer
    
Interesting point. So basically if I have a built-in function in PHP explode which takes a String and returns an Array, its implementation (i.e. the code that will operate on the string to produce the array) is written in C, right? –  Songo Nov 20 '12 at 15:47
    
@Songo: correct. Again, PHP is just a program, no different than Word or Apache or Notepad or vi or emacs. It reads data in and parses it according to a language specification, then does whatever the language specification says it should do. –  Bryan Oakley Nov 20 '12 at 17:00
    
This answer badly conflates language with implementation. –  Russell Borogove Nov 20 '12 at 19:17
    
I think this is the simplest and most direct answer and I don't see how it conflates anything. It even suggests that there might be more than one implementation of PHP. There are in fact, several, the original PHP, and the Facebook thingy, and there might be others. –  Warren P Nov 20 '12 at 19:33
    
@RussellBorogove: do you not think that "from the perspective of using a programming language" helps clarify the answer? Remember, we are dealing with an absolute beginner with ths question, so sacrificing a little precision to illustrate the point is fair, IMO. –  Bryan Oakley Nov 21 '12 at 0:38

"Language A is written in language B" means that the only implementation of language A (or the only one that is widely used) is the one that is actually a project developed in language B, and the only complete, up-to-date specification of A is the B source code which implements it such that if the documentation and the B program disagree, the B program is usually deemed correct.

share|improve this answer
    
There isn't one authoritative implementation of C++. In the case of C++, the spec is correct, and undefined behaviour in the spec may do anything in your implementation. So no, this is not correct. –  Warren P Nov 20 '12 at 19:39
    
I do not see what the previous comment has to do with my answer. I didn't make any universally quantified statement about all languages, and so the C++ counterexample is not applicable. A statement of the form "A is written in B", where A is "C++", does not make sense, except when B is "English". –  Kaz Nov 20 '12 at 20:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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