Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it practical to use a parser generator?

I'm writing a simple text-template language for a web application I'm writing (think google's ctemplate). When finished, it'll feature only a small number of possible actions, simple stuff like "evaluate and execute", "evaluate and print", "evaluate and escape html", "comment". I was thinking of hand writing the whole parser from scratch, but I started looking at parser generators like lex, flex and antlr. These seem like way more than I need for my simple syntax. So the question is, at what point is it practical to use a parser generator?

like image 428
lazyconfabulator Avatar asked Aug 04 '10 00:08

lazyconfabulator


1 Answers

Sooner rather than later. If you have a simple syntax now, using a parser generator is easy. It makes it easier still when you want to add variables and loops and conditionals.

But wait! - There is little reason to invent your own language unless it is very domain specific like eqn or TeX or molecular modeling languages. You are far better off embedding a language that was specifically designed for the purpose. Tcl is the old guard in that realm, with Python being a strong contender. Perl was also designed to be an embedded scripting language but I think it a poor candidate as it will likely yield very "write-only" code in the hands of your users.

Language design is hard and smoking all the fiddly bits out is harder still. With both Python and Tcl you can decide how much of the core language to expose to your users and open up closed bits as you find a need for them.

The first little language that I wrote (which astonishingly is still in production use) would have been so much better had Tcl been there to use instead.

like image 63
msw Avatar answered Oct 01 '22 00:10

msw