Flex program to check use of yyrestart() function

Flex program to check use of the yyrestart() function

This flex program is to check use of yyrestart() function. The yyrestart() function use to restart the input file scanning.In the program below, we will take a file as an input, to open a file we will be taking a file* pointer and using command fopen(“filename.txt”,mode(read(r),write(w),read and write(r+)). We first type in input inside the command prompt and see that one of the rule is matched and then we see that the program is executed again by the use of yyrestart() and this time input is taken from the file.

Check the output below to get a clearer picture.

Input File

Write anything in notepad ( like Codedost).

Save your file as yyrestart_input.txt (or any other name but see to it the name is same as in the program)

Program

%{
/* Implementation of yyrestart() */
#undef yywrap
#define yywrap() 1

%}

%%

[a-zA-Z]+ {
	printf("\nmixed word = " );
	ECHO;
	return;
}

%%

main()
{
	yylex();
	printf("\nEnd of 1st yylex");
	FILE *fpt;
	fpt=fopen("input.txt","r+");
	yyrestart(fpt);
	yylex();
	printf("\nEnd of 2nd yylex"); 	
}

Output

Share Me!