Sunday, 16 June 2019

Write a LEX program to collect tokens (numbers) and sort.




Code:

%{
#include<stdio.h>
int t, a[20],i,j,n,num;
%}
%%
[0-9]+ {num=0;
for(i=0;i<yyleng;i++)
num=num*10+(yytext[i]-'0');
a[n++]=num;
}
%%
main()
{
yyin=fopen("file.c","r");
yylex();
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
int yywrap()
{
return 1;
}

No comments:

Post a Comment

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...