/***************************************************************** JADE - Java Agent DEvelopment Framework is a framework to develop multi-agent systems in compliance with the FIPA specifications. Copyright (C) 2000 CSELT S.p.A. GNU Lesser General Public License This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, version 2.1 of the License. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *****************************************************************/ /* ACLParser.jj is the Grammar File of the FIPA ACL. To generate the Java source code: javacc ACLParser.jj To compile the Java source code: javac ACLParser*.java To run the parser: cd ..; java ACLParser.ACLParser < TestMessagges.txt Known bugs and/or limitations: - MIME Extension not supported - Content is parsed according to the ACL Grammar and not the Content Language Grammar */ options { LOOKAHEAD = 1; CHOICE_AMBIGUITY_CHECK = 2; OTHER_AMBIGUITY_CHECK = 1; STATIC = false; DEBUG_PARSER = false; DEBUG_LOOKAHEAD = false; DEBUG_TOKEN_MANAGER = false; ERROR_REPORTING = true; JAVA_UNICODE_ESCAPE = false; UNICODE_INPUT = false; IGNORE_CASE = true; USER_TOKEN_MANAGER = false; USER_CHAR_STREAM = false; BUILD_PARSER = true; BUILD_TOKEN_MANAGER = true; SANITY_CHECK = true; FORCE_LA_CHECK = true; // Force LookAhead Cecking } PARSER_BEGIN(ACLParser) package jade.lang.acl; import java.io.*; import jade.core.AID; /** Javadoc documentation for the file @author Fabio Bellifemine - CSELT S.p.A @version $Date: 2001/11/26 13:22:26 $ $Revision: 2.7 $ */ public class ACLParser { ACLMessage msg = new ACLMessage(ACLMessage.NOT_UNDERSTOOD); public static void main(String args[]) throws ParseException { ACLParser parser = new ACLParser(System.in); while (true) { try { ACLMessage result = parser.Message(); System.out.println(result); } catch(ParseException pe) { pe.printStackTrace(); System.exit(1); } } } public static ACLParser create() { Reader r = new StringReader(""); return new ACLParser(r); } public ACLMessage parse(Reader text) throws ParseException { ReInit(text); return Message(); } private String trimQuotes(String s) throws ParseException { s = s.trim(); if(s.startsWith("\"") && (s.endsWith("\""))) s = s.substring(1, s.length() - 1); return unescape(s); } private String unescape(String s) throws ParseException { boolean unescaped = false; StringBuffer result = new StringBuffer(s.length()); for( int i=0; i MessageType() (MessageParameter())* { return msg; } } void MessageType() : { Token t; } { t= { msg.setPerformative(ACLMessage.getInteger(t.image));} } void MessageParameter() : { String s; Token t; AID aid;} { aid=AgentIdentifier() { msg.setSender(aid); token_source.SwitchTo(MESSAGEPARAMETERSTATE);} | {msg.clearAllReceiver();} (aid=AgentIdentifier() {msg.addReceiver(aid);})* { token_source.SwitchTo(MESSAGEPARAMETERSTATE);} | s=Content() { msg.setContent(s); token_source.SwitchTo(MESSAGEPARAMETERSTATE);} | s=Expression() { msg.setReplyWith(s); token_source.SwitchTo(MESSAGEPARAMETERSTATE);} | s=DateTimeToken() { try { msg.setReplyByDate(ISO8601.toDate(s));} catch (Exception e) {} token_source.SwitchTo(MESSAGEPARAMETERSTATE);} | s=Expression() { msg.setInReplyTo(s); token_source.SwitchTo(MESSAGEPARAMETERSTATE);} | {msg.clearAllReplyTo();} (aid=AgentIdentifier() {msg.addReplyTo(aid);})* { token_source.SwitchTo(MESSAGEPARAMETERSTATE);} | s=Expression() { msg.setEncoding(s); token_source.SwitchTo(MESSAGEPARAMETERSTATE);} | s=Expression() { msg.setLanguage(s); token_source.SwitchTo(MESSAGEPARAMETERSTATE);} | s=Expression() { msg.setOntology(s); token_source.SwitchTo(MESSAGEPARAMETERSTATE);} | s=Word() { msg.setProtocol(s); token_source.SwitchTo(MESSAGEPARAMETERSTATE);} | s=Expression() { msg.setConversationId(s); token_source.SwitchTo(MESSAGEPARAMETERSTATE);} | t= s=Expression() { msg.addUserDefinedParameter(t.image.substring(3),s); token_source.SwitchTo(MESSAGEPARAMETERSTATE);} } String Content(): { Token t; String s;} { s = Stringa() { return s; } | t = { return trimQuotes(t.image); } } AID AgentIdentifier(): {Token t; String s; AID aid; AID cur = new AID();} { ( ( t= { cur.setName(t.image); token_source.SwitchTo(AIDSTATE);}) | ( {token_source.SwitchTo(CONTENTSTATE);} (t= {cur.addAddresses(t.image);} )* {token_source.SwitchTo(AIDSTATE);}) | ( (aid=AgentIdentifier() {cur.addResolvers(aid);})* {token_source.SwitchTo(AIDSTATE);}) | (t= s=Expression() {cur.addUserDefinedSlot(t.image.substring(3),s); token_source.SwitchTo(AIDSTATE);}) )+ {return cur;} } String Expression(): { String s; String s1=new String(); } { s=Word() {return s;} | s=Stringa() {return s;} | s=Number() {return s;} | s=DateTimeToken() {return s;} | ( s=Expression() {s1+=(s+" ");} )* {return "("+s1+")";} } String Word(): { Token t; } { t= { return t.image; } } String Stringa(): { String s; } { s=StringLiteral() {return s;} | s=ByteLengthEncodedString() {return s;} } String StringLiteral(): { Token t; } { t= {return trimQuotes(t.image);} } String ByteLengthEncodedString(): { Token t; /* int i=0; char c; */} { t= {return t.image;} } String Number(): { String s;} { s=Digit() {return s;} | s=Integer() {return s;} | s=Float() {return s;} } String DateTimeToken(): { Token t; String s;} { t= {return t.image;} } String Digit(): { Token t;} { t= {return t.image;} } String Integer(): { Token t; String s=new String();} { // (t="+" {s+=t.image;} | t="-" {s+=t.image;})? ( t= {s+=t.image;} )+ {return s;}*/ t= {return t.image;} } String Float(): { Token t;} { t= {return t.image;} | t= {return t.image;} } SKIP : { " " | "\t" | "\n" | "\r" } TOKEN: { : MESSAGETYPESTATE } SKIP : { " " | "\t" | "\n" | "\r" } TOKEN: { : MESSAGEPARAMETERSTATE } SKIP : { " " | "\t" | "\n" | "\r" } TOKEN: { : AIDSTATE | : AIDSTATE | : CONTENTSTATE | : CONTENTSTATE | : CONTENTSTATE | : CONTENTSTATE | : AIDSTATE | : CONTENTSTATE | : CONTENTSTATE | : CONTENTSTATE | : CONTENTSTATE | : CONTENTSTATE | : CONTENTSTATE | : DEFAULT } SKIP : { " " | "\t" | "\n" | "\r" } TOKEN: { < DATETIME : (["0"-"9"]) (["0"-"9"]) (["0"-"9"]) (["0"-"9"]) (["0"-"9"]) (["0"-"9"]) (["0"-"9"]) (["0"-"9"]) ["t","T"] (["0"-"9"]) (["0"-"9"]) (["0"-"9"]) (["0"-"9"]) (["0"-"9"]) (["0"-"9"]) (["0"-"9"]) (["0"-"9"]) (["0"-"9"]) (["a"-"z","A"-"Z"])? > | < WORD : ["!" , "\"" , "$"-"'" , "*" , "+" , "," , "." , "/" , ":"-"~"] (["*"-"~","!"-"'"])* > | | < DIGIT : ["0"-"9"] > | < INTEGER : (["+","-"])? (["0"-"9"])+ > | < FLOATONE : (["+","-"])? ((["0"-"9"])+ "." (["0"-"9"])* | (["0"-"9"])* "." (["0"-"9"])+) (["e","E"] (["-","+"])? (["0"-"9"])+)? > | < FLOATTWO : (["+","-"])? (["0"-"9"])+ ["e","E"] (["-","+"])? (["0"-"9"])+ > | "\"" > { String tmp = matchedToken.image.substring(1, matchedToken.image.length() - 1); int numBytes = Integer.parseInt(tmp); if (numBytes < 0) throw new TokenMgrError("ERROR: PrefixByteLengthEncodedString with length < 0", TokenMgrError.STATIC_LEXER_ERROR); char[] bytes = new char[numBytes]; int i = 0; while (numBytes-- > 0) try { bytes[i++] = input_stream.readChar(); } catch (IOException e) { System.out.println("IOException during PREFIXBYTELENGTHENCODEDSTRING"); throw new TokenMgrError(true, curLexState, input_stream.getEndLine(), input_stream.getEndColumn(),input_stream.GetImage(), curChar, TokenMgrError.LEXICAL_ERROR); } // If you want, you can add bytes to matchedToken.image here. matchedToken.image = new String(bytes); } | | } SKIP : { " " | "\t" | "\n" | "\r" } TOKEN: { | | | : CONTENTSTATE | | | : CONTENTSTATE | | }