#include <cstdio> #include <cstring> #include <string> #include <vector> #include <map> #include <algorithm> #include "ts.h" using namespace std; extern map<INSTR,INSTR> instr; extern vector<string> state; extern vector<char> pr,pl; extern INFO info; extern char chars[256]; map<string,int> sm; int find_state(string s) { if(sm.count(s)>0) return sm[s]; if(state.size()>=MAX_STAV) return -1; sm[s]=state.size(); state.push_back(s); return state.size()-1; } int find_char(char *c,int allow) { if(strlen(c)!=1) return 0; if(allow&&c[0]=='<') return 1; if(allow&&c[0]=='>') return 1; return chars[c[0]]; } int load_input(void) { FILE *s; char tbuf[2048]=""; s=fopen(info.infile,"r"); if(s==0) return -1; memset(chars,0,sizeof(chars)); if(fgets(tbuf,2048,s)==NULL){ fclose(s); return -2; } int l=strlen(tbuf)-1; if(tbuf[0]=='-'){ char *tok; tok=strtok(tbuf," \n"); while(tok){ if(!stricmp(tok,"-t0")){ info.cl=0;info.cr=0; } if(!stricmp(tok,"-t1")){ info.cl=1;info.cr=0; } if(!stricmp(tok,"-t2")){ info.cl=1;info.cr=1; } if(!stricmp(tok,"-ot")){ info.output=0; } if(!stricmp(tok,"-os")){ info.output=1; } if(!stricmp(tok,"-oc")){ info.output=2; } tok=strtok(NULL," \n"); } if(fgets(tbuf,2048,s)==NULL){ fclose(s); return -2; } int l=strlen(tbuf)-1; } info.zero=tbuf[0]; info.one=tbuf[1]; for(int i=0;i<l;i++){ if(tbuf[i]=='\n') break; chars[tbuf[i]]=1; } if(fgets(tbuf,2048,s)==NULL){ fclose(s); return -3; } l=strlen(tbuf); for(int i=0;i<l;i++){ if(tbuf[i]=='\n') break; chars[tbuf[i]]=1; pr.push_back(tbuf[i]); } fclose(s); return 0; } int add_instruction(INSTR a,INSTR b) { if(instr.count(a)>0) return -1; instr[a]=b; return 0; } int load_ts(void) { FILE *s; int r=0; char s1[528],s2[528],p1[528],p2[528],m[528],tbuf[2048]; find_state("q0"); find_state("qF"); s=fopen(info.tsfile,"r"); if(s==0) return -1; instr.clear(); while(1){ if(fgets(tbuf,2048,s)==NULL) break; if(sscanf(tbuf,"%s %s %s %s %s",s1,p1,p2,s2,m)==5){ if(s1[0]!='#'){ if(instr.size()>=MAX_INSTR){ r=-3; break; } INSTR a,b; a.m=0; a.s=find_state(s1); if(a.s<0){ r=-2; break; } if(!find_char(p1,1)){ r=-4; break; } if(p1[0]=='<'||p1[0]=='>'){ if(!find_char(p2,1)){ r=-4; break; } if(p1[0]!=p2[0]) { r=-4; break; } } else{ if(!find_char(p2,0)){ r=-4; break; } } a.p=p1[0]; b.p=p2[0]; b.s=find_state(s2); if(b.s<0){ r=-2; break; } b.m=-1; if(!stricmp(m,"R")) b.m=MOVE_RIGHT; if(!stricmp(m,"L")) b.m=MOVE_LEFT; if(!stricmp(m,"N")) b.m=MOVE_DONT; if(b.m<0){ r=-6; break; } if(add_instruction(a,b)!=0){ r=-5; break; } } } } fclose(s); sm.clear(); return r; }