A user should be able to define a grammar
The user should then be able to define a parser
Finally, the user should be able to define a semantic analyzer that takes all the valid syntax trees for
Tree data structure. A constituent consists of a label and an array of child constituents.
Say we define a grammar with the following production rules:
These production rules can be rewritten as an array of 2-tuples:
let production_rules: Array<[string, string]> = [['S', 'NP VP'], ['NP', 'Det N\''], ['VP', 'V_i']];
This set of production rules can then be fed to the Grammar class which produces a hashmap with the right-hand side of the production rules as map keys and the left-hand side of the production rules as values.
Assuming the production rules that were defined above are being used, the relevant starting symbol would be 'S'
. To define a grammar that uses the above production rules and the starting symbol 'S'
:
let G = Grammar(production_rules, 'S');
Takes a string and lexes it into an array of Constituent
element or an array of Constituent
nodes, as well as a Grammar
.
// create a new state using a string
const s = new State('the cat meows', grammar);
// or create a new state using an array of Constituents
const constituents = [new Constituent('the'), new Constituent('cat'), new Constituent('meows')];
const s = new State(constituents, grammar);
On creation, builds an array of all valid production rules that can be applied to it. Used to build the parser's search tree.