S-expression
data structure representing the S-expression (* 2 (+ 3 4)) In computer programming, an S-expression (or symbolic expression, abbreviated as sexpr or sexp) is an expression in a like-named notation for nested list (tree-structured) data. S-expressions were invented for, and popularized by, the programming language Lisp, which uses them for source code as well as data.
data structure representing the S-expression (* 2 (+ 3 4)) In computer programming, an S-expression (or symbolic expression, abbreviated as sexpr or sexp) is an expression in a like-named notation for nested list (tree-structured) data. S-expressions were invented for, and popularized by, the programming language Lisp, which uses them for source code as well as data. #n=(x y . #n#) The definition of an atom varies per context; in the original definition by John McCarthy, it was assumed that there existed "an infinite set of distinguishable atomic symbols" represented as "strings of capital Latin letters and digits with single embedded blanks" (a subset of character string and numeric literals). Most modern sexpr notations allow more general quoted strings (for example including punctuation or full Unicode), and use an abbreviated notation to represent lists with more than 2 members, so that (x y z) stands for (x . (y . (z . NIL))) NIL is the special end-of-list object (alternatively written () , which is the only representation in Scheme ). In the Lisp family of programming languages, S-expressions are used to represent both source code and data. Other uses of S-expressions are in Lisp-derived languages such as DSSSL, and as mark-up in communication protocols like IMAP and John McCarthy's CBCL. It is also used as text representation of WebAssembly. 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 and prefix notation. ==Datatypes and syntax== There are many variants of the S-expression format, supporting a variety of different syntaxes for different datatypes. The most widely supported are: Lists and pairs: (1 () (2 . 3) (4)) Symbols: with-hyphen ?@!$ |a symbol with spaces| Strings: "Hello, world!" Integers: -9876543210 Floating-point numbers: -0.0 6.28318 6.022e23 The character # is often used to prefix extensions to the syntax, e.g. #x10 for hexadecimal integers, or #\C for characters. ==Uses== ===Use in Lisp=== When representing source code in Lisp, the first element of an S-expression is commonly an operator or function name and any remaining elements are treated as arguments. This is called "prefix notation" or "Polish notation". As an example, the Boolean expression written 4 == (2 + 2) in C, is represented as (= 4 (+ 2 2)) in Lisp's s-expr-based prefix notation. As noted above, the precise definition of "atom" varies across LISP-like languages. A quoted string can typically contain anything but a quote, while an unquoted identifier atom can typically contain anything but quotes, whitespace characters, parentheses, brackets, braces, backslashes, and semicolons. In either case, a prohibited character can typically be included by escaping it with a preceding backslash. Unicode support varies. The recursive case of the definition of S-expressions is traditionally implemented using cons cells. 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. This means that Lisp is homoiconic; that is, the primary representation of programs is also a data structure in a primitive type of the language itself. Nested lists can be written as S-expressions: ((milk juice) (honey marmalade)) is a two-element S-expression whose elements are also two-element S-expressions. The whitespace-separated notation used in Lisp (and this article) is typical. Line breaks (newline characters) usually qualify as separators. This is a simple context-free grammar for a tiny subset of English written as an S-expression, where S = sentence, NP = Noun Phrase, VP = Verb Phrase, V = Verb: (((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. Example in Common Lisp: (defun factorial (x) (if (zerop 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 (note: with two Ps, short for pretty-print). Lisp programs 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). An S-expression preceded by a single quotation mark, as in 'x , is syntactic sugar for a quoted S-expression, in this case (quote x) . ===Other uses=== S-expressions are also used for WebAssembly text format. ==See also== CAR and CDR Fexpr Lambda calculus M-expression Comparison of data serialization formats ==References== ==External links== sfsexp the small, fast S-expression library for C/C++ on GitHub minilisp, by Léon Bottou. S-expressions on Rosettacode has implementations of readers and writers in many languages.
References
- Common Lisp HyperSpec: 22.4 - The Printer Dictionary: *PRINT-CIRCLE* · retrieved 2026-11-21 · rev 1378346663
Cite this reference
MLA "Common Lisp HyperSpec: 22.4 - The Printer Dictionary: *PRINT-CIRCLE*." "Common Lisp HyperSpec: 22. 2026-11-21. http://clhs.lisp.se/Body/v_pr_cir.htm. Accessed 24 November 2026.
Chicago "Common Lisp HyperSpec: 22.4 - The Printer Dictionary: *PRINT-CIRCLE*." "Common Lisp HyperSpec: 22. 2026-11-21. http://clhs.lisp.se/Body/v_pr_cir.htm.
APA "Common Lisp HyperSpec: 22. (2026). Common Lisp HyperSpec: 22.4 - The Printer Dictionary: *PRINT-CIRCLE*. Retrieved November 24, 2026, from http://clhs.lisp.se/Body/v_pr_cir.htm
- Revised 7 Report on the Algorithmic Language Scheme: Section 2.4: Datum Labels · retrieved 2026-11-21 · rev 1378346663
Cite this reference
MLA "Revised 7 Report on the Algorithmic Language Scheme: Section 2.4: Datum Labels." "Revised 7 Report on the Algorithmic Language Scheme: Section 2. 2026-11-21. https://small.r7rs.org/attachment/r7rs.pdf#section.2.4. Accessed 24 November 2026.
Chicago "Revised 7 Report on the Algorithmic Language Scheme: Section 2.4: Datum Labels." "Revised 7 Report on the Algorithmic Language Scheme: Section 2. 2026-11-21. https://small.r7rs.org/attachment/r7rs.pdf#section.2.4.
APA "Revised 7 Report on the Algorithmic Language Scheme: Section 2. (2026). Revised 7 Report on the Algorithmic Language Scheme: Section 2.4: Datum Labels. Retrieved November 24, 2026, from https://small.r7rs.org/attachment/r7rs.pdf#section.2.4
- Revised^5 Report on the Algorithmic Language Scheme · retrieved 2026-11-21 · rev 1378346663
Cite this reference
MLA "Revised^5 Report on the Algorithmic Language Scheme." 2026-11-21. https://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html. Accessed 24 November 2026.
Chicago "Revised^5 Report on the Algorithmic Language Scheme." 2026-11-21. https://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html.
APA encyclopegia.org. (2026). Revised^5 Report on the Algorithmic Language Scheme. Retrieved November 24, 2026, from https://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html
- G. Gazdar, Ch. Melish, Natural Language Processing in LISP Wikipedia revision 1378346663, retrieved 2026-11-22 · retrieved 2026-11-21 · rev 1378346663
Cite this reference
MLA "G. Gazdar, Ch. Melish, Natural Language Processing in LISP Wikipedia revision 1378346663, retrieved 2026-11-22." G. 2026-11-21. https://en.wikipedia.org/w/index.php?oldid=1378346663. Accessed 24 November 2026.
Chicago "G. Gazdar, Ch. Melish, Natural Language Processing in LISP Wikipedia revision 1378346663, retrieved 2026-11-22." G. 2026-11-21. https://en.wikipedia.org/w/index.php?oldid=1378346663.
APA G. (2026). G. Gazdar, Ch. Melish, Natural Language Processing in LISP Wikipedia revision 1378346663, retrieved 2026-11-22. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1378346663
- Revised6 Report on the Algorithmic Language Scheme · retrieved 2026-11-21 · rev 1378346663
Cite this reference
MLA "Revised6 Report on the Algorithmic Language Scheme." Sperber, Michael; Dybvig, R. 2026-11-21. https://en.wikipedia.org/wiki/Doi_(identifier). Accessed 24 November 2026.
Chicago "Revised6 Report on the Algorithmic Language Scheme." Sperber, Michael; Dybvig, R. 2026-11-21. https://en.wikipedia.org/wiki/Doi_(identifier).
APA Sperber, Michael; Dybvig, R. (2026). Revised6 Report on the Algorithmic Language Scheme. Retrieved November 24, 2026, from https://en.wikipedia.org/wiki/Doi_(identifier)
- 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 Wikipedia revision 1378346663, retrieved 2026-11-22 · retrieved 2026-11-21 · rev 1378346663
Cite this reference
MLA "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 Wikipedia revision 1378346663, retrieved 2026-11-22." S-expressions , Network Working Group, Internet Draft, Expires November 4, 1997 - R. 2026-11-21. https://web.archive.org/web/20230223024606/http://people.csail.mit.edu/rivest/Sexp.txt. Accessed 24 November 2026.
Chicago "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 Wikipedia revision 1378346663, retrieved 2026-11-22." S-expressions , Network Working Group, Internet Draft, Expires November 4, 1997 - R. 2026-11-21. https://web.archive.org/web/20230223024606/http://people.csail.mit.edu/rivest/Sexp.txt.
APA S-expressions , Network Working Group, Internet Draft, Expires November 4, 1997 - R. (2026). 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 Wikipedia revision 1378346663, retrieved 2026-11-22. Retrieved November 24, 2026, from https://web.archive.org/web/20230223024606/http://people.csail.mit.edu/rivest/Sexp.txt
- rivest sexp , Google Scholar (search) Wikipedia revision 1378346663, retrieved 2026-11-22 · retrieved 2026-11-21 · rev 1378346663
Cite this reference
MLA "rivest sexp , Google Scholar (search) Wikipedia revision 1378346663, retrieved 2026-11-22." 2026-11-21. https://scholar.google.com/scholar?hl=en&lr=&safe=off&q=rivest+sexp&btnG=Search. Accessed 24 November 2026.
Chicago "rivest sexp , Google Scholar (search) Wikipedia revision 1378346663, retrieved 2026-11-22." 2026-11-21. https://scholar.google.com/scholar?hl=en&lr=&safe=off&q=rivest+sexp&btnG=Search.
APA encyclopegia.org. (2026). rivest sexp , Google Scholar (search) Wikipedia revision 1378346663, retrieved 2026-11-22. Retrieved November 24, 2026, from https://scholar.google.com/scholar?hl=en&lr=&safe=off&q=rivest+sexp&btnG=Search
- SEXP (S-expressions) · retrieved 2023-05-04 · rev 1378346663
Cite this reference
MLA "SEXP (S-expressions)." 2023-05-04. https://web.archive.org/web/20230223024606/http://people.csail.mit.edu/rivest/Sexp.txt. Accessed 24 November 2026.
Chicago "SEXP (S-expressions)." 2023-05-04. https://web.archive.org/web/20230223024606/http://people.csail.mit.edu/rivest/Sexp.txt.
APA encyclopegia.org. (2023). SEXP (S-expressions). Retrieved November 24, 2026, from https://web.archive.org/web/20230223024606/http://people.csail.mit.edu/rivest/Sexp.txt
- a b John McCarthy (1960/2006). Recursive functions of symbolic expressions Archived 2004-02-02 at the Wayback Machine . Originally published in Communications of the ACM . Wikipedia revision 1378346663, retrieved 2026-11-22 · retrieved 2026-11-21 · rev 1378346663
Cite this reference
MLA "a b John McCarthy (1960/2006). Recursive functions of symbolic expressions Archived 2004-02-02 at the Wayback Machine . Originally published in Communications of the ACM . Wikipedia revision 1378346663, retrieved 2026-11-22." a b John McCarthy (1960/2006). 2026-11-21. http://www-formal.stanford.edu/jmc/recursive/recursive.html. Accessed 24 November 2026.
Chicago "a b John McCarthy (1960/2006). Recursive functions of symbolic expressions Archived 2004-02-02 at the Wayback Machine . Originally published in Communications of the ACM . Wikipedia revision 1378346663, retrieved 2026-11-22." a b John McCarthy (1960/2006). 2026-11-21. http://www-formal.stanford.edu/jmc/recursive/recursive.html.
APA a b John McCarthy (1960/2006). (2026). a b John McCarthy (1960/2006). Recursive functions of symbolic expressions Archived 2004-02-02 at the Wayback Machine . Originally published in Communications of the ACM . Wikipedia revision 1378346663, retrieved 2026-11-22. Retrieved November 24, 2026, from http://www-formal.stanford.edu/jmc/recursive/recursive.html
- S-expression · retrieved 2026-10-19 · rev 1378346663
Cite this reference
MLA "S-expression." Wikipedia. 2026-10-19. https://en.wikipedia.org/w/index.php?oldid=1378346663. Accessed 24 November 2026.
Chicago "S-expression." Wikipedia. 2026-10-19. https://en.wikipedia.org/w/index.php?oldid=1378346663.
APA Wikipedia. (2026). S-expression. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1378346663
Cite this article
MLA
"S-expression." encyclopegia.org, 2026, https://encyclopegia.org/wiki/s-expression. Accessed 24 November 2026.
Chicago
"S-expression." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/s-expression.
APA
encyclopegia.org. (2026). S-expression. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/s-expression
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON
Claims
| Predicate | Value | Source |
|---|---|---|
| members | 2 members | Wikipedia |