开发者

Possible to write a Compiler with Javascript?

Is it possible to use Javascript to write a compiler that can support other kind of language as scripting?

Let's say, I have a piece of HTML.

<script language="cpp" id="cppScriptBlock" EntryPoint="main">
    int main() {
        cout << "<h1>CPPHeader</h1>";
    }
</script>

<script language="java" id="javaScriptBlock" EntryPoint="MyJavaClass">
    public class MyJavaClass {
        public final void main() {
开发者_JAVA百科            java.lang.System.out.println("<h1>JavaHeader</h1>");
        }
    }
</script>

<script language="csharp" id="csharpScriptBlock" EntryPoint="MyCSharpClass ">
    public class MyCSharpClass {
        public static void Main() {
            System.Console.WriteLine("<h1>CSharpHeader</h1>");
        }
    }
</script>


<script language="javascript">
    $("#cppScriptBlock").compileAndRun();
    $("#javaScriptBlock").compileAndRun();
    $("#csharpScriptBlock").compileAndRun();
</script>

And finally generate the following HTML

<h1>CPPHeader</h1>
<h1>JavaHeader</h1>
<h1>CSharpHeader</h1>

Is it possible?

Alex


Yes, it's very much possible using Jison.

It generates a JavaScript parser based on the language constructs you define.

Jison takes a context-free grammar as input and outputs a JavaScript file capable of parsing the language described by that grammar. You can then use the generated script to parse inputs and accept, reject, or perform actions based on the input.

-- from the documentation

PS: CoffeeScript! was also created using this. :)


Yes, but there's a lot of work you'd have to do. Just like a real compiler, you'd have to parse the code, convert it into intermediate code, etc. After that, you'd have to simulate the environment including all of the runtime libraries included with those languages. In short, it's not practical, but it is possible.


Yes, Javascript is Turing Complete. You can code anything in it that you can code in any language. Of course that includes compilers. I can't imagine any reason to ever do this though. If you're good enough at Javascript to write a compiler in it, you'd probably like to just write your code in javascript instead of another language.


See Metacompiler tutorial about how to write arbitrary compilers (and compier-compilers) in general, using Javascript as an implementation language.


Yes, it is possible. But instead of writing your parser by hand I would encourage you to use a good parser generator.

For example ANTLR by Terence Parr is a very powerful parser generator that has a JavaScript target. It works in environments supporting ECMAScript 5.1. (tested in Firefox, Safari, Chrome, Internet Explorer and Node.js). It is open source (BSD license) with extensive documentation and a very good book available.

Using a tool like this, instead of writing your own parser, you write a grammar and the parser is generated for you.


You should take a look into JS tempting languages. Specifically the following:

  • http://mustache.github.com/
  • http://www.handlebarsjs.com/


Yes it's possible.

It would be much easier however, to write an interpreter that converts from one language into Javascript and then have the browser handle generation and execution of byte code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