Potential Project Over-Kill: I’m using ANTLR

I’ve known of ANTLR for years. I’ve never used it.

In fact, somehow I’ve managed to skate by all these years without building a proper language lexer or parser. I know, it’s almost shameful. I’ve done a lot more code-wise at work, and at home, than most of the people I’ve worked with will ever dream of doing, (difference between doing a job and doing a hobby you can get paid for), but since I managed to skip out on that whole four-years-of toil for a BS in CS, one thing I missed out on was language / compiler design.

Sure, I know how it all works. I understand the basics. I’d taught myself that long ago.

But today. Today I decided it was time to take the plunge and really learn ANTLR. Why? Well…

We have this file that an upstream vendor exports. We need to use this file as a descriptor of objects in our system to play nice with the upstream vendor’s product. This file is a flat-file, in a non-xml, non-csv, stupid format that doesn’t include line terminators, and is nothing like what you’d expect. Trust me, it’s nasty.

I’d written a tokenizing parser more than a year ago, that wasn’t as reliable or accurate as I wanted. Sure, it worked, but it was nasty recursive, wasn’t nearly clean, and relied way too much upon things just happening to work, rather than good, structured methods of parsing. Originally I thought this would be a one-time shot, so I did most of it programmatic, then cleaned up the resulting output by hand. Yick.

So today I sat down and now I’ve got an ANTLR generated lexer/parser. Nice. Then I figured out the rewrite rules and now it’s constructing a nice AST. Next up: Walking the tree, building up the object model I need. This will let us include the exported upstream vendor file as a resource that gets parsed on app startup, which means zero code maintenance to handle updates to the file. Yay!

This tool, ANTLR, rocks.

 

Of course, this is the way I wanted to do all this last year, but I was too busy trying to get a functioning application written in less than half the time I estimated it would take me. The whole project suffered from the incredibly compressed timeline, but one year out from that disaster, we’re finally starting to be able to address some of the shortcomings. Finally.

Leave a Reply

You must be logged in to post a comment.