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've been programming for a while, I've written some rudimentary programs, and I want to keep learning. I've reached that point where you just don't know what to learn next, and I'd like to ask a question for my own curiosity.

The question, in a nutshell, is if you can combine multiple programming languages into 1 result? For example, can this code be possible?

<html>
cout << "Hello world!";
</html>

or

import java.util.Scanner;
cout << "Insert a number from 1 to 10";
Scanner n = new Scanner(System.in);
System.out.println("The value you entered was" +n.newLine());

This feels like a silly question but I can't possible know if it's possible or not, so that's why I'm asking it. In this question I notice he is using Python code in html code, if my above example is not possible, what did he do?

share|improve this question
12  
For this to work (without making people insane), one needs at least strict rules regarding how they interact and which parts should be processed as which language. –  delnan Feb 27 '12 at 19:12
6  
It would take one hellova lexer/parser. –  hydroparadise Feb 27 '12 at 21:26
2  
@Brian Lua is embedded in that the interpreter is linked into some C or C++ code and used to run Lua code stored in strings or external files, possibly exposing C/C++ objects to the Lua code. Lua is not (not frequently, if at all) "embedded" in the sense of this question. –  delnan Feb 27 '12 at 21:38
2  
That's like making five alphabets each with different symbols for the 26 letters and intermixing them in a single book: pointless, irritating and redundant. –  ThomasX Feb 28 '12 at 8:00
6  
I think people are clearly forgetting the most common (and only valid real-life that I can think of) use case of "combining languages" -- ASM fragments in C/C++ programs, usually for performance reasons. –  TC1 Feb 28 '12 at 10:02
show 8 more comments

15 Answers

up vote 40 down vote accepted

You first example is sort of possible. Usually such things happen in PHP (and other related web-programming languages) like this:

<HTML>
<?PHP
call_some_php_function(1,2,"a","b"); /* This is may return nothing, a text string, or actual HTML markup code */
?>
</HTML>

Some important points to note about this example:

  • HTML is NOT a progamming language, it is a markup language.
  • The PHP and HTML and not executed/interpreted in the same place: PHP code is executed by a PHP interpreter running on the server and the result is "injected" into the surrounding HTML. Then that whole blob is sent to the client/browser which renders the complete HTML.

Your second example looks like some sort of mash-up of C++ and Java. It's possible to have compiled modules written in different languages talk to each other, but to combine Java and C++ in the same source file would be extremely confusing and difficult: how would the compiler know which statements are Java and which are C++?

I suppose in theory you could write a special compiler/pre-processor with "language" indicators such as:

Java
{
    import java.util.Scanner;
}
C++
{
   cout << "Insert a number from 1 to 10";
}
Java
{
    Scanner n = new Scanner(System.in); //Actually, this line *could* be a C++ line - it's hard for me to tell just by looking at it.
    System.out.println("The value you entered was" +n.newLine());
}

But I'm honestly not sure you'd gain anything useful by doing this.

Also, how would this hybrid language environment handle language features which are incompatible between the two?

share|improve this answer
    
An example everyone forgot about (including myself), so please feel free to add it to your answer: CUDA. It's a mixture of two very different C languages, with some functions compiled for a device (GPU), and some remaining on a host, with communication layer being inferred. –  SK-logic Feb 28 '12 at 7:53
5  
It's not uncommon to have JavaScript as well as HTML and PHP on one page. Horrible, but not uncommon. –  Michael Borgwardt Feb 28 '12 at 8:07
1  
Not so familiar with PHP, but ASP.NET (whether C# or VB.NET) code generates HTML and JavaScript - and the JavaScript is often embedded right there in the .aspx page. Total separation is often quoted as an ideal, but a quick overview of the questions on SO tagged ASP.NET will give you an idea of how complex that can be. –  sq33G Feb 28 '12 at 8:33
    
@sq33G - ASP.NET generates HTML code. You can include javascript if you want. ASP.NET certainly is NOT what the author describes as wanting to do. –  Ramhound Feb 28 '12 at 12:50
    
I was going to bring up PHP/HTML/JS in one document if no one else had. –  Ben Brocka Feb 29 '12 at 15:48
add comment

Short Answer

Not really.


Long Answer

Embedding

Generally, a single source file contains code for exactly one programming language. It is uncommon for multiple languages to be combined in a single file for a couple of reasons:

  • Parsing several syntactically-different languages at once is extremely difficult (if not outright impossible).
  • Different languages treat programming differently. Haskell's notion of a function is different than C++'s.

Linking

Different programming languages that share a common application binary interface may be combined to form a single executable or library. Getting the two languages' signatures available inside each other often takes a bit of work, but tools exist to ease the process.

Polyglots

Polyglot code is valid and equivalent in more than one language. Stack Overflow's 404 page features one such program:

Stack Overflow 404 polyglot

This prints "404" in Python, Perl, Ruby, C, Brainfuck and Befunge.

Conclusion

Languages are rarely mixed within files, and when they are, it is for laughs. People even try to avoid mixing languages within projects because of the extra hassle it introduces. So, while it can be technically possible, mixing different languages is neither common nor pragmatic.

share|improve this answer
    
I see no Ruby here... –  Jason Lewis Feb 28 '12 at 4:00
    
@JasonLewis I do not know Ruby, but this post says that it evaluates line #4. –  Maxpm Feb 28 '12 at 4:03
7  
Languages aren't always mixed for laughs. Most C compilers allow you to mix Assembly and C, and so do many Forths. Back in the Eighties, one of the best BASIC dialects in the game, Acorn's BBC Basic, allowed you to mix 6502 assembly and that was a very desirable feature then. It certainly made BBC users laugh, but pretty much everyone else cried, especially the ones who had to assemble 6502 machine code by hand and stick raw opcodes in their programs in DATA statements. Not that I'm bitter or anything. Noooo. –  Alexios Feb 28 '12 at 7:33
1  
@SK-logic SQL is a Structured Query Language, not a programming language. –  Maxpm Feb 28 '12 at 20:26
1  
One of the sad things about modern software development is that multiple language apps are so much harder now. Back in the DOS days, nearly everything compiled to the same object file and used the same parameter passing conventions. It was real easy to just link them together. Nowadays, not so much... –  Brian Knoblauch Feb 29 '12 at 16:00
show 2 more comments

Yes, it is possible indeed. Not the way you've imagined that, of course. There are some languages out there built specifically for this purpose.

In practice, it might be very useful to mix a number of domain-specific languages into a single host language. There's rarely a need in mixing two or more equally "general purpose" languages together in single source code file, although you'll often find projects written in both, say, VB.NET and C#, with different language components compiled separately.

As for HTML, it's just a markup language. You may often find a server-side code (e.g., in VB.NET), HTML markup and client-side Javascript code mixed into a single source file, although it is considered a bad practice by many.

share|improve this answer
add comment

It is possible to do some combination of languages using Perl Inline which allows to write a Perl script and insert sections of code written in a different language:

Inline has support for C, C++, Java, Python, Ruby, Tcl, Assembler, Basic, Guile, Befunge, Octave, Awk, BC, TT (Template Toolkit), WebChat and even PERL

share|improve this answer
    
I'd never heard of Perl Inline. It's an interesting idea, but does it give any advantage over what is probably the usual approach of having separate modules for non-PERL code? –  FrustratedWithFormsDesigner Feb 27 '12 at 21:21
1  
@FrustratedWithFormsDesigner: Inline Perl is a great module, but it has some flaws. It works well, but only to the extent of fairly basic code. Although it has been worked on for a while, I wouldn't use it in any kind of production. –  Dynamic Feb 28 '12 at 0:19
add comment

This is often called polyglot code - there are some fun/insane examples if you follow the link or in various other places on the web. Most of these are just for fun / to prove it is possible.

More seriously, there are various real life examples where two or more different languages can be usefully combined:

  • Web templating - languages such as PHP or JSP files mix code into HTML. Opinions differ widely on whether this is a good idea or not.
  • Macro languages - often a macro language is mixed into the source file such as C/C++ preprocessor macros. There are also interesting cases like Lisp where the macro language is itself Lisp (the only difference is whether the code is run at compile time or runtime)
  • DSLs - often a domain specific language is defined to help solve a particular problem effectively, which is embedded into the source code of another language. Here's an example of a beautiful DSL for SQL which can be embedded in Clojure code.
  • Scripting - some dynamic languages are particularly useful for short scripts and are designed to be embedded within software written in another language. Groovy scripts for example are very easy to embed within a Java application.
  • Polyglot projects - sometimes it makes sense to use multiple languages just to exploit the different capabilities of each. For example, the JVM supports multiple languages that can interoperate fairly transparently, so you might mix Java (for speed and statically typed OOP) with Clojure (for interactive development, concurrency and functional programming). Such projects still typically separate the different languages into separate source files/folders, but they are compiled at the same time to produce a single application.
share|improve this answer
add comment

Embedded SQL was a common way to embed SQL statements into programs of other languages.

These days it has been replaced almost entirely by easier-to-compile API-based Database access that doesn't need to modify the host language, but use its normal abilities instead.

share|improve this answer
1  
These days an embedded SQL made its comeback in form of LINQ, and it is here to stay. –  SK-logic Feb 28 '12 at 7:50
    
@SK-logic: how is LINQ anything like embedded SQL? It's not a different language, it's simply the application of additional language constructs in the host language. –  Joachim Sauer Feb 28 '12 at 9:00
    
LINQ is a very typical embedded DSL, semantically alien to its host language. And, in case of LINQ2SQL, it behaves exactly the same way as the old embedded SQL did, it is translated literally to SQL. –  SK-logic Feb 28 '12 at 9:11
    
But embedded SQL is not "translated to SQL". You write the literal SQL inside your C code (and not usually within string constants either). Even 'though LINQ uses language features that were (as far as I know) developed specifically for LINQ it's "just" an API within its host language. –  Joachim Sauer Feb 28 '12 at 10:28
1  
Pro*C and Pro*Fortran translated SQL statements a bit. And LINQ is not "just" an API, it's an API with syntax sugar and a bunch of interchangeable compiler back-ends. Which makes it a perfect example of a full-blown embedded DSL. –  SK-logic Feb 28 '12 at 10:39
show 2 more comments

Several programming languages can be used to form 1 exe. One way is to use DLLs. Of course there are various concerns about this. For example, parameter compatibility, COM compatibility and such. In fact, if you think of how you call a database system to do you work, you might find that the DBMS is not always written in a language you know. You probably don't even care as long as the interface is known.

This concept is taken further when your solution utilizes Web Services, which is even a cleaner way combine multiple software components.

In the .NET world, all of the above applies, more over, on the interface level XAML and HTML interfaces can live together in Silverlight.

In the UNIX world, at one time, we used KShell scripts to launch C++ and COBOL programs so that the solution could work.

share|improve this answer
add comment

You can "mix" languages in HTML. It's actually important to how a lot of websites work that you can embed javascript into html. But of course, HTML is markup, not a programming language.

I think to do what you propose would entail writing a new language anyway. Either you would have to create an interpreter/compiler that spoke all of those languages and could frame them consistently, or you would need some way of evaluating line by line explicitly in what would be a meta-language. Those two options are basically the same except for what the programmer has to do to make the languages work together.

share|improve this answer
    
+1 on mixing languages in HTML, but I personally believe it's cleaner to keep the HTML/CSS and JS separated. The web designers can do their thing without trying to wade through stringy JavaScript that's embedded throughout the site. –  jmort253 Feb 28 '12 at 3:29
    
You can mix languages in HTML, but not many of them are programming languages, which is the topic at hand. –  Alexios Feb 28 '12 at 7:34
    
Eh? Javascript isn't a programming language? PHP? I've also seen perl and python injections although that was with Apache modules. –  2ck Feb 29 '12 at 17:20
add comment

Examples of combining languages: Jython (python in Java), Cog (python used as an embedded code generator in pretty much anything). I've often used Perl code to generate C++, if you count code generation.

share|improve this answer
add comment

Language is a tool. Before choosing a tool (moreover, making some weird hummer-PC-spectroscope combination) one should ask himself - what exactly is it that I want to do?? Once the answer is given, you will find out that such kind of languages mixture is seldom necessary.

share|improve this answer
add comment

I think it would be worth mentioning Cython here. It's a superset of Python for writing C extensions and although it is really a unique language in itself, it pretty much allows you to use C code in Python code, if you conform to Cython's Python style syntax

share|improve this answer
add comment

The .NET framework is built for allowing several languages to participate in one solution. It works as long as the language can produce CLR-compatible byte code.

http://www.geekinterview.com/question_details/1047

share|improve this answer
    
I don't think that's what the original poster meant. The resulting bytecode has very little to do with the way it was written in the language from which it was compiled. –  perdian Mar 6 '12 at 9:22
    
I don't think you understood the question. The author is talking about combining multiple languages in one obscure file. –  Craige Mar 6 '12 at 19:20
add comment

Yes, it is possible to mix programming languages so long as your interpreter/compiler can understand it. For instance mixing javascript, PHP, python in HTML or C++ in C#.

Mixing languages can make it more difficult to read and modify code, however, so it must be done cautiously. One way around this is to create separate files for code in the language you need to call and to import it rather than embed it.

share|improve this answer
add comment

Skipping the fact that HTML isn't a programming language, yes you can combine programming languages. You can do it in several ways, as independent modules that call each other (VB.NET and C# projects for instance), as independent source files that get combined where possible (most things that get compiled down to native code), and also as inline code from a different langauge. The classic example of the later is C compilers frequently allow inline assembly.

share|improve this answer
add comment

It's very hard, very ugly and often useless to combine multiple programming languages in 1 file.

It is however possible to have a big project written in more than 1 language. For example, both Mozilla Firefox and MySQL have C and C++ code in them. When it comes to big projects, this practice is often used because a particular language provides some features that another does not. In PHP, for example, you can ask for the execution of a binary executable, take its result and use it in your PHP code from that point on.

If you're curious about what languages are used for some of the most important open source projects in the world, you can check out Ohloh.net. Statistics about the source code of many projects can be found there.

share|improve this answer
add comment

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.