是“正则表达式”吗? 现代编程语言中真的有“上下文相关语法”吗?

发布于 2024-07-14 17:25:35 字数 115 浏览 7 评论 0原文

多年来,“正则表达式”模式匹配变得越来越强大,以至于我想知道:它真的只是上下文相关的语法匹配吗? 它是上下文无关语法匹配的变体/扩展吗? 它现在在哪里,为什么我们不直接称呼它而不是旧的、限制性的“正则表达式”呢?

Over the years, "regex" pattern matching has been getting more and more powerful to the point where I wonder: is it really just context-sensitive-grammar matching? Is it a variation/extension of context-free-grammar matching? Where is it right now and why don't we just call it that instead of the old, restrictive "regular expression"?

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

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

发布评论

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

评论(3

妥活 2024-07-21 17:25:35

特别是捕获括号的反向引用使正则表达式比常规、上下文无关或上下文相关的语法更复杂。 这个名字只是历史悠久的(就像很多词一样)。 另请参阅 Wikipedia 中的本节和此来自 Perl 的示例说明

In particular backreferences to capturing parentheses make regular expressions more complex than regular, context-free, or context-sensitive grammars. The name is simply historically grown (as many words). See also this section in Wikipedia and this explanation with an example from Perl.

触ぅ动初心 2024-07-21 17:25:35

我的看法是:

  • 常规语言:
    • 由状态机匹配。 只能用一个变量来表示当前
      待匹配语法中的“location”:无法实现递归
  • 上下文无关语言:
    • 由堆栈机匹配。 语法中的当前“位置”由堆栈以一种或另一种形式表示。 无法“记住”之前发生的任何事情
  • 上下文相关语言之前发生的任何事情:
    • 大多数编程语言
    • 全部大多数人类语言

我所知道的大多数人类语言都具有正则表达式解析器,它们允许您匹配解析器已经遇到的内容,从而实现类似于上下文相关语法的功能。

尽管如此,正则表达式解析器无论多么复杂,都不允许递归应用规则,而这是上下文无关语法的明确要求。

在我看来,术语正则表达式主要指的是用于表达这些常规语法(星号和问号)的语法

The way I see it:

  • Regular languages:
    • Matched by state machines. Only one variable can be used to represent the current
      "location" in the grammar to be matched: Recursion cannot be implemented
  • Context-free languages:
    • Matched by a stack machine. The current "location" in the grammar is represented by a stack in one or another form. Cannot "remember" anything that occurred before
  • Context-sensitive languages:
    • Most programming languages
    • All Most human languages

I do know of regular expression parsers that allow you to match against something the parser has already encountered, achieving something like a context-sensitive grammar.

Still, regular expression parsers, however sophisticated they may be, don't allow for recursive application of rules, which is a definite requirement for context-free grammars.

The term regex, in my opinion, mostly refers to the syntax used to express those regular grammars (the stars and question marks).

寄意 2024-07-21 17:25:35

现代正则表达式实现中的某些功能违反了经典正则表达式定义。

例如 Microsoft 的 .NET 平衡组 (?<< /code>name1-name2> … )

^(?:0(?<L>)|1(?<-L>))*(?(L)(?!))$

确实匹配语言L₀₁ = {ε, 01, 0011, 000111, … }。 但根据 Pumping Lemma,这种语言是不规则的。

There are features in modern regular expression implementations that break the rules of the classic regular expression definition.

For example Microsoft’s .NET Balancing Group (?<name1-name2> … ):

^(?:0(?<L>)|1(?<-L>))*(?(L)(?!))$

This does match the language L₀₁ = {ε, 01, 0011, 000111, … }. But this language is not regular according to the Pumping Lemma.

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