Friday, September 9

LEX Program to identify Keywords and convert it into uppercase


Guys, apply simple logic. Detect all keywords( This program deal with a small number of keywods for sake of simplicity).
On detecting any keyword in C, convert it into uppercasr letter using the funtion 'toupper' . Game over!!




%{#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;
}

OutputLet num.c contains following program fragment.

main()
{
int num;
scanf("%d",&num);
if(num%2)printf("Odd");
else printf("Even")
}


The output will be,

MAIN()
{
INT num;
SCANF("%d",&num);
IF(num%2)PRINTF("Odd");
ELSE PRINTF("Even")
}





4 comments :

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

error, undefined definion, keyword

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

in the first program what do we do to ignore the keyword given inside printf ? Eg : printf(" do u eat this");

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

hey there is error undefined definition keyword

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

for error , undefined definion, keyword
line "keyword main|int|scanf|printf|if|else" should be written in a new line.

Post a Comment