@fauton/cfg
Version:
A package to work with context free grammars and LL1 parsers
25 lines (24 loc) • 1.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeProductionRules = void 0;
const setOperations_1 = require("./setOperations");
function removeProductionRules(cfg) {
const { productionRules, removedVariables, variables } = cfg;
// Remove production variables from the production rules record
removedVariables.forEach((removedVariable) => {
delete productionRules[removedVariable];
});
const removedVariablesSet = new Set(removedVariables);
// Now we need to remove all the production rules that references any removed production variables
Object.entries(productionRules).forEach(([productionVariable, productionRulesSubstitutions]) => {
productionRules[productionVariable] = productionRulesSubstitutions.filter((productionRulesSubstitution) => {
// productionRulesSubstitution = Verb Adj Noun
const productionRulesSubstitutionTokensSet = new Set(productionRulesSubstitution.split(' '));
const difference = (0, setOperations_1.setDifference)(productionRulesSubstitutionTokensSet, removedVariablesSet);
// If we dont need to remove any variables then the size before and after the set difference would be similar
return difference.size === productionRulesSubstitutionTokensSet.size;
});
});
return Array.from((0, setOperations_1.setDifference)(new Set(variables), new Set(removedVariables)));
}
exports.removeProductionRules = removeProductionRules;