Monday, December 26, 2011

Good Riddance to JFlex

"A man does good business when he rids himself of a turd."

Edward Longshanks reportedly spoke these words after his conquest of Scotland around the year 1305. The quotation sprang to mind when I decided to ditch JFlex in favor of writing my own lexer generator.

From the beginning, I wanted to use a push-driven lexer for Weapon M. But the few Java-based lexer generators out there seem to be exclusively pull-driven. I tried modifying the JFlex skeleton file, but I found the format too inflexible. I considered modifying JFlex itself, but found the code too difficult to understand. I reluctantly resigned myself to designing my helper around a pull-driven lexer.

But this proved endlessly frustrating. A few weeks ago, while attempting to mate my clicky SwingTerminal with a lexer that would make links out of sector numbers, I decided to take another look at Anders Møller's Automaton library.

Automaton is not a lexer generator, merely a DFA generator. But what a joy! JFlex and Automaton are like night and day. The Automaton code is easy to read, with a logical structure and sensibly-chosen identifier names. In a matter of hours, I understood it better than I understand JFlex. After a few days of experiments, I was able to make the simple modifications required to use a RunAutomaton as a lexer.

The next step will be to write a lexer generator that uses my modified Automaton library. My experience using Sun's CodeModel library to write KFlex should make this a snap. I've actually already written the generators for the support classes.

I'd hoped to have some kind of simple TradeWars terminal ready to show off by Christmas, but I'm glad I chose not to press ahead with JFlex. I only wish I'd made that decision sooner. My new goal is to have a prototype of Weapon M ready in time for Cruncher's Cabin Fever Tournament in February.

No comments:

Post a Comment