// Finds the set of frontier nodes.  The definition of a frontier node differs
// from Galley et al's (2004) in the following ways:
//
// 1. A node with an empty span is not a frontier node (this excludes
//    unaligned target subtrees).
// 2. Target word nodes are not frontier nodes.
// 3. Source word nodes are not frontier nodes.
// 4. Unless the --AllowUnary option is used, a node is not a frontier node if
//    it has the same span as its parent.
void AlignmentGraph::ComputeFrontierSet(Node *root,
                                        const Options &options,
                                        std::set<Node *> &frontierSet) const
{
  // Don't include word nodes or unaligned target subtrees.
  if (root->GetType() != TREE || root->GetSpan().empty()) {
    return;
  }

  if (!SpansIntersect(root->GetComplementSpan(), Closure(root->GetSpan()))) {
    // Unless unary rules are explicitly allowed, we use Chung et al's (2011)
    // modified defintion of a frontier node to eliminate the production of
    // non-lexical unary rules.
    assert(root->GetParents().size() <= 1);
    if (options.allowUnary
        || root->GetParents().empty()
        || root->GetParents()[0]->GetSpan() != root->GetSpan()) {
      frontierSet.insert(root);
    }
  }

  const std::vector<Node *> &children = root->GetChildren();
  for (std::vector<Node *>::const_iterator p(children.begin());
       p != children.end(); ++p) {
    ComputeFrontierSet(*p, options, frontierSet);
  }
}
Beispiel #2
0
/*
function goto(I, X);
begin
    let J be the set of items [A -> aX*b, a] such that
        [A -> a*Xb, a] is in I;
    return closure(J)
end;
*/
LR_ITEM_SET* Goto(LR_ITEM_SET* I, int X, GRAMMAR_TABLE* G)
{
    // J := {};
    LR_ITEM_SET* J = NULL;
    
    // for each item A -> a*Xb in I
    LR_ITEM_SET* itr;
    for (itr = I; itr; itr = itr->next)
    {
        LR_ITEM item = itr->item;
        int B = G->rules[item.production].rhs[item.dot];
        if (B == X)
        {
            // add A -> aX*b to J
            LR_ITEM_SET* add = malloc(sizeof(LR_ITEM_SET));
            add->item = item;
            add->item.dot++;
            add->next = J;
            J = add;
        }
    }
    
    // return closure(J)
    LR_ITEM_SET* K = Closure(J, G);
    FreeItemSet(J);
    return K;
}
Beispiel #3
0
/*
 * This routine is called by the domain manager to complete initialisation
 * of the first domain.
 */
