【问题标题】:Is there a BNF mode for Emacs?Emacs 有 BNF 模式吗?
【发布时间】:2009-11-25 21:50:30
【问题描述】:

我必须编辑大量 .bnf 格式的语法文件。 Emacs 中有这种模式吗?

我查看了 CEDET 的语义包,似乎它曾经有一个 bnf 模式,但现在没有了。这个 sn-p 可以谷歌搜索,但语义 bnf 模式似乎不存在:

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

【问题讨论】:

    标签: emacs bnf


    【解决方案1】:

    谢谢唐。我稍微改进了代码,这是一个新版本。

    (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.")
    

    【讨论】:

    • 喜欢它,非常感谢!稍微调整了一下以适应我编写 EBNF 的个人喜好,但这是一个了不起的开始;节省了我几个小时:)
    【解决方案2】:

    我刚刚创建了一个GNU Emacs major mode for editing BNF grammars

    目前为 BNF 文件提供基本语法和字体锁定。 EBNF 和 ABNF 在我近期的计划中。

    【讨论】:

      【解决方案3】:

      为了作为答案更具可读性和可查找性,jmmcd 用以下内容回答了他自己的问题。您可以在 emacs 帮助 > elisp > 23.2.6 Generic Modes 中找到更多信息。


      “我把它放在我的 .emacs 中,它似乎可以工作。”

      (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.")
      

      【讨论】:

        【解决方案4】:

        语义 bnf 模式用于其自己的内部解析器格式。最初的“bnf”名称是一个双关语,最终使人们感到困惑。

        现有的 wisent-grammar-mode 和 bovine-grammar-mode 等语义模式是针对 CEDET 使用的语法,与原来的 bnf-mode 类似,并不代表真正的 BNF 风格语法。

        您可能对 ebnf2ps 更感兴趣,它将 ebnf 语法(yacc 等)翻译成语法图表,虽然我自己没有使用过。

        【讨论】:

        • 感谢 Eric 澄清这一点。我决定尝试实现一个非常简单的模式,仅用于语法高亮。我把它放在我的 .emacs 中,它似乎工作。 (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 "BNF 高亮的主要模式。")
        猜你喜欢
        • 2010-11-09
        • 1970-01-01
        • 2012-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-11
        • 1970-01-01
        • 2010-11-28
        相关资源
        最近更新 更多