Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tutorials for writing a parser with Javascript [closed]

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?

like image 838
exupero Avatar asked May 03 '11 19:05

exupero


People also ask

How do you parse text in JavaScript?

Use the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

What is a JavaScript parser?

Parsing means analyzing and converting a program into an internal format that a runtime environment can actually run, for example the JavaScript engine inside browsers. The browser parses HTML into a DOM tree.


2 Answers

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.

like image 75
danja Avatar answered Oct 11 '22 12:10

danja


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.

like image 25
Ira Baxter Avatar answered Oct 11 '22 11:10

Ira Baxter