Emacs 有 BNF 模式吗?

发布于 2024-08-12 15:47:14 字数 290 浏览 5 评论 0原文

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

我查看了 CEDET 的语义包,它似乎曾经有 bnf 模式,但现在不再有。这段代码可以在谷歌上搜索,但语义 bnf-mode 似乎不存在:

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

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))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遥远的她 2024-08-19 15:47:14

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

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

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.")
孤独患者 2024-08-19 15:47:14

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

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

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

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.

森罗 2024-08-19 15:47:14

为了使答案更具可读性和可查找性,jmmcd 用以下内容回答了他自己的问题。您可以在 emacs 帮助 > 中找到更多信息省略号> 23.2.6 通用模式。


“我把它放在我的 .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.")

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.")
日记撕了你也走了 2024-08-19 15:47:14

我刚刚创建了一个 用于编辑 BNF 语法的 GNU Emacs 主要模式

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

输入图片此处描述

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文