Luckily there are many Parsing Libraries in the JavaScript eco-system, So we won't have to find out if this convoluted approach is worthwhile :)
Re:Write a parser with JavaScript
by asdasdasdasdwd on Thursday December 12, 2019 @11:58AM
(#21773610)
BNF sounds like the standard approach. Are there any BNF parsers + generators usable from Javascript?
Some of the top of my Head:
- Antlr (https://github.com/antlr/antlr4/blob/master/doc/javascript-target.md)
- PegJS (https://github.com/pegjs/pegjs)
- Nearley (https://github.com/kach/nearley)PEG is another popular approach
Re:Write a parser with JavaScript
by jerrysievert on Thursday December 12, 2019 @01:04PM
(#21774444)
jison works fairly well, and is mostly compatible with bison.
The main reason for using a parser genarator tool is being able to use the canonical BNF version of the grammar, as you said. Another good thing is that LR parser generations can also help detect ambiguities in the grammar -- those appear as shift-reduce or reduce-reduce conflicts.
But there can also be good reasons to use a hand-rolled recursive descent parser. It simplifies the build system, and it can also be easier to produce good error messages.
Re:Write a parser with JavaScript
by AlchemistCamp on Thursday December 12, 2019 @04:54PM
(#21776952)
If you're into this stuff and want to keep it all in the JS ecosytem, PEG.js isn't a bad choice. It powered CoffeeScript Redux, among other widely used projects.
I've been looking at the WASM side myself and suspect it's only a matter of time before there's a JS/TS PEG library written mostly in Rust.
I think the (not maintained, I presume from the activity) jison[0] project was trying to achieve was auto generating a parser in JavaScript based on a similar idea.
Re:Write a parser with JavaScript
by chrisseaton on Thursday December 12, 2019 @01:36PM
(#21774761)
The problem is... those tools are usually context-free, and almost every language is context-sensitive.
Presuming for a moment that there isn’t a parser-generator utility that emits JavaScript, though, I’d be led to wonder whether it wouldn’t still be a more efficient use of my time (compared to hand-rolling a parser, usually a pretty finicky task) to just use yacc itself to emit C, and then use Emscripten to get WASM, and then call that from JavaScript. I’d probably do that first—it’d take about an hour—and then profile the resulting parser (including the JS-WASM overhead.) Hopefully, it’d fall within the tolerances for parsing time. Only if it didn’t would I sigh and begin to hand-roll a parser.