# How can the intersection of CFLs and REGs be CFL if REG is a proper subset of CFL?

Intersection of CFL and regular is always CFL. But according to Chomsky hierarchy diagram, regular languages lie completely inside CFL. So, as regular set is completely inside CFL set, their intersection should be regular right ?

Am I interpreting Chomsky diagram wrongly ?

No, you are misinterpreting stuff.

The set of languages (DFL, regular, ...) and the set of strings in a language are independent.

It is entirely possible that a regular language contains strings that are not inside a Context free language.

The trivial example is where the regular language is all strings $\Sigma^*$ and the CFL is any other language. The intersection between them is the original CFL.

• In this diagram encrypted-tbn3.gstatic.com/…. Regular language is subset of CFL. So according to subset property of sets , any set's intersection with it's subset is the subset itself right (which is regular) ? – Rajesh R Dec 4 '17 at 15:06
• But each language is a set of strings so when talking about the intersection between 2 languages you are talking about intersecting the set of strings. That is independent from the set of languages. – ratchet freak Dec 4 '17 at 15:18
• Ok so Chomsky hierarchy says intersection of set of all CFL's and set of all Regular languages is set of regular languages right? – Rajesh R Dec 4 '17 at 15:50

You are confusing two different statements.

1. $\mathrm{CFL} \cap \mathrm{REG} = \mathrm{REG}$ or, equivalently,
for all $L_1 \in \mathrm{REG}$ : $L_1 \in \mathrm{CFL}$.
2. For all $L_1 \in \mathrm{REG}$, $L_2 \in \mathrm{CFL}$ : $L_1 \cap L_2 \in \mathrm{CFL}$.

The first makes a statement on two sets of languages (aka language classes), the second makes one over (many) pairs of sets of strings (aka languages). So in mixing the two, you are creating a type error.

Also, you seem to suggest that if a language is context-free, it's not regular. That is false; we have $\mathrm{REG} \subsetneq \mathrm{CFL}$ (cf. 1.), so showing a language is CFL (using 2.) does not preclude it from being regular. Specifically, it's easy to find examples of a non-regular context-free and a regular language whose intersection is regular.

• Very clear distinction between the two statements, but which one is correct? (I know they're not mutually exclusive, but if I understood the other answer correctly, only one of them happens to be true.) – Wildcard Dec 4 '17 at 16:17
• Both are true. The false one is $\forall L_1 \in REG, L_2 \in CFL: L_1 \cap L_2 \in REG$. – reinierpost Dec 4 '17 at 16:45