資源簡介
前段時間自己業余抽空花了三個月編了一個c語言編譯器,將c語言編譯成masm32。總共4個文件,2000多行的代碼,其中sca.c是詞法,p.c生成語法樹,table.c是符號表,g.c是轉化成具體masm32代碼,只支持char,short,int,double四種類型數據,其他的有興趣的可以自己修改增加,由于masm32限制,目前該編譯器只支持局部變量在函數開頭定義,全局變量不能和局部變量同名。不支持變量強制轉變,也不支持switch語句,個人感覺swich沒用,#include不支持,只支持“ ”,現在打包給大家分享,包括源代碼(感興趣的可以自己修改或者轉發學習,不允許商業用途),里面還有一些例子,想學習編譯原理或者masm32的同志可以看看(特別是學習masm32的,可以把寫好的c語言直接編譯成masm32代碼)!遇到問題可以跟我聯系,盡量跟你們回復!

代碼片段和文件信息
#include?“table.c“
void?error_gencode(TreeNode?*);
void?printtree(TreeNode?*);
void?gencode(TreeNode?*);
void?op_pointer(TreeNode?*);
void?op_array(TreeNode?*);
void?op_struct(TreeNode?*);
void?op_inc(TreeNode?*);
void?op_para(TreeNode?*);
void?op_call(TreeNode?*);
void?cmp_handle(TreeNode?*char?*int);
void?exp_handle(TreeNode?*char?*int);
void?load_var(TreeNode?*char?*int);
void?print_g();
void?print_l(TreeNode?*);
int?sizecal(TreeNode?*);
int?double_test(TreeNode?*);
void?getmainarg(TreeNode?*);
int?get_level(TreeNode?*);
void?error_gencode(TreeNode?*p){
????printf(“GENCODE?ERROR:file?%s?line?%d?errormsg=%s\n“filenamep->linenoerrormsg);
????exit(1);
}
int?get_level(TreeNode?*p){
int?level=0;
if(p->t==ARRAY){
level=1;
p=p->child[1];
while(p&&p->t==LBRACE){
level++;
p=p->child[1];
}
}
return?level;
}
int?sizecal(TreeNode?*p){
TreeNode?*q*t*k;
int?offset=0size=0maxsize=0;
q=p->child[0]->child[0]->child[1];
while(q){
t=q->child[0];
while(t){
k=t->child[0];
if(k->t==POINTER){
size=4;
if(offset%size)offset=(offset/size+1)*size;
offset=offset+4;
}else{
size=size_of_type(q->t);
if(offset%size)offset=(offset/size+1)*size;
if(k->t==ID)offset=offset+size;
if(k->t==ARRAY)offset=offset+size*get_array_num(k);
}
if(size>maxsize)maxsize=size;
t=t->child[1];
}
q=q->child[1];
}
//if(offset%maxsize)offset=(offset/maxsize+1)*maxsize;
//printf(“size=%d\n“offset);
return?offset;
}
void?assign_struct(TreeNode?*p){
TreeNode?*pl*pr*temp*t;
pl=p->child[0];
pr=p->child[1];
temp=test(pl);
if(temp->t==ARRAY){printtree(pl);printf(“\timul?esitype?_%s\n\tlea?edi[ebx+esi]\n“temp->temp);}
else?printf(“\tlea?edi_%s\n“pl->id);
if(pr->t==DOT||pr->t==ARROW){
op_struct(pr);
t=test(pr);
if(t->t==ARRAY){op_array(pr);printf(“\timul?esitype?_%s\n\tlea?esi[ebx+esi]\n“temp->temp);}
else?printf(“\tlea?esi%s\n“pr->id);
}else?if(pr->t==ARRAY){op_array(pr);printf(“\timul?esitype?_%s\n\tlea?esi[ebx+esi]\n“temp->temp);}
else?printf(“\tlea?esi_%s\n“pr->id);
printf(“\tmov?ecxtype?_%s\n\trep?movsb\n“temp->temp);
}
int?double_test(TreeNode?*p){
????TreeNode?*temp;
????if(!p){p=NewNode(ERROR);errormsg=“double?test?1“;error_gencode(p);}
????if(p->t==REAL)return?1;
????if(p->t==ID){
????????temp=test(p);
????????if(temp->t==VAR&&temp->type==DOUBLE)return?1;
????}
????if(p->t==ARRAY){temp=test(p);if(temp->type==DOUBLE)return?1;}
????if(p->t==DOT||p->t==ARROW){temp=struct_test(p);if(temp->type==DOUBLE)return?1;}
????if(p->t==POINTER){
????????while(p){
????????????if(p->t==ID)break;
????????????p=p->child[0];
????????}
????????if(!p){p=NewNode(ERROR);errormsg=“double_test?2“;error_gencode(p);}
????????temp=test(p);
????????if(temp->type==DOUBLE)return?1;
????}
????if(p->t==CAL||p->t==CMP||p->t==ASSIGN){
????????int?flfr;
????????fl=double_test(p->child[0]);
????????fr=doub
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????89923??2018-10-15?14:10??bianyi\a.exe
?????文件?????????90??2018-09-04?16:23??bianyi\example\beep.c
?????文件???????1584??2018-09-18?09:37??bianyi\example\bm.c
?????文件???????5486??2018-10-11?15:12??bianyi\example\bp.c
?????文件???????3213??2018-09-14?09:07??bianyi\example\cal.c
?????文件???????1563??2018-09-11?18:32??bianyi\example\find.c
?????文件???????1154??2018-10-04?14:41??bianyi\example\kmp.c
?????文件???????2537??2018-10-15?10:07??bianyi\example\maze.c
?????文件???????6124??2018-10-03?17:24??bianyi\example\pe.c
?????文件????????862??2018-09-11?14:08??bianyi\example\q.c
?????文件???????3951??2018-09-10?11:54??bianyi\example\scan.c
?????文件???????1417??2018-09-17?08:43??bianyi\example\t.c
?????文件????????101??2018-10-05?11:46??bianyi\example\win.c
?????文件???????7576??2018-09-17?14:33??bianyi\example\x.c
?????文件??????43870??2018-10-12?09:59??bianyi\g.c
?????文件??????15903??2018-10-12?08:46??bianyi\p.c
?????文件???????7851??2018-10-08?16:04??bianyi\sca.c
?????文件???????3358??2018-10-15?10:41??bianyi\stdio.h
?????文件??????15568??2018-10-12?09:22??bianyi\table.c
?????目錄??????????0??2018-10-15?14:14??bianyi\example
?????目錄??????????0??2018-10-15?14:10??bianyi
-----------?---------??----------?-----??----
???????????????212131????????????????????21
- 上一篇:多目標進化算法評價
- 下一篇:空間后方、前方交會算法的C++實現
評論
共有 條評論