Example #1
0
void
TestBitArray(FILE * fp)
{
    int idx, numbits;
    CcBitArray_t ba0, ba1, ba2;

    for (numbits = 0; numbits < 32; ++numbits) {
	COCO_ASSERT((CcBitArray(&ba0, numbits)));
	COCO_ASSERT((CcBitArray(&ba1, numbits)));
	ATest(fp, &ba0, &ba1);
	CcBitArray_Destruct(&ba0); CcBitArray_Destruct(&ba1);

	COCO_ASSERT((CcBitArray(&ba0, numbits))); CcBitArray_SetAll(&ba0, 1);
	COCO_ASSERT((CcBitArray(&ba1, numbits)));
	ATest(fp, &ba0, &ba1);
	CcBitArray_Destruct(&ba0); CcBitArray_Destruct(&ba1);

	COCO_ASSERT((CcBitArray(&ba0, numbits)));
	COCO_ASSERT((CcBitArray(&ba1, numbits))); CcBitArray_SetAll(&ba1, 1);
	ATest(fp, &ba0, &ba1);
	CcBitArray_Destruct(&ba0); CcBitArray_Destruct(&ba1);

	COCO_ASSERT((CcBitArray(&ba0, numbits))); CcBitArray_SetAll(&ba0, 1);
	COCO_ASSERT((CcBitArray(&ba1, numbits))); CcBitArray_SetAll(&ba1, 1);
	ATest(fp, &ba0, &ba1);
	CcBitArray_Destruct(&ba0); CcBitArray_Destruct(&ba1);

	for (idx = 0; idx < 128; ++idx) {
	    COCO_ASSERT((CcBitArray(&ba0, numbits))); CcBitArray_RandomSet(&ba0);
	    COCO_ASSERT((CcBitArray(&ba1, numbits))); CcBitArray_RandomSet(&ba1);
	    ATest(fp, &ba0, &ba1);

	    COCO_ASSERT((CcBitArray_Clone(&ba2, &ba0)));
	    CcBitArray_Subtract(&ba2, &ba1);

	    ATest(fp, &ba0, &ba2);
	    ATest(fp, &ba2, &ba0);
	    ATest(fp, &ba1, &ba2);
	    ATest(fp, &ba2, &ba1);

	    CcBitArray_Destruct(&ba0); CcBitArray_Destruct(&ba1); CcBitArray_Destruct(&ba2);
	}
    }
}
Example #2
0
static CcsBool_t
CcSyntax_NoCircularProductions(CcSyntax_t * self)
{
    CcBitArray_t singles;
    SymbolPair_t * firstPair, * prevPair, * curPair, * curPair0;
    CcSymbolNT_t * sym; CcArrayListIter_t iter; int index;
    CcsBool_t ok, changed, onLeftSide, onRightSide;
    CcSymbol_t * leftsym, * rightsym;
    CcSymbolTable_t * symtab = &self->globals->symtab;
    CcArrayList_t * ntarr = &symtab->nonterminals;

    firstPair = NULL;
    CcBitArray(&singles, ntarr->Count);
    for (sym = (CcSymbolNT_t *)CcArrayList_First(ntarr, &iter);
	 sym; sym = (CcSymbolNT_t *)CcArrayList_Next(ntarr, &iter)) {
	CcBitArray_SetAll(&singles, FALSE);
	/* Get nonterminals s such that sym-->s */
	GetSingles(&singles, sym->graph);
	for (index = 0; index < ntarr->Count; ++index) {
	    if (!CcBitArray_Get(&singles, index)) continue;
	    curPair = CcMalloc(sizeof(SymbolPair_t));
	    curPair->left = sym->base.kind;
	    curPair->right = index;
	    curPair->next = firstPair;
	    firstPair = curPair;
	}
    }
    CcBitArray_Destruct(&singles);

    do {
	changed = FALSE;
	prevPair = NULL; curPair = firstPair;
	while (curPair) {
	    onLeftSide = FALSE; onRightSide = FALSE;
	    for (curPair0 = curPair; curPair0; curPair0 = curPair0->next) {
		if (curPair->left == curPair0->right) onRightSide = TRUE;
		if (curPair->right == curPair0->left) onLeftSide = TRUE;
	    }
	    if (onLeftSide && onRightSide) { /* Circular Production found. */
		prevPair = curPair; curPair = curPair->next;
	    } else { /* Remove non-circular nonterminal symbol pair. */
		curPair0 = curPair->next;
		if (prevPair == NULL)  firstPair = curPair0;
		else  prevPair->next = curPair0;
		CcFree(curPair);
		curPair = curPair0;
		changed = TRUE;
	    }
	}
    } while (changed);

    ok = TRUE;
    for (curPair = firstPair; curPair; curPair = curPair0) {
	ok = FALSE;
	leftsym = (CcSymbol_t *)CcArrayList_Get(ntarr, curPair->left);
	rightsym = (CcSymbol_t *)CcArrayList_Get(ntarr, curPair->right);
	CcsErrorPool_Error(self->globals->errpool, NULL, " '%s' --> '%s'",
			   leftsym->name, rightsym->name);
	curPair0 = curPair->next;
	CcFree(curPair);
    }
    return ok;
}
static void
SCSOS_GenCode(CcCSharpBaseOutputScheme_t * self, CcOutput_t * output,
	      CcNode_t * p, const CcBitArray_t * IsChecked)
{
    int err; CcsBool_t equal, useSwitch; int index;
    CcNode_t * p2; CcBitArray_t s1, s2, isChecked;
    CcNodeNT_t * pnt; CcNodeT_t * pt; CcNodeWT_t * pwt;
    CcNodeSEM_t * psem; CcNodeSYNC_t * psync;
    CcSyntax_t * syntax = &self->base.globals->syntax;
    CcArrayList_t * terminals = &self->base.globals->symtab.terminals;

    CcBitArray_Clone(&isChecked, IsChecked);
    while (p != NULL) {
	if (p->base.type == node_nt) {
	    pnt = (CcNodeNT_t *)p;
	    if (pnt->pos) {
		CcPrintfIL(output, "%s(%s);", pnt->sym->name, pnt->pos->text);
	    } else {
		CcPrintfIL(output, "%s();", pnt->sym->name);
	    }
	} else if (p->base.type == node_t) {
	    pt = (CcNodeT_t *)p;
	    if (CcBitArray_Get(&isChecked, pt->sym->kind))
		CcPrintfIL(output, "Get();");
	    else
		CcPrintfIL(output, "Expect(%d);", pt->sym->kind);
	} else if (p->base.type == node_wt) {
	    pwt = (CcNodeWT_t *)p;
	    CcSyntax_Expected(syntax, &s1, p->next, self->curSy);
	    CcBitArray_Or(&s1, syntax->allSyncSets);
	    CcPrintfIL(output, "ExpectWeak(%d, %d);",
		       pwt->sym->kind, CcSyntaxSymSet_New(&self->symSet, &s1));
	    CcBitArray_Destruct(&s1);
	} else if (p->base.type == node_any) {
	    CcPrintfIL(output, "Get();");
	} else if (p->base.type == node_eps) {
	} else if (p->base.type == node_rslv) {
	} else if (p->base.type == node_sem) {
	    psem = (CcNodeSEM_t *)p;
	    CcSource(output, psem->pos);
	} else if (p->base.type == node_sync) {
	    psync = (CcNodeSYNC_t *)p;
	    err = CcSyntax_SyncError(syntax, self->curSy);
	    CcBitArray_Clone(&s1, psync->set);
	    SCSOS_GenCond(self, output, "while (!(", ")) {", &s1, p);
	    output->indent += 4;
	    CcPrintfIL(output, "SynErr(%d); Get();", err);
	    output->indent -= 4;
	    CcPrintfIL(output, "}");
	    CcBitArray_Destruct(&s1);
	} else if (p->base.type == node_alt) {
	    CcSyntax_First(syntax, &s1, p);
	    equal = CcBitArray_Equal(&s1, &isChecked);
	    CcBitArray_Destruct(&s1);
	    useSwitch = SCSOS_UseSwitch(self, p);
	    if (useSwitch)
		CcPrintfIL(output, "switch (la.kind) {");
	    p2 = p;
	    while (p2 != NULL) {
		CcSyntax_Expected(syntax, &s1, p2->sub, self->curSy);
		if (useSwitch) {
		    CcPrintfI(output, "");
		    for (index = 0; index < terminals->Count; ++index)
			if (CcBitArray_Get(&s1, index))
			    CcPrintf(output, "case %d: ", index);
		    CcPrintfL(output,"{");
		} else if (p2 == p) {
		    SCSOS_GenCond(self, output, "if (", ") {", &s1, p2->sub);
		} else if (p2->down == NULL && equal) {
		    CcPrintfIL(output, "} else {");
		} else {
		    SCSOS_GenCond(self, output,
				 "} else if (", ") {", &s1, p2->sub);
		}
		CcBitArray_Or(&s1, &isChecked);
		output->indent += 4;
		SCSOS_GenCode(self, output, p2->sub, &s1);
		if (useSwitch) CcPrintfIL(output, "break;");
		output->indent -= 4;
		if (useSwitch) CcPrintfIL(output, "}");
		p2 = p2->down;
		CcBitArray_Destruct(&s1);
	    }
	    if (equal) {
		CcPrintfIL(output, "}");
	    } else {
		err = CcSyntax_AltError(syntax, self->curSy);
		if (useSwitch) {
		    CcPrintfIL(output, "default: SynErr(%d); break;", err);
		    CcPrintfIL(output, "}");
		} else {
		    CcPrintfIL(output, "} else SynErr(%d);", err);
		}
	    }
	} else if (p->base.type == node_iter) {
	    p2 = p->sub;
	    if (p2->base.type == node_wt) {
		CcSyntax_Expected(syntax, &s1, p2->next, self->curSy);
		CcSyntax_Expected(syntax, &s2, p->next, self->curSy);
		CcPrintfIL(output,
			   "while (WeakSeparator(%d, %d, %d)) {",
			   ((CcNodeWT_t *)p2)->sym->kind,
			   CcSyntaxSymSet_New(&self->symSet, &s1),
			   CcSyntaxSymSet_New(&self->symSet, &s2));
		CcBitArray_Destruct(&s1); CcBitArray_Destruct(&s2);
		CcBitArray(&s1, terminals->Count);
		if (p2->up || p2->next == NULL) p2 = NULL; else p2 = p2->next;
	    } else {
		CcSyntax_First(syntax, &s1, p2);
		SCSOS_GenCond(self, output, "while (", ") {", &s1, p2);
	    }
	    output->indent += 4;
	    SCSOS_GenCode(self, output, p2, &s1);
	    output->indent -= 4;
	    CcPrintfIL(output, "}");
	    CcBitArray_Destruct(&s1);
	} else if (p->base.type == node_opt) {
	    CcSyntax_First(syntax, &s1, p->sub);
	    SCSOS_GenCond(self, output, "if (", ") {", &s1, p->sub);
	    output->indent += 4;
	    SCSOS_GenCode(self, output, p->sub, &s1);
	    output->indent -= 4;
	    CcPrintfIL(output, "}");
	    CcBitArray_Destruct(&s1);
	}
	if (p->base.type != node_eps && p->base.type != node_sem &&
	    p->base.type != node_sync)
	    CcBitArray_SetAll(&isChecked, FALSE);
	if (p->up) break;
	p = p->next;
    }
    CcBitArray_Destruct(&isChecked);
}