Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save primaryobjects/1d2f7ee668b62ca99095 to your computer and use it in GitHub Desktop.
Save primaryobjects/1d2f7ee668b62ca99095 to your computer and use it in GitHub Desktop.
Javascript parser for STRIPS PDDL grammar parser from pegjs.org. This code parses the result of the pegjs grammar produced from the file grammar.txt https://gist.github.com/primaryobjects/22363e71112d716ea183 and returns a JSON object with the action operators, conditions, and effects.
/*
grammar.txt: saved from https://gist.github.com/primaryobjects/22363e71112d716ea183
domain.txt:
(define (domain random-domain)
(:requirements :strips :typing)
(:action op1
:parameters (?x1 ?x2 ?x3)
:precondition (and (S ?x1 ?x2) (R ?x3 ?x1))
:effect (and (S ?x2 ?x1) (S ?x1 ?x3) (not (R ?x3 ?x1))))
(:action op2
:parameters (?x1 ?x2 ?x3)
:precondition (and (S ?x3 ?x1) (R ?x2 ?x2))
:effect (and (S ?x1 ?x3) (not (S ?x3 ?x1)))))
*/
var fs = require('fs');
var PEG = require("pegjs");
var util = require('util');
fs.readFile('grammar.txt', 'utf8', function(err, grammar) {
if (err) throw err;
var parser = PEG.buildParser(grammar);
fs.readFile('domain.txt', 'utf8', function(err, domain) {
if (err) throw err;
var result = parser.parse(domain);
console.log(JSON.stringify(result));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment