开发者

Tutorials for writing a parser with Javascript [closed]

开发者 https://www.devze.com 2023-03-03 03:25 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.

Closed 9 years ago.

Improve this question

I've seen a couple of languages (namely CoffeeScript and LessCSS) that are built on Javascript.

Are there tutorials anywhere for writing languages/parsers with Javascript?


Jison is 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.


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.

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:

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.


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

See the list of parser generators at the bottom of that page that make things a bit easier, such as jison and 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.

0

精彩评论

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