Life Of Navin

Random Musings, Random Bullshit.

kc

An(n)atomy Of College Priorities!

You're either with me or you (probably) support an alternative version of the LokPal bill which automatically makes you an immoral anarchist governmental pig hell bent upon destroying the integrity of this great country. Also, you most probably can't get laid in a women's prison with a handful of pardons.Humko azadi aadhi raat ko mili thi aur savera aaj tak nahi hua.. -Shabana Azmi (on Twitter)

LEX and YACC on Windows

We've just started compiler design as part of the Language Translators subject at college and a practical aspect of it involves using LEX and YACC for Grammar Lexical Analysis and Parser Generation. Since LEX and YACC are both originally developed for the *nix OSes (read: Linux), there was a sudden rush of classmates downloading linux distros (Fedora being the default choice as it's used on lab systems). In many cases, people actually downloaded entire distro DVDs just for LEX/YACC!

NOTE: When people say they use LEX and YACC, almost always, what they mean is FLEX (Which is an alternative to Lex) and BISON (which is part of the GNU project) /BYACC respectively.

Sticking with the "When in doubt, try another hole" philosophy, I just googled around a little and came up with a much easier solution.


Here's how to set up FLEX and BISON on Windows systems

1) Download the Windows version of FLEX
Link2) Download the Windows version of BISON
3) Download a C/C++ Compiler (If you say TurboC, I'll reach out through the monitor and thump your head)... I recommend DevCPP or Code::Blocks
4) Install all 3. It's recommended to install in folders WITHOUT spaces in their names. I use 'C:\GNUWin32' for FLEX and BISON and 'C:\CodeBlocks' for Code::Blocks
5) Now add the BIN folders of both folders into your PATH variable. Incase you don't know how to go about this, see how to do it on Windows XP ,Windows Vista and Windows 7. You will have to add ';C:\GNUWin32\bin;C:\CodeBlocks\bin' to the end of your PATH
6) Open up a CMD prompt and type in the following

C:\flex --version
flex version 2.5.4

C:\>bison --version
bison (GNU Bison) 2.4.1
Written by Robert Corbett and Richard Stallman.

Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

C:\>gcc --version
gcc (GCC) 3.4.2 (mingw-special)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

If you do not get the output as above, restart your system and try again.

That's it! You now have a Bison and Flex set up! Now for some coding! :)

Create a Lex file, let's say "exp.l" and open it up in your favorite text editor (read: Notepad++)


%{
#include
#include "y.tab.h"
%}

%%
[0-9]*[02468]"\n" { return EVEN; }
[0-9]*[13579]"\n" { return ODD; }
%%
int main(void)
{
yyparse();
return 0;
}

int yywrap(void)
{
return 0;
}

int yyerror(void)
{
printf("Error\n");
exit(1);
}


Similarly create a YACC file as well, say "exp.y" and edit it as follows


%token EVEN ODD

%%

input:
EVEN { printf("EVEN NUMBER\n"); return 0; }
|ODD { printf("ODD NUMBER\n"); return 0; }
;


Now in the CMD prompt, navigate to the folder where the exp.l and exp.y files are and run the following commands:

C:\LT\example>flex exp.l

C:\LT\example>bison -dy exp.y

C:\LT\example>gcc lex.yy.c y.tab.c


At each step, assuming all is well, you won't get any output. Note that the final step compiles the C files generated by FLEX and BISON. And generates a executable named a.exe (To use a different name, change last command to "gcc lex.yy.c y.tab.c -o nameofexecutable.exe" )

C:\LT\example>a.exe
666
EVEN NUMBER
C:\LT\example>a.exe
667
ODD NUMBER


Now just run the program (through the command prompt itself). For and even number input, it prints out "EVEN NUMBER" and exits and "ODD NUMBER" is printed for odd numbers. A simple example, but I guess it gets the point through!

Enjoy! :)

PS The GnuWin project is a superawesome project which "provides ports of tools with a GNU or similar open source license, to Windows". The number of packages that they've ported to Windows is amazing. Do check it out.



PPS Yes, I'm a FOSS/Linux enthusiast, but that doesn't mean I have to praise Linux all day long! Projects like GNUWin, which take code to the masses, are nothing short of brilliant! Linux distributions have so many advantages over Windows for any real developer, but I honestly believe every developer has the right to "choose" his/her development OS and environment, be it PC, Linux or Mac. Do try out Linux distros as well (infact, I'd highly recommend you try a few before deciding which suits you best! Most distros are free!), but don't get dragged down by an environment! An OS is simply a tool to enable you, the developer, to work your magic! :)

Update: Here's a Zip file of FLEX, BISON and the Source Code used in this post.

Twitterverse

Prologue

Finally after all these years, here's to the beginning of what was there, what is there and hopefully what will remain!! So here are my thoughts & words -Online!!