May 2006

Archives

Thought I forgot about Lex?

Saw my previous post? Hey Yacc is only half the battle. So why don't I give it all away and post the lexical analyzer too? OK I will. But I hope you appreciate how difficult it was to post this code since many of the characters in here have meaning to html as well.

Compiling Yacc files using Yacc

Yet Another Compiler Compiler or yacc. From the way it was named it sounds like the latest tool in compiler building, right? But actually it is old and venerable. Please don't email to tell me about the much better tools out there. I refer you to the Wikipedia topic on compiler-compilers.

But when you are building a grammar visualization tool (as I am) one of the first thoughts to cross your mind is how to read the many pre-existing grammars already out there. There are literally dozens if not hundreds of yacc input files floating around. So I wanted to be able to read those files and generate my graphics based representation for editing with a mouse. Hmmm...generate, sounds sort of like compile. Yeah, make a compiler that accepts yacc files as input. So what tools do I have handy with which to build such a compiler? That's right, Yacc. The one thing about Yacc is that it, or it's close relative GNU Bison, is available on every computer out there. If you've got a Linux box or a Macintosh and a development system then you are ready to compile a compiler.

So how to write a grammar for Yacc?