Refactored lexer and rule generator