/* ACM Western European Region, 1994-95 Problem B, Bits Ed Karrels, October 1996 */ #include #include #include #include enum TokenType { COLON, LCURLY, RCURLY, ARRAY, OF, STRING, LPAREN, RPAREN, COMMA, LBRACE, RBRACE, PERIODS, ID, INTEGER, END }; char *literals[] = { ":", "{", "}", "array", "of", "string", "(", ")", ",", "[", "]", ".." }; struct Token { int type; char id[257]; int num; }; enum { RECORD_T, ARRAY_T, STRING_T, ENUM_T, RANGE_T }; struct Type { char name[257]; virtual int Size() = 0; Type *next; }; struct RecordType : public Type { Type *element_list; int Size(); }; int RecordType::Size() { int s=0; Type *t; t = element_list; while (t) { s += t->Size(); t = t->next; } return s; } struct ArrayType : public Type { Type *subtype; int nel; int Size() { return nel * subtype->Size(); } }; int Nbits(int n) { int a=2, b=1; while (aelement_list = 0; Type *subtype; while (tok = NextToken(inf), tok.type != RCURLY) { PushToken(tok); subtype = ReadType(inf, 1); subtype->next = rtype->element_list; rtype->element_list = subtype; } type = rtype; break; case ARRAY: // array atype = new ArrayType; atype->nel = ReadRange(inf); NextToken(inf); // "of" atype->subtype = ReadType(inf); type = atype; break; case STRING: // string stype = new StringType; NextToken(inf); // "(" tok = NextToken(inf); // string length stype->len = tok.num; NextToken(inf); // ")" type = stype; break; case LPAREN: // enum etype = new EnumType; etype->n_ids = 0; while (tok.type != RPAREN) { tok = NextToken(inf); // identifier tok = NextToken(inf); // comma or rparen etype->n_ids++; } type = etype; break; case LBRACE: // range gtype = new RangeType; PushToken(tok); gtype->range = ReadRange(inf); type = gtype; break; default: printf("syntax error.\n"); exit(0); } if (named) { strcpy(type->name, name); } return type; } int main(int argc, char **argv) { FILE *inf; Token tok; int ntypes, type_no; Type *type; inf = stdin; fscanf(inf, "%d", &ntypes); for (type_no=0; type_noname, type->Size()); } return 0; }