Here are some simple languages that we can use for the purpose of
seeing how to derive strings in a language and how to associate a
tree with a string.

These languages start out vaguely "expression" like but at the
end we will have a grammar for all of basic arithmetic.

---------------------------------------------------------------------

Language E1.

E ::= 'a' '+' E

This grammar has a problem because it does
not have a way to stop its recursion.

---------------------------------------------------------------------

Language E2.

A simple way to fix E1.

E ::= 'a' '+' E   // right recursion means right associative
    | 'a'

Some example strings are:
"a"
"a + a"
"a + a + a"
"a + a + a + a"

Derivation tree:        Expression tree:
     E                         +
    /|\                       / \
   / | \                     /   \
  a  +  E                   a     +
       /|\                       / \
      / | \                     /   \
     a  +  E                   a     +
          /|\                       / \
         / | \                     /   \
        a  +  E                   a     a
              |
              |
              a

---------------------------------------------------------------------

Language E3.

Almost the same as E2.

E ::= E '+' 'a'   // left recursion means left associative
    | 'a'

Some example strings are:
"a"
"a + a"
"a + a + a"
"a + a + a + a"

Derivation tree:        Expression tree:
           E                         +
          /|\                       / \
         / | \                     /   \
        E  +  a                   +     a
       /|\                       / \
      / | \                     /   \
     E  +  a                   +     a
    /|\                       / \
   / | \                     /   \
  E  +  a                   a     a
  |
  |
  a

---------------------------------------------------------------------

Language E4.

Almost the same as E2 and E3.

E ::= E '+' E  // left and right recursion means ambiguous
    | 'a'

Some example strings are:
"a"
"a + a"
"a + a + a"
"a + a + a + a"

The last sentence has lots of derivation trees.
The grammar is ambiguous.

---------------------------------------------------------------------

Language E5.

Allow grouping instead of forcing
left-to-right or right-to-left.

E ::=  E '+' 'a'         // left recursion means left associative
    | 'a' '+' '(' E ')'  // this is not "right recursion"
    | '(' E ')'
    | 'a'

Some example strings are:
"(a + a)"
"(a + a) + a"
"a + (a + a)"
"(a + a) + (a + a)"
"a + (a + (a + a))"
"((a + a) + a) + a"

"a + (a + a) + a"
Derivation tree:      Expression tree:
          E                   +
         /|\                 / \
        / | \               /   \
       E  +  a             +     a
      / \                 / \
     /   \               /   \
   a + ( E )            a     +
        /|\                  / \
       / | \                /   \
      E  +  a              a     a
      |
      |
      a

"a + ((a + a) + a)"
Derivation tree:      Expression tree:
        E                   +
       / \                 / \
      /   \               /   \
     a + ( E )           a     +
          /|\                 / \
         / | \               /   \
        E  +  a             +     a
       /|\                 / \
      / | \               /   \
     (  E  )             a     a
       /|\
      / | \
     E  +  a
     |
     |
     a

---------------------------------------------------------------------

Language E6.

Another way to add parentheses to
our addition language is to define
another nonterminal.

E ::= E '+' T   // left recursion means left associative
    | T
T ::= '(' E ')'
    | 'a'


"a + (a + a) + a"
Derivation tree:        Expression tree:
         E                     +
        /|\                   / \
       / | \                 /   \
      E  +  T               +     a
     /|\     \             / \
    / | \     \           /   \
   E  +  T     a         a     +
   |    /|\                   / \
   |   / | \                 /   \
   T  (  E  )               a     a
   |    /|\
   |   / | \
   a  E  +  T
      |     |
      |     |
      T     a
      |
      |
      a


"a + ((a + a) + a)"
Derivation tree:      Expression tree:
         E                   +
        /|\                 / \
       / | \               /   \
      E  +  T             a     +
      |    /|\                 / \
      |   / | \               /   \
      T  (  E  )             +     a
      |    /|\              / \
      |   / | \            /   \
      a  E  +  T          a     a
         |     |
         |     |
         T     a
        /|\
       / | \
      (  E  )
        /|\
       / | \
      E  +  T
      |     |
      |     |
      T     a
      |
      |
      a

---------------------------------------------------------------------

Language E7.

Addition and subtraction (but no parentheses).
Subtraction has the same precedence as addition.


E ::= E '+' 'a'   // left recursion means left associative
    | E '-' 'a'   // left recursion means left associative
    | 'a'


E ::= 'a' '+' E   // right recursion means right associative
    | 'a' '-' E   // right recursion means right associative
    | 'a'


NOTE: The following grammar does not work.
You cannot have both a left associative and a right
associative operator at the same level of precedence.

E ::= E '+' 'a'   // left recursion means left associative?
    | 'a' '-' E   // right recursion means right associative?
    | 'a'

"a - a + a"  // Parses two ways (so the grammar is ambiguous)!
"a + a - a"  // Doesn't parse (but we want it to)!

---------------------------------------------------------------------

Language E8.

Addition and multiplication (but no parentheses).
Multiplication should have higher precedence than addition.

As with parentheses, we need a second nonterminal.

E ::= E '+' T    // left recursion means left associative
    | T
T ::= T '*' 'a'  // left recursion means left associative
    | 'a'

"a * a + a"

Derivation tree:        Expression tree:
         E                     +
        /|\                   / \
       / | \                 /   \
      E  +  T               *     a
      |     |              / \
      |     |             /   \
      T     a            a     a
     /|\
    / | \
   T  *  a
   |
   |
   a

"a + a * a"

Derivation tree:        Expression tree:
         E                     +
        /|\                   / \
       / | \                 /   \
      E  +  T               a     *
      |    /|\                   / \
      |   / | \                 /   \
      T  T  *  a               a     a
      |  |
      |  |
      a  a


NOTE: This grammar defines both addition and
multiplication as left associative. What about
making them both right associative? Or define
addition as left associative and multiplication
as right associative. Or define addition as right
associative and multiplication as left associative.


Problem: What's wrong with the following grammar?
Or, to ask this another way, what language does
the grammar generate?

E ::= E '+' T
    | 'a'
T ::= T '*' 'a'
    | 'a'

---------------------------------------------------------------------

Language E9.

Add parentheses to E8. This requires
another nonterminal.

E ::= E '+' T    // left recursion means left associative
    | T
T ::= T '*' F    // left recursion means left associative
    | F
F ::= '(' E ')'
    | 'a'

This grammar says that
 1. "expressions are a sum of terms"
 2. "terms are a product of factors"
 3. "factors are parenthesized expressions"


"(a + a) * a"

Derivation tree:        Expression tree:
         E                     *
         |                    / \
         |                   /   \
         T                  +     a
        /|\                / \
       / | \              /   \
      T  *  F            a     a
      |     |
      |     |
      F     a
     /|\
    / | \
   (  E  )
     /|\
    / | \
   E  +  T
   |     |
   |     |
   T     F
   |     |
   |     |
   F     a
   |
   |
   a

---------------------------------------------------------------------

Language E10.

Add the negation operator to E9. Negation should have
higher precedence than multiplication. It can be put
into the production with the parentheses. This allows
expressions like "(-a + a) * -(a + a)"

E ::= E '+' T    // left recursion means left associative
    | T
T ::= T '*' F    // left recursion means left associative
    | F
F ::= '-' F      // right recursion means right associative
    | '(' E ')'
    | 'a'


"(-a + a) * -(a + a)"

Derivation tree:           Expression tree:
            E                        *
            |                       / \
            |                      /   \
            T                     +     -
           /|\                   / \     \
          / | \                 /   \     \
         T  *  F               -     a     +
        /     / \              |          / \
       /     /   \             |         /   \
      F     -     F            a        a     a
     /|\         /|\
    / | \       / | \
   (  E  )     (  E  )
     /|\         /|\
    / | \       / | \
   E  +  T     E  +  T
   |     |     |     |
   |     |     |     |
   T     F     T     F
   |     |     |     |
   |     |     |     |
   F     a     F     a
  / \          |
 /   \         |
-     F        a
      |
      |
      a

---------------------------------------------------------------------

Language E11.

Put subtraction and division into E10. Subtraction should
be the same precedence level as addition. Division should
be the same precedence level as multiplication.

E ::= E '+' T    // left recursion means left associative
    | E '-' T    // left recursion means left associative
    | T
T ::= T '*' F    // left recursion means left associative
    | T '/' F    // left recursion means left associative
    | F
F ::= '-' F      // right recursion means right associative
    | '+' F      // right recursion means right associative
    | '(' E ')'
    | 'a'


"+a * -a"

Derivation tree:         Expression tree:
           E                    *
           |                   / \
           |                  /   \
           T                 +     -
          /|\                |     |
         / | \               |     |
        T  *  F              a     a
       /     / \
      /     /   \
     F     -     F
    / \          |
   /   \         |
  +     F        a
        |
        |
        a