Live data from Hacker News

A Regular Expression Matcher (2007)

cs.princeton.edu

1–10 of 36 posts

Re: A Regular Expression Matcher (2007)

#2
I have a re.c file in my best-of collection of code examples locally - but I can't seem to find the original source quickly - nevertheless it was written by Russ Cox[0] and he's spent a lot of time making regular expressions accessible and elegant, his articles are a great resource.

[0] https://swtch.com/~rsc/regexp/

Re: A Regular Expression Matcher (2007)

#4
I'm impressed.

The code lives up to its goal of being a good example of programming. As Kernighan says, it shows the value of handling special cases early and of recursion. (It's also a good example of how pointer-arithmetic and nul-terminated strings should be used, but the less said about that, the better).

Students are likely to not notice the tail recursion, so converting this to an iterative version is probably a nice exercise.

But notice that explaining regexps is a non-goal of this example. There is an underlying state-machine here (encoded in a combination of the instruction pointer and the `text` variable), but it is not very obvious. For that, Russ Cox' articles are the go-to.

Re: A Regular Expression Matcher (2007)

#5
I found I didn't hate, loathe and detest unbraced blocks any less when it's Rob Pike code

    if(x) {
        do_y();} 
2 characters, no vertical space difference. Unbraced blocks only feature is they introduce bugs - they have no legitimate use. But man do you feel like a tough, macho-man "I'm such a hard man I leave my blocks un-braced."

Is there a list of languages that repeated this C idiocy (an optimization of syntax for the parser rather than the programmers?) I sure hope those languages that would like to replace C, ie D, go, Rust etc. Haven't repeated it this far into the 21st century.

Re: A Regular Expression Matcher (2007)

#6

I'm impressed. The code lives up to its goal of being a good example of programming. As Kernighan says, it shows the value of handling special cases early and of recursion. (It's also a good example of how pointer-arithmetic and nul-terminated strings should be used, but the less said about that, the better). Students are likely to not notice the tail recursion, so converting this to an iterative version is probably…

> (It's also a good example of how pointer-arithmetic and nul-terminated strings should be used, but the less said about that, the better).

Why the less said the better?

I'm always disappointed when people say C is bad at string manipulation. The "character at a time, no allocations needed, stop when you hit the end" style demonstrated here and elsewhere have always resonated deeply with me.

Re: A Regular Expression Matcher (2007)

#8
post #5

I found I didn't hate, loathe and detest unbraced blocks any less when it's Rob Pike code if(x) { do_y();} 2 characters, no vertical space difference. Unbraced blocks only feature is they introduce bugs - they have no legitimate use. But man do you feel like a tough, macho- man "I'm such a hard man I leave my blocks un-braced." Is there a list of languages that repeated this C idiocy (an optimization of syntax for th…

In Rust the {} are mandatory, but you don't need the (), so you can just write this:

    if a == b {
        stuff();
    }

Re: A Regular Expression Matcher (2007)

#9
I got the guided tour of this code from bwk back in... 2006?[1]

I wasn't sure I wanted to deal with computers on a day to day basis when I took the course. The lecture blew my mind, and I started looking for programming jobs the next day.

[1] COS333. Memories... man, I'm getting old.

Post reply on HN