SDF_Production PTProductionToSDFProduction(PT_Production ptProduction) { PT_Symbols ptLhs; PT_Symbol ptRhs; PT_Attributes ptAttributes; SDF_Symbols sdfLhs; SDF_Symbol sdfRhs; SDF_Attributes sdfAttributes; ptLhs = PT_getProductionLhs(ptProduction); sdfLhs = PTSymbolsToSDFSymbols(ptLhs); ptRhs = PT_getProductionRhs(ptProduction); sdfRhs = PTSymbolToSDFSymbol(ptRhs); ptAttributes = PT_getProductionAttributes(ptProduction); sdfAttributes = PTAttributesToSDFAttributes(ptAttributes); return SDF_makeProductionProd(sdfLhs, SDF_makeLayoutSpace(), SDF_makeLayoutSpace(), sdfRhs, SDF_makeLayoutSpace(), sdfAttributes); }
/* 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; } } } }
ATbool PT_isProductionInjection(PT_Production prod) { PT_Symbols lhs = PT_getProductionLhs(prod); if (PT_getSymbolsLength(lhs) == 1) { PT_Symbol symbol = PT_getSymbolsHead(lhs); if (!PT_isSymbolLit(symbol)) { return ATtrue; } } return ATfalse; }
static int lengthOfProd(PT_Production prod) { PT_Symbol rhs = PT_getProductionRhs(prod); if (PT_isProductionList(prod)) { return lengthOfSymbol(rhs); } else { PT_Symbols lhs = PT_getProductionLhs(prod); PT_Attributes attrs = PT_getProductionAttributes(prod); return lengthOfSymbols(lhs) + 4 + lengthOfSymbol(rhs) + lengthOfAttributes(attrs); } }
ATbool PT_isProductionVariable(PT_Production prod) { /* This implements: "prod([varsym(<term>)],cf(<term>),<term>)" */ if (PT_isProductionDefault(prod)) { PT_Symbols lhs = PT_getProductionLhs(prod); PT_Symbol rhs = PT_getProductionRhs(prod); if ((PT_isSymbolCf(rhs) || PT_isSymbolLex(rhs)) && PT_hasSymbolsHead(lhs)) { PT_Symbol lhssym = PT_getSymbolsHead(lhs); PT_Symbols tail = PT_getSymbolsTail(lhs); return PT_isSymbolsEmpty(tail) && PT_isSymbolVarSym(lhssym); } return ATfalse; } return ATfalse; }
static PT_Tree pAppl(PT_Tree tree, int *i) { int indent = *i; PT_Args args = PT_getTreeArgs(tree); PT_Production prod = PT_getTreeProd(tree); SDF_Production sprod = (SDF_Production) ATtableGet(mapping, (ATerm) prod); if (sprod != NULL) { PT_Args newArgs = PT_makeArgsEmpty(); SDF_SymbolList slhs = SDF_getSymbolsList(SDF_getProductionSymbols(sprod)); PT_Symbols lhs = PT_getProductionLhs(prod); PT_Tree nextLayout = NULL; for (; !PT_isArgsEmpty(args); args = PT_getArgsTail(args), lhs = PT_getSymbolsTail(lhs)) { PT_Tree head = PT_getArgsHead(args); PT_Symbol sym = PT_getSymbolsHead(lhs); // here we put in the layout that's in between the symbols on the lhs if (PT_isOptLayoutSymbol(sym)) { assert(nextLayout != NULL); newArgs = PT_makeArgsList(createLayoutTree(indent, nextLayout), newArgs); *i = calcIndentationLevel(indent, nextLayout); nextLayout = NULL; } else { SDF_Symbol ssym = SDF_getSymbolListHead(slhs); PT_Symbol real = PT_getSymbolSymbol(sym); newArgs = PT_makeArgsList(pTreeWithSymbol(head, i, real, ssym), newArgs); if (SDF_hasSymbolListTail(slhs)) { nextLayout = (PT_Tree) SDF_getSymbolListWsAfterFirst(slhs); slhs = SDF_getSymbolListTail(slhs); } } } tree = PT_setTreeArgs(tree, PT_reverseArgs(newArgs)); } else { tree = PT_setTreeArgs(tree, pArgs(args, i)); } return tree; }
/* For each symbol on the left hand side of the given production rule, if the * symbol is a non-terminal initialise the symbol's first set to empty, * otherwise if the symbol is a terminal or the end-of-string symbol, * initialise the symbol's first set to the symbol. */ void init_prod_first(PT_Production prod) { PT_Symbols symbols; PT_Symbol symbol; CC_Class *set; CC_Class *newset; symbols = PT_getProductionLhs(prod); while(!PT_isSymbolsEmpty(symbols)) { symbol = PT_getSymbolsHead(symbols); set = get_first_set(symbol, ATtrue); newset = CC_getCharClass(symbol); /*assert(CC_isEmpty(set) || CC_isEqual(set, newset));*/ CC_copy(newset, set); symbols = PT_getSymbolsTail(symbols); } }
static int yieldProd(PT_Production prod, int idx, char *buf, int bufSize) { PT_Symbol rhs = PT_getProductionRhs(prod); if (PT_isProductionList(prod)) { idx = yieldSymbol(rhs, idx, buf, bufSize); } else { PT_Symbols lhs = PT_getProductionLhs(prod); PT_Attributes attrs = PT_getProductionAttributes(prod); idx = yieldSymbols(lhs, idx, buf, bufSize); buf[idx++] = '-'; buf[idx++] = '>'; buf[idx++] = ' '; idx = yieldSymbol(rhs, idx, buf, bufSize); idx = yieldAttributes(attrs, idx, buf, bufSize); } return idx; }
ATbool PT_isLexicalInjectionProd(PT_Production prod) { /* This implements: "prod([lex(<term>)],cf(<term>),<term>)" */ if (PT_isProductionDefault(prod)) { PT_Symbols lhs = PT_getProductionLhs(prod); PT_Symbol rhs = PT_getProductionRhs(prod); if (PT_isSymbolCf(rhs) && PT_hasSymbolsHead(lhs)) { PT_Symbol rhsNestedSymbol = PT_getSymbolSymbol(rhs); PT_Symbol lhssym = PT_getSymbolsHead(lhs); PT_Symbols tail = PT_getSymbolsTail(lhs); if (PT_isSymbolsEmpty(tail) && PT_isSymbolLex(lhssym)) { PT_Symbol lhsNestedSymbol = PT_getSymbolSymbol(lhssym); return PT_isEqualSymbol(rhsNestedSymbol, lhsNestedSymbol); } } return ATfalse; } return ATfalse; }
static PT_Production renameInProduction(PT_Production prod, PT_Symbol formalParam, PT_Symbol actualParam) { if (PT_isProductionDefault(prod)) { PT_Symbols lhs = PT_getProductionLhs(prod); PT_Symbol rhs = PT_getProductionRhs(prod); PT_Symbols newLhs = renameInSymbols(lhs, formalParam, actualParam); PT_Symbol newRhs = renameInSymbol(rhs, formalParam, actualParam); return PT_setProductionRhs(PT_setProductionLhs(prod, newLhs), newRhs); } else { PT_Symbol rhs = PT_getProductionRhs(prod); PT_Symbol newRhs = renameInSymbol(rhs, formalParam, actualParam); return PT_setProductionRhs(prod, newRhs); } }
static PT_Production normalizeProduction(PT_Production prod) { PT_Symbol layout = PT_makeOptLayoutSymbol(); if (!PT_isProductionList(prod)) { PT_Symbols lhs = PT_getProductionLhs(prod); PT_Symbols newLhs = PT_makeSymbolsEmpty(); PT_Symbol rhs = PT_getProductionRhs(prod); for(; !PT_isSymbolsEmpty(lhs); lhs = PT_getSymbolsTail(lhs)) { PT_Symbol head = PT_getSymbolsHead(lhs); PT_Symbol newHead; if (!PT_isSymbolLit(head)) { newHead = PT_makeSymbolCf(head); } else { newHead = head; } newLhs = PT_makeSymbolsList(newHead, newLhs); if (PT_hasSymbolsTail(PT_getSymbolsTail(lhs))) { newLhs = PT_makeSymbolsList(layout, newLhs); } } prod = PT_setProductionRhs(prod, PT_makeSymbolCf(rhs)); prod = PT_setProductionLhs(prod, PT_reverseSymbols(newLhs)); return prod; } else { return prod; } }