- 論壇徽章:
- 0
|
本帖最后由 landker 于 2013-07-26 15:26 編輯
在《現(xiàn)代編譯原理——C語言描述(虎書)》中文版)中,對于第12章的內(nèi)容不太明白,想請教一下。里面第191頁中間處提到,如果要創(chuàng)建一個(gè)可運(yùn)行的編譯器,則需要使用到一個(gè)runtime.c的文件,實(shí)際上是一個(gè)對于操作系統(tǒng)的“系統(tǒng)調(diào)用”函數(shù)的使用。但這里就產(chǎn)生一個(gè)問題,所有的函數(shù)在定義時(shí)都要入符號表,那這些“系統(tǒng)調(diào)用”都在哪里聲明了?例如:putchar函數(shù)(在runtime.c里的print函數(shù)里),它的聲明在哪里?此外,該如何編譯(即比如說,我要使用 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;
- }
復(fù)制代碼 |
|