Context Free Grammars

What does the following context-free grammar describe?

S is the start symbol and the set of nonterminal symbols is { 0 , 1 } \{0,1\} . Here are the production rules:

  • S \rightarrow 0
  • S \rightarrow 0S
  • S \rightarrow 1S.
Strings that end in 0 Strings that have an odd number of 1’s Strings that have an equal number of 1’s and 0’s Strings that end in 1 Strings that have an even number of 1’s and 0’s

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Karleigh Moore
Jun 13, 2016

The correct answer is "strings that end in 0" The easiest way to figure this out is the provide examples of strings that can be made with these rules that eliminate some of the choices.

I’ll build up a string as I go through the rules: string = “”

S --> 0 (string = “0”)

S --> 0S (string = “00”)

S --> 1S (string = “100”)

S --> 1S (string = “1100”)

This string has an even (and equal) number of 1’s and 0’s so “strings that have an even number of 1’s and 0’s” and “ strings that have an equal number of 1’s and 0’s ” are eliminated.

Resetting the string to “”

S --> 0 (string = “0”)

S --> 1S (string = “10”)

This has an odd number of 1’s, so “strings that have an odd number of 1’s” is eliminated.

Now we are left with “strings that end in 1” and “strings that end in 0”

Is it possible to make a string that ends in a 1? No, because the start symbol must be 0 first. Any string that we make, in fact, will end in a 0 because we only ever add symbols to the left of the existing string. If the first symbol is always 0, then it doesn’t matter what we add to the left, the rightmost element (the end) will always be 0.

“This string has an even (and equal) number of 1’s and 0’s so “strings that have an even number of 1’s and 0’s” and “ strings that have an equal number of 1’s and 0’s ” are eliminated.” “This has an odd number of 1’s, so “strings that have an odd number of 1’s” is eliminated.”

I’m confused about these two sentences. Why is the conclusion eliminated when the evidence is found?

Ashley Zheng - 2 years, 2 months ago

Log in to reply

The explanation is slightly confusing. What wasn't mentioned is that although evidence was found for all 3 of those options, the options still don't hold true in all cases. For example, "strings that have an odd number of 1's" might be satisfied by string="10" as in the explanation, but another valid string="110" wouldn't work. Therefore, "strings that have an odd number of 1's" can't be correct and should be eliminated. "Strings that end in 0" is the only option that holds true under all possibilities specified by this CFG.

Terran Chao - 1 year, 8 months ago
  • (1) S -> 0 is what terminates the string since S is not called again. The string will end with a 0.
  • (2) S -> 0S implies that we can have a zero followed by another S
  • (3) S -> 1S implies that we can have a 1 followed by another S
  • We can call (2) and (3) however many times we like in any order we like, so there is no mandate of an even or odd number of 0s or 1s.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...