简单C语言编译器之头文件【待完善】

1、head.h

/* ++++++++++ 头文件部分 ++++++++++ */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 512
2、node.h

/* ++++++++++ 结点结构定义部分 ++++++++++ */ struct NodeSymbol // 符号栈定义 { char symbol[MAX]; int base; int top; }Symbol; struct NodeState // 状态栈定义 { int state[MAX]; int base; int top; }State; typedef struct node0 // 四元式链表 { char calc[5]; char opr1[20]; char opr2[20]; char tmpvar[20]; struct node0 *next; }FourExp; typedef struct node1 // 符号表 { char var[10]; int value; struct node1 *next; }SLink; typedef struct node2 // 词法分析生成的链 { int id; // 种别码 char style[5]; // 类型 char name[20]; // 单词符号 struct node2 *next; }Flink;
3、pubvar.h

char line[81]; // 一维数组,数组元素为字符,界对为1:80。用于读入一行字符的缓冲区。 int ll,cc; char A[11]; // 一维数组,数组元素为字符,界对[1:10] char WORD[14]; // 保留字表,一维数组,数组元素是以字符为元素的一维数组。界对[1:13]。查表方式采用二分法。 int k; char *CalId = "=+-*/"; char *LogicId[6] = {">","<",">=","<=","!=","=="}; char *BoundId = "(){};"; Flink *FHead; Flink *FHStack; FourExp *FEHead; SLink *SLHead; FILE *fp;
4、table.h

/* ++++++++++ 全局变量定义部分 ++++++++++ */ char *exp[] = { "W@F","F@D=X","D@d","X@G","X@X+G","X@X-G", "G@R","G@G*R","G@G/R","R@D","R@N","R@(X)","N@n"}; // 表达式表 char *VT = "+-*/=()dn;"; // VT集 char *VN = "FDXGRN"; // VN集 char *(ACT[22][10]) = { // ACTION表 "","","","","","","","S2","","", "","","","","","","","","","acc", "r2","r2","r2","r2","r2","","","","","r2", "","","","","S4","","","","","", "","","","","","S11","","S2","S10","", "S12","S13","","","","","","","","r1", "r3","r3","S14","S15","","","r3","","","r3", "r6","r6","r6","r6","","","r6","","","r6", "r9","r9","r9","r9","","","r9","","","r9", "r10","r10","r10","r10","","","r10","","","r10", "r12","r12","r12","r12","","","r12","","","r12", "","","","","","S11","","S2","S10","", "","","","","","S11","","S2","S10","", "","","","","","S11","","S2","S10","", "","","","","","S11","","S2","S10","", "","","","","","S11","","S2","S10","", "S12","S13","","","","","S21","","","", "r4","r4","S14","S15","","","r4","","","r4", "r5","r5","S14","S15","","","r5","","","r5", "r7","r7","r7","r7","","","r7","","","r7", "r8","r8","r8","r8","","","r8","","","r8", "r11","r11","r11","r11","","","r11","","","r11" }; int GOTO[22][6] = { // GOTO表 1,3,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,8,5,6,7,9, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,8,16,6,7,9, -1,8,-1,17,7,9, -1,-1,-1,18,7,9, -1,8,-1,-1,19,9, -1,8,-1,-1,20,9, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1 };
5、function.h

