Sunday, 22 October 2017

Token Generator in Lex for C Program

This is a simple token generation program written using lex.



Code


%{
%}


letter [a-zA-z]
digit [0-9]

%%
\n yylineno++;
#include<{letter}+(\.{letter}*)?> printf("%d\t%s\tPREPROCESSOR\n",yylineno,yytext);
return|main|for|while|case|switch|if|else printf("%d\t%s\tKEYWORD\n",yylineno,yytext);
int|float|char|struct printf("%d\t%s\tDATATYPE\n",yylineno,yytext);
\( printf("%d\t%s\tBRACKET OPEN\n",yylineno,yytext);
\) printf("%d\t%s\tBRACKET CLOSE\n",yylineno,yytext);
{letter}({letter}|{digit})* printf("%d\t%s\tIDENTIFIER\n",yylineno,yytext);
{digit}+ printf("%d\t%s\tINTEGER\n",yylineno,yytext);
\{ printf("%d\t%s\tBRACE OPEN\n",yylineno,yytext);
\} printf("%d\t%s\tBRACE CLOSE\n",yylineno,yytext);
{digit}+"E"(\+|\-)?{digit}+ printf("%d\t%s\tREAL NUMBER\n",yylineno,yytext);
{digit}+\.{digit}+(E(\+|\-)?{digit}+)? printf("%d\t%s\tFLOATING NUMBER\n",yylineno,yytext);
= printf("%d\t%s\tASSIGNMENT\n",yylineno,yytext);
; printf("%d\t%s\tTERMINATOR\n",yylineno,yytext);
\t ;
" " ;
\"({letter}|{digit}|" ")*\" printf("%d\t%s\tSTRING CONSTANT\n",yylineno,yytext);




%%


int yywrap(){
    return 1;
}

int main(int argc, char * argv[]){
    yyin=fopen(argv[1],"r");
    printf("LINE\tLEXME\tTOKEN\n");
    yylex();
    fclose(yyin);
    return 0;
}



Input File


#include<stdio.h>
int main(){
int a =0;
printf("Hello World");
return 0;
}

Output

[duke@duke-pc a3]$ lex lex.lex
[duke@duke-pc a3]$ gcc lex.yy.c
[duke@duke-pc a3]$ ./a.out file.c
LINE LEXME TOKEN
1 #include<stdio.h> PREPROCESSOR
2 int DATATYPE
2 main KEYWORD
2 ( BRACKET OPEN
2 ) BRACKET CLOSE
2 { BRACE OPEN
3 int DATATYPE
3 a IDENTIFIER
3 = ASSIGNMENT
3 0 INTEGER
3 ; TERMINATOR
4 printf IDENTIFIER
4 ( BRACKET OPEN
4 "Hello World" STRING CONSTANT
4 ) BRACKET CLOSE
4 ; TERMINATOR
5 return KEYWORD
5 0 INTEGER
5 ; TERMINATOR
6 } BRACE CLOSE
[duke@duke-pc a3]$

1 comment:

  1. I did this program but it's showing some error like yylineno undeclared

    ReplyDelete