Context Free Grammars

cs

Context free grammars are a concept originating in linguistics but widely used in (theoretical) computer science.

For this section, I'm going to use a bunch of mathematical notation, but I'll try to explain everything as it comes along.

A set is collection of elements where each element appears only once. The set of the number from one to four can be written as context_free_grammars_694b81cb574fadce2fb2b2630326c4ebc4aab556.svg .

An alphabet is a set with a finite number of elements (the natural numbers 1, 2, 3, …, would be an example for an infinite set).

A context free grammar has four components:

  1. An alphabet context_free_grammars_5c3e4878689fddea367a25dcbf6cc872ce67a81d.svg of terminals (read: “things where the expansion terminates”)
  2. An alphabet context_free_grammars_912f4e618488fc4c1098c8c48490a756f8bd8c48.svg of non-terminals (read: “things that can expand into other things”)
  3. A set context_free_grammars_43d3674afb7b19ac30f6a8dcfa7be1d3b093b0af.svg of production rules
  4. The start symbol context_free_grammars_a696b268abddefb68a424fbfaeefda3387c604ff.svg , an element of the set of non-terminals

It's important that the terminal and non-terminal alphabets are disjoint , i.e. there is no element that is part of both sets.

Production rules have the form context_free_grammars_c2e5c8cad5b59ee241b17d198a30f8847a0ba6fa.svg . context_free_grammars_c53bcff43ea8073fd66d6dbc5eb415671325dfd5.svg can be read as “Combine the sets context_free_grammars_5c3e4878689fddea367a25dcbf6cc872ce67a81d.svg and context_free_grammars_912f4e618488fc4c1098c8c48490a756f8bd8c48.svg ”, context_free_grammars_0fd589f26c9ef51c69c93ddde25793d0cd79cb72.svg is called Kleene star and can be read as “repeated zero or more times”.

The part on the right of the context_free_grammars_71f8f20bf230e5e4186e739635a03441434d814f.svg is called right hand side , or RHS , the part on the left left hand side or LHS .

A grammar is expanded starting with the start symbol and then replacing each non-terminal with the RHS of one of its rules.

Time for a simple example:

Starting from context_free_grammars_5542d9b736fda71065aa35fbfb19231ab57f1e8a.svg , the grammar expands into context_free_grammars_627ff87c9e13052ae321c2b08c7f1cea4e810617.svg , then each context_free_grammars_a64c93fed20752354d5e42bb2db5a6a07cac0b19.svg expands into context_free_grammars_0c22fc100bd61f8b91d297a6b4dcc6cf41880850.svg , so the end result is context_free_grammars_a5de709aa2427e432bde95883853db817d5e2237.svg .

There can be multiple rules with the same right hand side , so there are (possibly infinitely) many sequences a grammar can expand to.

context_free_grammars_60ec9a3315a23235f68e0e0fa3c16b028af2515b.svg is the notation for an empty sequence, so when expanding context_free_grammars_edd0749b940fdbf07da59ccec29c17d642d9b486.svg , the context_free_grammars_3e9ab3478b9acc1162e589434454cc00b381e885.svg just disappears.

Expanding this grammar, we have two options, either expand context_free_grammars_3e9ab3478b9acc1162e589434454cc00b381e885.svg to context_free_grammars_787f371723b0789b6770be8227164f2859b89b2f.svg or to context_free_grammars_60ec9a3315a23235f68e0e0fa3c16b028af2515b.svg .

Expanding context_free_grammars_787f371723b0789b6770be8227164f2859b89b2f.svg further, we have two options again, either expand it to context_free_grammars_90f29652037307dbe5d76f765005e49f246dc6c2.svg or to context_free_grammars_bf8ffeabf8ff79d4ebffcb5c1613ad019a7a4ad1.svg .

This can be repeated infinitely many times, so the grammar produces sequences of context_free_grammars_7c41eff3e5dffe9e398dbb4ed0123ec7f2fcee4e.svg of any length. (Remember the Kleene star? We can use it to write that as context_free_grammars_be50a80fc695dd553aa8bf2f823fca23b433705c.svg instead).

Backlinks


If you have an idea how this page could be improved or a comment send me a mail.