S-expression

From Seo Wiki - Search Engine Optimization and Programming Languages
Jump to navigationJump to search

The term S-expression or sexp (for symbolic expression) refers to a convention for representing semi-structured data in human-readable textual form. Symbolic expressions are mostly made of symbols and lists. S-expressions are probably best known for their use in the Lisp family of programming languages. Other uses of S-expressions are in Lisp-derived languages such as DSSSL, and as mark-up in communications protocols like IMAP and John McCarthy's CBCL. The details of the syntax and supported data types vary in the different languages, but the most common feature among these languages is the use of S-expressions as parenthesized prefix notation (sometimes known as Cambridge Polish notation).

S-expressions are used for both code and data in Lisp (see McCarthy Recursive Functions of Symbolic Expressions). S-expressions were originally intended only for data to be manipulated by M-expressions, but the first implementation of Lisp was an interpreter of S-expression encodings of M-expressions, and Lisp programmers soon became accustomed to using S-expressions for both code and data.

S-expressions can either be single objects (atoms) such as numbers, symbols including the special symbols nil and t, or cons pairs, written as (x . y). Longer lists are made up of nested cons pairs, for example (1 . (2 . (3 . nil))) which can also be written more legibly as (1 2 3). Nested lists can also be written as S-expressions: ((milk juice) (honey marmalade)). S-expressions are independent of whitespace such as space and newline. Whitespace is used as a boundary between atoms.

Example: a simple grammar written as s-expression (Gazdar/Melish, Natural Language Processing in Lisp):

(((S) (NP) (VP))
 ((VP) (V))
 ((VP) (V) (NP))
 ((V) died)
 ((V) employed)
 ((NP) nurses)
 ((NP) patients)
 ((NP) Medicenter)
 ((NP) Dr Chan))

Program code can be written in S-expressions, usually using prefix notation. An extra piece of syntactic sugar for writing Lisp programs is that the common expression (quote x) can be written with the abbreviation 'x.

Example in Common Lisp:

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

Example in Scheme:

(define (factorial x)
    (if (zero? x)
        1
        (* x (factorial (- x 1)))))

S-Expressions can be read in Lisp using the function READ. READ reads the textual representation of an s-expression and returns Lisp data. The function PRINT can be used to output an s-expression. The output then can be read with the function READ, when all printed data objects have a readable representation. Lisp has readable representations for numbers, strings, symbols, lists and many other data types. Program code can be formatted as pretty printed S-expressions using the function PPRINT.

Lisp progams are valid s-expressions, but not all s-expressions are valid Lisp programs. (1.0 + 3.1) is a valid s-expression, but not a valid Lisp program, since Lisp uses prefix notation and a floating point number (here 1.0) is not valid as an operation (the first element of the expression).

Standardization

In May of 1997, Ron Rivest submitted an Internet-Draft[1] to be considered for publication as an RFC. The draft defined a syntax based on Lisp S-expressions but intended for general-purpose data storage and exchange (similar to XML) rather than specifically for programming. It was never approved as an RFC, but it has since been cited and used by other RFCs (e.g. RFC 2693) and several other publications.[2] It was originally intended for use in SPKI.

Rivest's format defines an S-expression as being either an octet-string (a series of bytes) or a finite list of other S-expressions. It describes three interchange formats for expressing this structure. One is the "advanced transport", which is very flexible in terms of formatting, and is syntactically similar to Lisp-style expressions, but they are not identical. The advanced transport, for example, allows octet-strings to be represented verbatim (the string's length followed by a colon and the entire raw string), a quoted form allowing escape characters, hexadecimal, Base64, or placed directly as a "token" if it meets certain conditions. (Rivest's tokens differ from Lisp tokens in that the former are just for convenience and aesthetics, and treated exactly like other strings, while the latter have specific syntactical meaning.) Another interchange format, intended to be more compact, easier to parse, and unique for any abstract S-expression, is the "canonical representation" which only allows verbatim strings, and prohibits whitespace as formatting outside strings. Finally there is the "basic transport representation", which is either the canonical form or the same encoded as Base64 and surrounded by braces, the latter intended to safely transport a canonically-encoded S-expression in a system which might change spacing (e.g. an email system which has 80-character-wide lines and wraps anything longer than that).

This format has not been widely adapted for use outside of SPKI. Rivest's S-expressions web page provides C source code for a parser and generator, which could theoretically be adapted and embedded into other programs, though licensing on these programs is unclear. However, there are no restrictions on independently implementing the format.

See also

References

  1. S-Expressions, Network Working Group, Internet Draft, Expires November 4, 1997 - R. Rivest, May 4, 1997 draft-rivest-sexp-00.txt, Ronald L. Rivest, CSAIL MIT website
  2. rivest sexp, Google Scholar (search)

External Links

Free software implementations are available:

  • sfsexp the small, fast s-expression library on Sourceforge
  • minilisp, by Léon Bottou.

fr:S-expression ko:S-표현식 it:S-expression ja:S式 fi:S-lauseke zh:S-表达式

If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...