This time we are trying to implement the C like variable declaration in flex-bison:-
Here is the flex specification file: saved as identi.l
Here is the bison specification file: saved as identi.y
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;
Grammar:
S->S F | epsilon
F->E '\n'
E->K V
K->KEY
V->COMMALST | ID ';'
COMMALST->ID ',' CLST
CLST->ID ',' CLST | ID ';'
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;
Please drop a comment if you find any error...
ReplyDelete