Is there a BNF mode for Emacs?

前端 未结 4 813
忘掉有多难
忘掉有多难 2021-01-02 13:01

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

相关标签:
4条回答
  • 2021-01-02 13:53

    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.")
    
    0 讨论(0)
  • 2021-01-02 14:02

    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.

    0 讨论(0)
  • 2021-01-02 14:03

    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.")
    
    0 讨论(0)
  • 2021-01-02 14:05

    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.

    0 讨论(0)
提交回复
热议问题