Here is a simple language that we can use to understand associativity.


       L = { a, a+a, a+a+a, a+a+a+a, ... }


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


Here is an EBNF grammar for this language.


EBNF:    expr -> a [ + a ]*


Using this grammar, how might you parse the string "a+a+a+a"?
It doesn't seem that you can.


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


Here is an ambiguous BNF grammar for this language.


BNF:    expr -> expr + expr | a


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


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


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


BNF:    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))


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


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


BNF:    expr ->  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 from the last two BNF grammars that, in essence, left associative
means you have a (left recursive) grammar production that captures the
"big stuff" on the left side of an operator and it captures a "little thing"
on the right side of the operator, e.g.,
       expr -> expr + a
and right associative means you have a (right recursive) grammar production
that captures the "big stuff" on the right side of an operator and a
"little thing" on the left side of the operator, e.g.,
       expr -> a + expr


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

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


Let us look at how the recursion is removed from the previous BNF grammar.

Here is a left-most derivation of a sequence of sentential forms from the BNF grammar.

                     expr
               expr + a
           expr + a + a
       expr + a + a + a
   expr + a + a + a + a
      a + a + a + a + a

Notice how the "expression part" is growing on its left hand side, and in
each step it grows by concatenating the string "a +". So that means we can
think of growing a string in the language by starting with the sting "a"
and then concatenating on the left as many "a+" strings as we like. Hence,
the description [a+]*a, which uses iteration (the Kleene star) in place of
recursion.


Question: What does the following EBNF give you?

EBNF:   expr -> [ expr + ]* a