@multila/multila-parser-generator
Version:
An LR(1) Parser Generator written in TypeScript
122 lines (121 loc) • 3.84 kB
TypeScript
import { Lexer } from '@multila/multila-lexer';
import { LexerToken } from '@multila/multila-lexer/lib/token';
import { LR1_Rule } from './lr1rule';
import { LR1_Table } from './lr1table';
/**
* Error that may be thrown while table-construction or parsing.
*/
export declare class LR1Error extends Error {
constructor(msg: string);
}
/**
* LR1 parser generator and parser.
*/
export declare class LR1 {
/**
* list of production rules
*/
private rules;
/**
* first set for each non-terminal of rules
*/
private first;
/**
* states created while table creation
*/
private states;
/**
* LR(1) table
*/
private table;
/**
* Callback functions that are automatically called after rule reduction (if
* provided). Parameter "terminals" contains tokens for all terminal items
* of a rule. The i-th element in list refers to the i-th terminal in the
* right-hand side of a rule. Non-terminals are skipped while indexing.
*/
private callBacks;
constructor();
/**
* Gets the set of production rules.
* @returns rules as list. The first rule represents the root rule.
*/
getRules(): LR1_Rule[];
/**
* Gets the first set, i.e. the FIRST-set of each non-terminal.
* @returns first set
*/
getFirst(): {
[id: string]: Set<string>;
};
/**
* Gets the generated parse table.
* @returns the parse table or null if it has not yet been generated
*/
getTable(): LR1_Table;
/**
* Add a callback method that is automatically called after reduction of a
* production rule while parsing.
* @param id identifier of the callback function
* @param f implementation of the callback function
*/
addCallback(id: string, f: (terminals: LexerToken[]) => void): void;
/**
* Parses production rules in the form "u = v1 v2 ... -> callback;",
* specified in a small DSL. Grammar is as follows:
* rules = { rule };
* rule = ID "=" rhs { "|" rhs } ";";
* rhs = { item } [ "->" ID ];
* item = "INT" | "REAL" | "HEX" | "ID" | "STR" | ID | STR;
* @param src rules defined in the DSL specified above
*/
parseRules(src: string): void;
private parseRule;
private parseRhs;
private parseItem;
/**
* Create a new production rule.
* @param lhs non-terminal id of the rule
* @returns a new rule object
*/
addRule(lhs: string): LR1_Rule;
/**
* Parse an input program according to the specified grammar.
* Note: Method createTable must be called first!
* @param lexer lexer instance with already pushed source code
* @param verbose print verbose output
* @returns this method may throw an LR1Error
*/
parse(lexer: Lexer, verbose?: boolean): void;
/**
* prints the current parse stack
* @param stack current parse stack
*/
private printParseStack;
/**
* Calculate parsing table.
* @returns parsing table
*/
calcTable(): LR1_Table;
/**
* Calculates the first-set for each non-terminal.
* The first set is the set of terminals that are parsed next:
* rule 'x = y ...;' -> first(x) = first(x) uu first(y);
* rule 'x = "z" ...;' -> first(x) = first(x) uu { "z" };
* with non-terminals x and y, as well as terminal "z";
* "uu" denotes the union operator.
*/
private calcFirst;
/**
* Adds a new state while the parsing table is created.
* @param sNew the state to be added
* @returns true, if the state has been added; false if an equal state is
* already present
*/
private addState;
/**
* Stringifies the current object.
* @returns stringified representation
*/
toString(): string;
}