Expanding Expressions Given an expression of one-letter variables ([a-z]), operators (*, +, &amp;) and parenthesis, expand it using the following axioms:

a * b != b * a
a * b * c = (a * b) * c = a * (b * c)
a + b = b + a
a + b + c = (a + b) + c = a + (b + c)
a * (b + c) = a * b + a * c
a &amp; b = b &amp; a
a &amp; (b + c) = a &amp; b + a &amp; c

   | Comm | Assoc | Dist
 * | NO   | YES   |     
 + | YES  | YES   | *    
 &amp; | YES  | YES   | + *  


The user will input an expression, the syntax of the input expression is called "input form". It has the following grammar:

inputform ::= expr
var ::= [a-z] // one lowercase alphabet
expr ::= add
       | add &amp; expr
add ::= mult
      | mult + add
mult ::= prim
       | prim * mult
       | prim mult
prim ::= var
       | ( expr )


Before 
That means, the order of operations is * + &amp;, a + b * c &amp; d + e = (a + (b * c)) &amp; (d + e) 
Furthermore, the operator * can be omitted:
    a b (c + d) = ab(c + d) = a * b * (c + d)

Whitespace is stripped out before parsing.

Examples:

(a + b) * (c + d)
= (a + b)(c + d)
= (a + b)c + (a + b)d
= ac + bc + ad + bd
(a &amp; b)(c &amp; d)
= ac &amp; ad &amp; bc &amp; bd
(a &amp; b) + (c &amp; d)
= a + c &amp; a + d &amp; b + c &amp; b + d
((a &amp; b) + c)(d + e)
= ((a &amp; b) + c)d + ((a &amp; b) + c)e (I'm choosing the reduction order that is shortest, but you don't need to)
= ((a &amp; b)d + cd) + ((a &amp; b)e + ce)
= ((ad &amp; bd) + cd) + ((ae &amp; be) + ce)
= (ad + cd &amp; bd + cd) + (ae + ce &amp; be + ce)
= ad + cd + ae + ce &amp; ad + cd + be + ce &amp; bd + cd + ae + ce &amp; bd + cd + be + ce


Due to commutativity, order of some terms do not matter.

Your program will read an expression in input form, and expand the expression fully, and output the expanded expression in input form, with one space separating operators, and no spaces for multiplication. (a + bc instead of a+b * c or a + b * c or a + b c)

The fully expanded form can be written without any parens, for example, a + b &amp; a + c is fully expanded, because it has no parens, and a(b + c) is not.

Here is an example interactive session (notice the whitespaces in input)

$ expand
&gt; a(b + (c&amp;d))
ab + ac &amp; ab + ad
&gt; x y * (wz)
xywz
&gt; x-y+1
Syntax error
&gt; c(a +b
Syntax error
Ruby, 698 648
O=[A='&amp;',B='+',C='*']
M='Syntax error'
def u(x)c=x.shift;(c.nil?)?S[0]=~/[a-z]/?(S.shift):S[0]=='('?(S.shift;o=u(O*1);S[0]==')'?(S.shift;o):abort(M)):abort(M):(o=[c];loop{o&lt;&lt;u(x*1);break if S[0]!=c;S.shift};o)end
def d(e,a,b)a!=e[0]?e:(i=e.index{|x|x[0]==b};(i.nil?)?e:y([b]+e[i][1..-1].map{|k|z=e*1;z[i]=k;z}))end
def y(e)e.map!{|x|(x.is_a?Array)?(y(x)):(x)};o=[e[0]];e[1..-1].map{|x|x[0]==o[0]?(o+=x[1..-1]):(o&lt;&lt;x)};d(d(d(o.size==2?o[1]:o,C,B),C,A),B,A)end
def j(e)(e.is_a?Array)?(e.map!{|x|j(x)};o=e.shift;e*(o=='*'?'':&quot; #{o} &quot;)):e;end
S=gets.gsub(/ /,'').gsub(/([a-z)])(?=[a-z(])/,'\1*\2').split(//)
k=u(O*1)
puts S!=[&quot;\n&quot;]?(M):(j y k)

This solution consists of a basic recursive descent parser (method u) which builds an syntax tree which is then flattened (y) and transformed (d) according to the distribution axioms. The output (j) simply joins the operands again.
Unfortunately I broke the functionality when I later tried to reduce the code by restructuring. Nevertheless I think there is still some chance for golfing.