XML、S-Expressions 和重叠范围...它叫什么?
我正在阅读 XML 不是 S 表达式。 XML 范围有点严格,S 表达式也是如此。在我见过的每种编程语言中,都不能有以下内容:
BOLD BOTH ITALIC
== BOLD BOTH ITALIC
它甚至无法用 S 表达式来表达:
(bold "BOLD" (italic "BOTH" ) "ITALIC" )
== :(
有任何编程语言支持这种“重叠”范围吗?它有任何实际用途吗?
I was reading XML is not S-Expressions. XML scoping is kind of strict, as are S-expressions. And in every programming language I've seen, you can't have the following:
<b>BOLD <i>BOTH </b>ITALIC</i>
== BOLD BOTH ITALIC
It's not even expressible with S-Expressions:
(bold "BOLD" (italic "BOTH" ) "ITALIC" )
== :(
Does any programming language support this kind of "overlapping" scoping? Could there be any practical use for it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重叠标记结构有许多实际用途。考虑 人文学科文本分析的并发标记。 重叠结构标记国际研讨会指出:
文本编码倡议 (TEI) 发布指南处理非嵌套信息并提供重叠的 XML 语法。他们在 2004 年表示:
处理重叠结构的一些选项包括:
SGML 有一个CONCUR 功能可用于支持重叠结构,尽管 Goldfarb(标准)写道“因此我建议不要使用 CONCUR 创建文档的多个逻辑视图”。
GODDAG 提供了一种数据结构,用于表示具有重叠结构的文档。
XCONCUR 是一种实验性标记语言,其主要目标是提供一种方便的方法来在 XML 中表达并发层次结构 -喜欢时尚。
Overlapping markup structures has many practical uses. Consider for example applications of concurrent markup for text analysis in the humanities. The International Workshop on Markup of Overlapping Structures noted that:
The Text Encoding Initiative (TEI) publishes Guidelines to handle non-nesting information and provides an XML syntax for overlap. They stated in 2004 that:
Some options to handle overlapping structures include:
SGML has a CONCUR feature that can be used to support overlapping structures, although Goldfarb (the author of the standard) writes that "“I therefore recommend that CONCUR not be used to create multiple logical views of a document".
GODDAG provides a data structure for representing documents with overlapping structures.
XCONCUR is an experimental markup language with the major goal to provide a convenient method to express concurrent hierarchies in an XML-like fashion.
可能没有任何编程语言在其正式定义中支持重叠范围。虽然技术上可行,但它会使实施变得比需要的更加复杂。它还会使语言变得含糊不清,无法接受很可能被认为是错误的内容。
我现在能想到的唯一实际用途是它的打字量更少并且编写更直观,就像在标记中编写属性感觉更直观而无需不必要的引号一样,如
> 中所示。
而不是
。我认为强制执行嵌套结构也可以提高处理效率。通过强制执行嵌套结构,解析器可以将节点推入和弹出到单个堆栈上以跟踪打开节点的列表。对于重叠的范围,您需要一个开放范围的有序列表,每当遇到 begin-new-scope 令牌时都必须附加到该列表,然后在每次遇到end-scope 标记来查看哪个开放范围最有可能是它关闭的范围。
尽管没有编程语言支持重叠作用域,但有一些 HTML 解析器支持它作为其错误恢复算法的一部分,包括所有主要浏览器中的算法。
此外,C 中的
switch
语句允许看起来像重叠范围的构造,如 Duff's Device:因此,理论上,编程语言通常可以对作用域具有类似的语义,以便在需要时允许使用此类技巧进行优化,但可读性会非常低。
goto
语句以及某些语言中的break
和continue
还可以让您构建程序,使其表现得像重叠作用域:There probably isn't any programming language that supports overlapping scopes in its formal definition. While technically possible, it would make the implementation more complex than it needed to be. It would also make the language ambiguous as to accept as valid what would very likely supposed to be a mistake.
The only practical use I can think of right now is that it's less typing and is written more intuitively, just as writing attributes in mark-up feel more intuitive without uneccessary quotes, as in
<foo id=45 />
instead of<foo id="45" />
.I think that enforcing nested structures makes for more efficient processing, too. By enforcing nested structures, the parser can push and pop nodes onto a single stack to keep track of the list of open nodes. With overlapped scopes, you'd need an ordered list of open scopes that you'd have to append to whenever you come across a begin-new-scope token, and then scan each time you come across an end-scope token to see which open scope is most likely to be the one it closes.
Although no programming languages support overlapping scopes, there are HTML parsers that support it as part of their error-recovery algorithms, including the ones in all major browsers.
Also, the
switch
statement in C allows for constructs that look something like overlapping scopes, as in Duff's Device:So, in theory, a programming language can have similar semantics for scopes in general to allow these kinds of tricks for optimization when needed but readability would be very low.
The
goto
statement, along withbreak
andcontinue
in some languages also lets you structure programs to behave like overlapped scopes: