简体   繁体   中英

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. 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.

The CFG is this:

S is the starting symbol

<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.

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.

<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 . LHS can only be expanded.

Given a string you can verify if it is described by the CFG or not.

Lets take 1000 and see if it is described by this CFG or not.

We will start with S which can expand to A or e.

expr = S

e is a special symbol which says its termination of string. We will use A as it gives us hope instead of terminating with e .

expr = A      (S ->A)

A can expand to 0B or 1A. For our string we will use 1A.

expr = 1A     (A ->1A)

Now 1A has A. Looking up in the rule table A can expand to 0B or 1A. We will take 0B as it follows the given string. So now our resultant is 10B.

expr = 10B    (A -> 0B)

Lookup B which expands to 0C or 1B. We will take up 0C as it matches our given pattern. Therefore our string becoming 100C.

expr = 100C   (B -> 0C)

Similarly you can go on expanding the expression and terminate with 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). 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

So in short, from any CFG given we try to find out the set of strings that can be described in that CFG. These set of strings make the language of that particular 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:

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

So this CFG can have any type of string that has:

  1. at least one 1,
  2. at least two 0, and
  3. length >= 3, as by observing we get the minimum length string can be 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.

So this CFG describes a Context Free Language that has above 3 properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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