资源简介
前段时间自己业余抽空花了三个月编了一个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++实现
相关资源
- C语言面试笔试题,经典题目
- C++获取计算机的CPU ID,硬盘序列号等
- C++头文件转delphi工具 + 源码
- C语言编程常见问题解答.pdf
- GD32通过规则组寄存器 DMA获取多组AD
- 基于MFC的TCP调试助手源码95706
- 国际象棋的qt源代码
- 操作系统c语言模拟文件管理系统844
- C语言开发实战宝典
- C++中头文件与源文件的作用详解
- 基于mfc的多线程文件传输
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- C语言代码高亮html输出工具
- 猜数字游戏 c语言代码
- C语言课程设计
- 数字电位器C语言程序
- CCS FFT c语言算法
- 使用C语言编写的病房管理系统
- 通信过程中的RS编译码程序(c语言)
- 利用C++哈希表的方法实现电话号码查
- 计算机二级C语言上机填空,改错,编
- 用回溯法解决八皇后问题C语言实现
- 移木块游戏,可以自编自玩,vc6.0编写
- 简易教务管理系统c语言开发文档
- 操作系统课设 读写者问题 c语言实现
- 小波变换算法 c语言版
- C流程图生成器,用C语言代码 生成C语
- 3des加密算法C语言实现
- 简单的C语言点对点聊天程序
评论
共有 条评论