可能编写一个编译器与Javascript? [英] Possible to write a Compiler with Javascript?
问题描述
是否可以使用Javascript编写一个可以支持其他类型语言作为脚本的编译器?
Is it possible to use Javascript to write a compiler that can support other kind of language as scripting?
假设我有一个HTML。 p>
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.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>
最后生成以下HTML
And finally generate the following HTML
<h1>CPPHeader</h1>
<h1>JavaHeader</h1>
<h1>CSharpHeader</h1>
有可能吗?
Alex
推荐答案
是的,使用 >
它会根据您定义的语言结构生成一个JavaScript解析器。
Yes, it's very much possible using Jison.
It generates a JavaScript parser based on the language constructs you define.
Jison使用上下文无关文法作为输入,并输出一个能够解析该语法描述的语言的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.
$ b b
- 来自文档
PS : CoffeeScript !也是使用这个创建的。 :)
PS: CoffeeScript! was also created using this. :)
这篇关于可能编写一个编译器与Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Jison使用上下文无关文法作为输入,并输出一个能够解析该语法描述的语言的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.
$ b b