4 ms·
Both go and OCaml use LR parser generators, precisely because they make it easy to give excellent error messages. Basically the key insight is that you can use
by neel_k 7y ago
Both go and OCaml use LR parser generators, precisely because they make it easy to give excellent error messages.
Basically the key insight is that you can use the parsing automaton state to classify syntax errors, which makes it as easy as falling off a log to produce good messages. The paper originating this technique is Clinton Jeffery's TOPLAS 2003 paper “Generating LR Syntax Error Messages from Examples.” This is what go uses, and OCaml uses a more advanced version of this technique introduced by Francois Pottier in his CC 2016 paper "Reachability and Error Diagnosis in LR(1) Parsers".
- atombender 7y agoGo doesn't use a generated parser. Both the parser and the lexer are hand-written: https://github.com/golang/go/blob/master/src/go/parser/parser.go https://github.com/golang/go/blob/master/src/go/parser/parse... https://github.com/golang/go/blob/master/src/go/scanner/scanner.go https://github.com/golang/go/blob/master/src/go/scanner/scan... Why did you think it was generated?
- minieggs 7y agoIf you look closely at the Go code you'll see it was once C code generated by some parser generater (which I assume was Bison). Believe it was Russ Cox that wrote a program that transformed the generated C code into Go. Edit: I forked the compiler a while ago to add maybe types (a la Rust result). Looks like the compiler code has changed quite a bit from when I was playing w/ it.
- atombender 7y agoThat's a long time ago. You can see vestiges of it here and there (mostly in deeper areas of the compiler, which goes all the way back to Plan 9), but the parser now looks very clean.
- minieggs 7y agoVery true and agree. Parser is in good shape.
- 0xjnml 7y agoFTR: The go/parser package was _never_ used by the Go compiler. It used to be a yacc generated one when the compiler itself was in C. Then it was all mechanically translated to Go. And finally, a hand written lexer and parser were adopted, but different from the ones in the stdlib.