Lex programm to recgnize static words from english

, by Prashant Gunjal

%{
#include<stdio.h>
#include<string.h>

struct holder
{
char word[10];
char type[15];
}st[20];

int count=0,i,flag=0;
char type[15];
%}
newln [\n]+
noune [a-zA-Z]+

%%
{newln} {flag=0;}
^verb { flag=1;
strcpy(type,"verb");
}
^adjective {
flag=1;
strcpy(type,"adjective");
}
^preposition {
flag=1;
strcpy(type,"preposition");
}
^adverb {
flag=1;
strcpy(type,"adverb");
}
{noune} {
if(flag)
{
for(i=0;i<count;i++)
if(strcmp(st[i].word,yytext)==0)
break;
if(i==count)
{
strcpy(st[count].type,type);
strcpy(st[count].word,yytext);
count++;
}
}
else
{
for(i=0;i<count;i++)
if(strcmp(st[i].word,yytext)==0)
break;
if(i==count)
{
fprintf(yyout,"\n%s\t\tNOUNE",yytext);
}
else
fprintf(yyout,"\n%s\t\t%s",st[i].word,st[i].type);
}
}
. {;}
%%

int main()
{
yyout=fopen("out2.txt","w");
yylex();
fclose(yyout);
}


0 comments: