lexical analyser i have text file input txt with some text and number i want lex fil 5188458
lexical analyser :
i have text file (input.txt) with some text and number
i want lex file read text file and print if read letter print (letter) and line number , if read num prnt it and line number ,identefier the same
this is my code :
%{
#include “y.tab.h”
#include
void yyerror(char *);
%}
DIGIT [0-9]
ID [a-z][a-z0-9]*
%%
%%
“;” printf(“Semicolonn”);
“:” printf(“Colonn”);
“,” printf(“Comman”);
“+”|”-“|”*”|”/” printf( “Arithmetic operator: %sn”, yytext );
“:=” printf(“Assign Operatorn”);
[0-9] printf(“digitn”);
[0-9.] printf(“Floatn”);
“(“|”)” printf(“Parenthesesn”);
%%
“>=” return GE;
“
“==” return EQ;
“!=” return NE;
letter [a-z|A-Z]
{DIGIT}+ {
printf( “An integer: %s (%d)n”, yytext,
atoi( yytext ) );
}
{DIGIT}+”.”{DIGIT}* {
printf( “A float: %s (%g)n”, yytext,
atof( yytext ) );
}
{ID} printf( “An identifier: %sn”, yytext );
“{“[^}n]*”}” /* eat up one-line comments */
[ t] ; /* skip whitespace */
. yyerror(“Unknown character”);
%%
FILE *yyin;
%%
%%
void main(int argc, char **argv) {
/* Process command line args*/
yyin = fopen(“input.txt”, “r”);
yyparse();
fclose(yyin);
return 0;
}