context-free-grammarautomataformal-languagesautomata-theorycontext-sensitive-grammar

Is this grammar context free or not?


G:    S ---> aSb
      S ---> λ

As tought me the first production rule is context-free (because the left side is less than right side) but for second production rule, it isn't (because the left side length equals the right side).

Well, what can we say for this grammar in this statement. Is it context-free or not?


Solution

  • It is context free.

    "Context free" refers to the presence of context in the lefthand side of the production rule.
    It does not matter that the righthand side is equally long; it only matters that the lefthand side consists of a single nonterminal.
    When the lefthand side of a production rule consists of a single nonterminal, the rule can be applied everywhere that that nonterminal appears, regardless of the context in which it appears.

    If the rule had been, for example, aS ---> λ, then it would be context-sensitive; it could only be applied in those places where the nonterminal S was preceded by the terminal symbol a; when it was in the context of being preceded by a.

    For completeness, a grammar is only context-free if all its production rules are.