Sie sind auf Seite 1von 6

#include<stdio.h> #include<conio.h> #include<string.h> #include<malloc.h> #include<stdlib.h> //internally ischar 30 34 // 40-44 internally ischar //to do //handle multi line splicing.

// comment removal. typedef struct dstore { char *symbols; int ss;int ns; struct dstore * next; } Dstore; char keywords[][9]={"auto","break","case","char","const","continue","default","d o","double","else","enum","extern", "float","for","goto","if","int","long","register","return","short","signed","siz eof","static","struct","switch", "typedef","union","unsigned","void","volatile","while" ,"sizeof"}; char operators[][7]={"+","-","--","++","/","*","^","%","==","!=",">","<",">=","< =", "&&","||","!","&","|","~","<<",">>","=","+=","*=","/=","<<=",">>=","%=","&=","|= ","^=", "sizeof","?",":"}; char symbols[][2]={"{","}","[","]","(",")"}; typedef struct Smap { int state; char * type; struct Smap * next; } Map; Map * getMapnode(Map * start,int state,char * type) { start=(Map*)malloc(sizeof(Map)); start->state=state; start->type=(char *)malloc(strlen(type)+1); strcpy(start->type,type); start->next=NULL; return start; } Dstore * getnode() { Dstore * temp=(Dstore *)malloc(sizeof(Dstore)); temp->next=NULL; return temp; } Map * createMap() { FILE * inp= fopen("fstates.txt","r"); if(inp==NULL) printf("Cant read input.");

int s1; Map * start=NULL; Map * crawl=NULL; char temp[256]; fscanf(inp,"%d%s",&s1,&temp); start=getMapnode(start,s1,temp); crawl=start; while(s1!=1099){ fscanf(inp,"%d%s",&s1,&temp); crawl->next=getMapnode(crawl->next,s1,temp); crawl=crawl->next; } fclose(inp); return start; } void init(int s1,int s2,char str[],Dstore * ptr) { ptr->ss=s1; ptr->ns=s2; ptr->symbols=(char *)malloc(sizeof(char)*(strlen(str)+1)); strcpy(ptr->symbols,str); } int getnextState(Dstore * start,char c,int cs) { Dstore *temp=start; while(temp!=NULL) { if(temp->ss==cs) { if(strchr(temp->symbols,c)) return temp->ns; else temp=temp->next; } else temp=temp->next; } return 0; } Map * checkFinal(Map * newMap,int State) { while(newMap->next!=NULL) { if(newMap->state==State) return newMap; else newMap=newMap->next; } return NULL; } void AutomataFSM(Dstore * start,Map * newMap,char input[]) { int i=0,j=0;Map * temp; int State=1; int length=strlen(input)+1;

char store[length]; store[length-1]='\0'; while(input[i]!='\0') { char c=input[i]; if(c=='\n') {i++;continue; } store[j]=input[i]; store[j+1]='\0'; int prevState=State; State=getnextState(start,c,State); i++; if (State==0) { //checkForFinal State and make transition to State 1. if(prevState==42) { State=prevState;j++;continue; } if(prevState==36) { State=prevState;j++;continue; } if(prevState==30) { State=34;j++;continue; } if(prevState==34) { State=34;j++;continue; } Map * temp=checkFinal(newMap,prevState); if(temp==NULL) printf("%s\b\t%s\n",store,"Invalid"); else{ if(temp->state!=2){ if(temp==NULL) printf("%s\b\t%s\n",store,"Invalid"); else printf("%s\b\t%s\n",store,temp->type); } else { store[j]='\0'; int i=0;int keyword=0; for(i=0;i<=32;i++) { if(strcmp(store,keywords[i])==0) { if(strcmp(store,"sizeof")==0) printf("%s\t%s\n",store,"operator"); else printf("%s\t%s\n",store,"keyword"); keyword=1; break; }

} if(keyword!=1) printf("%s\t%s\n",store,"identifier"); } } //print the token with type. //now we need to set the store to begin again. State=1; j=0; i--; } else j++; } //check for keyword. temp=checkFinal(newMap,State); if(temp->state!=2){ if(temp==NULL) printf("%s\t%s\n",store,"Invalid"); else printf("%s\t%s\n",store,temp->type); } else {int i=0;int keyword=0; for(i=0;i<=32;i++) { if(strcmp(store,keywords[i])==0) { if(strcmp(store,"sizeof")==0) printf("%s\t%s\n",store,"operator"); else printf("%s\t%s\n",store,"keyword"); keyword=1; break; } } if(keyword!=1) printf("%s\t%s\n",store,"identifier"); } } Dstore * createFSM2() { FILE * inp= fopen("table.txt","r"); if(inp==NULL) printf("Cant read input."); int s1,s2; Dstore * start=NULL; Dstore * crawl=NULL; char temp[256]; fscanf(inp,"%d%d%s",&s1,&s2,&temp); start=getnode(); init(s1,s2,temp,start); crawl=start; while(s1!=1099){ fscanf(inp,"%d%d%s",&s1,&s2,&temp);

crawl->next=getnode(); init(s1,s2,temp,crawl->next); crawl=crawl->next; } fclose(inp); return start; } int main() { FILE * code =fopen("code.txt","r"); if(code==NULL) { printf("Code File can't be read.\n"); } Map * newMap=createMap();//contains final states with type of symbol. Dstore * start=createFSM2();//contains transitions. char input[256]; char * token; while(!feof(code)){ fgets(input,256,code); //handling single line commment. if(strstr(input,"//")) { char * ptr; ptr=strstr(input,"//"); *ptr='\0'; } if(strcmp(input,"")==0) continue; //move if comment from beginning. //handler ends here. token=strtok(input," \t"); //handle preprocessors. if(token[0]=='#' && token[0]!='\0') { int l=strlen(input)+1; while(l-->0) { if(input[l]=='\n') input[l]=' '; } printf("%s\n",token,strcat(input," Preprocessor")); continue; } while(token!=NULL) { if(strcmp(token,"\n")!=0) AutomataFSM(start,newMap,token); token=strtok(NULL," \t");

} } getch(); }

Das könnte Ihnen auch gefallen