简体   繁体   English

可以用Javascript编写编译器吗?

[英]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? 是否可以使用Javascript编写可以支持其他语言编写的编译器?

Let's say, I have a piece of HTML. 比方说,我有一段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.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 最后生成以下HTML

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

Is it possible? 可能吗?

Alex 亚历克斯

Yes, it's very much possible using Jison . 是的,使用Jison非常有可能。

It generates a JavaScript parser based on the language constructs you define. 它根据您定义的语言结构生成JavaScript解析器。

Jison takes a context-free grammar as input and outputs a JavaScript file capable of parsing the language described by that grammar. Jison将无上下文语法作为输入,并输出能够解析该语法描述的语言的JavaScript文件。 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 ! 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. 是的,Javascript是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. 如果你已经足够Javascript来编写编译器,你可能只想用javascript而不是其他语言编写代码。

请参阅Metacompiler教程,了解如何使用Javascript作为实现语言编写任意编译器(和编译器编译器)。

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 . 例如,Terence Parr的ANTLR是一个非常强大的解析器生成器,具有JavaScript目标 It works in environments supporting ECMAScript 5.1. 它适用于支持ECMAScript 5.1的环境。 (tested in Firefox, Safari, Chrome, Internet Explorer and Node.js). (在Firefox,Safari,Chrome,Internet Explorer和Node.js中测试过)。 It is open source (BSD license) with extensive documentation and a very good book available. 它是开源的(BSD许可证),提供大量文档和非常好的书籍。

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. 你应该看一下JS诱人的语言。 Specifically the following: 具体如下:

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. 然而,编写一个从一种语言转换为Javascript然后让浏览器处理字节代码的生成和执行的解释器会容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM