You are on page 1of 2

1.

keywords____________________________________________________________

i-->
%{
#include <stdio.h>
int keywordCount = 0;
%}

%%
auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|
goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|
union|unsigned|void|volatile|while {
printf("Keyword: %s\n", yytext);
keywordCount++;
}
[a-zA-Z][a-zA-Z0-9]* { /* Ignore identifiers */ }
.|\n { /* Ignore other characters */ }
%%

int main() {
char fileName[256];
printf("Enter the name of the C code file: ");
scanf("%255s", fileName);

FILE *file = fopen(fileName, "r");


if (file == NULL) {
perror("Error opening file");
return 1;
}

yyin = file; // Set input file for Lex

yylex();

printf("\nTotal number of keywords: %d\n", keywordCount);

fclose(file);
return 0;
}

ii-->
#include<stdio.h>
void main(){
//my single line comment
printf("my prog");
for(i=0;i<n;i++)
printf("sam.file");
return 0;
/*this ismulti line comment*/
}

output____________________
$ vi identifier.l
$ vi sam.c
$ lex identifier.l
$ cc lex.yy.c -o identifier -ll
$ ./identifier
Enter the name of the C code file: sam.c
Keyword: void
Keyword: for
Keyword: return

Total number of keywords: 3


$

You might also like