A Simple S-Expression Parser

S-expressions are a plain ASCII form for representing complex data structures. They can be used to serialize data to send over a communication path, to persist into a database, or to otherwise use as a string representation of a hierarchical data element. When displayed in indented form, S-expressions are even suitable for human comprehension, providing a simple and intuitive nesting syntax, with parentheses used as the nesting operators. Here is a sample S-expression describing an authentication certificate:

    (e |NFGq/E3wh9f4rJIQVXhS|)
    (n |d738/4ghP9rFZ0gAIYZ5q9y6iskDJwASi5rEQpEQq8ZyMZeIZzIAR2I5iGE=|))
    (e |NFGq/E3wh9f4rJIQVXhS|)
    (n |d738/4ghP9rFZ0gAIYZ5q9y6iskDJwASi5rEQpEQq8ZyMZeIZzIAR2I5iGE=|))
(not-after "1998-01-01_09:00:00")
(spend (account "12345678") (* numeric range "1" "1000"))))

The attraction of S-expressions is that they consist purely of lists of basic character or numeric strings, with structure represented using nested parentheses.

The languages Lisp and Scheme use S-expressions as their actual program syntax. Here is a factorial function written in Common Lisp:

(defun factorial (x)
   (if (zerop x) 1
       (* x (factorial (- x 1)))))

The online Wikipedia article (http://en.wikipedia.org/wiki/s-expression) has more background and additional links for further information on S-expressions.

In computer science classes, it is common ...

Get Getting Started with Pyparsing now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.