English 中文(简体)
Is there a BNF mode for Emacs?
原标题:
  • 时间:2009-11-25 21:50:30
  •  标签:
  • emacs
  • bnf

I have to edit lots of grammar files in .bnf format. Is there a mode for this in Emacs?

I ve looked at CEDET s semantic package, and it seems that it USED to have a bnf-mode, but not any more. This snippet is googlable, but semantic-bnf-mode doesn t seem to exist:

(autoload  semantic-bnf-mode "semantic-bnf" "Mode for Bovine Normal Form." t)
(add-to-list  auto-mode-alist  ("\.bnf$" . semantic-bnf-mode))
问题回答

Thanks Don. I improved the code very slightly, here s a new version.

(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
    ("^<.*?>" .  font-lock-function-name-face) ;; LHS nonterminals
    ("<.*?>" .  font-lock-builtin-face) ;; other nonterminals
    ("::=" .  font-lock-const-face) ;; "goes-to" symbol
    ("|" .  font-lock-warning-face) ;; "OR" symbol
    ("{:\|:}" .  font-lock-keyword-face) ;; special pybnf delimiters
   )
   ("\.bnf\ " "\.pybnf\ ") ;; filename suffixes
  nil ;; extra function hooks
  "Major mode for BNF highlighting.")

The Semantic bnf mode was for its own internal parser format. The original bnf name was a pun that ended up confusing people.

The existing Semantic modes such as wisent-grammar-mode and bovine-grammar-mode are for the grammars used by CEDET, and the original bnf-mode was similar, and did not represent a real BNF style grammar.

You are probably more interested in ebnf2ps, which translates ebnf grammars (yacc, etc) into syntax charts, though I haven t used it myself.

To be more readable and findable as an answer, jmmcd answered his own question with the following. You can find more in the emacs Help > elisp > 23.2.6 Generic Modes.


"I put this in my .emacs and it seems to work."

(define-generic-mode  bnf-mode 
   ("#") 
  nil 
   (("^<.*?>" .  font-lock-variable-name-face) 
    ("<.*?>" .  font-lock-keyword-face) 
    ("::=" .  font-lock-warning-face) 
    ("|" .  font-lock-warning-face))
   ("\.bnf\.pybnf\ ") 
  nil 
  "Major mode for BNF highlighting.")

I just created a GNU Emacs major mode for editing BNF grammars.

Currently provides basic syntax and font-locking for BNF files. EBNF and ABNF are in my plans for the near future.

enter image description here





相关问题
suppress additional braces in emacs electric mode

I started using ruby-electric-mode. I like it except that I am used to closing open brackets myself (the other pairing are still useful to me). How can I make emacs suppress additional brackets when ...

Setting up an emacs environment in windows?

I am currently constrained to a windows dev box and I want to migrate my projects from eclipse to emacs. What are some good references on setting up an emacs dev environment for windows? Anything ...

Emacs Setting which-function-mode

I would like to have which-function-mode on by default when I open up Emacs. I ve added the following lines to my .emacs file. (setq which-func-mode t) (setq which-function-mode t) When I open ...

Enabling go mode for emacs

I don t seem to be able to enable a go mode for emacs. C mode doesn t work without semicolons. The best I have found is the JavaScript mode by Karl Landstrom, since JavaScript also doesn t require ...

Custom Emacs key bindings are not working

The key bindings I ve defined in my .emacs file aren t working. Here s the file: ;init modes (menu-bar-mode 0) (tool-bar-mode 0) (cua-mode) (column-number-mode) (fset perl-mode cperl-mode) (cperl-...

eval during emacs lisp macro expansion

How can I fix the simple macro foo in (elisp)Eval During Expansion? None of the followings work: (defmacro foo1 (a) `(setq (eval ,a) t)) (defmacro foo2 (a) `(setq ,(eval a) t)) (defmacro foo3 (...

Get local cvs comment history when committing file/s in emacs

I often commit files with similar cvs comment but not in a single operation. I would like to able to bring up previous comments I ve used in a previous commit when I am in the process of writing a ...

热门标签