Lexer written in Javascript?

2019-01-08 09:58发布

I have a project where a user needs to define a set of instructions for a ui that is completely written in javascript. I need to have the ability to parse a string of instructions and then translate them into instructions. Is there any libraries out there for parsing that are 100% javascript? Or a generator that will generate in javascript? Thanks!

10条回答
Rolldiameter
2楼-- · 2019-01-08 10:49

Jison is probably the best and most active lexer & parser generator out there for Javascript. It mimics Bison and Yacc.

Jison: http://zaach.github.io/jison/

If you want just a light weight lexer (~100 sloc) you can take a look at Lexed.js: https://github.com/tantaman/lexed.js

查看更多
三岁会撩人
4楼-- · 2019-01-08 10:53

If you want to build JavaScript parsers and code generators, check out the MetaII implementation in Javascript.

A MetaII Compiler tutorial walks you through building a completely self-contained compiler system that can translate itself and other languages:

MetaII Compiler Tutorial

This is all based on an amazing little 10-page technical paper by Val Schorre: META II: A Syntax-Oriented Compiler Writing Language from honest-to-god 1964. The MetaII compiler complete self-description is about 30 lines! I learned how to build compilers from this back in 1970. There's a mind-blowing moment when you finally grok how the compiler can regenerate itself....

The tutorial explains MetaII, how it works, and implements MetaII compiling MetaII into JavaScript. You can easily modify this compiler to parse other langauges, and produce different Javascript.

I know the website author from my college days, but have nothing to do with the website.

查看更多
【Aperson】
5楼-- · 2019-01-08 10:55

Here is an example of a parser for a "pseudo" natural language of instructions, which was implemented in pure JavaScript with Chevrotain Parsing DSL:

https://github.com/SAP/chevrotain/blob/master/examples/parser/inheritance/inheritance.js

This example even includes support for multiple natural languages (English & German) using grammar inheritance.

Chevrotain falls under the category of "libraries out there for parsing that are 100% javascript" as it performs no code generation. Using Chevrotain is similar to "hand crafting" a recursive decent parser, only without most of the headache such as:

  • Lookahead function creation (deciding which alternative to take)
  • Automatic Error Recovery.
  • Left recursion detection
  • Ambiguity Detection.
  • Position information.
  • ...

as Chevrotain handles that automatically.

查看更多
登录 后发表回答