How To Install Flex And Bison

 

Step 1: Go to http://gnuwin32.sourceforge.net/packages/flex.htm this website and download flex 2.5.4a-1.


Step 2: Go to http://gnuwin32.sourceforge.net/packages/bison.htm this website and download bison 2.4.1.



Step 3: After this install both of this exe files. one things you note for installation chose those folder or directory of your preference without spaces in the name. i suggest install on c drive directly like c:\Gnuwin32.

Do not install it in the default (C:\Program Files (x86)\GnuWin32) because bison has problems with spaces in directory names, not to say parenthesis.

Step 4: Also download Dev-Cpp for gcc compilation. in this same rule is apply for installation. i mean install on those folder which folder name not contain any white space. i recommended path is c:\Dev-cpp.

Go to https://sourceforge.net/projects/orwelldevcpp/ this link and download dev-cpp.


Step 5: After this installation set Environment Variables for dev-cpp if you follow my path for installation then your Environment Variable path for dev-cpp is c:\dev-cpp\bin and for flex and bison c:\Gnuwin32\bin.set this two path to your environment variable.


Step 6: Open a command prompt, cd to the directory/folder where your ".l" and ".y" are save.


if you don't know how to open folder/directory on cmd(Command Prompt) then read this

How to run lex file ?

First open folder in cmd. like in D drive you create one folder lexprogram in this folder save this lex file with .l extension. then in cmd you type d: + enter then type cd D:\lexprogram.


Now we compile file by following steps.

  1. flex filename.l
  2. bison -dy filename.y
  3. gcc lex.yy.c y.tab.c
  4. a.exe



You will be able to run your program.

i give you one sample file for checking flex and bison tool.

Hello.l

%{

#include "y.tab.h"
int yyerror(char *errormsg);

%}

%%

("hi"|"oi")"\n"       { return HI;  }
("tchau"|"bye")"\n"   { return BYE; }
.                     { yyerror("Unknown char");  }

%%

int main(void)
{
   yyparse();
   return 0;
}

int yywrap(void)
{
   return 0;
}

int yyerror(char *errormsg)
{
    fprintf(stderr, "%s\n", errormsg);
    exit(1);
}


Hello.y


%{

#include <stdio.h>
#include <stdlib.h>
int yylex(void);
int yyerror(const char *s);

%}

%token HI BYE

%%

program: 
         hi bye
        ;

hi:     
        HI     { printf("Hello World\n");   }
        ;
bye:    
        BYE    { printf("Bye World\n"); exit(0); }
         ;


I hope all guys are able to run your flex and yacc program.

if any error or Do not understand anything then comment it.

Comments

Popular posts from this blog

Write A Program To Print Hello World Using Lex

Create a Student registration form using following tags form,input,textarea,button,select,optio. The registration form must consist of following information: first name,Middle name,last name, gender(use radio button),address,phone no,email id,hobbies(use checkbox),city,state,country,collage name (use dropdown menu).