Showing posts with label lex. Show all posts
Showing posts with label lex. Show all posts

Sunday, 26 May 2013

A flex and bison program for C like variable declaration...

This time we are trying to implement the C like variable declaration in flex-bison:-

Grammar:
S->S F | epsilon
F->E '\n'
E->K V
K->KEY
V->COMMALST | ID ';'
COMMALST->ID ',' CLST
CLST->ID ',' CLST | ID ';'

Here is the flex specification file: saved as identi.l

%{
//to identify the keyword and identifier
#include<stdlib.h>
#include"identi.tab.h"
%}
%%
"int" |
"char" |
"float" |
"long" |
"double" |
"signed" {return KEY;}
[A-Za-z_]+[0-9A-Za-z_]* {return ID;}
[,;\n] {return *yytext;}
. ;
%%

Here is the bison specification file: saved as identi.y
%{
#include<stdio.h>
void yyerror(char*,...);
int yywrap(void);
%}
%token KEY ID;
%%
S:S F
 |
 ;
F:E '\n'
 ;
E:K V {printf("\nValid Declaration\n");}
 ;
K:KEY
 ;
V:COMMALST
 |ID ';'
 ;
COMMALST:ID ',' CLST
 ;
CLST:ID ',' CLST
 |ID ';'
 ;
%%
int main(void)
{
 yyparse();
 return 0;
}
int yywrap(void)
{
 return 1;
}
void yyerror(char* msg, ...)
{
 printf("\nError Occurred\n");
}

Now all you have to do is run the following commands:


flex identi.l

bison -d identi.y

gcc -o identi lex.yy.c identi.tab.c


Now you have to run the program on your linux OS as:

./identi

INPUT EXAMPLES:

int a;

float ad3,t4,e3;


Monday, 13 May 2013

Second package of flex and bison usage... another grammar implementation...

This is my second attempt at using flex and bison... you might find it useful...

Grammar:
E->E+E
E->(E)
E->T
T->id


Here is the flex specification file: saved as clexer2.l

%{
//family cparser2.y - clang2
#include<stdlib.h>
#include"cparser2.tab.h"
int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext);return id;}
[+()] return *yytext;
\n |
. ;
%%


Here is the bison specification file: saved as cparser2.y

%{
//family clexer2.l clang2
#include<stdio.h>
int yywrap(void);
void yyerror(char*);
%}
%left '+'
%left '('
%left ')'
%token id;
%%
S:S E {$$=$2;}
|
;
E:E'+'E {$$=$1+$3; printf("Ans: %d\n",$$);}
|'('E')' {$$=$2;}
|T {$$=$1;}
;
T:id {$$=$1;}
;
%%
int main(void)
{
yyparse();
return 0;
}
int yywrap(void)
{
return 1;
}
void yyerror(char* msg)
{
printf("%s",msg);
}


Now all you have to do is run the following commands:



flex clexer2.l

bison -d cparser2.y

gcc -o clang2 lex.yy.c cparser2.tab.c


Now you have to run the program on your linux OS as:

./clang2

Flex Bison Beginning.... baby steps....

I have been trying to learn lex and yacc of late. Well the tools that I actually use are flex and bison.
Anyways, here is a small program to demonstrate the use of both in conjunction...

Grammar:
E->E+T
E->T
T->id


Here is the flex specification file: saved as clexer1.l

%{
//This lexer is for cparser1.y together they make clang1
#include<stdio.h>
#include"cparser1.tab.h"
int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext);return id;}
[+] return *yytext;
\n |
. ;
%%


Here is the bison specification file: saved as cparser1.y

%{
//this is the parser for the grammar E->E+T; E->T; T->id used in conjunction with clexer1.l
#include<stdio.h>
#include<stdlib.h>
void yyerror(char*);
int yywrap(void);
%}
%token id
%%
S:S E {$$=$2;}      //merely to augment the grammar
|       //represents epsilon
;
E:E'+'T {$$=$1+$3; printf("Ans: %d\n",$$);}
|T {$$=$1;}
;
T:id {$$=$1;}
;
%%
int main(void)
{
yyparse();
return 0;
}
void yyerror(char *msg)
{
printf("%s\n",msg);
}
int yywrap(void)
{
return 1;
}


Now all you have to do is run the following commands:



flex clexer1.l

bison -d cparser1.y

gcc -o clang1 lex.yy.c cparser1.tab.c


Now you have to run the program on your linux OS as:

./clang1

Sunday, 7 April 2013

Compiler Design Tools installation in windows....

To install compiler design tools like flex, bison and gcc in your Windows platform follow the following steps:

1. Install Flex:


a.  After you've downloaded the setup from the link above double click to     install.

b. Make sure you DO NOT install in the default folder "C:\Program Files\...." but, to some some other folder which doesn't have a space in the name: "C:\compiler\".

2. Install Bison:


a.  After you've downloaded the setup from the link above double click to install.

b. Make sure you DO NOT install in the default folder "C:\Program Files\...." but, to some some other folder which doesn't have a space in the name: "C:\compiler\".

3. Install gcc setup:


a.  After you've downloaded the setup from the link above double click to install.

b. Make sure you DO NOT install in the default folder "C:\Program Files\...." but, to some some other folder which doesn't have a space in the name: "C:\compiler\".


or you may download the entire setup package from here: Compiler Tools


4. How to set the PATH variable in Windows: 

a.  First obtain the path for each of the tool's bin directory as follows  

for XP: (indicative)



for 7: (again indicative)



do the above for each tool separately save them in a notepad file.

b.  Now open the properties of your computer, by right clicking the icon:

for XP:




for Win 7:






c.  Now add the paths you previously obtained in the PATH variable separated by semicolons ";" and then logout and login again.



This should make your tools usable as they did in my case. Now you may work from any directory and create your programs.

In case of query or problem feel free to comment... I will definitely respond...

Enjoy...!!!