📄 jshop2.g
字号:
//------------------------------- Header ----------------------------header{ package JSHOP2; import java.io.IOException; import java.util.LinkedList; import java.util.Vector;}//------------------------------- Parser ----------------------------class JSHOP2Parser extends Parser;//------------------------------- Options ---------------------------options{ k = 2; exportVocab = JSHOP2; codeGenMakeSwitchThreshold = 2; codeGenBitsetTestThreshold = 3; defaultErrorHandler = false; buildAST = false;}//------------------------------- Parser Code -----------------------{ //-- The object that represents the domain being parsed. private InternalDomain domain; //-- The lexer object that does the lexing for this parser. private JSHOP2Lexer lexer; //-- A Vector of String names of variable symbols seen so far in the domain. private Vector<String> vars; //-- To store the maximum number of the variables seen in any variable scope. private int varsMaxSize; //-- The function to initialize this object. It must be called right after //-- the constructor. public void initialize(JSHOP2Lexer lexerIn, InternalDomain domainIn) { lexer = lexerIn; domain = domainIn; vars = new Vector<String>(); varsMaxSize = 0; }}//------------------------------- Grammar ---------------------------//------------------------------- Command ---------------------------//-- TODO: Other possible commandscommand throws IOException : prob;//------------------------------- Problem ---------------------------prob throws IOException : LP DEFPROBLEM pn:ID dn:ID { //-- Set the problem and domain names. domain.setProbName(new String(pn.getText().toLowerCase())); domain.setName(new String(dn.getText().toLowerCase())); //-- Initialize the necessary data structures to compile the command, //-- e.g. read the names from the text file associated with this domain. domain.commandInitialize(); //-- The list of initial world states, one for each planning problem in //-- this domain. LinkedList<Vector<Predicate>> states = new LinkedList<Vector<Predicate>>(); //-- The list of task lists, one for each planning problem in this //-- domain. LinkedList<TaskList> taskLists = new LinkedList<TaskList>(); } ( { //-- The next atom to be added to the initial state of the world. Predicate p; //-- The initial state of the world as specified in the problem //-- description. Vector<Predicate> state = new Vector<Predicate>(); //-- The task list to be achieved as specified in the problem //-- description. TaskList tList; } ( (LP (p = la { //-- Atoms in the initial state of the world must be ground. if (!p.isGround()) { System.out.println("\nERROR: The atoms in the initial state of the " + "world must be ground, non-ground atom found in line: " + lexer.getLine() + "\n"); System.exit(2); } //-- Add the read atom to the initial state of the world. state.add(p); } )* RP) | NIL ) tList = tl { states.add(state); taskLists.add(tList); } )+ RP { //-- Convert the command to Java code and write it. domain.commandToCode(states, taskLists); };//------------------------------- Domain ----------------------------//-- Domaindomain throws IOException : { String str; } LP DEFDOMAIN dn:ID { //-- Set the domain name. domain.setName(new String(dn.getText().toLowerCase())); } LP (pde)+ { //-- Convert the domain description to Java code and write it. domain.close(varsMaxSize); } RP RP;//-- Planning Domain Elementpde : method| op| axiom;//-------------------------------- Method ---------------------------method : { //-- The current branch label. String branchLabel; //-- A Vector of String labels of branches. Vector<String> labels = new Vector<String>(); //-- The logical precondition of the current branch of the method. LogicalPrecondition lPre; //-- A Vector of logical preconditions, one for each branch of the method. Vector<LogicalPrecondition> pres = new Vector<LogicalPrecondition>(); //-- The task decomposition the current branch of the method represents. TaskList sub; //-- A Vector of possible task decompositions, one for each branch of the //-- method. Vector<TaskList> subs = new Vector<TaskList>(); //-- The argument list of the head of the method. List tn; } LP METHOD LP mn:ID tn = terml RP ( { //-- Generate a default label for this branch. branchLabel = "Method" + domain.getMethodNo() + "Branch" + labels.size(); } ( bn:ID { branchLabel = bn.getText(); } )? lPre = lp sub = tl { //-- Add the label, the precondition, and the task decomposition //-- of this branch to the appropriate Vectors. labels.add(branchLabel); pres.add(lPre); subs.add(sub); } )+ { //-- Add the compound task this method decomposes to the list of compound //-- tasks in the domain. int index = domain.addCompoundTask(mn.getText().toLowerCase()); //-- Create the head of the method. Predicate p = new Predicate(index, vars.size(), new TermList(tn)); //-- Create the object that represents the method, and add it to the list //-- of the methods in the domain. domain.addMethod(new InternalMethod(p, labels, pres, subs)); //-- The scope for the variables in a method is within that method, so as //-- soon as we get out of the method body we should empty our list of //-- variables after updating the value of 'varsMaxSize'. if (vars.size() > varsMaxSize) varsMaxSize = vars.size(); vars.clear(); } RP;//-------------------------------- Operator -------------------------op : { //-- The add list of the operator. Vector add; //-- Cost of the operator. Term cost = new TermNumber(1.0); //-- The delete list of the operator. Vector del; //-- The logical precondition of the operator. LogicalPrecondition pre; //-- The argument list of the head of the operator. List tn; } LP OPERATOR LP on:OPID tn = terml RP pre = lp del = da add = da ( cost = term )? { //-- Add the primtive task this operator can achieve to the list of //-- primitive tasks in the domain. int index = domain.addPrimitiveTask(on.getText().toLowerCase()); //-- Create the head of the operator. Predicate p = new Predicate(index, vars.size(), new TermList(tn)); //-- Create the object that represents the operator, and add it to the //-- list of the operators in the domain. domain.addOperator(new InternalOperator(p, pre, del, add, cost)); //-- The scope for the variables in an operator is within that operator, //-- so as soon as we get out of the operator body we should empty our //-- list of variables after updating the value of 'varsMaxSize'. if (vars.size() > varsMaxSize) varsMaxSize = vars.size(); vars.clear(); } RP;//-- Delete/Addda returns [Vector retVal] { //-- The current delete/add element. DelAddElement delAdd = null; //-- The return value, a Vector of delete/add elements. retVal = new Vector(); //-- The first element is set to NULL, meaning that this delete/add list is //-- a real list of predicates, and not a variable. retVal.add(null); }: LP (delAdd = dae { //-- Add the next delete/add element to the delete/add list. retVal.add(delAdd); } )* RP| var:VARID { //-- In case the delete/add list is a variable (to be instantiated to a //-- list of predicates at run time) and not a real list, just clear the //-- Vector (of its first element set to NULL) to signal this, and add //-- the index of the variable to the Vector. retVal.clear(); retVal.add(new Integer(vars.indexOf(var.getText().toLowerCase()))); }| NIL;//-- Delete/Add Elementdae returns [DelAddElement retVal] { //-- The logical atom to be deleted, added, protected or unprotected. Predicate p; }: p = la { //-- The atomic delete/add element. retVal = new DelAddAtomic(p); }| LP PROTECTION p = la RP { //-- The protection delete/add element. retVal = new DelAddProtection(p); }| LP FORALL { //-- The precondition of the ForAll delete/add element. LogicalExpression exp; //-- A Vector of atoms to be deleted/added under this ForAll delete/add //-- element. Vector<Predicate> atoms = new Vector<Predicate>(); } ( (LP (VARID)* RP) | NIL ) exp = le ( ( LP ( p = la { //-- Add the current predicate to the list of atoms to be //-- deleted/added under this ForAll delete/add element. atoms.add(p); } )* RP ) | NIL ) { //-- Create the delete/add element and return it. retVal = new DelAddForAll(exp, atoms); } RP;//-------------------------------- Axiom ----------------------------axiom : { //-- A Vector each element of which represents one branch of the axiom, //-- (i.e., one way to prove its head). Vector<LogicalPrecondition> a = new Vector<LogicalPrecondition>(); //-- The current branch label. String branchLabel; //-- A Vector of String labels of branches. Vector<String> labels = new Vector<String>(); //-- The current branch of the axiom being considered. LogicalPrecondition lPre; //-- The head of the axiom (i.e., the predicate it can be used to prove). Predicate p; } LP ah:AXIOM p = la ( { //-- Generate a default label for this branch. branchLabel = "Axiom" + domain.getAxiomNo() + "Branch" + labels.size(); } ( bn:ID { branchLabel = bn.getText(); } )? lPre = lp { //-- Add the current branch of the axiom to the list of its branches //-- and its label to the list of labels. a.add(lPre); labels.add(branchLabel); } )+ { p.setVarCount(vars.size()); //-- Create the object that represents the axiom, and add it to the list //-- of the axioms in the domain. domain.addAxiom(new InternalAxiom(p, a, labels)); //-- The scope for the variables in an axiom is within that axiom, so as //-- soon as we get out of the operator body we should empty our list of //-- variables after updating the value of 'varsMaxSize'. if (vars.size() > varsMaxSize) varsMaxSize = vars.size(); vars.clear(); } RP;//------------------------------- Task ------------------------------//-- Task Listtl returns [TaskList retVal] { //-- Whether or not the task list is ordered. boolean ordered = true; //-- The subtasks of the task list. Vector<TaskList> subtasks = new Vector<TaskList>(); //-- The current child of the task list, in case it is an atomic task list. TaskAtom tAtom; //-- The current child of the task list, in case it is a list itself. TaskList tList; }: LP ( UNORDERED { //-- This is an unordered task list. ordered = false; } )? ( tList = tl { //-- The next child of the task list is a list itself. subtasks.add(tList); } | tAtom = ta { //-- The next child of the task list is an atomic task. subtasks.add(new TaskList(tAtom)); } )* RP { //-- Create the object that represents this task list. retVal = TaskList.createTaskList(subtasks, ordered); }| NIL { //-- Empty task list. retVal = TaskList.empty; };//-- Task Atomta returns [TaskAtom retVal] { //-- Whether or not this is an immediate task atom. boolean immediate = false; //-- Whether or not this is a primitive task atom. boolean isPrimitive; //-- The argument list of this task atom. List param; //-- The index of the head of this task atom. int tn; }: LP ( IMMEDIATE { //-- This is an immediate task. immediate = true; } )? ( ctn:ID { //-- Add this compound task to the list of compound tasks in the //-- domain. tn = domain.addCompoundTask(ctn.getText().toLowerCase()); isPrimitive = false; } | stn:OPID { //-- Add this primitive task to the list of primitive tasks in the //-- domain.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -