Sunday, 16 June 2019

Write a LEX program that reads input file and writes valid tokens to output file.


Code:
%{
%}
%%
#.*> {fprintf(yyout,"Valid Header File: %s\n",yytext);}
(printf|scanf|if|else|while|do|break|continue|int|double|float|return|char|case|char|sizeof|
long|short|typedef|switch|unsigned|void|static|struct|goto) {fprintf(yyout,"Valid
Keyword: %s\n",yytext);}
(\+|\-|\*|\/|\=|\%|\>|\<) {fprintf(yyout,"Valid Operator: %s\n",yytext);}
[0-9]+\.?[0-9]+ { fprintf(yyout,"Valid Constant: %s\n",yytext);}
[a-zA-Z]+[0-9]* { fprintf(yyout,"Valid Identifier: %s\n",yytext);}
[\"].*[\"] { fprintf(yyout,"Valid Literal: %s\n",yytext);}
(\(|\)|\{|\}|\;|\,|\&) { fprintf(yyout,"Valid Special Symbols: %s\n",yytext);}
%%
main()
{
yyin=fopen("input.c","r");
yyout=fopen("output.txt","w");
yylex();
}
int yywrap()
{
return 1;

No comments:

Post a Comment

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...