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条回答
Evening l夕情丶
2楼-- · 2019-01-08 10:32

For simple parsing tasks I'm quite fond of using a variant of Pratt's Top Down Operator Precedence parser. While Pratt wrote the original paper using an old Lisp dialect, the same concepts can easily be used in most any language. In fact, Douglas Crockford wrote an excellent article on Top Down Operator Precedence parsing in JavaScript, which might be just what you need.

查看更多
Rolldiameter
3楼-- · 2019-01-08 10:35

Something like http://jscc.phorward-software.com/, maybe?

JS/CC is the first available parser development system for JavaScript and ECMAScript-derivates. It has been developed, both, with the intention of building a productive compiler development system and with the intention of creating an easy-to-use academic environment for people interested in how parse table generation is done general in bottom-up parsing.

The platform-independent software unions both: A regular expression-based lexical analyzer generator matching individual tokens from the input character stream and a LALR(1) parser generator, computing the parse tables for a given context-free grammar specification and building a stand-alone, working parser. The context-free grammar fed to JS/CC is defined in a Backus-Naur-Form-based meta language, and allows the insertion of individual semantic code to be evaluated on a rule's reduction.

JS/CC itself has been entirely written in ECMAScript so it can be executed in many different ways: as platform-independent, browser-based JavaScript embedded on a Website, as a Windows Script Host Application, as a compiled JScript.NET executable, as a Mozilla/Rhino or Mozilla/Spidermonkey interpreted application, or a V8 shell script on Windows, *nix, Linux and Mac OSX. However, for productive execution, it is recommended to use the command-line versions. These versions are capable of assembling a complete compiler from a JS/CC parser specification, which is then stored to a .js JavaScript source file.

查看更多
可以哭但决不认输i
4楼-- · 2019-01-08 10:36

if you're really looking for just a lexer, try prettify.

查看更多
甜甜的少女心
5楼-- · 2019-01-08 10:37

Depending on the design of the 'set of instructions', you may be able to use Javascript's built-in eval function, which parses Javascript source; you may be able to write a simple translator to convert the instructions to Javascript code.

By the way, be very careful about XSS holes.

查看更多
疯言疯语
6楼-- · 2019-01-08 10:44

I was looking for something similar that wouldn't have any security holes and I came across two resources. They don't parse the script, but actually run it in a "safe" environment - something you can't guarantee when using the eval function. So, I don't know if it's exactly what you are looking for but take a look:

  1. jsandbox - Javascript sandbox
  2. Google Caja - virtual iframe.
查看更多
我命由我不由天
7楼-- · 2019-01-08 10:47

If you want a lexer and nothing but a lexer then take a look at this: https://github.com/aaditmshah/lexer

It's a pure JavaScript lexer with lots of powerful features written in just a few lines of code.

查看更多
登录 后发表回答