implement Earley parser and some code cleanup