简体   繁体   English

这种无上下文的语法是正则表达式吗?

[英]Is this context-free grammar a regular expression?

I have a grammar defined as follows: 我的语法定义如下:

A -> aA*b | empty_string

Is A a regular expression? A正则表达式? I'm confused as to how to interpret a BNF grammar. 我对如何解释BNF语法感到困惑。

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; 这里, A是非终端; 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 . 鉴于您只有一个规则,它也是您的起始符号 - 此语法中的任何生成都必须以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. ab是终端符号 - 它们在语言的字母表中,不能扩展。 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 ) . 因此,这种语言指定的词语类似于平衡括号,除了ab而不是()

For instance, you could produce ab as follows: 例如,您可以按如下方式生成ab

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

Similarly, you could produce aabb : 同样,你可以生成aabb

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

Or even aababb : 甚至是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 A s. 它被称为Kleene-closure,它代表你可以使用0或更多A s所做的所有单词。

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) BNF语法是Context Free Grammars,它可以生成无上下文语言,可以使用Push Down Automata(堆栈机器)进行解析

Context Free Grammars can do everything Regular Grammars can and more. 上下文免费语法可以做普通语法可以做的一切。

A appears to be a BNF grammar rule. A似乎是BNF语法规则。 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. 所有带*的东西都不是正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM