a LEX program to identify keywords and convert it into uppercase

6.5k views Asked by At

Please help me to answer this question. I want to write a flex program(keyword.l) to identify keywords and convert it into uppercase. I get with this code but there is an error when I run flex by cmd (C:\GnuWin32\bin flex keyword.l) error is:

"can't open keyword.l"

%{#include<stdio.h>
int i;
%}keyword main|int|scanf|printf|if|else
%%

{keyword} {
for(i=0;i<yyleng;i++)
printf("%c",toupper(yytext[i]));
}
%%

main()
{
yyin=fopen("num.c","r");
yylex();
}

int yywrap()
{
return 1;
}
1

There are 1 answers

0
Arijeet Bhakat On

Your program is perfectly fine.

What might have gone wrong with it is that while saving it you wrote it as keyword.l. However, it would have saved as keyword.l.text so when you are calling with the command flex keyword.l it says it can't open. You shoud rather call for "flex keyword.l.txt" or change the extension to just "flex keyword.l". Then it should work.