Modifying mathlexer to the specified algorithm in the book.