这个上下文无关语法是正则表达式吗?

发布于 2024-08-20 04:25:12 字数 121 浏览 3 评论 0原文

我的语法定义如下:

A -> aA*b | empty_string

A 是正则表达式吗?我对如何解释 BNF 语法感到困惑。

I have a grammar defined as follows:

A -> aA*b | empty_string

Is A a regular expression? I'm confused as to how to interpret a BNF grammar.

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

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

发布评论

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

评论(3

×眷恋的温暖 2024-08-27 04:25:12

不,这个问题实际上与正则表达式无关。上下文无关语法指定无法用正则表达式描述的语言。

这里,A 是一个非终结符;也就是说,它是一个必须通过产生式规则扩展的符号。鉴于您只有一个规则,它也是您的开始符号 - 此语法中的任何产生式都必须以 A 开头。

产生式规则是

(1)    A -> aA*b | 
(2)         empty_string

ab 是终结符号 - 它们位于语言的字母表中,并且无法扩展。当左侧不再有非终结符时,就完成了。

因此,该语言指定的单词类似于平衡括号,只不过使用 ab 而不是 ()

例如,您可以生成 ab,如下所示:

A -> aA*b (using 1)
aAb -> ab (using 2)

同样,您可以生成 aabb:

A -> aA*b (1)
aAb -> aaA*bb (1)
aaAbb -> aabb (2)

甚至 aabbabb:

A -> aA*b
aA*b -> aabA*b:
aaba*b -> aababA*b:
aababA*b: -> aababb

明白了吗?星号可能有点令人困惑,因为您已经在正则表达式中看到过它,但实际上它在这里和那里做同样的事情。它称为 Kleene 闭包,它表示可以用 0 个或多个 A 组成的所有单词。

No, this question doesn't actually have to do with regular expressions. Context-free grammars specify languages that can't be described by regular expressions.

Here, A is a non-terminal; that is, it's a symbol that must be expanded by a production rule. Given that you only have one rule, it is also your start symbol - any production in this grammar must start with an A.

The production rule is

(1)    A -> aA*b | 
(2)         empty_string

a and b are terminal symbols - they are in the alphabet of the language, and cannot be expanded. When you have no more nonterminals on the left-hand side, you are done.

So this language specifies words that are like balanced parentheses, except with a and b instead of ( and ).

For instance, you could produce ab as follows:

A -> aA*b (using 1)
aAb -> ab (using 2)

Similarly, you could produce aabb:

A -> aA*b (1)
aAb -> aaA*bb (1)
aaAbb -> aabb (2)

Or even aababb:

A -> aA*b
aA*b -> aabA*b:
aaba*b -> aababA*b:
aababA*b: -> aababb

Get it? The star symbol may be a bit confusing because you have seen it in regular expressions, but actually it does the same thing here as there. It is called a Kleene-closure and it represents all words you can make with 0 or more As.

醉殇 2024-08-27 04:25:12

正则表达式生成正则语言,并且可以使用状态机进行解析。

BNF 语法是上下文无关语法,它生成上下文无关语言,并且可以使用下推自动机(堆栈机)进行解析。

上下文无关语法可以做常规语法可以做的所有事情,甚至更多。

Regular Expressions generate Regular languages and can be parsed with State Machines.

BNF grammars are Context Free Grammars which generate Context Free languages and can be be parsed with Push Down Automata (stack machines)

Context Free Grammars can do everything Regular Grammars can and more.

强辩 2024-08-27 04:25:12

A 似乎是 BNF 语法规则。我不太确定为什么你会将此与正则表达式混淆。您是否因为其中有*而感到困惑?所有带有 * 的东西都不是正则表达式。

A appears to be a BNF grammar rule. I'm not really sure why you have this confused with a regular expression. Are you confused because it has a * in it? Everything that has a * isn't a regular expression.

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