4 ms·
My intuition as someone who writes compilers, when handed a BNF grammar, is to reach for a parser-generator like yacc, so that I can keep the BNF grammar as the
by derefr 7y ago
My intuition as someone who writes compilers, when handed a BNF grammar, is to reach for a parser-generator like yacc, so that I can keep the BNF grammar as the canonical representation of the grammar, and have everything else derived automatically and kept in sync. (Even though, yes, in this case, JSON is a very static spec that hasn’t changed since it was introduced.)
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.
- bd82 7y agoLuckily there are many Parsing Libraries in the JavaScript eco-system, So we won't have to find out if this convoluted approach is worthwhile :) - https://tomassetti.me/parsing-in-javascript/ https://tomassetti.me/parsing-in-javascript/
- asdasdasdasdwd 7y agoBNF sounds like the standard approach. Are there any BNF parsers + generators usable from Javascript?
- dmitriid 7y agoPEG is another popular approach
- jerrysievert 7y agojison works fairly well, and is mostly compatible with bison.
- bd82 7y agoSome 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)
- no_wizard 7y agoI 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. [0] https://github.com/zaach/jison https://github.com/zaach/jison
- chrisseaton 7y agoThe problem is... those tools are usually context-free, and almost every language is context-sensitive.
- ufo 7y agoThe 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.
- AlchemistCamp 7y agoIf 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.