铁路图是否有扩展来捕获异常?

发布于 2024-10-15 08:42:05 字数 827 浏览 5 评论 0原文

铁路图是可视化上下文无关语法的流行方法,您可以将巴科斯-诺尔范式映射到这些图。但是BNF的一些变体,例如W3C-BNF 允许例外(由于上下文无关语言在差异下不封闭,这些例外必须保持规律)。我想在铁路图中可视化带有例外的语法。我应该发明自己的图形符号扩展还是有人已经尝试过这个?

这是一个带有例外的规则的示例(是的,您也可以无例外地表达此特定语法,但这不是重点):

comment := "<!--" (string - "--") "-->"

例外可以是任何常规语法。我想过添加通过某种特殊类型的箭头或线条连接到非终结符的异常(这里用感叹号表示):

[<] → [!] → [-] → [-] → (string) → [-] → [-] → [>]
                          !
                          ! → [-] → [-] → ↯

PS: 语法错误,应该是

comment := "<!--" (string - (string "--" string | string "-")) "-->"

也许是非直观的使用否定的否定是形式语法中很少使用的原因之一吗?

Railroad diagrams are a popular method to visualize context-free grammars and you can map Backus-Naur Form to these diagrams. But some variants of BNF, for instance W3C-BNF allow exceptions (as context-free languages are not closed under difference, these exceptions must be regular). I'd like to visualize a grammar with exceptions in a railroad diagram. Should I invent my own extension to the graphical notation or has someone already experimented with this?

Here is an example of a rule with exception (yes, you could also express this particular grammar without exception, but that's not the point):

comment := "<!--" (string - "--") "-->"

An exception can be any regular grammar. I thought about adding exception connected to non-terminal symbols by some special type of arrow or line (here indicated with exclamation marks):

[<] → [!] → [-] → [-] → (string) → [-] → [-] → [>]
                          !
                          ! → [-] → [-] → ↯

P.S: The grammar was wrong, it should be

comment := "<!--" (string - (string "--" string | string "-")) "-->"

Maybe the non-intuitive use of negation is one reason why it is little used in formal grammars?

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

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

发布评论

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

评论(1

终遇你 2024-10-22 08:42:05

最近在为W3C语法编写铁路图生成器时,我思考了一段时间如何表示这一点,但我无法想出一个令我满意的解决方案。也没有找到任何可以遵循的例子。

它不仅仅是集合差算子,传统铁路图中没有对应的图形。代码点、范围和补码的表示法也不太合适。

最后我没有以图形方式扩展模型。我所做的是画一个接线盒来保存原始符号中没有铁路等效项的语法片段。为了与文字框区分开来,它被设置为斜体。以下是 XML 推荐的示例:

XML comment Railroad diagram

EBNF 生成是:

Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'

编辑

根据 Jakob 提出的不同形状的建议,未解析的 EBNF 表达式现在以六边形显示:

在此处输入图像描述

When writing a Railroad Diagram Generator for W3C grammars recently, I thought for a while about how to represent this, but I could not come up with a solution that I was happy with. Did not find any examples to follow either.

It is not only the set-difference operator, for which there is no graphical equivalent in conventional railroad diagrams. The notation for codepoints, ranges, and complements also does not fit well.

In the end I refrained from extending the model graphically. What I did is draw a terminal box to hold the grammar fragment that has no railroad equivalent in the original notation. For distinguishing this from literal boxes, it is set in italics. Here is an example from the XML recommendation:

XML comment railroad diagram

The EBNF production was:

Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'

Edit:

Following Jakob's proposal for a different shape, unresolved EBNF expressions are now shown in a hexagon:

enter image description here

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