bnf-mode is a GNU Emacs major mode for editing BNF grammars.

Currently provides basic syntax and font-locking for BNF files. BNF notation is supported exactly form as it was first announced in the ALGOL 60 report. EBNF and ABNF are not supported but they are planned for the near future.

When developing this mode, the following documents were taken into account:


Installation and Activation

Known to work with GNU Emacs 24.3 and later. BNF Mode may work with older versions of Emacs, or with other flavors of Emacs (e.g. XEmacs) but this is not guaranteed.

The recommended way is to use ELPA, MELPA Stable or MELPA. If either is in your ‘package-archives’, do M-x ‘package-install’ RET ‘bnf-mode’ RET. To learn on how to use any other installation methods refer to relevant documentation.


Interactive Commands

Command for the M-x prompt are:

By default any file that matches the glob *.bnf is automatically opened in ‘bnf-mode’.


To customize various options, use command as follows: M-x ‘customize-group’ RET ‘bnf’ RET.


Feel free to ask question or make suggestions in issue tracker.

History of this page

Previously this page contained a way to create a simple mode for editing grammar files in BNF format. Here’s a small contribution which does some syntax highlighting. This approach has small issues which may not affect your workflow.

(define-generic-mode 'bnf-mode
 () ;; comment char: inapplicable because # must be at start of line
 nil ;; keywords
   ("^#.*" . 'font-lock-comment-face) ;; comments at start of line
   ("^<[^ \t\n]*?>" . 'font-lock-function-name-face) ;; LHS nonterminals
   ("<[^ \t\n]*?>" . 'font-lock-builtin-face) ;; other nonterminals
   ("::=" . 'font-lock-const-face) ;; "goes-to" symbol
   ("\|" . 'font-lock-warning-face) ;; "OR" symbol
 '("\\.bnf\\'") ;; filename suffixes
 nil ;; extra function hooks
 "Major mode for BNF highlighting.")

By jmmcd (jamesmichaelmcdermott ATTTT gmail)

That sounds like it could be useful when using Vinicius’s EBNF2PS. – DrewAdams