Flex
Flex
l :
Program :
%{
#include<stdio.h>
%}
letter [a-zA-Z]
digit [0-9]
%%
begin {printf("keyword : %s",yytext);}
if {printf("keyword : %s",yytext);}
{letter}({letter}|{digit})* {printf("id : %s",yytext);}
%%
int yywrap(void) {
return 1;
}
int main()
{
yylex();
return 0;
}
i/p data :
Output :
Wordcount.l :
Program :
%{
#include <stdlib.h>
#include <stdio.h>
int charCount=0;
int wordCount=0;
int lineCount=0;
%}
%%
\n {charCount++; lineCount++;}
[^ \t\n]+ {wordCount++; charCount+=yyleng;}
. {charCount++;}
%%
int yywrap(void) {
return 1;
}
int main(int argc, char** argv)
{
if (argc > 1)
{
FILE *file;
file = fopen(argv[1], "r");
if (!file)
{
fprintf(stderr, "Could not open %s\n", argv[1]);
exit(1);
}
yyin = file;
}
yylex();
printf("No: of Chararacter : %d \n", charCount);
printf("No: of Words : %d \n", wordCount);
printf("No: of Lines : %d \n", lineCount);
return 0;
}
output :