- 论坛徽章:
- 0
|
本帖最后由 landker 于 2013-07-26 15:26 编辑
在《现代编译原理——C语言描述(虎书)》中文版)中,对于第12章的内容不太明白,想请教一下。里面第191页中间处提到,如果要创建一个可运行的编译器,则需要使用到一个runtime.c的文件,实际上是一个对于操作系统的“系统调用”函数的使用。但这里就产生一个问题,所有的函数在定义时都要入符号表,那这些“系统调用”都在哪里声明了?例如:putchar函数(在runtime.c里的print函数里),它的声明在哪里?此外,该如何编译(即比如说,我要使用 printf,那我要链接哪些库?)?
这里附上 runtime.c 的 source- #undef __STDC__
- #include <stdio.h>
- int *initArray(int size, int init)
- {int i;
- int *a = (int *)malloc(size*sizeof(int));
- for(i=0;i<size;i++) a[i]=init;
- return a;
- }
- int *allocRecord(int size)
- {int i;
- int *p, *a;
- p = a = (int *)malloc(size);
- for(i=0;i<size;i+=sizeof(int)) *p++ = 0;
- return a;
- }
- struct string {int length; unsigned char chars[1];};
- int stringEqual(struct string *s, struct string *t)
- {int i;
- if (s==t) return 1;
- if (s->length!=t->length) return 0;
- for(i=0;i<s->length;i++) if (s->chars[i]!=t->chars[i]) return 0;
- return 1;
- }
- void print(struct string *s)
- {int i; unsigned char *p=s->chars;
- for(i=0;i<s->length;i++,p++) putchar(*p);
- }
- void flush()
- {
- fflush(stdout);
- }
- struct string consts[256];
- struct string empty={0,""};
- int main()
- {int i;
- for(i=0;i<256;i++)
- {consts[i].length=1;
- consts[i].chars[0]=i;
- }
- return tigermain(0 /* static link */);
- }
- int ord(struct string *s)
- {
- if (s->length==0) return -1;
- else return s->chars[0];
- }
- struct string *chr(int i)
- {
- if (i<0 || i>=256)
- {printf("chr(%d) out of range\n",i); exit(1);}
- return consts+i;
- }
- int size(struct string *s)
- {
- return s->length;
- }
- struct string *substring(struct string *s, int first, int n)
- {
- if (first<0 || first+n>s->length)
- {printf("substring([%d],%d,%d) out of range\n",s->length,first,n);
- exit(1);}
- if (n==1) return consts+s->chars[first];
- {struct string *t = (struct string *)malloc(sizeof(int)+n);
- int i;
- t->length=n;
- for(i=0;i<n;i++) t->chars[i]=s->chars[first+i];
- return t;
- }
- }
- struct string *concat(struct string *a, struct string *b)
- {
- if (a->length==0) return b;
- else if (b->length==0) return a;
- else {int i, n=a->length+b->length;
- struct string *t = (struct string *)malloc(sizeof(int)+n);
- t->length=n;
- for (i=0;i<a->length;i++)
- t->chars[i]=a->chars[i];
- for(i=0;i<b->length;i++)
- t->chars[i+a->length]=b->chars[i];
- return t;
- }
- }
- int not(int i)
- { return !i;
- }
- #undef getchar
- struct string *getchar()
- {int i=getc(stdin);
- if (i==EOF) return ∅
- else return consts+i;
- }
复制代码 |
|