KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ppg > atoms > SymbolList


1 package ppg.atoms;
2
3 import java.util.*;
4
5 public class SymbolList
6 {
7     public static final int TERMINAL = 0;
8     public static final int NONTERMINAL = 1;
9     
10     private int variety;
11     private String JavaDoc type;
12     private Vector symbols;
13     
14     public SymbolList(int which, String JavaDoc type, Vector syms) {
15         variety = which;
16         this.type = type;
17         symbols = syms;
18     }
19     
20     public boolean dropSymbol (String JavaDoc gs) {
21         for (int i=0; i < symbols.size(); i++) {
22             if (gs.equals(symbols.elementAt(i))) {
23                 symbols.removeElementAt(i);
24                 // assume we do not have duplicates
25
return true;
26             }
27         }
28         return false;
29     }
30     
31     public Object JavaDoc clone() {
32         String JavaDoc newType = (type == null) ? null : type.toString();
33         Vector newSyms = new Vector();
34         for (int i=0; i < symbols.size(); i++) {
35             newSyms.addElement( ((String JavaDoc)symbols.elementAt(i)).toString() );
36         }
37         return new SymbolList(variety, newType, newSyms);
38     }
39     
40     public String JavaDoc toString() {
41         String JavaDoc result = "";
42
43         if (symbols.size() > 0) {
44             switch (variety) {
45                 case (TERMINAL): result = "terminal "; break;
46                 case (NONTERMINAL): result = "non terminal "; break;
47             }
48         
49             if (type != null)
50                 result += type + " ";
51         
52             int size = symbols.size();
53             for (int i=0; i < size; i++) {
54                 result += (String JavaDoc)symbols.elementAt(i);
55                 if (i < size - 1)
56                     result += ", ";
57             }
58             result += ";";
59         }
60         
61         return result;
62     }
63 }
64
Popular Tags