void Binder_Done(DomainMgr_st *dm_st)
{
    PerDomain_st *bst = ROP_TO_PDS(dm_st->dm_dom);
    Closure_clp cl;

    bst->callback = dm_st->bcb;
    TRC(eprintf ("Binder_Done: bst=%x callback=%x.\n", bst, bst->callback));
    cl = Entry$Closure (Pvs(entry));
    Threads$Fork (Pvs(thds), cl->op->Apply, cl, 0);
    Threads$Fork (Pvs(thds), cl->op->Apply, cl, 0);
#if 0
    Threads$Fork (Pvs(thds), cl->op->Apply, cl, 0);
#endif /* 0 */
    TRC(eprintf ("Binder_Done: forked server thread\n"));
}
Beispiel #4
0
void ScfgRule::PushSourceLabel(const SyntaxNodeCollection *sourceNodeCollection,
                               const Node *node,
                               const std::string &nonMatchingLabel)
{
  ContiguousSpan span = Closure(node->GetSpan());
  if (sourceNodeCollection->HasNode(span.first,span.second)) { // does a source constituent match the span?
    std::vector<SyntaxNode*> sourceLabels =
      sourceNodeCollection->GetNodes(span.first,span.second);
    if (!sourceLabels.empty()) {
      // store the topmost matching label from the source syntax tree
      m_sourceLabels.push_back(sourceLabels.back()->label);
    }
  } else {
    // no matching source-side syntactic constituent: store nonMatchingLabel
    m_sourceLabels.push_back(nonMatchingLabel);
  }
}
// generates the got state for this symbol
dParserCompiler::dState* dParserCompiler::Goto (
	const dState* const state, 
	dCRCTYPE symbol, 
	const dTree<dTokenInfo, dCRCTYPE>& symbolList,
	const dTree<dList<void*>, dCRCTYPE>& ruleMap) const
{
	dList<dItem> itemSet;

	// iterate over each item contained on state
	for (dState::dListNode* node = state->GetFirst(); node; node = node->GetNext()) {
		const dItem& item = node->GetInfo();

		int index = 0;
		const dRuleInfo& ruleInfo = item.m_ruleNode->GetInfo();
		// for this item get it rule, and iterate over ever rule symbol.  		
		for (dRuleInfo::dListNode* infoSymbolNode = ruleInfo.GetFirst(); infoSymbolNode; infoSymbolNode = infoSymbolNode->GetNext()) {
			// see if this rule symbol match symbol
			const dSymbol& infoSymbol = infoSymbolNode->GetInfo();
			if (infoSymbol.m_nameCRC == symbol) {
				// a symbol was found, now see if it is at the right position in the rule
				if (index == item.m_indexMarker) {
					// the rule match symbol at the right position, add this rule to the new item set.
					dItem& newItem = itemSet.Append()->GetInfo();
					newItem.m_indexMarker = index + 1;
					newItem.m_ruleNode = item.m_ruleNode;
					newItem.m_lookAheadSymbolCRC = item.m_lookAheadSymbolCRC;
					newItem.m_lookAheadSymbolName = item.m_lookAheadSymbolName;
					break;
				}
			}
			index ++;
		}
	}

	//find the closure for this new item set.
	dState* const closureState = Closure (itemSet, symbolList, ruleMap);
	return closureState;
}
Beispiel #6
0
ConsoleControl_clp InitConsole(Wr_clp *console)
{
    struct Console_st *console_st;

    console_st = Heap$Malloc(Pvs(heap), sizeof(*console_st));

    CL_INIT(console_st->cl, &wr_op, console_st);
    CL_INIT(console_st->control_cl, &control_op, console_st);

    MU_INIT(&console_st->mu);

    LINK_INIT(&console_st->outputs);

    /* Chuck another thread into Pvs(entry) in case we need to do
       IDC to our own domain (serial driver, for example) */
    {
	Closure_clp c=Entry$Closure(Pvs(entry));
	Threads$Fork(Pvs(thds), c->op->Apply, c, 0);
    }

    *console=&console_st->cl;
    return &console_st->control_cl;
}
// generates the canonical Items set for a LR(1) grammar
void dParserCompiler::CanonicalItemSets (
	dTree<dState*, dCRCTYPE>& stateMap, 
	const dProductionRule& ruleList, 
	const dTree<dTokenInfo, dCRCTYPE>& symbolList, 
	const dOperatorsPrecedence& operatorPrecedence,
	FILE* const debugFile)
{
	dList<dItem> itemSet;
	dList<dState*> stateList;

	// start by building an item set with only the first rule
	dItem& item = itemSet.Append()->GetInfo();
	item.m_indexMarker = 0;
	item.m_lookAheadSymbolCRC = dCRC64 (DACCEPT_SYMBOL);
	item.m_lookAheadSymbolName = DACCEPT_SYMBOL;
	item.m_ruleNode = ruleList.GetFirst();

	// build a rule info map
	dTree<dList<void*>, dCRCTYPE> ruleMap;
	for (dProductionRule::dListNode* ruleNode = ruleList.GetFirst(); ruleNode; ruleNode = ruleNode->GetNext()) {
		dRuleInfo& info = ruleNode->GetInfo();

		dTree<dList<void*>, dCRCTYPE>::dTreeNode* node = ruleMap.Find(info.m_nameCRC);
		if (!node) {
			node = ruleMap.Insert(info.m_nameCRC);
		}
		dList<void*>& entry = node->GetInfo();
		entry.Append(ruleNode);
	}


	// find the closure for the first this item set with only the first rule
	dState* const state = Closure (itemSet, symbolList, ruleMap);
	operatorPrecedence.SaveLastOperationSymbol (state);

	stateMap.Insert(state, state->GetKey());
	stateList.Append(state);

	state->Trace(debugFile);

	// now for each state found 
	int stateNumber = 1;
	for (dList<dState*>::dListNode* node = stateList.GetFirst(); node; node = node->GetNext()) {
		dState* const state = node->GetInfo();

		dTree<dTokenInfo, dCRCTYPE>::Iterator iter (symbolList);
		for (iter.Begin(); iter; iter ++) {

			dCRCTYPE symbol = iter.GetKey();
			dState* const newState = Goto (state, symbol, symbolList, ruleMap);

			if (newState->GetCount()) {
				const dTokenInfo& tokenInfo = iter.GetNode()->GetInfo();
				dTransition& transition = state->m_transitions.Append()->GetInfo();
				transition.m_symbol = symbol;
				transition.m_name = tokenInfo.m_name; 
				transition.m_type = tokenInfo.m_type;
				dAssert (transition.m_symbol == dCRC64(transition.m_name.GetStr()));
				transition.m_targetState = newState;

				dTree<dState*, dCRCTYPE>::dTreeNode* const targetStateNode = stateMap.Find(newState->GetKey());
				if (!targetStateNode) {
					newState->m_number = stateNumber;

					stateNumber ++;
					stateMap.Insert(newState, newState->GetKey());
					newState->Trace(debugFile);
					stateList.Append(newState);

					operatorPrecedence.SaveLastOperationSymbol (newState);

				} else {
					transition.m_targetState = targetStateNode->GetInfo();
					delete newState;
				}
			} else {
				delete newState;
			}
		}

		dTrace (("state#:%d   items: %d   transitions: %d\n", state->m_number, state->GetCount(), state->m_transitions.GetCount()));
	}
}
Beispiel #8
0
/*
// LR(1) canonical collection of sets
procedure items(G');
begin
    C := {closure({[S' -> *S, $]})};
    repeat
        for each set of items I in C and each grammar symbol X
            such that goto(I, X) is not empty and not in C do
                add goto(I, X) to C
    until no more sets of items can be added to C
end
*/
LR_ITEM_COLLECTION* CanonicalCollection(GRAMMAR_TABLE* G)
{
    // C := {closure({[S' -> *S, $]})};
    LR_ITEM_COLLECTION* C = malloc(sizeof(LR_ITEM_COLLECTION));
    C->set = malloc(sizeof(LR_ITEM_SET));
    C->set->item.production = 0;
    C->set->item.dot = 0;
    C->set->item.lookahead = gSymbolEOF;
    C->set->next = NULL;
    C->next = NULL;
    
    LR_ITEM_SET* closure = Closure(C->set, G);
    FreeItemSet(C->set);
    C->set = closure;
    
    // repeat
    int count = 0;
    int finished;
    do {
        finished = 1;
        // for each set of items I in C
        LR_ITEM_COLLECTION* itr;
        for (itr = C; itr; itr = itr->next)
        {
            LR_ITEM_SET* I = itr->set;
            // and each grammar symbol X
            int i;
            for (i = 0; i < G->numSymbols + G->numTokens; i++)
            {
                int X = (i < G->numSymbols) ? K_SYMBOL + i + 1 :
                    K_TOKEN + (i - G->numSymbols) + 1;
                    
                LR_ITEM_SET* gotoSet = Goto(I, X, G);
                
                // such that goto(I, X) is not empty
                if (gotoSet == NULL) continue;

                // and not in C
                LR_ITEM_COLLECTION* find;
                for (find = C; find; find = find->next)
                {
                    if (CompareItemSets(find->set, gotoSet) == 0)
                    {
                        FreeItemSet(gotoSet);
                        gotoSet = NULL;
                        break;
                    }
                    else if (find->next == NULL)
                    {
                        // add goto(I, X) to C
                        find->next = malloc(sizeof(LR_ITEM_COLLECTION));
                        find = find->next;
                        find->next = NULL;
                        find->set = gotoSet;
                        finished = 0;
                        
                        // print for debugging
                        printf("I(%i);\n", count++);
                        //PrintItemSet(gotoSet, G);
                        //printf("\n");
                    }
                }
            }
        }
    } while (!finished);
    // until no more sets of items can be added to C
    
    return C;
}
Beispiel #9
0
group* Carttype(matrix* m)
{ group* type=mkgroup(s=Ssrank(grp)); /* rank bounds number of components */
  Closure(m,false,type); return type;
}