Live data from Hacker News

Write a parser with JavaScript

lihautan.com

21–26 of 26 posts

Re: Write a parser with JavaScript

#22
post #7

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 parse…

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

#23

Wait this is a JSON parser, not a JavaScript parser! Still nice work! There is something "raw" about writing parsers by hand (even if they are recursive descent). Teaches one a lot about clean design and modularity etc!

It's a parser written in JavaScript, so it is technically a JavaScript parser :)

Re: Write a parser with JavaScript

#26
post #7

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 parse…

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.

Post reply on HN