fork download
  1. #define F for
  2. #define R return
  3. #define E if(i/M||j/M)R-1;
  4. #define A(X)isalpha(o[X+x])
  5. enum{O=40,C,M=3999};char*T,Q[M],D[M],t[M],Z,v,*o=Q,*d=D;m,n,s,c,w,x,y;K(i,j,k){!Z?Z=t[O]=1,t[C]=-1:0;E;if(!o[i])R d[j]=0;if((c=t[o[i]]+t[o[i+1]])-2||o[i+2]-92){d[j++]=o[i++];R K(i,j,i);}F(i+=2,y=w=0;i<M&&o[i]&&c;y=o[i++]-46||y?y:i+1)c+=t[o[i]],w=w||c-1?w:i;E;if(c){F(;d[j++]=o[i++];)E;R 0;}F(c=y;c<w;++c)if(o[c]==92)F(n=0,m=w+2;m<i;++m){if(o[m]==o[c+2]){F(x=0;o[m+x]&&A(m)&&o[m+x]==o[c+2+x];++x);if(o[c+2+x]-46||A(m))continue;if(v>90)R-1;F(n=c+2;n<w;++n)if(o[n]==o[m]){F(x=0;o[m+x]&&A(m)&&o[m+x]==o[n+x];++x);memset(o+n,v,x*(o[m+x]==46&&!A(n)));}++v;}}F(c=y;c<w&&j<M;++c){F(x=0;o[c+x]&&o[c+x]==o[k+4+x]&&A(c);++x);if(o[k+4+x]==46&&!A(c)){F(m=w+2;m<i-1&&j<M;)d[j++]=o[m++];c+=x-1;}else d[j++]=o[c];}E;Z=2;R K(i,j,i);}*L(char*a){F(s=n=0;n<M?o[n]=a[n]:0;++n);if(n==M)R 0;F(v=65;++s<M;){n=K(0,0,Z=0);if(Z==2&&~n)T=d,d=o,o=T;else break;}R~n&&s<M?d:0;}
  6.  
  7. #define P printf
  8. main()
  9. {char*r[]={ "((\\ abc. (\\ b. (abc (abc (abc b))))) (\\ cc. (\\ dd. (cc (cc dd)))))",
  10. "((\\ fa. (\\ abc. (fa abc))) (\\ yy. (\\ abc. yy)))",
  11. "((\\ x. x) z)",
  12. "((\\ x. x) (\\ y. (\\ z. z)))",
  13. "(\\ x. ((\\ y. y) x))",
  14. "((\\ x. (\\ y. x)) (\\ a. a))",
  15. "(((\\ x. (\\ y. x)) (\\ a. a)) (\\ b. b))",
  16. "((\\ x. (\\ y. y)) (\\ a. a))",
  17. "(((\\ x. (\\ y. y)) (\\ a. a)) (\\ b. b))",
  18. "((\\ x. (x x)) (\\ x. (x x)))",
  19. "(((\\ x. (\\ y. x)) (\\ a. a)) ((\\ x. (x x)) (\\ x. (x x))))",
  20. 0}, *p;
  21. int w;
  22. for(w=0;r[w];++w){
  23. p=L(r[w]);
  24. P("o=%s d=%s\n",r[w],p==0?"Error ":p);
  25. }
  26. R 0;
  27. }
Success #stdin #stdout 0s 9440KB
stdin
Standard input is empty
stdout
o=((\ abc. (\ b. (abc (abc (abc b))))) (\ cc. (\ dd. (cc (cc dd))))) d=(\ b. (\ AA. (b (b (b (b (b (b (b (b AA))))))))))
o=((\ fa. (\ abc. (fa abc))) (\ yy. (\ abc. yy))) d=(\ AAA. (\ abc. AAA))
o=((\ x. x) z) d=z
o=((\ x. x) (\ y. (\ z. z))) d=(\ y. (\ z. z))
o=(\ x. ((\ y. y) x)) d=(\ x. x)
o=((\ x. (\ y. x)) (\ a. a)) d=(\ y. (\ a. a))
o=(((\ x. (\ y. x)) (\ a. a)) (\ b. b)) d=(\ a. a)
o=((\ x. (\ y. y)) (\ a. a)) d=(\ y. y)
o=(((\ x. (\ y. y)) (\ a. a)) (\ b. b)) d=(\ b. b)
o=((\ x. (x x)) (\ x. (x x))) d=Error 
o=(((\ x. (\ y. x)) (\ a. a)) ((\ x. (x x)) (\ x. (x x)))) d=(\ a. a)