Friday, December 28, 2007

A quick look at SNOBOL

SNOBOL (String Oriented Symbolic Language) is a computer programming language developed between 1962 and 1967 at AT&T Bell Laboratories by David J. Farber, Ralph E. Griswold and Ivan P. Polonsky.

SNOBOL is a language for string manipulations. Also from its Wikipedia entry:

... SNOBOL was widely used in the 1970s and 1980s as a text manipulation language ... its popularity has faded as newer languages such as Awk and Perl have made string manipulation by means of regular expressions popular ...

This language caught my attention while listening to the OOPSLA podcast episode on the excellent 50-in-50 talk by Guy Steele and Richard Gabriel.

Given that this a programming language exploration blog, learning more about this language provide an excellent opportunity to know more about the first languages for text manipulation.

This example asks for a name until the input is closed, that is end of file or Ctrl+D (in Linux). The ASK,DONE and END elements are labels; all of them (except for END) are user specified names. The :F(DONE) modifier means jump to DONE if failed and the :(ASK) modifier means jump to ASK.

The most interesting thing about the language is the string pattern matching capabilities. Here's an small(and very incomplete) example that extracts the parts of a simplified URL string:

In line 6, the contents of the LINE variable is matched against a pattern. The pattern contains the following elements:

The SPAN(LETTER) . PROTO "://" section says identify a sequence of letters followed by "://" and assign them to the variable called PROTO

The SPAN(LETTERORDOT) . HOST "/" secotion says take a sequence of letters and dots followed by "/" and assign then to the variable called HOST

Finally the last section takes the remaining letters and slash characters and assign them to the RES variable

To show a litte program that uses all the elements presented here, I wanted to create a small example that takes as input the authentication /var/log/auth.log and shows all the uses of sudo and the program that was executed. The desired lines look like this:

Here the &ANCHOR assignment tells SNOBOL that pattern matching is performed at any position of the specified string. The ARB element says any character before the next pattern succeeds and the RPOS(0) element is used to identify the end of line.

For future entries I'm going to show more interesting SNOBOL features.

4 comments:

Good times. I took a SNOBOL class in college back in maybe 1984 or so. After several assignments, our biggest one was to write a solitaire game. It worked, but I don't think I remember anything about the language now. Your post brought back memories (and it's around 8 years old itself).

Our project at the University of Michigan in 1973 was to write a simple BASIC interpreter in SNOBOL. We had an interpreter (SNOBOL) interpreting an interpreter (BASIC) interpreting BASIC statements. It did use up resources, especially with one test program in BASIC that computed factorials using a recursive subroutine.

Anonymous said...Our project at the University of Michigan in 1973 was to write a simple BASIC interpreter in SNOBOL. We had an interpreter (SNOBOL) interpreting an interpreter (BASIC) interpreting BASIC statements. It did use up resources, especially with one test program in BASIC that computed factorials using a recursive subroutine.

I am interested in your comments because I worked at CCAE computer centre in 1976-1977 and I was asked to look at providing a Basic interpreter. (Burroughs already provided a Basic compiler, and I think the ergonomics of running the system were very similar to running an interpreter. I think you could type "C" to compile and then maybe type "RUN" to run.

I recently noticed that an interpreter could have been provided in Fortran or Algol.

Only the other day I thought that a Snobol program could have been provided. I tested many of the examples in Gimpel's book on Snobol in 1977, but can't remember if it had Snobol code for a basic interpreter.