可以用 Javascript 编写编译器吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7343504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:47:48  来源:igfitidea点击:

Possible to write a Compiler with Javascript?

javascriptcompiler-construction

提问by Alex Yeung

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

亚历克斯

回答by Robin Maben

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. You can then use the generated script to parse inputs and accept, reject, or perform actions based on the input.

Jison 将上下文无关语法作为输入,并输出一个能够解析该语法所描述语言的 JavaScript 文件。然后,您可以使用生成的脚本来解析输入并根据输入接受、拒绝或执行操作。

-- from the documentation

——来自文档

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

PS:咖啡脚本!也是用这个创建的。:)

回答by icktoofay

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.

是的,但是您需要做很多工作。就像真正的编译器一样,您必须解析代码,将其转换为中间代码等。之后,您必须模拟包括这些语言中包含的所有运行时库在内的环境。简而言之,它不实用,但它是可能的。

回答by Paul

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.

是的,Javascript 是图灵完备的。您可以在其中编写任何可以用任何语言编写的代码。当然,这包括编译器。我无法想象有任何理由这样做。如果您足够精通 Javascript 以在其中编写编译器,那么您可能只想用 javascript 而不是另一种语言编写代码。

回答by Ira Baxter

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

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

回答by wesbos

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

你应该看看 JS 诱人的语言。具体如下:

回答by rsp

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

对的,这是可能的。但是我鼓励您使用好的解析器生成器,而不是手动编写解析器。

For example ANTLRby 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.

例如,Terence Parr 的ANTLR是一个非常强大的解析器生成器,它有一个 JavaScript 目标。它适用于支持 ECMAScript 5.1 的环境。(在 Firefox、Safari、Chrome、Internet Explorer 和 Node.js 中测试)。它是开源的(BSD 许可),有大量的文档和一本非常好的书。

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

使用这样的工具,您无需编写自己的解析器,而是编写语法并为您生成解析器。

回答by zellio

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 的解释器然后让浏览器处理字节码的生成和执行会容易得多。