What's really meant by context-free in the term context-free grammar?

I have been studying compilers for a while, and I have been searching what's meant by "context" in grammar and what it means for grammar to be "context-free", but with no result.

So can anyone help with this ?

• What do you mean by "really"? Which explanations have you read and what don't you understand? IIRC, every halfway decent textbook on the matter will explain what they mean. – Raphael Jan 3 '17 at 19:46
• Here's a relatable example. Consider the word "read". It's a single word that has two completely different meanings. One is the present tense "to read", the other is the past tense "I read". If you saw the word "read" in a piece of text, you cannot disambiguate which of the two meanings it represents, without looking at the context. Thus, English is a context-sensitive, because you can't parse each token (word) without considering it in context. A context-sensitive grammar is one in which the meaning of every token is unambiguously deducible from the single token that represents it. – Alexander - Reinstate Monica Jan 4 '17 at 8:26

The context can be explained with regards to the production rules allowed for different grammars in Chomsky hierarchy.

If you consider context-free grammars, their production rules have the following form:

$$A \rightarrow \alpha$$

So, you can observe that the left part of this kind of rules is made up of only one non-terminal symbol; thus, the substitution of the non-terminal symbol takes place without considering its "context", that is the other symbols it is surrounded by.

On the other hand, if you consider production rules of context-sensitive grammars, they have the following form:

$$\beta A \gamma \rightarrow \beta \alpha \gamma$$

where $A$ is a non-terminal and $\alpha$, $\beta$, $\gamma$ are sequences of non-terminals and terminals.

In this case the "context" (i.e., $\beta$ and $\gamma$) of the non-terminal symbol to be substituted influences the effect of the substitution and it is part of the rule itself.

You can find more details in this answer on mathematics and in this answer on software engineering.

• Thank you for the answer. But the strange thing for me is that a similar question was asked on mathematics SE. – Shady Atef Jan 3 '17 at 20:05
• Note that $\beta$ and $\gamma$ don't need to be part of the production's outcome. They also could've been substituted by some other sequence as one can see in @David Richerby's answer. – Frozn Jan 3 '17 at 21:26
• @Frozn AFAIK the one given here is the standard definition according to the Chmosky hierarchy. Sure, there are grammars more powerful than context-sensistive that allow any type of production, but the standard context-sensitive grammars do not. – Bakuriu Jan 4 '17 at 10:05
• @Frozn: Bakariu is right, here we are talking about grammars defined according to the Chomsky hierarchy, that is based on increasingly restrictive conditions on the production rules. In particular, context-free grammars are type-2 grammars, while context-sensitive ones are type-1. However, type-0 grammars have production rules that are not limited by any restriction and thus are called unrestricted rewriting systems. Here you can find a short description of Chomsky hierarchy with some examples. – PieCot Jan 4 '17 at 10:54
• @Bakuriu and PieCot Well thanks for that, I knew the Chomsky hierarchy. Somewhen someone introduced monotonic grammars as context-sensitiv and together with type-0 and type-1 from the Chomsky hierarchy this resulted in the general rule $\beta A\gamma \rightarrow \delta$ as long as $|\beta A\gamma| \leq |\delta|$. – Frozn Jan 4 '17 at 13:30

"Context" is surrounding text. Context-free grammars are context-free in the sense that the rules look like $A\to\text{things}$, rather than $\text{stuff}\,A\,\text{more-stuff}\to\text{things}$. The left-hand side of a rule is always a single non-terminal symbol. That is, the rules for expanding a non-terminal symbol don't depend on what text appears around that symbol (its context), but only depend on the symbol itself. For example, in the grammar for a programming language, the term $\mathrm{Expr}$ expands to the same kind of expression whether you're writing an assignment (e.g., x:=y+z), passing arguments to a function (e.g., f(y+z)) or returning a value from a function (e.g., return y+z).

Generically speaking, even regular languages may have context dependencies, meaning that you can determine - to some extent - in what ways symbols can appear in the vicinity of other symbols in a string that belongs to that language.

What is specific to context-free grammars is that when there are multiple ways of substituting a non-terminal symbol, by applying different rules with the same non-terminal on the right side, the choice of which rule to apply is never dependent on what is happening around this symbol during the derivation process.

You can think of them as context-free derivation languages, context-free languages for short.