[go: up one dir, main page]

0% found this document useful (0 votes)
52 views2 pages

Flex

This document contains code for two Lex programs. The first program defines tokens for keywords, identifiers, and uses printf to output the token type and text when each is found. The second program counts the characters, words, and lines in a file by defining tokens for newlines, strings of non-whitespace characters, and single characters, and incrementing the corresponding counters for each token type found. It then prints the final counts.

Uploaded by

srichand lulla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views2 pages

Flex

This document contains code for two Lex programs. The first program defines tokens for keywords, identifiers, and uses printf to output the token type and text when each is found. The second program counts the characters, words, and lines in a file by defining tokens for newlines, strings of non-whitespace characters, and single characters, and incrementing the corresponding counters for each token type found. It then prints the final counts.

Uploaded by

srichand lulla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

test1.

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 :

You might also like