1*c10df855Sminshall %{ 2*c10df855Sminshall typedef struct string { 3*c10df855Sminshall int 4*c10df855Sminshall hashval, 5*c10df855Sminshall length; 6*c10df855Sminshall char 7*c10df855Sminshall *string; 8*c10df855Sminshall struct string 9*c10df855Sminshall *next; 10*c10df855Sminshall } string_t; 11*c10df855Sminshall 12*c10df855Sminshall typedef struct same { 13*c10df855Sminshall int 14*c10df855Sminshall myname, 15*c10df855Sminshall values, /* If a variable, the values */ 16*c10df855Sminshall action; /* If a target, the action number */ 17*c10df855Sminshall struct same 18*c10df855Sminshall *moretokens, 19*c10df855Sminshall *depends; 20*c10df855Sminshall string_t 21*c10df855Sminshall *string; 22*c10df855Sminshall } same_t; 23*c10df855Sminshall 24*c10df855Sminshall same_t 25*c10df855Sminshall *add_depends(), 26*c10df855Sminshall *assign(), 27*c10df855Sminshall *token_cat(), 28*c10df855Sminshall *token_item(); 29*c10df855Sminshall %} 30*c10df855Sminshall 31*c10df855Sminshall %union { 32*c10df855Sminshall string_t *string; 33*c10df855Sminshall same_t *same; 34*c10df855Sminshall } 35*c10df855Sminshall 36*c10df855Sminshall %token <string> TOKEN 37*c10df855Sminshall %token END_OF_FILE NL 38*c10df855Sminshall %type <same> target assignment actions action tokens token 39*c10df855Sminshall %% 40*c10df855Sminshall makefile : lines END_OF_FILE; 41*c10df855Sminshall 42*c10df855Sminshall lines : line 43*c10df855Sminshall | lines line 44*c10df855Sminshall ; 45*c10df855Sminshall 46*c10df855Sminshall line : NL 47*c10df855Sminshall | target NL 48*c10df855Sminshall | assignment NL 49*c10df855Sminshall | actions NL 50*c10df855Sminshall ; 51*c10df855Sminshall 52*c10df855Sminshall target : tokens ':' tokens 53*c10df855Sminshall { 54*c10df855Sminshall $$ = add_depends($1, $3); 55*c10df855Sminshall } 56*c10df855Sminshall ; 57*c10df855Sminshall 58*c10df855Sminshall assignment : token '=' tokens 59*c10df855Sminshall { 60*c10df855Sminshall $$ = assign($1, $3); 61*c10df855Sminshall } 62*c10df855Sminshall ; 63*c10df855Sminshall 64*c10df855Sminshall actions: action 65*c10df855Sminshall | actions action 66*c10df855Sminshall { 67*c10df855Sminshall $$ = token_cat($1, $2); 68*c10df855Sminshall } 69*c10df855Sminshall ; 70*c10df855Sminshall 71*c10df855Sminshall action: '\t' tokens 72*c10df855Sminshall { 73*c10df855Sminshall $$ = $2; 74*c10df855Sminshall } 75*c10df855Sminshall ; 76*c10df855Sminshall 77*c10df855Sminshall tokens : token 78*c10df855Sminshall | tokens token 79*c10df855Sminshall { 80*c10df855Sminshall $$ = token_cat($1, $2); 81*c10df855Sminshall } 82*c10df855Sminshall ; 83*c10df855Sminshall token: TOKEN 84*c10df855Sminshall { 85*c10df855Sminshall $$ = token_item($1); 86*c10df855Sminshall } 87*c10df855Sminshall ; 88*c10df855Sminshall %% 89*c10df855Sminshall #include <stdio.h> 90*c10df855Sminshall 91*c10df855Sminshall static int last_char, last_saved = 0; 92*c10df855Sminshall static int column = 0, lineno = 1; 93*c10df855Sminshall 94*c10df855Sminshall 95*c10df855Sminshall static string_t *strings = 0; 96*c10df855Sminshall static same_t 97*c10df855Sminshall *variables = 0, 98*c10df855Sminshall *targets = 0, 99*c10df855Sminshall *actions = 0; 100*c10df855Sminshall 101*c10df855Sminshall extern char *malloc(); 102*c10df855Sminshall 103*c10df855Sminshall main() 104*c10df855Sminshall { 105*c10df855Sminshall #define YYDEBUG 106*c10df855Sminshall extern int yydebug; 107*c10df855Sminshall 108*c10df855Sminshall return yyparse(); 109*c10df855Sminshall } 110*c10df855Sminshall 111*c10df855Sminshall yyerror(s) 112*c10df855Sminshall char *s; 113*c10df855Sminshall { 114*c10df855Sminshall fprintf(stderr, "line %d, column %d: %s\n", lineno, column, s); 115*c10df855Sminshall } 116*c10df855Sminshall 117*c10df855Sminshall same_t * 118*c10df855Sminshall add_depends(list1, list2) 119*c10df855Sminshall same_t 120*c10df855Sminshall *list1, 121*c10df855Sminshall *list2; 122*c10df855Sminshall { 123*c10df855Sminshall } 124*c10df855Sminshall 125*c10df855Sminshall same_t * 126*c10df855Sminshall assign(variable, value) 127*c10df855Sminshall same_t 128*c10df855Sminshall *variable, 129*c10df855Sminshall *value; 130*c10df855Sminshall { 131*c10df855Sminshall } 132*c10df855Sminshall 133*c10df855Sminshall 134*c10df855Sminshall same_t * 135*c10df855Sminshall token_cat(tokens, token) 136*c10df855Sminshall same_t 137*c10df855Sminshall *tokens, 138*c10df855Sminshall *token; 139*c10df855Sminshall { 140*c10df855Sminshall same_t *ptr; 141*c10df855Sminshall 142*c10df855Sminshall if (tokens->moretokens == 0) { 143*c10df855Sminshall tokens->moretokens = token; 144*c10df855Sminshall } else { 145*c10df855Sminshall for (ptr = tokens; ptr->moretokens; ptr = ptr->moretokens) { 146*c10df855Sminshall ; 147*c10df855Sminshall } 148*c10df855Sminshall ptr->moretokens = token; 149*c10df855Sminshall } 150*c10df855Sminshall return tokens; 151*c10df855Sminshall } 152*c10df855Sminshall 153*c10df855Sminshall same_t * 154*c10df855Sminshall token_item(string) 155*c10df855Sminshall string_t *string; 156*c10df855Sminshall { 157*c10df855Sminshall same_t *ptr; 158*c10df855Sminshall 159*c10df855Sminshall if ((ptr = (same_t *)malloc(sizeof *ptr)) == 0) { 160*c10df855Sminshall fprintf(stderr, "No more space for tokens!\n"); 161*c10df855Sminshall exit(1); 162*c10df855Sminshall } 163*c10df855Sminshall 164*c10df855Sminshall ptr->string = string; 165*c10df855Sminshall ptr->moretokens = 0; 166*c10df855Sminshall return ptr; 167*c10df855Sminshall } 168*c10df855Sminshall 169*c10df855Sminshall int 170*c10df855Sminshall Getchar() 171*c10df855Sminshall { 172*c10df855Sminshall if (last_saved) { 173*c10df855Sminshall last_saved = 0; 174*c10df855Sminshall return last_char; 175*c10df855Sminshall } else { 176*c10df855Sminshall int c; 177*c10df855Sminshall c = getchar(); 178*c10df855Sminshall switch (c) { 179*c10df855Sminshall case '\n': 180*c10df855Sminshall lineno++; 181*c10df855Sminshall column = 0; 182*c10df855Sminshall break; 183*c10df855Sminshall default: 184*c10df855Sminshall column++; 185*c10df855Sminshall } 186*c10df855Sminshall return c; 187*c10df855Sminshall } 188*c10df855Sminshall } 189*c10df855Sminshall 190*c10df855Sminshall int 191*c10df855Sminshall hashof(string, length) 192*c10df855Sminshall char *string; 193*c10df855Sminshall int length; 194*c10df855Sminshall { 195*c10df855Sminshall register int i = 0; 196*c10df855Sminshall 197*c10df855Sminshall while (length--) { 198*c10df855Sminshall i = (i<<3) + *string ^ ((i>>28)&0x7); 199*c10df855Sminshall } 200*c10df855Sminshall return i; 201*c10df855Sminshall } 202*c10df855Sminshall 203*c10df855Sminshall string_t * 204*c10df855Sminshall lookup(string) 205*c10df855Sminshall char *string; 206*c10df855Sminshall { 207*c10df855Sminshall int hashval; 208*c10df855Sminshall int length = strlen(string); 209*c10df855Sminshall string_t *ptr; 210*c10df855Sminshall 211*c10df855Sminshall hashval = hashof(string, length); 212*c10df855Sminshall 213*c10df855Sminshall for (ptr = strings; ptr; ptr = ptr->next) { 214*c10df855Sminshall if ((ptr->hashval == hashval) && (ptr->length == length)) { 215*c10df855Sminshall if (memcmp(string, ptr->string, length) == 0) { 216*c10df855Sminshall return ptr; 217*c10df855Sminshall } 218*c10df855Sminshall } 219*c10df855Sminshall } 220*c10df855Sminshall if ((ptr = (string_t *)malloc(sizeof *ptr)) == 0) { 221*c10df855Sminshall fprintf(stderr, "No space to add string *%s*!\n", string); 222*c10df855Sminshall exit(1); 223*c10df855Sminshall } 224*c10df855Sminshall ptr->hashval = hashval; 225*c10df855Sminshall ptr->length = length; 226*c10df855Sminshall if ((ptr->string = malloc(length+1)) == 0) { 227*c10df855Sminshall fprintf(stderr, "No space to add literal *%s*!\n", string); 228*c10df855Sminshall exit(1); 229*c10df855Sminshall } 230*c10df855Sminshall memcpy(ptr->string, string, length+1); 231*c10df855Sminshall ptr->next = strings; 232*c10df855Sminshall strings = ptr; 233*c10df855Sminshall return ptr; 234*c10df855Sminshall } 235*c10df855Sminshall 236*c10df855Sminshall 237*c10df855Sminshall yylex() 238*c10df855Sminshall { 239*c10df855Sminshall #define ret_token(c) if (bufptr != buffer) { \ 240*c10df855Sminshall save(c); \ 241*c10df855Sminshall *bufptr = 0; \ 242*c10df855Sminshall bufptr = buffer; \ 243*c10df855Sminshall yylval.string = lookup(buffer); \ 244*c10df855Sminshall return TOKEN; \ 245*c10df855Sminshall } 246*c10df855Sminshall #define save(c) { last_char = c; last_saved = 1; } 247*c10df855Sminshall #define Return(c) if (yydebug) { \ 248*c10df855Sminshall printf("[%d]", c); \ 249*c10df855Sminshall fflush(stdout); \ 250*c10df855Sminshall } \ 251*c10df855Sminshall return c; 252*c10df855Sminshall 253*c10df855Sminshall static char buffer[100], *bufptr = buffer; 254*c10df855Sminshall static int eof_found = 0; 255*c10df855Sminshall int c; 256*c10df855Sminshall 257*c10df855Sminshall if (eof_found != 0) { 258*c10df855Sminshall eof_found++; 259*c10df855Sminshall if (eof_found > 2) { 260*c10df855Sminshall fprintf(stderr, "End of file ignored.\n"); 261*c10df855Sminshall exit(1); 262*c10df855Sminshall } 263*c10df855Sminshall Return(END_OF_FILE); 264*c10df855Sminshall } 265*c10df855Sminshall while ((c = Getchar()) != EOF) { 266*c10df855Sminshall switch (c) { 267*c10df855Sminshall case '#': 268*c10df855Sminshall ret_token(c); 269*c10df855Sminshall while (((c = Getchar()) != EOF) && (c != '\n')) { 270*c10df855Sminshall ; 271*c10df855Sminshall } 272*c10df855Sminshall save(c); 273*c10df855Sminshall break; 274*c10df855Sminshall case ' ': 275*c10df855Sminshall ret_token(' '); 276*c10df855Sminshall break; 277*c10df855Sminshall case '\t': 278*c10df855Sminshall ret_token(c); 279*c10df855Sminshall if (column == 1) { 280*c10df855Sminshall Return(c); 281*c10df855Sminshall } 282*c10df855Sminshall break; 283*c10df855Sminshall case ':': 284*c10df855Sminshall case '=': 285*c10df855Sminshall ret_token(c); 286*c10df855Sminshall Return(c); 287*c10df855Sminshall case '\n': 288*c10df855Sminshall if (bufptr != buffer) { 289*c10df855Sminshall if (bufptr[-1] == '\\') { 290*c10df855Sminshall bufptr--; 291*c10df855Sminshall break; 292*c10df855Sminshall } 293*c10df855Sminshall } 294*c10df855Sminshall ret_token(c); 295*c10df855Sminshall Return(NL); 296*c10df855Sminshall default: 297*c10df855Sminshall *bufptr++ = c; 298*c10df855Sminshall break; 299*c10df855Sminshall } 300*c10df855Sminshall } 301*c10df855Sminshall 302*c10df855Sminshall eof_found = 1; 303*c10df855Sminshall 304*c10df855Sminshall ret_token(' '); 305*c10df855Sminshall Return(END_OF_FILE); 306*c10df855Sminshall } 307