Let us take our simple language
    L = { a, a+a, a+a+a, a+a+a+a, ... }
and add a second operator to it, so L becomes
    L = { a, a+a, a*a, a+a+a, a+a*a, a*a+a, a*a*a, ... }

Here is an EBNF grammar for this language.

EBNF:    expr -> a [ (+|*) a ]*

This grammar does not seem to lend itself to parsing strings in the language.

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


1.) Here is an ambiguous BNF grammar for this language.


BNF:    expr -> expr + expr | expr * expr | a


Using this grammar you can parse the string "a+a*a+a" numerous ways.

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


2.) Here is an unambiguous, "right associative", BNF grammar
(and two EBNF grammars derived from it).


BNF:    expr -> a + expr | a * expr | a

EBNF:   expr -> a [ (+|*) expr ]    (notice that this is right recursive)

EBNF:   expr -> a [ (+|*) a ]*      (we removed (or "eliminated") the recursion)


Using this BNF grammar you parse the string "a+a*a+a" as if it were
"grouped" as a+(a*(a+a))

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


3.) Here is an unambiguous, "left associative", BNF grammar
(and two EBNF grammars derived from it).


BNF:    expr ->  expr + a | expr * a | a

EBNF:   expr -> [ expr (+|*) ] a  (notice that this is left recursive)

EBNF:   expr -> [ a (+|*) ]* a    (we removed (or "eliminated") the recursion)


Using this BNF grammar you parse the string "a+a*a+a" as if it were
"grouped" as ((a+a)*a)+a


Notice that the grammars that use the Kleene star don't tell you
how you should parse a string.

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


4.) Here is a BNF grammar that attempts to make + left associative and
make * right associative.


BNF:    expr ->  a * expr | expr + a | a


NOTE: This gramar defines a different language.

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


5.) Here is an ambiguous BNF grammar that gives * higher precedence than + but
does not specify the associativity for either operator.


BNF:   expr -> expr + expr | term
       term -> term * term | a


Notice how this needs two levels of non-terminals. This is our first grammar
that requires two non-terminals. Recall that we used two non-terminals when we
added parentheses to the one operator language, but the two non-terminals were
not required.

Using this BNF grammar, the strings "a*a+a" and "a+a*a" both have unique
parses (that give * higher precedence) but the strings "a+a+a" and "a*a*a"
both have two parses.

Exercise: Parse the strings
   "a+a+a*a", "a+a*a+a", "a+a*a*a", "a*a+a+a", "a*a+a*a", "a*a*a+a".
In what sense does * have higher precedence than +?

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


6.) Here is an ambiguous BNF grammar that gives * higher precedence than +,
makes + left associative, and does not specify the associativity *.


BNF:   expr -> expr + term | term
       term -> term * term | a


EBNF:  expr -> term [ + term ]*
       term -> term * term | a

Using this BNF grammar, the strings "a*a+a", "a+a*a" and "a+a+a" have unique
parses but the string "a*a*a" has two parses.

Exercise: Parse the strings
   "a+a+a*a", "a+a*a+a", "a+a*a*a", "a*a+a+a", "a*a+a*a", "a*a*a+a".

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


7.) Here is an ambiguous BNF grammar that gives * higher precedence than +,
makes + right associative, and does not specify the associativity *.


BNF:   expr -> term + expr | term
       term -> term * term | a


EBNF:  expr -> term [ + expr ]
       term -> term * term | a

Using this BNF grammar, the strings "a*a+a", "a+a*a" and "a+a+a" have unique
parses but the string "a*a*a" has two parses.

Exercise: Parse the strings
   "a+a+a*a", "a+a*a+a", "a+a*a*a", "a*a+a+a", "a*a+a*a", "a*a*a+a".

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


8.) Here is an ambiguous BNF grammar that gives * higher precedence than +,
makes * left associative, and does not specify the associativity +.


BNF:   expr -> expr + expr | term
       term -> term * a | a


EBNF:  expr -> expr + expr | term
       term -> a [ * a ]*

Using this BNF grammar, the strings "a*a+a", "a+a*a" and "a*a*a" have unique
parses but the string "a+a+a" has two parses.

Exercise: Parse the strings
   "a+a+a*a", "a+a*a+a", "a+a*a*a", "a*a+a+a", "a*a+a*a", "a*a*a+a".

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


9.) Here is a BNF left associative grammar that gives * higher precedence than +.


BNF:   expr -> expr + term | term
       term -> term * a | a


EBNF:  expr -> term [ + term ]*
       term -> a [ * a ]*

Exercise: Parse the strings
   "a+a+a*a", "a+a*a+a", "a+a*a*a", "a*a+a+a", "a*a+a*a", "a*a*a+a".

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


10.) Here is a BNF grammar that gives * higher precedence than +,
makes + left associative and makes * right associative.


BNF:   expr -> expr + term | term
       term -> a * term | a


EBNF:  expr -> term [ + term ]*
       term -> a [ * term ]

Exercise: Parse the strings
   "a+a+a*a", "a+a*a+a", "a+a*a*a", "a*a+a+a", "a*a+a*a", "a*a*a+a".

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