@fauton/cfg
Version:
A package to work with context free grammars and LL1 parsers
47 lines (46 loc) • 2.86 kB
TypeScript
import { IContextFreeGrammar, IContextFreeGrammarInput } from './types';
/**
*
* @param productionRuleEntries An array of tuples (production rule variable, production rule substitutions)
* **Example**: `["A", ["a b A", "b a b"]]`
* @param callBackFn A callback function that will be called for each production rule substitution
* @returns A tuple ([production rule variable, production rule substitution tokens, production rule substitution index ]) if a rule was found else null
*/
export declare function findSubstitution(productionRuleEntries: [string, string[]][], callBackFn: (tokens: string[]) => boolean): [string, string[], number] | null;
/**
* Returns the first production rule whose `tokens.length > 2`
* @param productionRuleEntries An array of tuples (production rule variable, production rule substitutions)
* **Example**: `["A", ["a b A", "b a b"]]`
* @returns A tuple ([production rule variable, production rule substitution tokens, production rule substitution index ]) if a long rule was found else null
*/
export declare function findLongSubstitution(productionRuleEntries: [string, string[]][]): [string, string[], number] | null;
/**
* Removes all the rules that have more than 2 tokens and add them as new production rules
* Which will result in all substitutions to have chunk length of at-most 2
* @param cfg production rules record and variables array of cfg
*/
export declare function processLongSubstitutions(cfg: Pick<IContextFreeGrammar, 'productionRules' | 'variables'>): void;
/**
* Returns the first production rule whose `tokens.length === 2`
* @param productionRuleEntries An array of tuples (production rule variable, production rule substitutions)
* **Example**: `["A", ["a b A", "b a b"]]`
* @returns A tuple ([production rule variable, production rule substitution tokens, production rule substitution index ]) if a substitution was found else null
*/
export declare function findSubstitutionOfLengthTwo(productionRuleEntries: Array<[string, string[]]>, variables: string[]): [string, string[], number] | null;
/**
* Processes all substitutions of chunk length two
* @param cfg Production rules record, variables and terminals array of cfg
*/
export declare function processSubstitutionsOfLengthTwo(cfg: Pick<IContextFreeGrammar, 'productionRules' | 'variables' | 'terminals'>): void;
/**
* Check if a variable references itself in its production rule
* @param rules Production rules for the variable
* @param variable Producing variable
*/
export declare function checkForSelfReference(rules: string[], variable: string): boolean;
/**
* Converts a cfg to cnf
* @param cfg Input cfg to convert to cnf
* @returns Resultant cfg converted to cnf
*/
export declare function convertToCnf(inputCfg: IContextFreeGrammarInput): IContextFreeGrammar;