34

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?

asked May 3, 2011 at 19:29
2
  • CoffeeScript is not based on Node.js ("The core compiler however, does not depend on Node") and it is not DSL. Commented May 3, 2011 at 19:37
  • 2
    zaach.github.com/jison Commented May 3, 2011 at 20:16

3 Answers 3

13

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.

answered Mar 8, 2012 at 21:48
Sign up to request clarification or add additional context in comments.

Comments

13

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.

answered May 3, 2011 at 21:19

2 Comments

Downvoter: Constructive feedback would be preferred instead of your apparant "flag". I don't see what it is about this response that doesn't directly address OP's request for ways to "build languages" with JavaScript,since that's exactly what the referenced tutorial does.
I think it's incredibly relevant. A parser is something that interprets a language to machine-level understanding. If the OP wants to have any real power over his parser, he is going to need to know how to write a compiler; otherwise, he won't have the independence to take his project very far. He'll post tutorial request after tutorial request and continue to work with poorly-documented spaghetti code at a slow crawl. If he writes a compiler, though, he'll be able to write code in his sleep.
1

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.

answered May 20, 2011 at 23:31

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.