Emacs Lisp: Syntax Table

By Xah Lee. Date: . Last updated: .

What is Syntax Table

Emacs has a concept of Syntax Table. The basic idea is, each character (every Unicode character), is categorized into a class. Classes are: letters, punctuations, brackets, programing language identifiers, comment character, string delimiters, etc.

Where is Syntax Table Used

Syntax table is heavily used in emacs. For example,

Syntax Table is Local to Buffer

Each buffer has its own version of syntax table. Typically, when a Major Mode is activated, it changes the current buffer's syntax table.

View Current Syntax Table

Alt+x describe-syntax to show current buffer's syntax table.

emacs describe-syntax 2021-07-26
Emacs Alt+x describe-syntax output. Left is char. Middle is the char's syntax class abbreviation.

Syntax Classes

Each syntax class is identified by a 1-char code.

Here's the most important character classes and their 1-char code.

Most Important Syntax Classes
CodeClass
-whitespace character.
wword. (typically the alphabets A to Z, and other languages's letters and Chinese characters.)
_symbol. (characters of symbol class, plus chars of “word” class, together is programing language identifier characters.)
.punctuation.
"string delimiter.
(left bracket.
)right bracket.
<comment start.
>comment end.
\escape character.

For complete list, see Syntax Class Table (ELISP Manual)

Syntax Descriptor

A syntax descriptor is a lisp string that specifies a syntax class (the 1-char code), a matching character (used only for characters in the bracket class) and flags (used for comment delimiters).

Syntax Descriptors (ELISP Manual)

You'll use Syntax Descriptor with the function modify-syntax-entry, when you create a syntax table. Here's a quick example.

;; make period to have syntax class of symbol
(modify-syntax-entry ?. "_" my-syn-table)

;; make the «French double quote» to be brackets
(modify-syntax-entry"(»" my-syn-table)
(modify-syntax-entry")«" my-syn-table)

;; make char's syntax for C++ style comment “// …”
(modify-syntax-entry ?/ ". 12b" my-syn-table)
(modify-syntax-entry ?\n "> b" my-syn-table)

Now let's look at the line

(modify-syntax-entry ?. "_" my-syn-table)

modify-syntax-entry take 3 args.

  1. A character. [see Emacs Lisp: Character Type]
  2. A syntax descriptor string.
  3. A variable whose value is a syntax table.

?. is the character period . (U+2E: FULL STOP).

"_" is the syntax descriptor string. It means the period character is in symbol class.

Now let's look at this line:

(modify-syntax-entry ?« "(»" my-syn-table)

It means, the character « is in the class of opening bracket, and its matching character is ».

Now, this line:

(modify-syntax-entry ?/ ". 12b" my-syn-table)

Let's look at the syntax descriptor string ". 12b". It means:

The flags in syntax descriptor is very complex. See elisp manual Syntax Flags (ELISP Manual)

For practical examples of using the syntax flags for comments, see Emacs Lisp: Syntax Color Comment in Major Mode

Basic Facts of Syntax Table

Create Syntax Table

Here's typical way to create a syntax table.

;; typical way to create and set syntax table

(defvar xpy-mode-syntax-table nil "Syntax table for `xpy-mode'.")

(setq xpy-mode-syntax-table
      (let ( (synTable (make-syntax-table)))

        ;; set/modify each char's class
        (modify-syntax-entry ?# "<" synTable)
        (modify-syntax-entry ?\n ">" synTable)
        ;; more lines here ...

        ;; return it
        synTable))

;; then, have this line inside your mode definition. So that, when user calls your major mode, it will set syntax table for whatever is the current buffer of user
(set-syntax-table xpy-mode-syntax-table)

Standard Syntax Table

Syntax Table Inheritance

Emacs syntax table has inheritance. That is, each syntax table you create inherits a parent syntax table. You do not need to set every character's syntax class. When a syntax table does not have entry for a character, it uses the parent table.

Syntax table commands have optional parameter for a table name of a parent table. When not specified, the parent syntax table is the standard syntax table.

Syntax Table Functions (ELISP Manual)