这个上下文无关语法是正则表达式吗?
我的语法定义如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,这个问题实际上与正则表达式无关。上下文无关语法指定无法用正则表达式描述的语言。
这里,
A
是一个非终结符;也就是说,它是一个必须通过产生式规则扩展的符号。鉴于您只有一个规则,它也是您的开始符号 - 此语法中的任何产生式都必须以A
开头。产生式规则是
a
和b
是终结符号 - 它们位于语言的字母表中,并且无法扩展。当左侧不再有非终结符时,就完成了。因此,该语言指定的单词类似于平衡括号,只不过使用
a
和b
而不是(
和)
。例如,您可以生成
ab
,如下所示:同样,您可以生成
aabb
:甚至
aabbabb
:明白了吗?星号可能有点令人困惑,因为您已经在正则表达式中看到过它,但实际上它在这里和那里做同样的事情。它称为 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 anA
.The production rule is
a
andb
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
andb
instead of(
and)
.For instance, you could produce
ab
as follows:Similarly, you could produce
aabb
:Or even
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
A
s.正则表达式生成正则语言,并且可以使用状态机进行解析。
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.
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.