Ejemplo n.º 1
0
/*------------- check if resolvers are legal  --------------------*/
static void
CcSyntax_CheckRes(CcSyntax_t * self, CcNode_t * p, CcsBool_t rslvAllowed)
{
    CcNode_t * q;
    CcBitArray_t expected, soFar, fs, fsNext;
    CcSymbolTable_t * symtab = &self->globals->symtab;

    while (p != NULL) {
	if (p->base.type == node_alt) {
	    CcBitArray(&expected, symtab->terminals.Count);
	    for (q = p; q != NULL; q = q->down) {
		CcSyntax_Expected0(self, &fs, q->sub, self->curSy);
		CcBitArray_Or(&expected, &fs);
		CcBitArray_Destruct(&fs);
	    }
	    CcBitArray(&soFar, symtab->terminals.Count);
	    for (q = p; q != NULL; q = q->down) {
		if (q->sub->base.type == node_rslv) {
		    CcSyntax_Expected(self, &fs, q->sub->next, self->curSy);
		    if (CcBitArray_Intersect(&fs, &soFar))
			CcsErrorPool_Warning(self->globals->errpool, NULL,
					     "Resolver will never be evaluated. "
					     "Place it at previous conflicting alternative.");
		    if (!CcBitArray_Intersect(&fs, &expected))
			CcsErrorPool_Warning(self->globals->errpool, NULL,
					     "Misplaced resolver: no LL(1) conflict.");
		    CcBitArray_Destruct(&fs);
		} else {
		    CcSyntax_Expected(self, &fs, q->sub, self->curSy);
		    CcBitArray_Or(&soFar, &fs);
		    CcBitArray_Destruct(&fs);
		}
		CcSyntax_CheckRes(self, q->sub, TRUE);
	    }
	    CcBitArray_Destruct(&expected); CcBitArray_Destruct(&soFar);
	} else if (p->base.type == node_iter || p->base.type == node_opt) {
	    if (p->sub->base.type == node_rslv) {
		CcSyntax_First(self, &fs, p->sub->next);
		CcSyntax_Expected(self, &fsNext, p->next, self->curSy);
		if (!CcBitArray_Intersect(&fs, &fsNext))
		    CcsErrorPool_Warning(self->globals->errpool, NULL,
					 "Misplaced resolver: no LL(1) conflict.");
	    }
	    CcSyntax_CheckRes(self, p->sub, TRUE);
	} else if (p->base.type == node_rslv) {
	    if (!rslvAllowed)
		CcsErrorPool_Warning(self->globals->errpool, NULL,
				     "Misplaced resolver: no alternative.");
	}
	if (p->up) break;
	p = p->next;
	rslvAllowed = FALSE;
    }
}
Ejemplo n.º 2
0
static CcsBool_t
SCSOS_UseSwitch(CcCSharpBaseOutputScheme_t * self, CcNode_t * p)
{
    CcBitArray_t s1, s2; int nAlts;
    CcSyntax_t * syntax = &self->base.globals->syntax;
    CcArrayList_t * terminals = &self->base.globals->symtab.terminals;

    if (p->base.type != node_alt) return FALSE;
    nAlts = 0;
    CcBitArray(&s1, terminals->Count);
    while (p != NULL) {
	CcSyntax_Expected0(syntax, &s2, p->sub, self->curSy);
	if (CcBitArray_Intersect(&s1, &s2)) goto falsequit2;
	CcBitArray_Or(&s1, &s2);
	CcBitArray_Destruct(&s2);
	++nAlts;
	if (p->sub->base.type == node_rslv) goto falsequit1;
	p = p->down;
    }
    CcBitArray_Destruct(&s1);
    return nAlts > 5;
 falsequit2:
    CcBitArray_Destruct(&s2);
 falsequit1:
    CcBitArray_Destruct(&s1);
    return FALSE;
}
Ejemplo n.º 3
0
void
CcSyntax_Expected(CcSyntax_t * self, CcBitArray_t * ret,
		  CcNode_t * p, const CcSymbol_t * curSy)
{
    CcSyntax_First(self, ret, p);
    if (CcNode_DelGraph(p))
	CcBitArray_Or(ret, ((const CcSymbolNT_t *)curSy)->follow);
}
Ejemplo n.º 4
0
static void
CcSyntax_FindAS(CcSyntax_t * self, CcNode_t * p)
{
    CcNode_t * a, * q;
    CcBitArray_t s0, s1;
    CcSymbolTable_t * symtab = &self->globals->symtab;

    while (p != NULL) {
	if (p->base.type == node_opt || p->base.type == node_iter) {
	    CcSyntax_FindAS(self, p->sub);
	    a = CcSyntax_LeadingAny(self, p->sub);
	    if (a != NULL) {
		CcSyntax_First(self, &s0, p->next);
		CcBitArray_Subtract(((CcNodeANY_t *)a)->set, &s0);
		CcBitArray_Destruct(&s0);
	    }
	} else if (p->base.type == node_alt) {
	    CcBitArray(&s1, symtab->terminals.Count);
	    q = p;
	    while (q != NULL) {
		CcSyntax_FindAS(self, q->sub);
		a = CcSyntax_LeadingAny(self, q->sub);
		if (a != NULL) {
		    CcSyntax_First(self, &s0, q->down);
		    CcBitArray_Or(&s0, &s1);
		    CcBitArray_Subtract(((CcNodeANY_t *)a)->set, &s0);
		    CcBitArray_Destruct(&s0);
		} else {
		    CcSyntax_First(self, &s0, q->sub);
		    CcBitArray_Or(&s1, &s0);
		    CcBitArray_Destruct(&s0);
		}
		q = q->down;
	    }
	    CcBitArray_Destruct(&s1);
	}
	if (p->up) break;
	p = p->next;
    }
}
Ejemplo n.º 5
0
static void
ATest(FILE * fp, CcBitArray_t * ba0, CcBitArray_t * ba1)
{
    CcBitArray_t ba2;
    int idx, cnt, numbits = CcBitArray_getCount(ba0);

    CcBitArray_Dump(ba0, fp, "BitArray 0: ", "\n");
    CcBitArray_Dump(ba1, fp, "BitArray 1: ", "\n");

    cnt = 0;
    for (idx = 0; idx < numbits; ++idx)
	if (CcBitArray_Get(ba0, idx) && CcBitArray_Get(ba1, idx)) cnt = 1;
    COCO_ASSERT((CcBitArray_Intersect(ba0, ba1) == cnt));

    COCO_ASSERT((CcBitArray_Clone(&ba2, ba0)));
    COCO_ASSERT((CcBitArray_Equal(ba0, &ba2)));
    cnt = 0;
    for (idx = 0; idx < numbits; ++idx) {
	COCO_ASSERT(CcBitArray_Get(ba0, idx) == CcBitArray_Get(&ba2, idx));
	if (CcBitArray_Get(ba0, idx)) ++cnt;
    }
    COCO_ASSERT((CcBitArray_Elements(ba0) == cnt));

    COCO_ASSERT((CcBitArray_Or(&ba2, ba1) == 0));
    CcBitArray_Dump(&ba2, fp, "Or: ", "\n");
    for (idx = 0; idx < numbits; ++idx)
	COCO_ASSERT((CcBitArray_Get(ba0, idx) || CcBitArray_Get(ba1, idx)) == CcBitArray_Get(&ba2, idx));
    CcBitArray_Destruct(&ba2);

    COCO_ASSERT((CcBitArray_Clone(&ba2, ba0)));
    COCO_ASSERT((CcBitArray_And(&ba2, ba1) == 0));
    CcBitArray_Dump(&ba2, fp, "And: ", "\n");
    for (idx = 0; idx < numbits; ++idx)
	COCO_ASSERT((CcBitArray_Get(ba0, idx) && CcBitArray_Get(ba1, idx)) == CcBitArray_Get(&ba2, idx));
    CcBitArray_Destruct(&ba2);

    COCO_ASSERT((CcBitArray_Clone(&ba2, ba0)));
    CcBitArray_Subtract(&ba2, ba1);
    CcBitArray_Dump(&ba2, fp, "Subtract: ", "\n");
    for (idx = 0; idx < numbits; ++idx)
	COCO_ASSERT((CcBitArray_Get(ba0, idx) && !CcBitArray_Get(ba1, idx)) == CcBitArray_Get(&ba2, idx));
    COCO_ASSERT(!CcBitArray_Intersect(ba1, &ba2));
    CcBitArray_Destruct(&ba2);

    fprintf(fp, "\n");
}
Ejemplo n.º 6
0
static void
CcSyntax_Complete(CcSyntax_t * self, CcSymbolNT_t * sym)
{
    CcSymbolNT_t * s; CcArrayListIter_t iter;
    CcSymbolTable_t * symtab = &self->globals->symtab;
    CcArrayList_t * ntarr = &symtab->nonterminals;

    if (CcBitArray_Get(self->visited, sym->base.kind)) return;
    CcBitArray_Set(self->visited, sym->base.kind, TRUE);
    for (s = (CcSymbolNT_t *)CcArrayList_First(ntarr, &iter);
	 s; s = (CcSymbolNT_t *)CcArrayList_Next(ntarr, &iter)) {
	if (CcBitArray_Get(sym->nts, s->base.kind)) {
	    CcSyntax_Complete(self, s);
	    CcBitArray_Or(sym->follow, s->follow);
	    if ((CcSymbol_t *)sym == self->curSy)
		CcBitArray_Set(sym->nts, s->base.kind, FALSE);
	}
    }
}
Ejemplo n.º 7
0
/* FIX ME */
static void
CcSyntax_CheckAlts(CcSyntax_t * self, CcNode_t * p)
{
    CcBitArray_t s1, s2; CcNode_t * q;
    CcSymbolTable_t * symtab = &self->globals->symtab;

    while (p != NULL) {
	if (p->base.type == node_alt) {
	    q = p;
	    CcBitArray(&s1, symtab->terminals.Count);
	    while (q != NULL) { /* For all alternatives */
		CcSyntax_Expected0(self, &s2, q->sub, self->curSy);
		CcSyntax_CheckOverlap(self, &s1, &s2, 1);
		CcBitArray_Or(&s1, &s2);
		CcSyntax_CheckAlts(self, q->sub);
		q = q->down;
		CcBitArray_Destruct(&s2);
	    }
	    CcBitArray_Destruct(&s1);
	} else if (p->base.type == node_opt || p->base.type == node_iter) {
	    /* E.g. [[...]] */
	    if (CcNode_DelSubGraph(p->sub)) {
		CcSyntax_LL1Error(self, 4, NULL);
	    } else {
		CcSyntax_Expected0(self, &s1, p->sub, self->curSy);
		CcSyntax_Expected(self, &s2, p->next, self->curSy);
		CcSyntax_CheckOverlap(self, &s1, &s2, 2);
		CcSyntax_CheckAlts(self, p->sub);
		CcBitArray_Destruct(&s1);
		CcBitArray_Destruct(&s2);
	    }
	} else if (p->base.type == node_any) {
	    /* E.g. {ANY} ANY or [ANY] ANY */
	    if (CcBitArray_Elements(((CcNodeANY_t *)p)->set) == 0)
		CcSyntax_LL1Error(self, 3, NULL);
	}
	if (p->up) break;
	p = p->next;
    }
}
Ejemplo n.º 8
0
static void
CcSyntax_First0(CcSyntax_t * self, CcBitArray_t * ret,
		CcNode_t * p, CcBitArray_t * mark)
{
    CcBitArray_t fs0;
    CcSymbolTable_t * symtab = &self->globals->symtab;

    CcBitArray(ret, symtab->terminals.Count);
    while (p != NULL && !CcBitArray_Get(mark, p->base.index)) {
	CcBitArray_Set(mark, p->base.index, TRUE);
	if (p->base.type == node_nt) {
	    CcNodeNT_t * p0 = (CcNodeNT_t *)p;
	    CcSymbolNT_t * sym = (CcSymbolNT_t *)p0->sym;
	    if (sym->firstReady) {
		CcBitArray_Or(ret, sym->first);
	    } else {
		CcSyntax_First0(self, &fs0, sym->graph, mark);
		CcBitArray_Or(ret, &fs0);
		CcBitArray_Destruct(&fs0);
	    }
	} else if (p->base.type == node_t) {
	    CcNodeT_t * p0 = (CcNodeT_t *)p;
	    CcBitArray_Set(ret, p0->sym->kind, TRUE);
	} else if (p->base.type == node_wt) {
	    CcNodeWT_t * p0 = (CcNodeWT_t *)p;
	    CcBitArray_Set(ret, p0->sym->kind, TRUE);
	} else if (p->base.type == node_any) {
	    CcNodeANY_t * p0 = (CcNodeANY_t *)p;
	    CcBitArray_Or(ret, p0->set);
	} else if (p->base.type == node_alt) {
	    CcSyntax_First0(self, &fs0, p->sub, mark);
	    CcBitArray_Or(ret, &fs0);
	    CcBitArray_Destruct(&fs0);
	    CcSyntax_First0(self, &fs0, p->down, mark);
	    CcBitArray_Or(ret, &fs0);
	    CcBitArray_Destruct(&fs0);
	} else if (p->base.type == node_iter || p->base.type == node_opt) {
	    CcSyntax_First0(self, &fs0, p->sub, mark);
	    CcBitArray_Or(ret, &fs0);
	    CcBitArray_Destruct(&fs0);
	}
	if (!CcNode_DelNode(p)) break;
	p = p->next;
    }
}
Ejemplo n.º 9
0
static void
CcSyntax_CompSync(CcSyntax_t * self, CcNode_t * p)
{
    CcNodeSYNC_t * syncp;

    while (p != NULL && !CcBitArray_Get(self->visited, p->base.index)) {
	CcBitArray_Set(self->visited, p->base.index, TRUE);
	if (p->base.type == node_sync) {
	    syncp = (CcNodeSYNC_t *)p;
	    CcsAssert(syncp->set == NULL);
	    CcSyntax_Expected(self, &syncp->setSpace, p->next, self->curSy);
	    syncp->set = &syncp->setSpace;
	    CcBitArray_Set(syncp->set, self->eofSy->kind, TRUE);
	    CcBitArray_Or(self->allSyncSets, syncp->set);
	} else if (p->base.type == node_alt) {
	    CcSyntax_CompSync(self, p->sub);
	    CcSyntax_CompSync(self, p->down);
	} else if (p->base.type == node_opt || p->base.type == node_iter) {
	    CcSyntax_CompSync(self, p->sub);
	}
	p = p->next;
    }
}
Ejemplo n.º 10
0
static void
CcSyntax_CompFollow(CcSyntax_t * self, CcNode_t * p)
{
    CcBitArray_t s;
    CcSymbolNT_t * sym;

    while (p != NULL && !CcBitArray_Get(self->visited, p->base.index)) {
	CcBitArray_Set(self->visited, p->base.index, TRUE);
	if (p->base.type == node_nt) {
	    CcSyntax_First(self, &s, p->next);
	    sym = (CcSymbolNT_t *)((CcNodeNT_t *)p)->sym;
	    CcBitArray_Or(sym->follow, &s);
	    CcBitArray_Destruct(&s);
	    if (CcNode_DelGraph(p->next))
		CcBitArray_Set(sym->nts, self->curSy->kind, TRUE);
	} else if (p->base.type == node_opt || p->base.type == node_iter) {
	    CcSyntax_CompFollow(self, p->sub);
	} else if (p->base.type == node_alt) {
	    CcSyntax_CompFollow(self, p->sub);
	    CcSyntax_CompFollow(self, p->down);
	}
	p = p->next;
    }
}
Ejemplo n.º 11
0
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);
}