There are lots of definitions online about what a Context-Free Grammar is, but nothing I find is satisfying my primary trouble:

What context is it free of?

To investigate, I Googled "context sensitive grammar" but I still failed to find what the "context" was all about.

Can someone please explain what the context term refers to in these names?

  • 5
    $\begingroup$ I find the wikipedia explanation pretty good - "A formal grammar is considered "context free" when its production rules can be applied regardless of the context of a nonterminal. No matter which symbols surround it, the single nonterminal on the left hand side can always be replaced by the right hand side." - it can be rephrased and simplified to become "Plain English", but the gist of it seems rather clear to me. $\endgroup$
    – jkff
    May 27 '15 at 4:30
  • 1
    $\begingroup$ @jkff it's great that you find the explanation good, but I am still failing to understand what "context" really means here. I need to see an example where there is context, and where there is no context. To me, it seems every grammar I've seen has context $\endgroup$ May 27 '15 at 5:06
  • $\begingroup$ Isn't it clear from the definition? $\endgroup$
    – Raphael
    May 27 '15 at 6:46
  • 2
    $\begingroup$ Ironically, the crucial bit of context was missing from that definition, so I just added a sentence to explain it. $\endgroup$ May 27 '15 at 10:10
  • 3
    $\begingroup$ Example: In C++11, override can either be a variable name or a keyword, depending on where it is used (ie, its context). If used after a method declaration, it is a keyword. Otherwise it isn't. This is an example of a context sensitive grammar. $\endgroup$ May 29 '15 at 15:23

You are right, there always is a context in some sense. I don't think you can understand what "context" means in "context-free" without understanding a production.

A production is a substitution rule. It says that, to generate strings within the language, you can substitute what is on the left for what is on the right:

A -> xy

This means that the abstract sequence A can be replaced by the character "x" followed by the character "y". You can also have more complex productions:

zA -> xy

This means that the character "z" followed by the abstract sequence A can be replaced by the characters "x" and "y".

A context-free production simply means that there is only one thing on the left hand side. The first example is context-free, because A can be replaced by "x" and "y" no matter what comes before or after it. However, in the second example, the character "z" has to appear before the A, and then the combination can be replaced by "x" and "y", so there is some context involved.

A context-free grammar is then just a grammar with only context-free productions.

The second example is actually an example of an unrestricted production. There is another category that is between context-free and unrestricted called "context-sensitive". An example of a context-sensitive production is:

zA -> zxy

The difference being that what comes before A (and after) on the left hand side has to be preserved on the right. This effectively means that only A is substituted, but can only be substituted in the proper context.

  • 2
    $\begingroup$ Thank you that is a huge help! I have never actually seen a grammar example yet with more than one variable on the left side as you showed. I guess that's why I couldn't see what the "context" was. Thank you $\endgroup$ May 27 '15 at 5:28
  • 4
    $\begingroup$ Perhaps a simpler contextual example would be zA -> zxy : A is still replaced by xy, but only after z. $\endgroup$
    – MSalters
    May 27 '15 at 10:22
  • $\begingroup$ In more detail, this answer talks about an unrestricted grammar, whereas @MSalters points out that context sensitive grammar would make for a better example of the meaning of context. $\endgroup$
    – user5386
    May 28 '15 at 9:17
  • $\begingroup$ @MSalters: You have a good point. I modified my answer. I had a hard time coming up with a way of adding those details to my description without it sounding more complex, so what I ended up doing was just adding more detail at the end. $\endgroup$ May 28 '15 at 14:03

Consider the rule $$ A \rightarrow \beta $$ and lets say you have a sentential form $$ αAδ $$ then A reducing to β does not depend on what α and δ are. So that way it is context free as it does not depend on surrounding context.

  • 1
    $\begingroup$ What is beta? Is it a terminal or a variable? And can you explain "sentential form"? Does that mean it can't be derived further? $\endgroup$ May 27 '15 at 5:07
  • $\begingroup$ Beta can be anything its just that A->beta is a rule. Sentential form means that given your starting symbol using the rules of grammar we transform it into a result. This result is called sentential form. After every using every rule we get a new sentential form. $\endgroup$ May 27 '15 at 5:10
  • 3
    $\begingroup$ @Imray You need to go look at the formal definitions for grammars and context-free grammars. There's not shortcut to understanding formal objects. $\endgroup$
    – Raphael
    May 27 '15 at 6:47
  • 1
    $\begingroup$ @Imray A sentential form is a string of terminal and non-terminal symbols that derives from the axiom (also called initial symbol) by application of grammar rules (also called productions). A sentential form can be derived into another one by applying a rule to one of its non-terminal symbols. When there are no non-terminals left the sentential form is a sentence, i.e. a string or word in the language defined or generated by the grammar. The terminology comes from the linguists who were at one time the main contributors to this part of formal language theory. $\endgroup$
    – babou
    May 27 '15 at 8:52
  • $\begingroup$ I thought a sentential form can't have any variables in it - it needs to be just terminals. $\endgroup$ May 27 '15 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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