ported most of GrammarMain to python. have to finish the solver