简体   繁体   English

谁能向我解释这种与上下文无关的语法?

[英]Can anyone explain this context-free grammar to me?

I need to understand this for a homework. 我需要为作业做些了解。 You would not be giving me the answer by telling me this, you'd simply help me understand the question being asked. 告诉我这一点,您不会给我答案,您只是在帮助我理解所提出的问题。

I've read my class notes which haven't been very helpful, as well as searching all over the internet for context-free grammar info. 我阅读了课堂笔记,这些笔记并没有什么帮助,还可以在Internet上搜索不受上下文限制的语法信息。 I can't find anything that looks like what I've been given, and I'm very confused. 我找不到任何看起来像我得到的东西,而且我很困惑。

If anyone could tell me what this CFG describes, or give me a good resource to explain this subject, I would really appreciate it. 如果有人可以告诉我这个CFG的描述,或者给我很好的资源来解释这个主题,我将非常感谢。

The CFG is this: CFG是这样的:

S is the starting symbol S是开始符号

<S> → <A> | ε
<A> → 0<B> | 1<A>
<B> → 0<C> | 1<B>
<C> → 0<D> | 1<C>
<D> → 1<D> | 0<B> | ε

CFG defines a patterns of string. CFG定义了字符串模式。

Here the string can be a pattern of 1,0,e.(alphabets) The rules of CFG tell how to expand the expressions into strings of alphabets. 这里的字符串可以是1,0,e。(字母)的模式。CFG的规则说明了如何将表达式扩展为字母字符串。

<S> → <A> | ε
<A> → 0<B> | 1<A>
<B> → 0<C> | 1<B>
<C> → 0<D> | 1<C>
<D> → 1<D> | 0<B> | ε

Here A can be expanded to 0B or 1A . 在这里A可以扩展为0B1A LHS can only be expanded. LHS只能扩展。

Given a string you can verify if it is described by the CFG or not. 给定一个字符串,您可以验证它是否由CFG描述。

Lets take 1000 and see if it is described by this CFG or not. 让我们取1000,看看它是否由此CFG描述。

We will start with S which can expand to A or e. 我们将从S展开,该S可以扩展为A或e。

expr = S

e is a special symbol which says its termination of string. e是一个特殊符号,表示其字符串终止。 We will use A as it gives us hope instead of terminating with e . 我们将使用A因为它给了我们希望,而不是终止于e

expr = A      (S ->A)

A can expand to 0B or 1A. A可以扩展为0B或1A。 For our string we will use 1A. 对于我们的弦,我们将使用1A。

expr = 1A     (A ->1A)

Now 1A has A. Looking up in the rule table A can expand to 0B or 1A. 现在1A有了A。在规则表中查找可以扩展为0B或1A。 We will take 0B as it follows the given string. 在给定的字符串之后,我们将取0B。 So now our resultant is 10B. 所以现在我们的结果是10B。

expr = 10B    (A -> 0B)

Lookup B which expands to 0C or 1B. 查找B扩展为0C或1B。 We will take up 0C as it matches our given pattern. 我们将采用0C,因为它与我们给定的模式匹配。 Therefore our string becoming 100C. 因此,我们的弦变为100C。

expr = 100C   (B -> 0C)

Similarly you can go on expanding the expression and terminate with e. 同样,您可以继续扩展表达式并以e结尾。

expr = 1000D  (C -> 0D)
expr = 1000e  (D -> e)

CFG: A context-free grammar (CFG) is a term used in formal language theory to describe a certain type of formal grammar.In context-free grammars, all rules are one to one, one to many, or one to none.Languages generated by context-free grammars are known as context-free languages (CFL). CFG:上下文无关文法(CFG)是形式语言理论中用来描述某种形式的形式文法的术语。在上下文无关文法中,所有规则都是一对一,一对多或一对一。由上下文无关文法生成的语言称为上下文无关语言(CFL)。 Different context-free grammars can generate the same context-free language. 不同的无上下文语法可以生成相同的无上下文语言。 It is important to distinguish properties of the language from properties of a particular grammar. 区分语言的属性和特定语法的属性很重要。 The language equality question (do two given context-free grammars generate the same language?) is undecidable. 语言相等性问题(两个给定的上下文无关文法是否会产生相同的语言?)是不确定的。

Above lines are chosen part from CFG 以上几行是CFG的一部分

So in short, from any CFG given we try to find out the set of strings that can be described in that CFG. 简而言之,从给出的任何CFG中,我们尝试找出可以在该CFG中描述的字符串集。 These set of strings make the language of that particular CFG. 这些字符串集构成该特定CFG的语言。

Below is the solution of your question in graphical form: 下面是图形形式的问题解决方案:

CFG解决方案

In solution, the strings in rectangular boxes are the terminating steps. 在解决方案中,矩形框中的字符串是终止步骤。

So the given CFG can have strings like: 因此,给定的CFG可以具有以下字符串:

{ 100 ,0100, 1001, 1010, 01001, 10011, 10101, 010011, 0100111, ...... } {100,0100,1001,1010,01001,10011,10101,010011,0100111,......}

So this CFG can have any type of string that has: 因此,此CFG可以具有以下任何类型的字符串:

  1. at least one 1, 至少一个1
  2. at least two 0, and 至少两个0,并且
  3. length >= 3, as by observing we get the minimum length string can be 100: length> = 3,通过观察我们得到的最小长度字符串可以是100:

     S --> A --> 1B --> 10C --> 100D --> 100e --> 100 

And by observing the strings on each step you can easily get that this CFG can have any type of strings, that has above three properties. 通过观察每个步骤中的字符串,您可以轻松地获得此CFG可以具有任何三种类型的字符串的特性。

So this CFG describes a Context Free Language that has above 3 properties. 因此,此CFG描述了一种具有以上3个属性的上下文无关语言。

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

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