Union in context-free languages - complexity-theory

Is the union of a collection of context-free languages always context-free ? Justify your answer .....
I know that the answer is yes, but how can I prove it ?

To show that the finite union of context-free languages is context-free you just have to build a context-free grammar for the union language, exactly as you would do to prove that the union of two context-free languages is context-free.
If G1,...,GN are the context-free grammars for the N context-free languages you have, rename all the symbols in the each grammar (add a subscript just to avoid symbol name clashes) and then make a new grammar G with all the productions from the N grammars, plus the production:
S -> S1 | S2 | ... | SN
This grammar generates the union language, and it is context-free.

Related

Regular language for number of A's in the string

L={w|w€{a,b}, number of a is divisible by 2 }is the language. Can someone help me with the regular grammer of this?
The language is the set of all strings of a and b with an even number of a. This is a regular language and the goal is to produce a regular grammar for it.
Unless the regular grammar you're going to need is trivial, I would recommend always writing down the finite automaton first, and then converting it into a grammar. Converting a finite automaton into a grammar is very easy, and solving this problem is easy with a finite automaton. We will have two states: one will correspond to having seen an even number of a, the other an odd number. The state corresponding to having seen an even number of a will be accepting, and seeing b will not cause us to change states. The DFA is therefore:
b b
/-\ /-\
| V | V
----->(q0)--a-->(q1)
^ |
| a |
\---------/
A regular grammar for this can be formed by writing the transitions down as productions, using the states as nonterminal symbols, and including an empty production for the accepting state:
(q0) -> b(q0) | a(q1) | e
(q1) -> b(q1) | a(q0)
For the sake of completeness, you could run some other algorithms on the grammar or automaton and get a regular expression, maybe like this: b*(ab*ab*)* (just wrote that down, not sure if it's right or not, left as an exercise).

Regular grammar produced strings?

I have a paper that states:
(...) languajes such as strings of x's followed by the same number of y's (for example xxxxyyyy) cannot be specified by a regular grammar or Finite States Automaton because these devices have no mechanism for remembering how many x's were generated when the time comes to derive the y's. This shortcoming is remedied by means of rules such as S → xSy, which always generate an x and a y at the same time. (...)
So, I don't understand this statement, as far as I know, such strings can be generated with the regular grammar with production rules:
S → xS
S → yS
S → y
Where x,y are terminals, S is the starting unique non terminal. This grammar produces the derivation
S→xS→xxS→xxxS→xxxxS→xxxxyS→xxxxyyS→xxxxyyyS→xxxxyyyy
Grammar must generate every string in language, and no strings not in language.
Your grammar also generates invalid string y with the third production, or xxy, or xyxy, hence you can NOT say this is a grammar for your language.

Is the complement of any context free language context free?

I read multiple answers that state if a language is not context free, then its complement is context free (correct me if I am wrong). Is this true for the opposite? the complement of a context free language is context free?
Neither statement is true. The complement of a context-free language can be context-free or not; the complement of a non-context free language can be context-free or not.
Every regular language is context-free. Regular languages are closed under complement, so the complement of a regular language is regular. Consequently, any regular language and its complement are a pair of complementary context-free languages.
The classic example of a non-context-free language whose complement is context-free is {ww|w∈{0,1}*}. (The proof that its complement is context-free is in the answer to this question.)
For a non-context-free language whose complement is also not context-free, a simple example is the language whose valid strings are all pairs {(i, x) i halts on input x} (where i is the description of a Turing machine). That language is not context-free, but it is recursively enumerable. Its complement is not even recursively-enumerable. (See Wikipedia)

Grammar for a^n

Currently i am studying computation theory and i came up with this T - F question :
The following Grammar
S -> aS | Sa
produces the Language {a^n | n>=1}.
Well i do not know if we can say that, because this grammar does not halt. It just creates a bunch but it does accept nothing. It would surely be if we had an ε derivation rule.
It is exactly the same as having the following:
that never comes to a final state.

Challenge on Alphabet and Formal Grammar and Language

We know set A is countable if A is finite or in a one-to-one mapping to natural numbers.
Suppose ALPH be an arbitrary finite alphabet.
I summarize my inference:
a) Each arbitrary Language on ALPH is Countable. (I think this is True)
b) the set of all language from ALPH is Countable.(I think this is False)
c) for Each arbitrary Language on ALPH we have a generative formal grammar. (I think this is False)
d) Each arbitrary Language on ALPH that can be generated by formal grammar, is recursive. (I think this is True)
anyone could help me, and maybe correct me?
Without loss of generality, we can assume that ALPH is merely the set {0,1}. (Any other finite language can of course be encoded using the set {0,1}). Assuming that by a language L that you intend some arbitrary subset of ALPH*, we can assume that L is an arbitrary subset of {0,1}*.
Let S = {0,1}*.
a) The set S is countable. Since L is a subset of S, L is countable.
b) The set of all languages over S then is the powerset of S, which can be put into 1-1 correspondence with the Real numbers. Hence, not countable.
c) I believe that this is false, agreeing with your supposition. However, it depends on your definition of a 'generative formal grammar'. If you allow for formal grammars where individual rules of the grammar are undecidable, and/or allow for infinite generation rules, this becomes less clear. For any particular definition for 'generative formal grammar', where the collection of 'generative formal grammars' is enumerable, then of course, the answer is false.
d) In general, I believe that the answer to this is false. If you restrict yourself to formal grammars corresponding to context-free languages, then of course, your answer is true. However, consider http://en.wikipedia.org/wiki/Post_correspondence_problem The problem is undecidable, yet the generation rules are pretty clear.

Resources