Skip to content

OpenReasoning/parser.js

Repository files navigation

parser.js

Test codecov NPM Version

A parser to be used in logic programs.

This parses formulas within three styles (prefix, infix, and functional) returning a tree structure of the captured elements.

Installation

npm install @openreasoning/parser

Usage

import {prefixParser, infixParser, functionalParser, Node} from '@openreasoning/parser'

let parsed = prefixParser('(and a b)');
let parsed2 = infixParser('(a and b)');
let parsed3 = functionalParser('and(a, b)');

function isEqual(a: Node, b: Node): bool {
    if (a.value !== b.value || a.children.length !== b.children.length) {
        return false;
    }
    for (let i = 0; i < a.children.length; i++) {
        if (!isEqual(a.children[i], b.children[i])) {
            return false;
        }
    }
    return true;
}

console.log(isEqual(parsed, parsed2)); // true
console.log(isEqual(parsed2, parsed3)); // true

License

This is uses the MIT License