static void test_chars() { CC_Class *cc[CC_BITS], *cc_input, *temp, *all; ATerm input, output; int i; input = ATparse("char-class([0,range(9,10),12,23,range(30,50),range(128,256)])"); cc_input = CC_ClassFromTerm(input); temp = CC_makeClassEmpty(); CC_addATermClass(temp, input); assert(CC_isEqual(temp, cc_input)); CC_free(temp); temp = CC_makeClassEmpty(); assert(CC_isEmpty(temp)); for (i=0; i<CC_BITS; i++) { cc[i] = CC_ClassFromInt(i); if (i == CC_EOF) { assert(CC_isEOF(cc[i])); } else { assert(!CC_isEOF(cc[i])); } CC_union(temp, cc[i], temp); assert(!CC_isEOF(temp)); } all = CC_makeClassAllChars(); assert(!CC_isEmpty(all)); assert(CC_isEqual(temp, all)); CC_difference(temp, all, temp); assert(CC_isEmpty(temp)); CC_copy(all, temp); CC_difference(temp, cc_input, temp); CC_complement(temp, temp); assert(CC_isEqual(temp, cc_input)); CC_copy(all, temp); CC_difference(temp, cc_input, temp); CC_union(temp, cc_input, temp); assert(CC_isEqual(temp, all)); CC_copy(all, temp); CC_intersection(temp, cc_input, temp); assert(CC_isEqual(temp, cc_input)); assert(CC_isSubset(cc_input, all)); output = CC_ClassToTerm(cc_input); /* ATwarning("input = %t\noutput = %t\n", input, output); assert(ATisEqual(output, input)); */ }
/* For each production, get its first set. */ void calc_first_table() { CC_Class *firstset; CC_Class copy; PT_Symbol symbol; PT_Symbols symbols; PT_Production prod; ATbool changed = ATtrue; int ip; while(changed) { changed = ATfalse; for(ip = MIN_PROD_NUM; ip < PGEN_getMaxProductionNumber(); ip++) { prod = PGEN_getProductionOfProductionNumber(ip); assert(PT_isValidProduction(prod)); symbols = PT_getProductionLhs(prod); symbol = PT_getProductionRhs(prod); firstset = get_first_set(symbol, ATfalse); if(firstset) { CC_copy(firstset, ©); first(firstset, symbols); CC_union(firstset, ©, firstset); changed |= !CC_isEqual(©, firstset); } else { firstset = get_first_set(symbol, ATtrue); first(firstset, symbols); changed = ATtrue; } } } }
static void first(CC_Class *cc, PT_Symbols symbols) { PT_Symbol symbol; CC_Class *set; while(!PT_isSymbolsEmpty(symbols)) { symbol = PT_getSymbolsHead(symbols); symbols = PT_getSymbolsTail(symbols); set = get_first_set(symbol, ATtrue); CC_union(cc, set, cc); if (!CC_containsChar(set, CC_EPSILON)) { CC_removeChar(cc, CC_EPSILON); return; } } CC_addChar(cc, CC_EPSILON); }