Lexical Scanning in Go - Rob Pike
GoogleFOSSSydney13 HN recommenders · 2012–2025
162K views · talk · evergreen · Uploaded 2011-08-30
Rob Pike shows how the Go text/template lexer is built, using state functions, goroutines and channels. The talk was given at a Google Technology User Group in Sydney in 2011.
HN returns to it for years as an elegant introduction to writing a lexer that is easy to follow even without a parsing background.
36:45Rich Harris - Rethinking reactivity
You Gotta Love Frontend
1:12:27CppCon 2015: Andrei Alexandrescu “std::allocator...”
CppCon
1:31:06William Byrd on "The Most Beautiful Program Ever Written" [PWL NYC]
PapersWeLove
59:34CppCon 2017: Matt Kulukundis “Designing a Fast, Efficient, Cache-friendly Hash Table, Step by Step”
CppCon
42:30RustConf 2018 - Closing Keynote - Using Rust For Game Development by Catherine West
Rust Programming Language
1:19:52CppCon 2016: Jason Turner “Rich Code for Tiny Computers: A Simple Commodore 64 Game in C++17”
CppCon
57:38Clasp: Common Lisp using LLVM and C++ for Molecular Metaprogramming
Google TechTalks
1:55:24Ideas about a new programming language for games.
Jonathan Blow
20 comments from Hacker News
As an aside: watching Pike's talk on lexing and parsing in Go really hits this point home. It shows how language support for cheap threads (goroutines) and channels really change the way you come up with an algorithm. [VIDEO]
Personally, I have a penchant for writing my own pull parsers. Its a mind-expanding exercise. The neat thing about Go is that parsers can return functions that consume the next token. Rob Pike has an excellent video about this: [VIDEO]
Check out the std lib: [LINK] -- that's canonical Go code. It's my language of choice for personal projects. A neat and compact language that is very trivial to pick up, feels light, and yet sufficiently subtle to intellectually engage your mind. Very productive, good performance, and very portable. [VIDEO] caveat: /imho/ the runtime is not yet fully cooked. ETA is Go1.1 (end of year I believe) for beefed up internals.
> Building a lexer out of a recursive function type actually sounds like a really cool thing to try Rob Pike did just that for Go's text/template package ( [LINK] ). See the source lexer source code at [LINK] and note that stateFn is defined recursively. There's a video by Pike describing how he built the parser at [VIDEO] .
type state struct { // add any fields you need to persist here } type transition func(*state) transition same as in Lexical Scanning in Go ( [VIDEO] )
> What most people don't realize is that for concurrency to be useful, there must be parallelism. No, as far as I understand from this and other talks, Rob Pike argues that concurrency is useful even without parallelism: it's a better way to structure software. A nice example for this is shown in his talk about lexical scanning in Go: [VIDEO]
I'm not sure I like the idea of parsing templates with regular expressions. There is a better way, as demonstrated in Robe Pike's video[1] detailing how the Go template language is implemented. Edit: which isn't meant to detract from the fact that this is a great article. [1] [VIDEO]
For those who don't know who Rob Pike is, he's pretty awesome, helped create UTF-8 among other things, and now works on Go. [LINK] He has given some really great Go talks which are available on youtube, about an hour each: Origins of Go Concurrency Style - [LINK] Lexical Scanning in Go - [VIDEO] Go Concurrency Patterns (One of my favorites) - [LINK]
Rob Pike did a really good talk entitled 'Lexical Scanning In Go'[0], which covers how the lexing for text/template works. It's far more interesting than it sounds, and even if you don't know much about lexing/parsing it's easy to follow. [0] [VIDEO]
I thought this would be a good opportunity to use 'state functions' to implement a shell for Bytengine. Rob Pike mentions the use of state functions in his talk "Lexical Scanning in Go" [VIDEO]
Wow, perfect timing! I'm currently writing my first lexer/parser in D as well ( [LINK] ) for a Scheme-like language used in Halo 3. I found Rob Pike's talk on lexical scanning to be pretty useful ( [VIDEO] ) and I've modeled mine pretty heavily after the text/template lexer: [LINK] .
I would like to mention that most, if not all, of the Go compiler and related utilities is written in Go itself, with high quality, open source code that can be found in the "go" package of the standard library: [LINK] I would recommend using that as a template for one's own compiler, because it's written in idiomatic Go (originally by the language creators) and is cleanly split across abstraction boundaries (packages "scanner", "parser", "ast", and so on.) There's also a smaller, simpler parser in the "text/template/parse" package, that is used to parse the templating languages in "text/template" and "html/template". As an introduction to it, I recommend the talk "Lexical Scanning in Go" by Rob Pike: [VIDEO]
Here's a video of Rob Pike, developer of Go, talking about a lexer he built for the Go text/template package: [VIDEO] It supports unicode.
Lexical Scanning in Go by Rob Pike [VIDEO] I love everything about this talk. It walks you through building a lexer from scratch in a simple and elegant way, through a very interesting use of coroutines. I appreciate the bits of humor in the talk as well.
I have to admit that when I first heard the Jackson reference in [VIDEO] I thought it was about the other Jackson.
Rob Pike’s lexer was very nicely done and struck a chord with me. It’s not nearly as magical as some of the other things here. It’s just great engineering: [VIDEO]
> I think Go spent a lot of time implying that goroutines were some sort of special language magic, but I think everybody knows now that they are basically a really good implementation of green threads that can take advantage of some internal knowledge to do optimal scheduling to avoid context switches. It took me a while to work this out, and I got a lot of pushback from the go team when I pointed this out internally. This comment deeply fascinates me, because I get the same feeling every time I go back and read/watch early Go resources from Rob Pike and others, but I've never actually heard it articulated before. I've always thought that surely they weren't that ignorant about PL theory and history? Or maybe Rob Pike himself was, but surely his team knew better? It really feels like they thought they were making some special hybrid between threads and coroutines, combining the advantages of both. Like this 2011 talk[1] which demonstrated a classic coroutine code pattern implemented with goroutines and channels. But as time went on, goroutines became fully pre-emptive, and their programming model is now identical to threads. Using them as coroutines only leads to misery (read: data races) in my experience. They're faster than OS threads, sure, but that's an implementation detail at this point. When do you think they eventually realized they were re-inventing the same green threads Java had created and scrapped 10 years earlier? [1] [VIDEO]
Related, I love Rob Pike's talk about lexical Scanning in Go (2011). Educational and elegant approach. [VIDEO]
There are many explanations. The most famous one Rob Pike in his lexer talk 2011 [VIDEO] at 10.48 Or [LINK] Or several articles why you should not parse with regex, like [LINK]
If you enjoyed the state machine + parsing part and also I recommend the older "Lexical Scanning in Go" talk that Rob Pike gave: [VIDEO] It's in Go, but you can apply most of it easily in other languages. I'm so happy that modern languages like Rust, Zig, and Go support unicode/runes/graphemes natively. So many problem just disappear compared to Java, .net, C++, or scripting languages.