31

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?

6
  • 19
    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, 2012 at 11:41
  • Interestingly BCPL was mostly written in BCPL Nov 20, 2012 at 16:26
  • 7
    PHP "per se", is not a formal definition. It is a C program.
    – Kaz
    Nov 20, 2012 at 16:28
  • 8
    s/written/implemented/ and it's much clearer.
    – TMN
    Nov 20, 2012 at 17:02
  • 2
    @ugoren There were plenty of C compilers written in assembly. Not so much this century, though. Nov 21, 2012 at 2:36

7 Answers 7

30

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 a Python version written in (a subset of) 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.
5
  • 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, 2012 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. Nov 20, 2012 at 17:41
  • Does anyone feel like the term "compiled" has been diluted for marketing purposes?
    – Philip
    Nov 20, 2012 at 19:28
  • 2
    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, 2012 at 20:14
  • Also a good source: youtube.com/watch?v=e4ax90XmUBc
    – Adam
    Jul 10, 2019 at 22:20
34

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.

6
  • Does anybody know why I see three upvotes for my answer the moment I posted it? Nov 20, 2012 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
    Nov 20, 2012 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. Nov 20, 2012 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, 2012 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, 2012 at 18:07
10

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.

8
3

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.

7
  • 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, 2012 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. Nov 20, 2012 at 17:00
  • This answer badly conflates language with implementation. Nov 20, 2012 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, 2012 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. Nov 21, 2012 at 0:38
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).

2
  • "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, 2012 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. Nov 21, 2012 at 9:42
3

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++.

0

"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.

2
  • 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, 2012 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, 2012 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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