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 ?

  • 7
    $\begingroup$ 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. $\endgroup$
    – Raphael
    Jan 3, 2017 at 19:46
  • 2
    $\begingroup$ 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. $\endgroup$
    – Alexander
    Jan 4, 2017 at 8:26

3 Answers 3


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.

  • $\begingroup$ Thank you for the answer. But the strange thing for me is that a similar question was asked on mathematics SE. $\endgroup$
    – Shady Atef
    Jan 3, 2017 at 20:05
  • 1
    $\begingroup$ 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. $\endgroup$
    – Frozn
    Jan 3, 2017 at 21:26
  • 1
    $\begingroup$ @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. $\endgroup$
    – Bakuriu
    Jan 4, 2017 at 10:05
  • 2
    $\begingroup$ @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. $\endgroup$
    – PieCot
    Jan 4, 2017 at 10:54
  • $\begingroup$ @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|$. $\endgroup$
    – Frozn
    Jan 4, 2017 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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.