Computer Science Canada Don't Like Any Programming Languages? Write Your Own! |
Author: | rdrake [ Tue Nov 14, 2006 12:31 pm ] | ||||||||||||
Post subject: | Don't Like Any Programming Languages? Write Your Own! | ||||||||||||
Too often we hear people complaining about the current languages out there. They whine the languages are too large, they're too small, they're too complicated, or even too simple. Not often do people actually step up and write their own compiler for a language that is entirely their own. There are three steps in writing your own little compiler:
The parser does something a little different, it takes the output from the scanner along with a series of grammatical rules on order to generate a tree of the program. The code generator does as its name implies, generates the code. We could write all of these tools from scratch, or use pre-existing ones to speed up the process. For these examples, I will be using the tools (f)lex, yacc (bison), and gcc. Flex is an open source implementation of lex, bison is a free software foundation version of yacc, and gcc is... well gcc. Lex/flex and yacc/bison can typically be used interchagably. Note: If you are on a *nix based system, these tools should already be installed. If not, use your favourite method to fetch them. If on Windows, you're going to have to google up locations of win32 binaries. Let's move into the interesting stuff now... The Lexer Let's make this first bit simple. Say we wanted to write a compiler that turns a virtual light switch on and off. We would just need a very simple lexer file, dubbed "switch.l".
You may be wondering why we care? Well, this will be addressed in the next section. Yacc The example above is all find and dandy for controlling just one light switch, but we want to control many. Let's create a lexer file which allows for slightly more complicated statements.
Here's where the magic comes in, our switch.y file.
You see, all the scanner does is scan the text for patterns. It then reports these findings to the parser. The magic comes in when the parser takes the results from the scanner and acts accordingly. Enough of this, let's compile the above example.
Well, I think that's just about it for now. We've built a nice, simple language for turing our light switches on and off. Our work here is done. |
Author: | Tony [ Tue Nov 14, 2006 1:42 pm ] |
Post subject: | Re: Don't Like Any Programming Languages? Write Your Own! |
rdrake wrote: We've built a nice, simple language for turing our light switches on and off.
Excellent. This is pretty much the approach that will be taken towards the openTuring project. |