使用 Javascript 编写解析器的教程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5874609/
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
Tutorials for writing a parser with Javascript
提问by exupero
I've seen a couple of languages (namely CoffeeScriptand LessCSS) that are built on Javascript.
我见过几种基于 Javascript的语言(即CoffeeScript和LessCSS)。
Are there tutorials anywhere for writing languages/parsers with Javascript?
是否有使用 Javascript 编写语言/解析器的教程?
采纳答案by danja
Jisonis modeled on the GNU Bison parser generator. It takes a language grammar in Bison-like or JSON format and outputs a Javascript parser for the language. If you're wanting to make an interpreter that's based on on another well-known language, there's probably a Bison grammar around somewhere you can tweak for Jison. I've found it very straightforward to get started on a from-scratch DSL.
Jison以 GNU Bison 解析器生成器为模型。它采用 Bison-like 或 JSON 格式的语言语法,并为该语言输出一个 Javascript 解析器。如果您想制作基于另一种知名语言的解释器,那么您可以在某处为 Jison 调整 Bison 语法。我发现从头开始 DSL 非常简单。
回答by Ira Baxter
Why would you think the fundamental concepts of implementing languages "on JavaScript" are fundamentally dependent on JavaScript? Mostly its just a programming language and standard compiler-like approaches can be applied; one "merely" compiles to JavaScript instead of machine instructions.
为什么你会认为“在 JavaScript 上”实现语言的基本概念从根本上依赖于 JavaScript?大多数情况下,它只是一种编程语言,可以应用标准的类似编译器的方法;一个“仅仅”编译成 JavaScript 而不是机器指令。
Here's a tutorial on writing compilers using very straightforward metacompiling methods. It happens to target JavaScript as a starting place, but it isn't committed to JavaScript either. This tutorial is based on a paper by Val Schorre on "MetaII", a kind of metacompiler .... dated 1964(yes, you read that right) . I learned how to build my first compiler from this paper (but not with JavaScript :), and it is still a valuable technique:
这是一个关于使用非常简单的元编译方法编写编译器的教程。它碰巧将 JavaScript 作为起点,但它也不致力于 JavaScript。本教程基于 Val Schorre 关于“MetaII”的论文,MetaII 是一种元编译器......日期为1964 年(是的,你没看错)。我从这篇论文中学习了如何构建我的第一个编译器(但不是使用 JavaScript :),它仍然是一项有价值的技术:
Meta II Compiler Tutorial targeting JavaScript
If you want something more immediate, consider writing a recursive descent parser by hand.. After you've written a few of these, you'll really appreciate what bit of genius MetaII is.
如果您想要更直接的东西,请考虑手动编写递归下降解析器。. 在您编写了其中的一些之后,您将真正体会到 MetaII 的天才之处。
回答by edtechdev
I would start by looking at more languages that compile to javascript and see what they do. Here's a list: https://github.com/jashkenas/coffee-script/wiki/List-of-languages-that-compile-to-JS
我将首先查看更多编译为 javascript 的语言,然后看看它们做了什么。这是一个列表:https: //github.com/jashkenas/coffee-script/wiki/List-of-languages-that-compile-to-JS
See the list of parser generators at the bottom of that page that make things a bit easier, such as jison and peg.js.
请参阅该页面底部的解析器生成器列表,它们使事情变得更容易一些,例如 jison 和 peg.js。
There are certain limits or hurdles when writing an alternative language that compiles to javascript, since javascript wasn't designed to be a 'bytecode' or a runtime for other languages. There are no static types or class system, for example, like in java and C#. If you're just doing a minor alteration to fix some of javascript's issues like coffeescript and others listed at the top of the page at that link, stuff like that isn't a problem, but then a bigger issue is why not just contribute to coffeescript or similar languages instead.
编写可编译为 javascript 的替代语言时存在某些限制或障碍,因为 javascript 并非设计为“字节码”或其他语言的运行时。没有静态类型或类系统,例如在 java 和 C# 中。如果你只是做一个小的改动来解决一些 javascript 的问题,比如在该链接的页面顶部列出的咖啡脚本和其他问题,那么这样的东西不是问题,但更大的问题是为什么不只是贡献coffeescript 或类似的语言。