Lex program to search for a word in a file

Lex program to search a word in a file - This program will search a word in the file input.txt and display the message FOUND or NOT FOUND

*************************************

%{

#include<string.h>

void search(char *);

%}

%option noyywrap

/* Rule Section */

%%

[a-zA-Z]+ search(yytext);

%%

int main()

{

        // The function that starts the analysis

        yylex();

        return 0;

}

void search(char *str)

{/* fp as pointer

        of File type */

        FILE *fp;

        char temp[30];

        /* fp points to the file input.txt

        and opens it in read mode */

        fp=fopen("input.txt", "r");

        while((fscanf(fp, "%s", temp))!=EOF)

        {

                if(!(strcmp(temp, str)))

                {

                        printf("FOUND\n");

                        return;

                }

        }

        printf("NOT FOUND\n");

                return;

}

Execution
input.txt file
$ cat input.txt
binu
biju
biji
bini
aji
vidhu
avani
aditi
pooja
abhijith
abhi
padma
arpita
deepa

$flex searchwrd.lex
$ gcc lex.yy.c
$ ./a.out
manu
NOT FOUND

binu
FOUND

avani
FOUND

aji
FOUND

Comments

Popular posts from this blog

KTU Compiler Lab CSL411 - Dr Binu V P

lexical analyzer for a c program

13.Precedence and conflict resolution in yacc