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

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


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


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


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

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


8) Here is an unambiguous, "right associative", BNF grammar
(and two EBNF grammars derived from it). Neither operator has
precedence over the other.


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


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

EBNF:   expr -> 'a ( ('+'|'*') 'a' )*    // "eliminate" the recursion


Using this BNF grammar we can parse the string "a+a*a+a" only one way, as
if it were "grouped" as a+(a*(a+a)).

Notice that the EBNF grammar that uses the Kleene star doesn't tell us
how we should parse a string.

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


9.) Here is an unambiguous, "left associative", BNF grammar
(and three EBNF grammars derived from it). Neither operator has
precedence over the other.


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


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

EBNF:   expr -> ( 'a' ('+'|'*') )* 'a'   // "eliminate" the recursion

EBNF:   expr -> 'a' ( ('+'|'*') 'a' )*

Using this BNF grammar we can parse the string "a+a*a+a" only one way, as
if it were "grouped" as ((a+a)*a)+a.


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

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


10.) Here is an ambiguous BNF grammar that makes '+' right associative
but does not specify the associativity of '*'. Neither operator has
precedence over the other.


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

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


11.) Here is a BNF grammar that attempts to make '+' left associative and
make '*' right associative. Neither operator has precedence over the other.


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.
      See one of the other pages for more details about this example.

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


12.) 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 '+'?

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


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

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


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

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


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

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


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


BNF:   expr -> expr '+' term | term    // expr is left recursive
       term -> 'a' '*' term | 'a'      // term is right recursive


EBNF:  expr -> term ( '+' term )*      // expr is now iterative
       term -> 'a' [ '*' term ]        // term is right recursive


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


Exercise: Rewrite the grammars to give '*' higher precedence than '+',
and make '+' right associative and '*' left associative. Then redo
the previous exercise.

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


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


BNF:   expr -> expr '+' term | term    // expr is left recursive
       term -> term '*' 'a' | 'a'      // term is left recursive


EBNF:  expr -> term ( '+' term )*      // expr is now iterative
       term -> 'a' ( '*' 'a' )*        // term is now iterative


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


Exercise: Rewrite the grammars to give '*' higher precedence than '+',
and make both '+' and '*' right associative. Then redo the previous
exercise.

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