📄 jshop2parser.java
字号:
// $ANTLR 2.7.2: "JSHOP2.g" -> "JSHOP2Parser.java"$ package JSHOP2; import java.io.IOException; import java.util.LinkedList; import java.util.Vector;import antlr.TokenBuffer;import antlr.TokenStreamException;import antlr.TokenStreamIOException;import antlr.ANTLRException;import antlr.LLkParser;import antlr.Token;import antlr.TokenStream;import antlr.RecognitionException;import antlr.NoViableAltException;import antlr.MismatchedTokenException;import antlr.SemanticException;import antlr.ParserSharedInputState;import antlr.collections.impl.BitSet;public class JSHOP2Parser extends antlr.LLkParser implements JSHOP2TokenTypes { //-- 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; }protected JSHOP2Parser(TokenBuffer tokenBuf, int k) { super(tokenBuf,k); tokenNames = _tokenNames;}public JSHOP2Parser(TokenBuffer tokenBuf) { this(tokenBuf,2);}protected JSHOP2Parser(TokenStream lexer, int k) { super(lexer,k); tokenNames = _tokenNames;}public JSHOP2Parser(TokenStream lexer) { this(lexer,2);}public JSHOP2Parser(ParserSharedInputState state) { super(state,2); tokenNames = _tokenNames;} public final void command() throws RecognitionException, TokenStreamException, IOException { prob(); } public final void prob() throws RecognitionException, TokenStreamException, IOException { Token pn = null; Token dn = null; match(LP); match(DEFPROBLEM); pn = LT(1); match(ID); dn = LT(1); match(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>(); { int _cnt8=0; _loop8: do { if ((LA(1)==LP||LA(1)==NIL)) { //-- 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; { switch ( LA(1)) { case LP: { { match(LP); { _loop7: do { if ((LA(1)==LP||LA(1)==VARID)) { 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); } else { break _loop7; } } while (true); } match(RP); } break; } case NIL: { match(NIL); break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } tList=tl(); states.add(state); taskLists.add(tList); } else { if ( _cnt8>=1 ) { break _loop8; } else {throw new NoViableAltException(LT(1), getFilename());} } _cnt8++; } while (true); } match(RP); //-- Convert the command to Java code and write it. domain.commandToCode(states, taskLists); } public final Predicate la() throws RecognitionException, TokenStreamException { Predicate retVal; Token pn = null; Token var = null; //-- The argument list of the logical atom. List l; switch ( LA(1)) { case LP: { match(LP); pn = LT(1); match(ID); l=terml(); //-- Add the constant symbol to the list of constant symbols in the //-- domain. int index = domain.addConstant(pn.getText().toLowerCase()); //-- Create the logical atom. retVal = new Predicate(index, vars.size(), new TermList(l)); match(RP); break; } case VARID: { var = LT(1); match(VARID); //-- Create the variable predicate (i.e., a predicate that is only a //-- variable at compile time, but will be bound to a predicate at run //-- time. retVal = new Predicate(vars.indexOf(var.getText().toLowerCase()), vars.size()); break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } return retVal; } public final TaskList tl() throws RecognitionException, TokenStreamException { 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; switch ( LA(1)) { case LP: { match(LP); { switch ( LA(1)) { case UNORDERED: { match(UNORDERED); //-- This is an unordered task list. ordered = false; break; } case LP: case RP: case NIL: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } { _loop38: do { if ((LA(1)==LP||LA(1)==NIL) && (_tokenSet_0.member(LA(2)))) { tList=tl(); //-- The next child of the task list is a list itself. subtasks.add(tList); } else if ((LA(1)==LP) && (_tokenSet_1.member(LA(2)))) { tAtom=ta(); //-- The next child of the task list is an atomic task. subtasks.add(new TaskList(tAtom)); } else { break _loop38; } } while (true); } match(RP); //-- Create the object that represents this task list. retVal = TaskList.createTaskList(subtasks, ordered); break; } case NIL: { match(NIL); //-- Empty task list. retVal = TaskList.empty; break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } return retVal; } public final void domain() throws RecognitionException, TokenStreamException, IOException { Token dn = null; String str; match(LP); match(DEFDOMAIN); dn = LT(1); match(ID); //-- Set the domain name. domain.setName(new String(dn.getText().toLowerCase())); match(LP); { int _cnt11=0; _loop11: do { if ((LA(1)==LP)) { pde(); } else { if ( _cnt11>=1 ) { break _loop11; } else {throw new NoViableAltException(LT(1), getFilename());} } _cnt11++; } while (true); } //-- Convert the domain description to Java code and write it. domain.close(varsMaxSize); match(RP); match(RP); } public final void pde() throws RecognitionException, TokenStreamException { if ((LA(1)==LP) && (LA(2)==METHOD)) { method(); } else if ((LA(1)==LP) && (LA(2)==OPERATOR)) { op(); } else if ((LA(1)==LP) && (LA(2)==AXIOM)) { axiom(); } else { throw new NoViableAltException(LT(1), getFilename()); } } public final void method() throws RecognitionException, TokenStreamException { Token mn = null; Token bn = null; //-- 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; match(LP); match(METHOD); match(LP); mn = LT(1); match(ID); tn=terml(); match(RP); { int _cnt16=0; _loop16: do { if ((_tokenSet_2.member(LA(1)))) { //-- Generate a default label for this branch. branchLabel = "Method" + domain.getMethodNo() + "Branch" + labels.size(); { switch ( LA(1)) { case ID: { bn = LT(1); match(ID); branchLabel = bn.getText(); break; } case LP: case NIL: case VARID: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } 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); } else { if ( _cnt16>=1 ) { break _loop16; } else {throw new NoViableAltException(LT(1), getFilename());} } _cnt16++; } while (true); } //-- 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(); match(RP); } public final void op() throws RecognitionException, TokenStreamException { Token on = null; //-- 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; match(LP); match(OPERATOR); match(LP); on = LT(1); match(OPID); tn=terml(); match(RP); pre=lp(); del=da(); add=da(); { switch ( LA(1)) { case LP: case ID: case NIL: case VARID: case NUM: { cost=term(); break; } case RP: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } //-- 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));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -