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, ... }

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


1.) 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.

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


2.) 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.

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


3.) 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))

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


4.) 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.

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


5.) Here is an ambiguous BNF grammar that makes '+' right associative but
does not specify the associativity of '*'.


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


It is pretty obvious that we can also define grammars that make '+' left
associative without specifying an associativity for '*', or we can make
'*' left (or right) associative without specifying an associativity for '+'.
But we cannot make '+' left associative and '*' right associative
(see the next language).

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


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


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


NOTE: This grammar defines a different language. For example, the string
      "a+a*a" will not parse (and the string "a*a+a" has two parses).

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


7.) Here is an ambiguous BNF grammar that gives '*' higher precedence than
'+' but does not specify the associativity of 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 '+'?

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


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


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".

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


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


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".

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


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


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".

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


11.) 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".

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


12.) Here is a BNF grammar that gives '*' higher precedence than '+'
and makes both '+' and '*' left associative.


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".

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