/* ++++++++++ 函数定义部分 ++++++++++ */ /* ========== 错误处理 ========== */ int Error() { printf("输入字符串不合法!/n"); exit(0); return 0; } /* ========== 查ACTION表并匹配 ========== */ char * ActionMatch( int X, char a ) { int j; int k = strlen(VT); for( j = 0; j < k; j++ ) if( a == VT[j] ) break; return (ACT[X][j]); } /* ========== 查GOTO表并匹配 ========== */ int GotoMatch( int i, char a ) { int j; int k = strlen(VN); for( j = 0; j < k; j++ ) if( a == VN[j] ) break; return GOTO[i][j]; } /* ========== 接受 ========== */ void Accept() { printf("/n/nAccept!/n"); } /* ========== 初始化符号栈 ========== */ void InitStackSymbol() { Symbol.base = 0; Symbol.top = 0; } /* ========== 初始化状态栈 ========== */ void InitStackState() { State.base = 0; State.top = 0; } /* ========== 入符号栈 ========== */ void PushSymbol( char c ) { Symbol.symbol[++Symbol.top] = c; } /* ========== 入状态栈 ========== */ void PushState( int k ) { State.state[++State.top] = k; } /* ========== 出符号栈 ========== */ void PopSymbol( char *c ) { *c = Symbol.symbol[Symbol.top--]; } /* ========== 出状态栈 ========== */ void PopState( int *k ) { *k = State.state[State.top--]; } /* ========== 取得符号栈顶元素 ========== */ int GetStateTop() { return State.state[State.top]; } /* ========== 将符号栈中符号转化为字符串方便输出 ========== */ char * PrintSymbolStack() { char *bf; int i; bf = (char *)malloc( 20*sizeof(char) ); if( Symbol.top == 0 ) return NULL; for( i = 1; i<= Symbol.top; i++ ) { bf[i-1] = Symbol.symbol[i]; } bf[i-1] = '/0'; return bf; } /* ========== 将状态栈中数字转化为字符串方便打印输出 ========== */ char * PrintStateStack() { char *bf; int i; int k; int n; bf = (char *)malloc( 20*sizeof(char) ); k = State.top; if( State.top == 0 ) return NULL; for( i = 1; i <= k; i++ ) { if(State.state[i] < 10) bf[i-1] = State.state[i] + '0'; else // 如果数字大于十则需要将其转化为字符串处理 { n = State.state[i]; bf[i-1] = '('; i++; k++; while(n) { bf[i-1] = n/10 + '0'; i++; k++; n /= 10; } bf[i-1] = ')'; } } bf[i-1] = '/0'; return bf; } /* ========== 将字符串转化为整型数 ========== */ int CharToInt( char *ch ) { int k; int t = 0; int i; k = strlen( ch ); for( i = 0; i < k; i++ ) { if( ch[i] < '0' || ch[i] > '9' ) // 如果串中含有非数字字符则返回负一表示出错 return -1; t += ch[i] - '0'; if( i < k-1 ) t *= 10; } return t; } Flink * PopFHStack() { Flink *rt; rt = FHStack->next; if(rt) { FHStack->next = rt->next; rt ->next = NULL; } return rt; } int InsertFourExp(FourExp *nod) { FourExp *fe,*p; p = FEHead; fe = (FourExp *)malloc(sizeof(FourExp)); *fe = *nod; while(p->next) p = p->next; p->next = fe; return 0; } int InsertTmpVar(SLink *slk) { SLink *fd,*p; p = (SLink *)malloc(sizeof(SLink)); *p = *slk; p->next = NULL; fd = SLHead->next; while(fd) { if(!strcmp(p->var,fd->var)) { fd->value = p->value; return 0; } fd = fd->next; } fd = p; return 0; } int FindSLink(int *rt,char *name) { int flag = 0; SLink *p; p = SLHead->next; while(p) { if(!strcmp(name,p->var)) { *rt = p->value; return 1; } p = p->next; } return flag; } int Teams( int l ) { FILE *FExp; Flink *fhs; SLink *slk; int retn,flag; FourExp *nod = (FourExp *)malloc(sizeof(FourExp)); slk = (SLink *)malloc(sizeof(SLink)); FExp = fopen("result.txt","a+"); switch (l) { case 1: fhs = PopFHStack(); if( !strcmp(fhs->style,"n") ) { strcpy(nod->calc,"="); strcpy(nod->opr1,fhs->name); strcpy(nod->opr2,","); slk->value = CharToInt( fhs->name ); fhs = PopFHStack(); strcpy(slk->var,fhs->name); strcpy(nod->tmpvar,fhs->name); nod->next = NULL; InsertTmpVar(slk); InsertFourExp(nod); } else if( !strcmp(fhs->style,"d") ) { strcpy(nod->calc,"="); strcpy(nod->opr1,fhs->name); strcpy(nod->opr2,","); flag = FindSLink(&retn,fhs->name); if(!flag) { printf("变量%s没有初值!/n",fhs->name); exit(0); } slk->value = retn; fhs = PopFHStack(); strcpy(slk->var,fhs->name); strcpy(nod->tmpvar,fhs->name); nod->next = NULL; InsertTmpVar(slk); InsertFourExp(nod); } fprintf(FExp,"(%s,%s,,%s)/n",nod->calc,nod->opr1,nod->tmpvar); printf("(%s,%s,,%s)/n",nod->calc,nod->opr1,nod->tmpvar); break; case 4: fprintf(FExp,"(%c,%c,%c,%c)/n",'+','a','b','T'); printf("(%c,%c,%c,%c)/n",'+','a','b','T'); break; case 5: fprintf(FExp,"(%c,%c,%c,%c)/n",'-','c','d','P'); printf("(%c,%c,%c,%c)/n",'-','c','d','P'); break; case 7: fprintf(FExp,"(%c,%c,%c,%c)/n",'*','e','f','L'); printf("(%c,%c,%c,%c)/n",'*','e','f','L'); break; case 8: fprintf(FExp,"(%c,%c,%c,%c)/n",'/','g','h','K'); printf("(%c,%c,%c,%c)/n",'/','g','h','K'); break; default:break; } printf("/n/n/n规约的表达式为:%d,%s/n/n/n",l,exp[l]); fclose(FExp); return 0; } int PushFPStack(Flink *pt) // 入栈 { Flink *p; p = (Flink *)malloc(sizeof(Flink)); if(!p) { printf("Error!"); exit(0); } *p = *pt; if( !FHStack->next ) { p->next = NULL; FHStack->next = p; } else { p->next = FHStack->next; FHStack ->next = p; } return 0; } int Analysis() { char ch; // 存放Buff中的一个待分析的字符 int k,t,y; // 中间变量,用于接受函数的返回值 char *CH; // 指向待规约的表达式 int x; // 存放返回的栈顶元素 char *f; // 指向ACTION表中的字符串 int tmp; // 临时变量,用来存放出状态栈的值 char tp; // 临时变量,用来存放出符号栈的值 int mm; // 存放字符串转化后的数字 Flink *pt = FHead->next; Flink *pre = pt; if(!pt) exit(0); InitStackSymbol(); // 初始化 InitStackState(); PushSymbol(';'); // ;入符号栈,0入状态栈 PushState(0); do { ch = pt->style[0];// 取缓冲区首字符 x = GetStateTop();// 取得状态栈顶元素 f = ActionMatch(x,ch); // 查ACTION表 if(f[0] == 'a') // 接受 { Accept(); return 0; } else if(f[0] == 'S') // 状态转移 { PushSymbol(ch); // ch 入符号栈 pre = pt; pt = pt->next; // 下移一个字符 mm = CharToInt(&f[1]); // 状态字符串转化为数字方便入栈 PushState(mm); // mm入状态栈 } else if( f[0] == 'r' ) // 规约 { int l = CharToInt(&f[1]); CH = exp[l]; // 取得规约的表达式 if( l==2||l==12 ) // 如果由终结符规约则将其结点入栈 PushFPStack(pre); if( l==1||l==4||l==5||l==7||l==8 ) Teams(l); l = strlen( CH ); // 计算表达式的长度,用以确定出栈的个数 for( k = 0 ; k < l - 2 ; k++ ) // 状态栈、符号栈出栈 { PopState( &tmp ); PopSymbol( &tp ); } PushSymbol( CH[0] ); // CH0符号栈,即规约过程 y = GetStateTop(); // 取得状态栈顶元素 t = GotoMatch( y, CH[0] );// 查GOTO表,返回转移状态 PushState( t ); // 将得到的新状态入状态栈 } else { Error(); // 出错处理 exit(0); } }while( ch != '/0' ); return 0; } int InsertFlink(char *name,int id) { Flink *p = FHead; Flink *temp; while(p->next) p = p->next;// 将指针指向链尾 temp = (Flink *)malloc(sizeof(Flink)); temp->id = id; strcpy(temp->name,name); strcpy(temp->style,name); switch (id) { case 10:strcpy(temp->style,"d");break; case 20:strcpy(temp->style,"n");break; } temp->next = NULL; p->next = temp; return 0; } int IsCalc(char c,int *ord) { int i; for(i = 0;i<strlen(CalId);i++) { if(CalId[i]==c) { *ord = i + 21; return 1; } } return 0; } int IsBound(char c,int *ord) { int i; for(i = 0;i<strlen(BoundId);i++) { if(BoundId[i]==c) { *ord = i + 26; return 1; } } return 0; } int IsLogic() { return 0; } int IsReserve(int *ord) { FILE *fop; char ch; int i,j; int k; int l = 0; fop = fopen("id.ini","r"); if(!fop) { printf("Can/'t open this file./n"); exit(1); } i = strlen(A); while(!feof(fop)) { ch = fgetc(fop); // 将fop中的值读入到WORD中 j = 0; while(!feof(fop)&&ch!=',') { WORD[j++]=ch; ch = fgetc(fop); } WORD[j] = '/0'; if(i==j) { for(k = 0;k<i;k++) { if(A[k]!=WORD[k]) return 0; } *ord = l + 1; return 1; } l++; } return 0; fclose(fop); } int Check(char c,int *ord) { if(c>='a'&&c<='z'||c>='A'&&c<='Z') return 1; else if(c>='0'&&c<='9') return 2; else if(IsBound(c,ord)) return 3; else if(IsCalc(c,ord)) return 4; else if(IsLogic()) return 5; else return 0; } void WriteFile() { Flink *p = FHead->next; FILE *wp = fopen("rel.txt","a+"); if(!wp) { printf("Can/'t open this file./n"); exit(1); } while(p) { fprintf(wp,"(%d,%s)/n",p->id,p->name); p = p->next; } fclose(wp); }

你可能感兴趣的:(c,struct,File,action,语言,编译器)