View Javadoc

1   /* Generated By:JavaCC: Do not edit this line. SL0Parser.java */
2   package sl;
3   
4   import java.io.BufferedReader;
5   import java.io.BufferedWriter;
6   import java.io.StringReader;
7   import java.io.InputStreamReader;
8   import java.io.OutputStreamWriter;
9   
10  import java.util.List;
11  import java.util.ArrayList;
12  
13  public class SL0Parser implements SL0ParserConstants {
14  
15  
16    /***
17    * @return a List of Frame, each Frame represents an element of a t-uple
18    */
19    public List parse(String s) throws ParseException, TokenMgrError {
20      ReInit(new StringReader(s));
21      return SL0Content();
22    }
23  
24  
25    public static void main(String[] args) {
26      System.out.println("SL0 Parser started...");
27      SL0Parser theParser = new SL0Parser(new BufferedReader(new InputStreamReader(System.in)));
28      SL0Codec codec = new SL0Codec();
29      Ontology o = new DefaultOntology();
30  
31      while (true) {
32          System.out.println("insert an SL0 expression to parse: ");
33        try {
34          List result = theParser.SL0Content();
35          System.out.println("AFTER ENCODING: "+codec.encode(result,o));
36        }
37        catch(ParseException pe) {
38          pe.printStackTrace();
39          System.exit(1);
40        }
41        catch(TokenMgrError tme) {
42          tme.printStackTrace();
43        }
44      }
45    }
46  
47  /* 
48  
49     The following grammar productions try to match as closely as
50     possible the SL0 grammar given in FIPA 2000 (document no. 8) 
51     The differences are as follows:
52  
53     1) In order to keep single lookahead, braces have been factored out
54        whenever possible. So, the parser methods corresponding to
55        ActionExpression and to FunctionalTerm don't expect
56        neither opening nor closing brace. Braces are explicitly
57        indicated in the calling methods/productions.
58  
59     2) Identical productions have been merged into one; these are:
60        - PropositionSymbol, "true" and "false", that all result in a single String.
61  
62     3) Due to the production merging of the previous point, the
63        AtomicFormula production is no more needed. 
64        The
65        only difference with the original grammar is that here "result"
66        is no more a reserved word.
67  */
68  
69  
70  
71  /*   P R O D U C T I O N    R U L E S  */
72  
73  /***
74  * This production rule represents the more general expression that can
75  * serve as content for an ACL message. Since different communicative
76  * acts have different content (action expressions for
77  * <code>request</code>, predicate for <code>inform</code>, etc.), any
78  * allowed SL content expression can be parsed from here.
79  */
80    final public List SL0Content() throws ParseException {
81    Frame result;
82    List tuple = new ArrayList();
83      jj_consume_token(LBRACE);
84      label_1:
85      while (true) {
86        result = ContentExpression();
87                                           tuple.add(result);
88        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
89        case LBRACE:
90        case WORD:
91        case STRING_LITERAL:
92          ;
93          break;
94        default:
95          jj_la1[0] = jj_gen;
96          break label_1;
97        }
98      }
99      jj_consume_token(RBRACE);
100   {if (true) return tuple;}
101     throw new Error("Missing return statement in function");
102   }
103 
104   final public Frame ContentExpression() throws ParseException {
105   Frame result;
106   String s;
107     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
108     case WORD:
109     case STRING_LITERAL:
110       s = String();
111                  result = new Frame(s);
112       break;
113     case LBRACE:
114       jj_consume_token(LBRACE);
115       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
116       case ACTION:
117         result = ActionExpression();
118         break;
119       case WORD:
120       case STRING_LITERAL:
121         result = Wff();
122         break;
123       default:
124         jj_la1[1] = jj_gen;
125         jj_consume_token(-1);
126         throw new ParseException();
127       }
128       jj_consume_token(RBRACE);
129       break;
130     default:
131       jj_la1[2] = jj_gen;
132       jj_consume_token(-1);
133       throw new ParseException();
134     }
135    {if (true) return result;}
136     throw new Error("Missing return statement in function");
137   }
138 
139   final public Frame Wff() throws ParseException {
140   Frame result;
141   String s;
142   Object obj;
143     s = String();
144                result = new Frame(s);
145     label_2:
146     while (true) {
147       obj = Term();
148                  result.putSlot(obj);
149       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
150       case LBRACE:
151       case INTEGER:
152       case HEXINTEGER:
153       case FLOATONE:
154       case FLOATTWO:
155       case WORD:
156       case STRING_LITERAL:
157       case DATETIME:
158         ;
159         break;
160       default:
161         jj_la1[3] = jj_gen;
162         break label_2;
163       }
164     }
165    {if (true) return result;}
166     throw new Error("Missing return statement in function");
167   }
168 
169   final public Object Term() throws ParseException {
170  Object obj;
171  Token  t;
172  String s;
173     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
174     case WORD:
175     case STRING_LITERAL:
176       obj = String();
177         // if it is true or false, then converts the String into a Boolean
178                   if (obj.toString().equalsIgnoreCase("true") ||
179                       obj.toString().equalsIgnoreCase("false") )
180                     obj = new Boolean(obj.toString());
181       break;
182     case INTEGER:
183     case HEXINTEGER:
184     case FLOATONE:
185     case FLOATTWO:
186       obj = Number();
187       break;
188     case DATETIME:
189       t = jj_consume_token(DATETIME);
190                     try { obj=ISO8601.toDate(t.image);}
191                     catch (Exception e) { obj = new String(t.image); }
192       break;
193     case LBRACE:
194       jj_consume_token(LBRACE);
195       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
196       case WORD:
197       case STRING_LITERAL:
198         s = String();
199                           obj = new Frame(s);
200         switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
201         case LBRACE:
202         case INTEGER:
203         case HEXINTEGER:
204         case FLOATONE:
205         case FLOATTWO:
206         case WORD:
207         case STRING_LITERAL:
208         case PARAMETERNAME:
209         case DATETIME:
210           FunctionalTermParams((Frame)obj);
211           break;
212         default:
213           jj_la1[4] = jj_gen;
214           ;
215         }
216         break;
217       case ACTION:
218         obj = ActionExpression();
219         break;
220       default:
221         jj_la1[5] = jj_gen;
222         jj_consume_token(-1);
223         throw new ParseException();
224       }
225       jj_consume_token(RBRACE);
226       break;
227     default:
228       jj_la1[6] = jj_gen;
229       jj_consume_token(-1);
230       throw new ParseException();
231     }
232     {if (true) return obj;}
233     throw new Error("Missing return statement in function");
234   }
235 
236   final public Frame ActionExpression() throws ParseException {
237   Frame result;
238   Object agentID;
239   Object action;
240     jj_consume_token(ACTION);
241     agentID = Term();
242     action = Term();
243     result = new Frame(SL0Codec.NAME_OF_ACTION_FRAME);
244     result.putSlot(SL0Codec.NAME_OF_ACTOR_SLOT, agentID);
245     result.putSlot(SL0Codec.NAME_OF_ACTION_SLOT, action);
246     {if (true) return result;}
247     throw new Error("Missing return statement in function");
248   }
249 
250   final public void FunctionalTermParams(Frame result) throws ParseException {
251   Object obj;
252     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
253     case LBRACE:
254     case INTEGER:
255     case HEXINTEGER:
256     case FLOATONE:
257     case FLOATTWO:
258     case WORD:
259     case STRING_LITERAL:
260     case DATETIME:
261       label_3:
262       while (true) {
263         obj = Term();
264                  result.putSlot(obj);
265         switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
266         case LBRACE:
267         case INTEGER:
268         case HEXINTEGER:
269         case FLOATONE:
270         case FLOATTWO:
271         case WORD:
272         case STRING_LITERAL:
273         case DATETIME:
274           ;
275           break;
276         default:
277           jj_la1[7] = jj_gen;
278           break label_3;
279         }
280       }
281       break;
282     case PARAMETERNAME:
283       label_4:
284       while (true) {
285         Parameter(result);
286         switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
287         case PARAMETERNAME:
288           ;
289           break;
290         default:
291           jj_la1[8] = jj_gen;
292           break label_4;
293         }
294       }
295       break;
296     default:
297       jj_la1[9] = jj_gen;
298       jj_consume_token(-1);
299       throw new ParseException();
300     }
301   }
302 
303 /***
304 Notice that this production, as a side effect, also adds a slot to result.
305 The initial char ':' is removed from the slotName
306 **/
307   final public void Parameter(Frame result) throws ParseException {
308   Token name;
309   Object value;
310     name = jj_consume_token(PARAMETERNAME);
311     value = Term();
312     result.putSlot(name.image.substring(1), value);
313   }
314 
315 /***
316 * <p> <code> String = WORD | STRING_LITERAL  </code>
317 */
318   final public String String() throws ParseException {
319   Token t;
320     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
321     case WORD:
322       t = jj_consume_token(WORD);
323                          {if (true) return t.image;}
324       break;
325     case STRING_LITERAL:
326       t = jj_consume_token(STRING_LITERAL);
327                          {if (true) return (t.image.equalsIgnoreCase("\"action\"")?"action":t.image);}
328       break;
329     default:
330       jj_la1[10] = jj_gen;
331       jj_consume_token(-1);
332       throw new ParseException();
333     }
334     throw new Error("Missing return statement in function");
335   }
336 
337   final public Object Number() throws ParseException {
338   Token t;
339     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
340     case INTEGER:
341       t = jj_consume_token(INTEGER);
342                   {if (true) return new Long(t.image);}
343       break;
344     case HEXINTEGER:
345       t = jj_consume_token(HEXINTEGER);
346                      {if (true) return new String(t.image);}
347       break;
348     case FLOATONE:
349       t = jj_consume_token(FLOATONE);
350                    {if (true) return new Double(t.image);}
351       break;
352     case FLOATTWO:
353       t = jj_consume_token(FLOATTWO);
354                    {if (true) return new Double(t.image);}
355       break;
356     default:
357       jj_la1[11] = jj_gen;
358       jj_consume_token(-1);
359       throw new ParseException();
360     }
361     throw new Error("Missing return statement in function");
362   }
363 
364   public SL0ParserTokenManager token_source;
365   ASCII_UCodeESC_CharStream jj_input_stream;
366   public Token token, jj_nt;
367   private int jj_ntk;
368   private int jj_gen;
369   final private int[] jj_la1 = new int[12];
370   final private int[] jj_la1_0 = {0x3020,0x3080,0x3020,0xbf20,0xff20,0x3080,0xbf20,0xbf20,0x4000,0xff20,0x3000,0xf00,};
371 
372   public SL0Parser(java.io.InputStream stream) {
373     jj_input_stream = new ASCII_UCodeESC_CharStream(stream, 1, 1);
374     token_source = new SL0ParserTokenManager(jj_input_stream);
375     token = new Token();
376     jj_ntk = -1;
377     jj_gen = 0;
378     for (int i = 0; i < 12; i++) jj_la1[i] = -1;
379   }
380 
381   public void ReInit(java.io.InputStream stream) {
382     jj_input_stream.ReInit(stream, 1, 1);
383     token_source.ReInit(jj_input_stream);
384     token = new Token();
385     jj_ntk = -1;
386     jj_gen = 0;
387     for (int i = 0; i < 12; i++) jj_la1[i] = -1;
388   }
389 
390   public SL0Parser(java.io.Reader stream) {
391     jj_input_stream = new ASCII_UCodeESC_CharStream(stream, 1, 1);
392     token_source = new SL0ParserTokenManager(jj_input_stream);
393     token = new Token();
394     jj_ntk = -1;
395     jj_gen = 0;
396     for (int i = 0; i < 12; i++) jj_la1[i] = -1;
397   }
398 
399   public void ReInit(java.io.Reader stream) {
400     jj_input_stream.ReInit(stream, 1, 1);
401     token_source.ReInit(jj_input_stream);
402     token = new Token();
403     jj_ntk = -1;
404     jj_gen = 0;
405     for (int i = 0; i < 12; i++) jj_la1[i] = -1;
406   }
407 
408   public SL0Parser(SL0ParserTokenManager tm) {
409     token_source = tm;
410     token = new Token();
411     jj_ntk = -1;
412     jj_gen = 0;
413     for (int i = 0; i < 12; i++) jj_la1[i] = -1;
414   }
415 
416   public void ReInit(SL0ParserTokenManager tm) {
417     token_source = tm;
418     token = new Token();
419     jj_ntk = -1;
420     jj_gen = 0;
421     for (int i = 0; i < 12; i++) jj_la1[i] = -1;
422   }
423 
424   final private Token jj_consume_token(int kind) throws ParseException {
425     Token oldToken;
426     if ((oldToken = token).next != null) token = token.next;
427     else token = token.next = token_source.getNextToken();
428     jj_ntk = -1;
429     if (token.kind == kind) {
430       jj_gen++;
431       return token;
432     }
433     token = oldToken;
434     jj_kind = kind;
435     throw generateParseException();
436   }
437 
438   final public Token getNextToken() {
439     if (token.next != null) token = token.next;
440     else token = token.next = token_source.getNextToken();
441     jj_ntk = -1;
442     jj_gen++;
443     return token;
444   }
445 
446   final public Token getToken(int index) {
447     Token t = token;
448     for (int i = 0; i < index; i++) {
449       if (t.next != null) t = t.next;
450       else t = t.next = token_source.getNextToken();
451     }
452     return t;
453   }
454 
455   final private int jj_ntk() {
456     if ((jj_nt=token.next) == null)
457       return (jj_ntk = (token.next=token_source.getNextToken()).kind);
458     else
459       return (jj_ntk = jj_nt.kind);
460   }
461 
462   private java.util.Vector jj_expentries = new java.util.Vector();
463   private int[] jj_expentry;
464   private int jj_kind = -1;
465 
466   final public ParseException generateParseException() {
467     jj_expentries.removeAllElements();
468     boolean[] la1tokens = new boolean[16];
469     for (int i = 0; i < 16; i++) {
470       la1tokens[i] = false;
471     }
472     if (jj_kind >= 0) {
473       la1tokens[jj_kind] = true;
474       jj_kind = -1;
475     }
476     for (int i = 0; i < 12; i++) {
477       if (jj_la1[i] == jj_gen) {
478         for (int j = 0; j < 32; j++) {
479           if ((jj_la1_0[i] & (1<<j)) != 0) {
480             la1tokens[j] = true;
481           }
482         }
483       }
484     }
485     for (int i = 0; i < 16; i++) {
486       if (la1tokens[i]) {
487         jj_expentry = new int[1];
488         jj_expentry[0] = i;
489         jj_expentries.addElement(jj_expentry);
490       }
491     }
492     int[][] exptokseq = new int[jj_expentries.size()][];
493     for (int i = 0; i < jj_expentries.size(); i++) {
494       exptokseq[i] = (int[])jj_expentries.elementAt(i);
495     }
496     return new ParseException(token, exptokseq, tokenImage);
497   }
498 
499   final public void enable_tracing() {
500   }
501 
502   final public void disable_tracing() {
503   }
504 
505 }