CFG

Context-Free Grammar

A context-free grammar (CFG) is a set of recursive rewriting rules (or productions) used to generate patterns of strings.

A CFG consists of the following components:
- a set of terminal symbols, which are the characters of the alphabet that appear in the strings generated by the grammar.
- a set of nonterminal symbols, which are placeholders for patterns of terminal symbols that can be generated by the nonterminal symbols.
- a set of productions, which are rules for replacing (or rewriting) nonterminal symbols (on the left side of the production) in a string with other nonterminal or terminal symbols (on the right side of the production).
- a start symbol, which is a special nonterminal symbol that appears in the initial string generated by the grammar.

To generate a string of terminal symbols from a CFG, we:

- Begin with a string consisting of the start symbol;
- Apply one of the productions with the start symbol on the left hand size, replacing the start symbol with the right hand side of the production;
- Repeat the process of selecting nonterminal symbols in the string, and replacing them with the right hand side of some corresponding production, until all nonterminals have been replaced by terminal symbols.

Finite set of grammar rules is a quadruple (V, T, P, S) where

- V is a set of non-terminal symbols.

- T is a set of terminals where V ∩ T = NULL.

- P is a set of rules

- S is the start symbol.

Draw with rules
  • Complicate

  • name -> Aragorn | Sam | Legolas | Mithrandir | Faramir | Frodo | Gimli

    creature -> dragon | warg | great spider | great eagle | werewolf | giant | troll | fellbeast

    mood -> vexed | indignant | impassioned | wistful | astute | courteous | frightened

    story -> name traveled with his pet creature. name was never mood, for the creature was always too mood.

Samples

Simple

  • Click button for sample.
  • Normal

  • Click button for sample.
  • Complicate

  • Click button for sample.
  • Tree
    Sample Parse Tree

    Lets take these words as examples.

    w = abaaba

    For this rule our Grammar instances are like these:
    V  =  {S},   T  =  {a,  b},  P = S -> ε  |  aSa  |  bSb, 
    S  =  S



    w = the wolf captured by the lion

    For this bunch of rules our Grammar instances are like these:
    V = {S,  NP,  VP, N,  V},  
    T = {the,  by,  bear,  lion,  rabbit,  wolf,  beaten,  haunted,  captured,  killed},  
    P  =   S   -> NP  VP,
    NP   ->  the  N,
    VP  ->  V  | V  by  NP,
    N  ->  bear  |  lion  |  rabbit  |  wolf,
    V   ->  beaten  |  haunted  |  captured  |  killed
    S  =  S