Question

In: Computer Science

Give a context-free grammar for {w ∈ {b, c}∗| #c(w) = #b(w)}.

Give a context-free grammar for {w ∈ {b, c}∗| #c(w) = #b(w)}.

Solutions

Expert Solution

L = {w | w ∈ {b, c} ∗ , #c(w) = #b(w)}, where #i(w) is the number of occurrences of a symbol i in a string w. In other words, L is the language of all strings that has an equal number of b’s and c’s.

Hence, Take w ∈ L. Then either – w = bx, where #b(x) = #c(x) − 1, or – w = cy, where #a(y) = #c(y) + 1. Let A to be a nonterminal that generates LA = {x |#b(x) = #c(x) − 1}, and B be a nonterminal that generates LB = {y |#b(y) = #c(x) + 1}. Then, the grammar for L is S → bA | cB |ε

Let’s deal with A. Take x ∈ LA, i.e. #b(x) = #c(x) − 1. Then, either

  • x = bz, where #b(z) = #c(z) − 2, or
  • x = ct, where #b(t) = #c(t)

In the second case, t ∈ L, and so we will write A → cS. What to do with the first case ? Observation: z must be a concatenation of two strings from LA. Why ? Intuitively – z has 2 less a’s than b’s. At the beginning of z, the difference between the numbers of b’s and c’s is 0, at the end of z the difference is -2. Since this difference may only change by 1 on every additional symbol of z, it had to become -1 before it became -2.

Formally: let f(w) = #b(w) − #c(w), and let wi stand for the prefix of w of length i. Then, for a string w of length n, w ∈ LA implies that f(w0) = 0, while f(wn) = −2. It follows that for some i, f(wi) = −1. Thus, w = wiu, where u ∈ LA, and v ∈ LA. Thus, for LA, we have the following rule A → bAA | cS. Similarly, for LB we obtain B → cBB | bS.


Related Solutions

Consider the context-free grammar G = ( {S}, {a, b}, S, P) where P = {...
Consider the context-free grammar G = ( {S}, {a, b}, S, P) where P = { S -> aaSb | aab }. Construct a NPDA M that such that L(M) = L(G). I would like the transition graph for this NPDA please.
What makes a deterministic context free grammar deterministic?
What makes a deterministic context free grammar deterministic?
Let G = (AN , AT , S, P) be a context-free grammar in Chomsky normal...
Let G = (AN , AT , S, P) be a context-free grammar in Chomsky normal form. Prove that if there exists a word w ∈ L(G) generated by a derivation that uses more than |P| + |AT | steps, then L(G) is infinite.
Prove that if G is a context-free grammar in which every variable occurs on the left...
Prove that if G is a context-free grammar in which every variable occurs on the left side of at most one production, then G is unambiguous.
Write down a Context Free Grammar with 8 productions including 4 variables and 3 terminals. i....
Write down a Context Free Grammar with 8 productions including 4 variables and 3 terminals. i. Check whether the Context Free Grammar is ambiguous or not. Justify your answer with necessary example. ii. Finally, construct the reduced grammar corresponding to your generated grammar.
Consider the following context-free grammar G: E ® T + E ® * T i E...
Consider the following context-free grammar G: E ® T + E ® * T i E ® f i E ® * f + T ® + Questions: (5 points) Compute the Canonical LR(1) Closure set for state I0 for grammar G. (10 points) Compute (draw) the DFA that recognizes the Canonical LR(1) sets of items for grammar G. (5 points) Construct the corresponding Canonical LR(1) parsing table. (10 points) Compute (draw) the DFA for LALR(1). (5 points) Construct LALR(1)...
#1 We are given the grammar rules A ➝ F B E B ➝ A C...
#1 We are given the grammar rules A ➝ F B E B ➝ A C These rules are only some of the rules of a larger grammar G, but we are not given the remaining rules of G. We are told that A is the start symbol of G and that the following holds: {ε, c, d} ⊆ FIRST(C) {ε, e} ⊆ FIRST(E) {ε, f, g} ⊆ FIRST(F) Recall that end of file is denoted EOF. The symbol ⊆...
Draw DFA C = { w is an element of {a,b}* and #a(w) is even and...
Draw DFA C = { w is an element of {a,b}* and #a(w) is even and #b(w) us a multiple of 3)
a) Give 5 good examples of Regular language. b) Give 5 good examples of Regular Grammar.
a) Give 5 good examples of Regular language. b) Give 5 good examples of Regular Grammar.
Simplify the grammar G. Does L(G) contain ε ? S -> A B C | B...
Simplify the grammar G. Does L(G) contain ε ? S -> A B C | B a B A -> a A | B a C | a a a B -> b B b | a | D C -> C A | A C D -> ε
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT