Vous êtes sur la page 1sur 3

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

FILE *fp;

int isSymbol( char x )


{
char *y = "?:;[](){}*/,-+%=<>&|!^~#.";
int l = strlen( y );
int flag = 0;
for( int i = 0; i < l; i++ )
if( x == y[i] )
flag = i + 1;

char c = fgetc( fp );
if( c == EOF )
{
if( flag )
printf( "<%c> ", x );
return 1;
}
char b [3];
b[0] = x;
b[1] = c;
b[2] = '\0';
char * r[] = {"==",">=","<=","!=","&&","||","++","--","+=","-
=","*=","/=","%=","<<",">>"};
l = sizeof( r ) / sizeof( r[0] );
for( int i = 0; i < l; i++ )
if( !strcmp( r[i], b ) )
{
printf( "<%s> ", b );
return 1;
}
fseek( fp, -1L, SEEK_CUR );
if( flag ){
printf( "<%c> ", y[flag-1] );
return 1;
}
return 0;
}
void main ()
{
int i, index = 0, q = 0, found = 0, place = 0, z, check = 0, l = 0,
in_chk = 0;
char fn[50], bfr[100], c, table[1000][1000];
char *y="?:;[](){}*/,-+%=<>&|!^~";
char
key[][20]={"if","else","for","int","double","float","short","long","void"
,"main","while","switch","case","do","include","printf","scanf","sizeof",
"return"};
printf( "Enter filename" );
scanf( "%s", fn );
fp = fopen( fn, "r" );
c = fgetc( fp );
while ( c != EOF )
{
if ( c == '\n' )
{
printf( "\n" );
c = fgetc(fp);
}

while ( isalnum( c ) )
{
if ( isdigit( c ) )
{
index = 0;
bfr[index++] = c;
c = fgetc( fp );
while ( isdigit( c ) )
{
bfr[index++] = c;
c = fgetc( fp );
}
bfr[index] = '\0';
printf( "<num,%s> ", bfr );
check = 1;
}
else
{
while ( isalnum( c ) )
{
bfr[index++] = c;
c = fgetc(fp);
}
}
}
bfr[index] = '\0';
for ( i = 0; i < 19; i++ )
{
if ( strcmp( bfr, key[i] ) == 0 )
{
printf( "<%s> ", key[i] );
check = 1;
break;
}
}

if( bfr[0] != '\0' && check == 0 )


{
for ( z = 0; z < q; z++ )
{
if ( strcmp( bfr, table[z] ) == 0 )
{
found = 1;
place = z;
break;
}
}
if ( found == 1 )
{
found = 0;
printf( "<id,%d> ", place + 1 );
}
else
{
strcpy( table[q++], bfr );
printf( "<id,%d> ", q );
}
}
index = 0;
check = 0;
if ( isSymbol( c ) );
c = fgetc( fp );
}
}