Annotation of researchv9/cmd/pp/scan.l, revision 1.1

1.1     ! root        1: %{
        !             2: #include "pp.h"
        !             3: #include <stdio.h>
        !             4: #ifdef TEST
        !             5: main(){
        !             6:        register type, c;
        !             7:        while((type=yylex())!=0) switch(type){
        !             8:        case OTHER:             printf("%s", yytext); break;
        !             9:        case KEYWORD:           printf("\\fB%s\\fR", yytext); break;
        !            10:        case FUNCTION:          printf("Function: %s", yytext); break;
        !            11:        case COMMENT:
        !            12:                printf("\\fI%s", yytext);
        !            13:                for(;;){
        !            14:                        putchar(c=input());
        !            15:                        if(c==0)
        !            16:                                break;
        !            17:                        if(c=='*'){
        !            18:                        GotStar:
        !            19:                                putchar(c=input());
        !            20:                                if(c=='/')
        !            21:                                        break;
        !            22:                                if(c=='*')
        !            23:                                        goto GotStar;
        !            24:                        }
        !            25:                        if(c=='\n'){
        !            26:                                while((c=input())=='\t')
        !            27:                                        putchar(c);
        !            28:                                if(c==' ')
        !            29:                                        putchar('^');
        !            30:                                else{
        !            31:                                        putchar(c);
        !            32:                                        if(c=='*')
        !            33:                                                goto GotStar;
        !            34:                                }
        !            35:                        }
        !            36:                }
        !            37:                printf("\\fR");
        !            38:                break;
        !            39:        }
        !            40: }
        !            41: #endif
        !            42: char *keyword[]={
        !            43: "asm", "auto", "break", "char", "case", "continue", "double",
        !            44: "default", "do", "extern", "else", "enum", "for", "float",
        !            45: "fortran", "goto", "if", "int","long",  "return", "register",
        !            46: "switch", "struct", "sizeof", "short", "static", "unsigned",
        !            47: "union", "void", "while", "typedef",
        !            48: NULL
        !            49: };
        !            50: #define        NWORD   150
        !            51: char *hashtab[NWORD];
        !            52: int kwinit=0;
        !            53: hash(s)
        !            54: register char *s;
        !            55: {
        !            56:        register i=0, j=0;
        !            57:        while(*s)
        !            58:                i += *s++ * ++j;        /* got that? */
        !            59:        if(i<0)
        !            60:                return(i%NWORD+NWORD);
        !            61:        return(i%NWORD);
        !            62: }
        !            63: char *copy(s)
        !            64: register char *s;
        !            65: {
        !            66:        char *malloc();
        !            67:        register char *t=malloc(strlen(s)+1);
        !            68:        if(t==NULL)
        !            69:                error("Out of space", (char *)0);
        !            70:        strcpy(t, s);
        !            71:        return(t);
        !            72: }
        !            73: insert(s)
        !            74: register char *s;
        !            75: {
        !            76:        register char **h;
        !            77:        register i;
        !            78:        h = &hashtab[hash(s)];
        !            79:        for(i=0;i!=NWORD && *h!=NULL;i++){
        !            80:                if(strcmp(*h, s)==0)
        !            81:                        return;
        !            82:                if(++h==&hashtab[NWORD])
        !            83:                        h=hashtab;
        !            84:        }
        !            85:        if(*h!=NULL)
        !            86:                error("keyword hash table overflow", (char *)0);
        !            87:        *h = s;
        !            88: }
        !            89: lookup(s)
        !            90: register char *s;
        !            91: {
        !            92:        register char **h;
        !            93:        register i;
        !            94:        if(!kwinit){
        !            95:                for(h=keyword;*h!=NULL;h++)
        !            96:                        insert(*h);
        !            97:                kwinit++;
        !            98:        }
        !            99:        h = &hashtab[hash(s)];
        !           100:        for(i=0;i!=NWORD && *h!=NULL;i++){
        !           101:                if(strcmp(s, *h)==0)
        !           102:                        return(KEYWORD);
        !           103:                if(++h==&hashtab[NWORD])
        !           104:                        h=hashtab;
        !           105:        }
        !           106:        return(OTHER);
        !           107: }
        !           108: ckeywords(file)
        !           109:        char *file;
        !           110: {
        !           111:        char buf[128], *fgets(), *malloc();
        !           112:        FILE *f=fopen(file, "r");
        !           113:        register char *p;
        !           114:        if(f==NULL){
        !           115:                if(*file!='/'){
        !           116:                        sprintf(buf, "/usr/lib/pp/%s", file);
        !           117:                        f=fopen(buf, "r");
        !           118:                }
        !           119:                if(f==NULL)
        !           120:                        error("can't find keyword file", file);
        !           121:        }
        !           122:        while(fgets(buf, sizeof buf, f)){
        !           123:                buf[strlen(buf)-1]=0;
        !           124:                p=malloc(strlen(buf)+1);
        !           125:                if(p==NULL)
        !           126:                        error("can't malloc a string", (char *)0);
        !           127:                strcpy(p, buf);
        !           128:                insert(p);
        !           129:        }
        !           130:        fclose(f);
        !           131: }
        !           132: %}
        !           133: WS     ([ \t]*)
        !           134: %%
        !           135: ^#{WS}ifdef                    return(KEYWORD);
        !           136: ^#{WS}ifndef                   return(KEYWORD);
        !           137: ^#{WS}endif                    return(KEYWORD);
        !           138: ^#{WS}else                     return(KEYWORD);
        !           139: ^#{WS}if                       return(KEYWORD);
        !           140: ^#{WS}define                   return(KEYWORD);
        !           141: ^#{WS}undef                    return(KEYWORD);
        !           142: ^#{WS}include                  return(KEYWORD);
        !           143: ^#{WS}line                     return(KEYWORD);
        !           144: [_a-zA-Z][_a-zA-Z0-9]*         return(lookup(yytext));
        !           145: "/*"                           return(COMMENT);
        !           146: ^[^ \t#\n].*"(".*[){]{WS}$     return(FUNCTION);
        !           147: \"(\\(.|\n)|[^"\n])*["\n]      return(OTHER);
        !           148: .|\n                           return(OTHER);

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.