News for hackers. Stuff that matters.
Re:Write a parser with JavaScript by derefr on Thursday December 12, 2019 @11:41AM (#21773417)
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.

Re:Write a parser with JavaScript by bd82 on Thursday December 12, 2019 @11:51AM (#21773513)
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 :)

- https://tomassetti.me/parsing-in-javascript/

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?
Re:Write a parser with JavaScript by bd82 on Thursday December 12, 2019 @01:34PM (#21774739)
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)
Re:Write a parser with JavaScript by dmitriid on Thursday December 12, 2019 @12:04PM (#21773680)
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.
Re:Write a parser with JavaScript by ufo on Thursday December 12, 2019 @02:55PM (#21775670)
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.

Re:Write a parser with JavaScript by no_wizard on Thursday December 12, 2019 @01:07PM (#21774481)
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.

[0] https://github.com/zaach/jison

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.