Last active
December 16, 2015 20:18
-
-
Save vintesh/5490948 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<conio.h> | |
void main() | |
{ | |
FILE *in,*out; | |
char c; | |
short flag=0,i=0; | |
const char *outPath="C:\\tc\\bin\\out.txt"; | |
clrscr(); | |
in = fopen("C:\\tc\\bin\\src.txt","r"); | |
if(in==NULL) | |
{ | |
printf("Can't Open the src file"); | |
exit(0); | |
} | |
out = fopen(outPath,"w"); | |
while(!feof(in)) | |
{ | |
c=fgetc(in); | |
if(c==' ' || c=='\n') | |
{ | |
if(flag==0) | |
{ | |
if (i!=0) | |
fputc('\n',out); | |
flag=1; | |
} | |
} | |
else if(c==',' || c=='+' || c=='-' || c=='*' || c=='(' || c==')' | |
|| c=='[' || c==']' || c=='{' || c=='}'|| c==';' || c=='=' | |
|| c=='"' || c=='\''|| c=='/' || c=='^' || c=='%' || c=='!' | |
|| c=='|' || c=='#' || c=='<' || c=='>') | |
{ | |
if(flag==0) | |
{ | |
if(i!=0) | |
fputc('\n',out); // flag=1; | |
} | |
fputc(c,out); | |
fputc('\n',out); | |
flag=1; | |
} | |
else{ | |
fputc(c,out); | |
flag=0; | |
} | |
i++; | |
} | |
fclose(in); | |
fclose(out); | |
printf("Output file generated--at loc:\n\r"); | |
puts(outPath); | |
getch(); | |
} | |
//TOKEN GENERATION | |
#include<stdio.h> | |
#include<conio.h> | |
short isOp(char st[]); | |
short isDelim(char st[]); | |
short isIden(char st[]); | |
short isKeywrd(char st[]); | |
short isPrepDire(char st[]); | |
short isInteger(char st[]); | |
int main() | |
{ | |
FILE *in,*out; | |
char str[63]; | |
short i; | |
clrscr(); | |
in = fopen("C:\\tc\\bin\\out.txt","r"); | |
if(in==NULL) | |
{ | |
printf("Can't Open the file"); | |
} | |
out = fopen("C:\\tc\\bin\\outTok.txt","w"); | |
while(!feof(in)) | |
{ | |
fgets(str,63,in); | |
if(isOp(str)) | |
{ | |
fputc(str[0],out); | |
fprintf(out," | Operator"); | |
fputc('\n',out); | |
} | |
else if(isDelim(str)) | |
{ | |
fputc(str[0],out); | |
fprintf(out," | Delimiter"); | |
fputc('\n',out); | |
} | |
else if (isKeywrd(str)) | |
{ | |
for(i=0;str[i]!='\n';i++); | |
str[i]='\0'; | |
fputs(str,out); | |
fprintf(out," | Keyword"); | |
fputc('\n',out); | |
} | |
else if(isPrepDire(str)) | |
{ | |
for(i=0;str[i]!='\n';i++); | |
str[i]='\0'; | |
fputs(str,out); | |
fprintf(out," | Preprocessor Directive"); | |
fputc('\n',out); | |
} | |
else if(isIden(str)) | |
{ | |
for(i=0;str[i]!='\n';i++); | |
str[i]='\0'; | |
fputs(str,out); | |
fprintf(out," | Identifier"); | |
fputc('\n',out); | |
} | |
else if(isInteger(str)) | |
{ | |
for(i=0;str[i]!='\n';i++); | |
str[i]='\0'; | |
fputs(str,out); | |
fprintf(out," | Constant Number"); | |
fputc('\n',out); | |
} | |
else if(str[0]=='#') | |
{ | |
for(i=0;str[i]!='\n';i++); | |
str[i]='\0'; | |
fputs(str,out); | |
fprintf(out," | Hash directive"); | |
fputc('\n',out); | |
} | |
else // if Invalid | |
{ | |
for(i=0;str[i]!='\n';i++); | |
str[i]='\0'; | |
fprintf(out,str); | |
fprintf(out," | Invalid"); | |
fputc('\n',out); | |
} | |
} | |
fclose(in); | |
fclose(out); | |
printf("Output file generated--\n\r"); | |
getch(); | |
return 0; | |
} | |
short isOp(char st[]) | |
{ | |
if((st[1]=='\n') && (st[0]=='+' || st[0]=='-' || st[0]=='/' || st[0]=='*' || st[0]=='%' || st[0]=='^' || st[0]=='=')) | |
return 1; | |
else | |
return 0; | |
} | |
short isInteger(char st[]) | |
{ | |
int i; | |
for(i=0;st[i]!='\n';i++) | |
{ | |
if(isdigit(st[i])) | |
{ | |
return 1; | |
} | |
} | |
return 0; | |
} | |
short isDelim(char st[]) | |
{ | |
if(st[0]==';' || st[0]==',' || st[0]=='(' || st[0]==')' || st[0]=='[' || st[0]==']' || st[0]=='{' || st[0]=='}' || st[0]=='{' || st[0]=='}' || st[0]=='<' || st[0]=='>' || st[0]=='"' || st[0]=='\'') | |
return 1; | |
else | |
return 0; | |
} | |
short isIden(char st[]) | |
{ | |
int i=0,flag=0; | |
if(st[0]=='_' || (st[0]>='A' && st[0]<='Z') || (st[0]>='a' && st[0]<='z')) // Checks for the 1st char | |
{ | |
if(st[1]=='\n') | |
return 1; | |
for(i=1;st[i]!='\n';i++) | |
{ | |
if((st[i]>='A' && st[i]<='Z') || (st[i]>='a' && st[i]<='z') || (st[i]>='0' && st[i]<='9') || st[i]=='_') | |
flag=1; | |
else | |
{ | |
flag=0; | |
break; | |
} | |
} | |
} | |
if(flag==1) | |
return 1; | |
else | |
return 0; | |
} | |
short isKeywrd(char st[]) | |
{ | |
char keywrd[15]; | |
short i,flag=0; | |
FILE *kWrd; | |
kWrd = fopen("C:\\tc\\bin\\KEYWORDL.txt","r"); // Points to the file containing the list of keywords | |
if(kWrd == NULL) | |
{ | |
printf("\nCan't Open the file Keywordlist...."); | |
{ | |
getch(); | |
exit(0); | |
} | |
} | |
while(!feof(kWrd)) | |
{ | |
fgets(keywrd,15,kWrd); | |
for(i=0;keywrd[i]!='\n';i++) | |
{ | |
if(st[i]==keywrd[i]) | |
flag=1; | |
else | |
{ | |
flag=0; | |
break; | |
} | |
} | |
if(flag==1) | |
{ | |
fclose(kWrd); | |
return 1; | |
} | |
} | |
fclose(kWrd); | |
return 0; | |
} | |
short isPrepDire(char st[]) | |
{ | |
char direc[15]; | |
short i,flag=0; | |
FILE *direct; | |
direct = fopen("C:\\tc\\bin\\direcs.txt","r"); // Points to the file containing the list of keywords | |
if(direct == NULL) | |
{ | |
printf("\nCan't Open the file directiv...."); | |
getch(); | |
exit(0); | |
} | |
while(!feof(direct)) | |
{ | |
fgets(direc,15,direct); | |
for(i=0;direc[i]!='\n';i++) | |
{ | |
if(st[i]==direc[i]) | |
flag=1; | |
else | |
{ | |
flag=0; | |
break; | |
} | |
} | |
if(flag==1) | |
{ | |
fclose(direct); | |
return 1; | |
} | |
} | |
fclose(direct); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment