Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<ctype.h>
- #include<string.h>
- int main(int argc, char const *argv[])
- {
- char ch,buffer[100],operators[]="+.*/%=";
- FILE *fp;
- int i,j=0;
- // open the source file given in first argument
- if(argc != 2)
- printf("Source file is not provided.\n");
- fp=fopen(argv[1],"r");
- //if can't open file throw error
- if(fp==NULL)
- {
- printf("Error while opening the file\n");
- exit(2);
- }
- int fl=1, cnt=1;
- //parse the file, one by one character
- while((ch=fgetc(fp))!=EOF){
- //check if the char is a operator
- for(i=0;i<6;i++)
- {
- if(ch==operators[i])
- {
- printf("%c is operator\n",ch);
- if(!fl)buffer[j++]=',';
- fl=1;
- }
- }
- // cehck is the char is alphanumeric
- if(isalnum(ch))
- {
- buffer[j++]=ch;
- fl=0;
- }
- else if((ch=='\n')&& j!=0)
- {
- buffer[j]='\0';
- j=0;
- printf("%s is identifier\n",buffer);
- fl=1;
- printf("Above tokens are from line %d \n",cnt);
- // if the current char is a newline increase the newline counter
- if( ch == '\n')
- cnt++;
- }
- }
- fclose(fp);
- return 0;
- }
Add Comment
Please, Sign In to add comment